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

Commit 6d064860 authored by Shai Barack's avatar Shai Barack
Browse files

Emit trace sections for warmup and benchmark phases

Change-Id: Iba3e00c8d3eaf8df6bce9ea93512b5627c86703a
Flag: EXEMPT test-only code
parent 61258e61
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.app.Activity;
import android.app.Instrumentation;
import android.os.Bundle;
import android.os.Debug;
import android.os.Trace;
import android.util.Log;

import androidx.test.InstrumentationRegistry;
@@ -129,17 +130,23 @@ public final class BenchmarkState {
    }

    private void beginWarmup() {
        Trace.beginSection("Warmup");
        mStartTimeNs = System.nanoTime();
        mIteration = 0;
        mState = WARMUP;
    }

    private void endWarmup() {
        Trace.endSection();
    }

    private void beginBenchmark(long warmupDuration, int iterations) {
        if (ENABLE_PROFILING) {
            File f = new File(InstrumentationRegistry.getContext().getDataDir(), "benchprof");
            Log.d(TAG, "Tracing to: " + f.getAbsolutePath());
            Debug.startMethodTracingSampling(f.getAbsolutePath(), 16 * 1024 * 1024, 100);
        }
        Trace.beginSection("Benchmark");
        mMaxIterations = (int) (TARGET_TEST_DURATION_NS / (warmupDuration / iterations));
        mMaxIterations = Math.min(MAX_TEST_ITERATIONS,
                Math.max(mMaxIterations, MIN_TEST_ITERATIONS));
@@ -150,6 +157,10 @@ public final class BenchmarkState {
        mStartTimeNs = System.nanoTime();
    }

    private void endBenchmark() {
        Trace.endSection();
    }

    private boolean startNextTestRun() {
        final long currentTime = System.nanoTime();
        mResults.add((currentTime - mStartTimeNs - mPausedDurationNs) / mMaxIterations);
@@ -165,6 +176,7 @@ public final class BenchmarkState {
                return true;
            }
            mState = FINISHED;
            endBenchmark();
            return false;
        }
        mPausedDurationNs = 0;
@@ -189,6 +201,7 @@ public final class BenchmarkState {
                // don't yet have a target iteration count.
                final long duration = System.nanoTime() - mStartTimeNs;
                if (mIteration >= WARMUP_MIN_ITERATIONS && duration >= WARMUP_DURATION_NS) {
                    endWarmup();
                    beginBenchmark(duration, mIteration);
                }
                return true;
@@ -208,6 +221,7 @@ public final class BenchmarkState {
                mCustomizedIterations++;
                if (mCustomizedIterations >= mMaxCustomizedIterations) {
                    mState = FINISHED;
                    endBenchmark();
                    return false;
                }
                mCustomizedIterationListener.onStart(mCustomizedIterations);