Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4777ee06 authored by Eric Holk's avatar Eric Holk
Browse files

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
parent 13926c38
Loading
Loading
Loading
Loading
+15 −17
Original line number Diff line number Diff line
@@ -35,7 +35,8 @@ import android.widget.TextView;
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 @@ class Benchmark {

                @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();

                    // Compute mean
                    long sum = Arrays.stream(results).sum();
                    resultMean = (double) sum / results.length;
                        count++;
                        double elapsedVariance = (double) elapsed - resultMean;
                        resultMean += elapsedVariance / count;
                        resultStdev += elapsedVariance * ((double) elapsed - resultMean);

                    // Compute standard deviation
                    double variance = 0;
                    for (long i : results) {
                        double t = (double) i - resultMean;
                        variance += t * t;
                        if (System.nanoTime() - startTime > TIME_LIMIT * 1e9) {
                            break;
                        }
                    variance /= results.length - 1;

                    resultStdev = Math.sqrt(variance);
                    }
                    resultStdev = Math.sqrt(resultStdev / (count - 1));

                    return null;
                }