Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 
     17 package android.os;
     18 
     19 import android.perftests.utils.BenchmarkState;
     20 import android.perftests.utils.PerfStatusReporter;
     21 
     22 import androidx.test.filters.LargeTest;
     23 import androidx.test.runner.AndroidJUnit4;
     24 
     25 import com.android.internal.os.BinderCallsStats;
     26 import com.android.internal.os.BinderInternal.CallSession;
     27 import com.android.internal.os.CachedDeviceState;
     28 
     29 import org.junit.After;
     30 import org.junit.Before;
     31 import org.junit.Rule;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 
     35 /**
     36  * Performance tests for {@link BinderCallsStats}
     37  */
     38 @RunWith(AndroidJUnit4.class)
     39 @LargeTest
     40 public class BinderCallsStatsPerfTest {
     41     private static final int DEFAULT_BUCKET_SIZE = 1000;
     42     private static final int WORKSOURCE_UID = 1;
     43     static class FakeCpuTimeBinderCallsStats extends BinderCallsStats {
     44         private int mTimeMs;
     45 
     46         FakeCpuTimeBinderCallsStats() {
     47             super(new BinderCallsStats.Injector());
     48             setDeviceState(new CachedDeviceState(false, false).getReadonlyClient());
     49         }
     50 
     51         protected long getThreadTimeMicro() {
     52             return mTimeMs++;
     53         }
     54 
     55         protected long getElapsedRealtimeMicro() {
     56             return mTimeMs++;
     57         }
     58     }
     59 
     60     @Rule
     61     public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
     62     private BinderCallsStats mBinderCallsStats;
     63 
     64     @Before
     65     public void setUp() {
     66         mBinderCallsStats = new BinderCallsStats(new BinderCallsStats.Injector());
     67         CachedDeviceState deviceState = new CachedDeviceState(false, false);
     68         mBinderCallsStats.setDeviceState(deviceState.getReadonlyClient());
     69     }
     70 
     71     @After
     72     public void tearDown() {
     73     }
     74 
     75     @Test
     76     public void timeCallSession() {
     77         mBinderCallsStats.setDetailedTracking(true);
     78         runScenario(DEFAULT_BUCKET_SIZE);
     79     }
     80 
     81     @Test
     82     public void timeCallSessionOnePercentSampling() {
     83         mBinderCallsStats.setDetailedTracking(false);
     84         mBinderCallsStats.setSamplingInterval(100);
     85         runScenario(DEFAULT_BUCKET_SIZE);
     86     }
     87 
     88     @Test
     89     public void timeCallSessionTrackingDisabled() {
     90         mBinderCallsStats.setDetailedTracking(false);
     91         runScenario(DEFAULT_BUCKET_SIZE);
     92     }
     93 
     94     @Test
     95     public void timeCallSession_1000_buckets_cpuNotRecorded() {
     96         mBinderCallsStats = new FakeCpuTimeBinderCallsStats();
     97         mBinderCallsStats.setSamplingInterval(1);
     98         runScenario(/* max bucket size */ 1000);
     99     }
    100 
    101     @Test
    102     public void timeCallSession_500_buckets_cpuNotRecorded() {
    103         mBinderCallsStats = new FakeCpuTimeBinderCallsStats();
    104         mBinderCallsStats.setSamplingInterval(1);
    105         runScenario(/* max bucket size */ 500);
    106     }
    107 
    108     @Test
    109     public void timeCallSession_100_buckets_cpuNotRecorded() {
    110         mBinderCallsStats = new FakeCpuTimeBinderCallsStats();
    111         mBinderCallsStats.setSamplingInterval(1);
    112         runScenario(/* max bucket size */ 100);
    113     }
    114 
    115     // There will be a warmup time of maxBucketSize to initialize the map of CallStat.
    116     private void runScenario(int maxBucketSize) {
    117         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
    118         Binder b = new Binder();
    119         while (state.keepRunning()) {
    120             for (int i = 0; i < 10000; i++) {
    121                 CallSession s = mBinderCallsStats.callStarted(b, i % maxBucketSize, WORKSOURCE_UID);
    122                 mBinderCallsStats.callEnded(s, 0, 0, WORKSOURCE_UID);
    123             }
    124         }
    125     }
    126 }
    127