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

Commit b1bf611b authored by Victor Chang's avatar Victor Chang
Browse files

Avoid mockito in MotionPredictorBenchmark

Mockito dominates the time to create MotionPredictor, and
it can be avoided.

The median time reduces from ~39614ns to ~683ns. (-98%).

Bug: 321913450
Test:  atest CorePerfTests:android.input.MotionPredictorBenchmark
Change-Id: Idea21b5627a25af8d5941bb8c7a1ff567eb2504d
parent c40bde3f
Loading
Loading
Loading
Loading
+3 −20
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

package android.input

import android.content.Context
import android.content.res.Resources
import android.os.SystemProperties
import android.perftests.utils.PerfStatusReporter
import android.view.InputDevice
@@ -38,8 +36,6 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.mock
import org.mockito.Mockito.`when`

import java.time.Duration

@@ -68,18 +64,6 @@ private fun getStylusMotionEvent(
                InputDevice.SOURCE_STYLUS, /*flags=*/0)
}

private fun getPredictionContext(offset: Duration, enablePrediction: Boolean): Context {
    val context = mock(Context::class.java)
    val resources: Resources = mock(Resources::class.java)
    `when`(context.getResources()).thenReturn(resources)
    `when`(resources.getInteger(
            com.android.internal.R.integer.config_motionPredictionOffsetNanos)).thenReturn(
                offset.toNanos().toInt())
    `when`(resources.getBoolean(
            com.android.internal.R.bool.config_enableMotionPrediction)).thenReturn(enablePrediction)
    return context
}

@RunWith(AndroidJUnit4::class)
@LargeTest
class MotionPredictorBenchmark {
@@ -115,7 +99,7 @@ class MotionPredictorBenchmark {
        var eventPosition = 0f
        val positionInterval = 10f

        val predictor = MotionPredictor(getPredictionContext(offset, /*enablePrediction=*/true))
        val predictor = MotionPredictor(/*isPredictionEnabled=*/true, offset.toNanos().toInt())
        // ACTION_DOWN t=0 x=0 y=0
        predictor.record(getStylusMotionEvent(
            eventTime, ACTION_DOWN, /*x=*/eventPosition, /*y=*/eventPosition))
@@ -141,12 +125,11 @@ class MotionPredictorBenchmark {
     */
    @Test
    fun timeCreatePredictor() {
        val context = getPredictionContext(
                /*offset=*/Duration.ofMillis(20), /*enablePrediction=*/true)
        val offsetNanos = Duration.ofMillis(20).toNanos().toInt()

        val state = perfStatusReporter.getBenchmarkState()
        while (state.keepRunning()) {
            MotionPredictor(context)
            MotionPredictor(/*isPredictionEnabled=*/true, offsetNanos)
        }
    }
}
+17 −5
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;

import com.android.internal.annotations.VisibleForTesting;

import libcore.util.NativeAllocationRegistry;

/**
@@ -57,11 +59,21 @@ public final class MotionPredictor {
     * @param context The context for the predictions
     */
    public MotionPredictor(@NonNull Context context) {
        mIsPredictionEnabled = context.getResources().getBoolean(
                com.android.internal.R.bool.config_enableMotionPrediction);
        final int offsetNanos = context.getResources().getInteger(
                com.android.internal.R.integer.config_motionPredictionOffsetNanos);
        mPtr = nativeInitialize(offsetNanos);
        this(
                context.getResources().getBoolean(
                        com.android.internal.R.bool.config_enableMotionPrediction),
                context.getResources().getInteger(
                        com.android.internal.R.integer.config_motionPredictionOffsetNanos));
    }

    /**
     * Internal constructor for testing.
     * @hide
     */
    @VisibleForTesting
    public MotionPredictor(boolean isPredictionEnabled, int motionPredictionOffsetNanos) {
        mIsPredictionEnabled = isPredictionEnabled;
        mPtr = nativeInitialize(motionPredictionOffsetNanos);
        RegistryHolder.REGISTRY.registerNativeAllocation(this, mPtr);
    }