• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 package android.multiuser;
17 
18 import android.os.Bundle;
19 import android.os.SystemClock;
20 import android.perftests.utils.ShellHelper;
21 
22 import java.util.ArrayList;
23 
24 // Based on //platform/frameworks/base/apct-tests/perftests/utils/BenchmarkState.java
25 public class BenchmarkRunner {
26 
27     private static final long COOL_OFF_PERIOD_MS = 1000;
28 
29     private static final int NUM_ITERATIONS = 4;
30 
31     private static final int NOT_STARTED = 0;  // The benchmark has not started yet.
32     private static final int RUNNING = 1;  // The benchmark is running.
33     private static final int PAUSED = 2; // The benchmark is paused
34     private static final int FINISHED = 3;  // The benchmark has stopped.
35 
36     private final BenchmarkResults mResults = new BenchmarkResults();
37     private int mState = NOT_STARTED;  // Current benchmark state.
38     private int mIteration;
39 
40     public long mStartTimeNs;
41     public long mPausedDurationNs;
42     public long mPausedTimeNs;
43 
keepRunning()44     public boolean keepRunning() {
45         switch (mState) {
46             case NOT_STARTED:
47                 mState = RUNNING;
48                 prepareForNextRun();
49                 return true;
50             case RUNNING:
51                 mIteration++;
52                 return startNextTestRun();
53             case PAUSED:
54                 throw new IllegalStateException("Benchmarking is in paused state");
55             case FINISHED:
56                 throw new IllegalStateException("Benchmarking is finished");
57             default:
58                 throw new IllegalStateException("BenchmarkRunner is in unknown state");
59         }
60     }
61 
startNextTestRun()62     private boolean startNextTestRun() {
63         mResults.addDuration(System.nanoTime() - mStartTimeNs - mPausedDurationNs);
64         if (mIteration == NUM_ITERATIONS) {
65             mState = FINISHED;
66             return false;
67         } else {
68             prepareForNextRun();
69             return true;
70         }
71     }
72 
prepareForNextRun()73     private void prepareForNextRun() {
74         SystemClock.sleep(COOL_OFF_PERIOD_MS);
75         ShellHelper.runShellCommand("am wait-for-broadcast-idle");
76         mStartTimeNs = System.nanoTime();
77         mPausedDurationNs = 0;
78     }
79 
pauseTiming()80     public void pauseTiming() {
81         if (mState != RUNNING) {
82             throw new IllegalStateException("Unable to pause the runner: not running currently");
83         }
84         mPausedTimeNs = System.nanoTime();
85         mState = PAUSED;
86     }
87 
resumeTiming()88     public void resumeTiming() {
89         if (mState != PAUSED) {
90             throw new IllegalStateException("Unable to resume the runner: already running");
91         }
92         mPausedDurationNs += System.nanoTime() - mPausedTimeNs;
93         mState = RUNNING;
94     }
95 
getStatsToReport()96     public Bundle getStatsToReport() {
97         return mResults.getStatsToReport();
98     }
99 
getStatsToLog()100     public Bundle getStatsToLog() {
101         return mResults.getStatsToLog();
102     }
103 
getAllDurations()104     public ArrayList<Long> getAllDurations() {
105         return mResults.getAllDurations();
106     }
107 }