BenchmarkRunner.java revision 5f76e1fa85f5c189d57860b0b5b02cace139204b
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 */ 16package android.multiuser; 17 18import android.os.Bundle; 19import android.os.SystemClock; 20 21import java.util.ArrayList; 22 23// Based on //platform/frameworks/base/apct-tests/perftests/utils/BenchmarkState.java 24public class BenchmarkRunner { 25 26 private static long COOL_OFF_PERIOD_MS = 2000; 27 28 private static final int NUM_ITERATIONS = 4; 29 30 private static final int NOT_STARTED = 0; // The benchmark has not started yet. 31 private static final int RUNNING = 1; // The benchmark is running. 32 private static final int PAUSED = 2; // The benchmark is paused 33 private static final int FINISHED = 3; // The benchmark has stopped. 34 35 private final BenchmarkResults mResults = new BenchmarkResults(); 36 private int mState = NOT_STARTED; // Current benchmark state. 37 private int mIteration; 38 39 public long mStartTimeNs; 40 public long mPausedDurationNs; 41 public long mPausedTimeNs; 42 43 public boolean keepRunning() { 44 switch (mState) { 45 case NOT_STARTED: 46 mState = RUNNING; 47 prepareForNextRun(); 48 return true; 49 case RUNNING: 50 mIteration++; 51 return startNextTestRun(); 52 case PAUSED: 53 throw new IllegalStateException("Benchmarking is in paused state"); 54 case FINISHED: 55 throw new IllegalStateException("Benchmarking is finished"); 56 default: 57 throw new IllegalStateException("BenchmarkRunner is in unknown state"); 58 } 59 } 60 61 private boolean startNextTestRun() { 62 mResults.addDuration(System.nanoTime() - mStartTimeNs - mPausedDurationNs); 63 if (mIteration == NUM_ITERATIONS) { 64 mState = FINISHED; 65 return false; 66 } else { 67 prepareForNextRun(); 68 return true; 69 } 70 } 71 72 private void prepareForNextRun() { 73 // TODO: Once http://b/63115387 is fixed, look into using "am wait-for-broadcast-idle" 74 // command instead of waiting for a fixed amount of time. 75 SystemClock.sleep(COOL_OFF_PERIOD_MS); 76 mStartTimeNs = System.nanoTime(); 77 mPausedDurationNs = 0; 78 } 79 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 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 96 public Bundle getStats() { 97 return mResults.getStats(); 98 } 99 100 public ArrayList<Long> getAllDurations() { 101 return mResults.getAllDurations(); 102 } 103}