Run system_server benchmarks for 5 seconds
Especially for short running methods, this gives us better results and also
gives us more data for things like simpleperf to work on.
Since we don't know ahead of time how many times each benchmark will run, we had
to switch the mean and standard deviation formulas to one that supports running
updates. This has the advantage of making it constant space.
Bug: 140743821
Change-Id: I3e6b6b44d3bd573bad8afc8831226194147b108a
diff --git a/startop/apps/test/src/SystemServerBenchmarkActivity.java b/startop/apps/test/src/SystemServerBenchmarkActivity.java
index c370924..61d4322 100644
--- a/startop/apps/test/src/SystemServerBenchmarkActivity.java
+++ b/startop/apps/test/src/SystemServerBenchmarkActivity.java
@@ -35,7 +35,8 @@
import java.util.Arrays;
class Benchmark {
- public static final int NUM_ITERATIONS = 1000;
+ // Time limit to run benchmarks in seconds
+ public static final int TIME_LIMIT = 5;
public Benchmark(ViewGroup parent, CharSequence name, Runnable thunk) {
Context context = parent.getContext();
@@ -57,28 +58,25 @@
@Override
protected Object doInBackground(Object... _args) {
- long[] results = new long[NUM_ITERATIONS];
+ long startTime = System.nanoTime();
+ int count = 0;
// Run benchmark
- for (int i = 0; i < results.length; i++) {
- results[i] = -System.nanoTime();
+ while (true) {
+ long elapsed = -System.nanoTime();
thunk.run();
- results[i] += System.nanoTime();
+ elapsed += System.nanoTime();
+
+ count++;
+ double elapsedVariance = (double) elapsed - resultMean;
+ resultMean += elapsedVariance / count;
+ resultStdev += elapsedVariance * ((double) elapsed - resultMean);
+
+ if (System.nanoTime() - startTime > TIME_LIMIT * 1e9) {
+ break;
+ }
}
-
- // Compute mean
- long sum = Arrays.stream(results).sum();
- resultMean = (double) sum / results.length;
-
- // Compute standard deviation
- double variance = 0;
- for (long i : results) {
- double t = (double) i - resultMean;
- variance += t * t;
- }
- variance /= results.length - 1;
-
- resultStdev = Math.sqrt(variance);
+ resultStdev = Math.sqrt(resultStdev / (count - 1));
return null;
}