diff options
author | John Reck <jreck@google.com> | 2016-10-14 15:05:05 -0700 |
---|---|---|
committer | John Reck <jreck@google.com> | 2016-10-17 13:49:16 -0700 |
commit | c1d6ba4053b4d5f7a9c3c255073d22fbe4af392f (patch) | |
tree | 85db120f684c137cd56b0767bc91adf97db858ac | |
parent | c93a7ef9cbba1afe1c1c24b500c3ebf67d4ef9b1 (diff) |
Fix pause/resume timing
Test: run SystemPerfTest#testPauseResumeOverhead
Change-Id: Icc34355e29f3a6ba8f7d73a904bf549d2685eee4
-rw-r--r-- | apct-tests/perftests/core/src/java/lang/perftests/SystemPerfTest.java | 17 | ||||
-rw-r--r-- | apct-tests/perftests/utils/src/android/perftests/utils/BenchmarkState.java | 22 |
2 files changed, 29 insertions, 10 deletions
diff --git a/apct-tests/perftests/core/src/java/lang/perftests/SystemPerfTest.java b/apct-tests/perftests/core/src/java/lang/perftests/SystemPerfTest.java index 80ce22eed925..1e9c49cf702a 100644 --- a/apct-tests/perftests/core/src/java/lang/perftests/SystemPerfTest.java +++ b/apct-tests/perftests/core/src/java/lang/perftests/SystemPerfTest.java @@ -24,6 +24,8 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; +import java.util.concurrent.TimeUnit; + @RunWith(AndroidJUnit4.class) @LargeTest public class SystemPerfTest { @@ -43,4 +45,19 @@ public class SystemPerfTest { BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); while (state.keepRunning()) {} } + + void spinBlock(long durationNs) { + long start = System.nanoTime(); + while (System.nanoTime() - start < durationNs) {} + } + + @Test + public void testBenchmarkPauseResumeOverhead() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + state.pauseTiming(); + spinBlock(TimeUnit.MICROSECONDS.toNanos(5)); + state.resumeTiming(); + } + } } diff --git a/apct-tests/perftests/utils/src/android/perftests/utils/BenchmarkState.java b/apct-tests/perftests/utils/src/android/perftests/utils/BenchmarkState.java index bf04f6d2013b..519d5248a347 100644 --- a/apct-tests/perftests/utils/src/android/perftests/utils/BenchmarkState.java +++ b/apct-tests/perftests/utils/src/android/perftests/utils/BenchmarkState.java @@ -43,13 +43,13 @@ import java.util.concurrent.TimeUnit; * } */ public final class BenchmarkState { + private static final String TAG = "BenchmarkState"; private static final int NOT_STARTED = 0; // The benchmark has not started yet. private static final int WARMUP = 1; // The benchmark is warming up. private static final int RUNNING = 2; // The benchmark is running. - private static final int RUNNING_PAUSED = 3; // The benchmark is temporary paused. - private static final int FINISHED = 4; // The benchmark has stopped. + private static final int FINISHED = 3; // The benchmark has stopped. private int mState = NOT_STARTED; // Current benchmark state. @@ -63,6 +63,7 @@ public final class BenchmarkState { private static final int REPEAT_COUNT = 5; private long mStartTimeNs = 0; // Previously captured System.nanoTime(). + private boolean mPaused; private long mPausedTimeNs = 0; // The System.nanoTime() when the pauseTiming() is called. private long mPausedDurationNs = 0; // The duration of paused state in nano sec. @@ -118,24 +119,24 @@ public final class BenchmarkState { // Stops the benchmark timer. // This method can be called only when the timer is running. public void pauseTiming() { - if (mState == RUNNING_PAUSED) { + if (mPaused) { throw new IllegalStateException( "Unable to pause the benchmark. The benchmark has already paused."); } mPausedTimeNs = System.nanoTime(); - mState = RUNNING_PAUSED; + mPaused = true; } // Starts the benchmark timer. // This method can be called only when the timer is stopped. public void resumeTiming() { - if (mState == RUNNING) { + if (!mPaused) { throw new IllegalStateException( "Unable to resume the benchmark. The benchmark is already running."); } mPausedDurationNs += System.nanoTime() - mPausedTimeNs; mPausedTimeNs = 0; - mState = RUNNING; + mPaused = false; } private void beginWarmup() { @@ -194,11 +195,12 @@ public final class BenchmarkState { if (mIteration >= mMaxIterations) { return startNextTestRun(); } + if (mPaused) { + throw new IllegalStateException( + "Benchmark step finished with paused state. " + + "Resume the benchmark before finishing each step."); + } return true; - case RUNNING_PAUSED: - throw new IllegalStateException( - "Benchmark step finished with paused state. " + - "Resume the benchmark before finishing each step."); case FINISHED: throw new IllegalStateException("The benchmark has finished."); default: |