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