AndroidBenchmarkRunner


open class AndroidBenchmarkRunner : AndroidJUnitRunner


Instrumentation runner for benchmarks, used to increase stability of measurements and minimize interference.

To use this runner, put the following in your module level build.gradle:

android {
defaultConfig {
testInstrumentationRunner "androidx.benchmark.junit4.AndroidBenchmarkRunner"
}
}

Minimizing Interference

This runner launches a simple opaque activity used to reduce benchmark interference from other windows. Launching other activities is supported e.g. via ActivityTestRule and ActivityScenario - the opaque activity will be relaunched if not actively running before each test, and after each test's cleanup is complete.

For example, sources of potential interference:

  • live wallpaper rendering

  • homescreen widget updates

  • hotword detection

  • status bar repaints

  • running in background (some cores may be foreground-app only)

Clock Stability

While it is better for performance stability to lock clocks with the ./gradlew lockClocks task provided by the gradle plugin, this is not possible on most devices. The runner provides a fallback mode for preventing thermal throttling.

On devices that support android.view.Window.setSustainedPerformanceMode, the runner will set this mode on the window of every Activity launched (including the opaque Activity mentioned above). The runner will also launch a continuously spinning Thread. Together, these ensure that the app runs in the multithreaded stable performance mode, which locks the maximum clock frequency to prevent thermal throttling. This ensures stable clock levels across all benchmarks, even if a continuous suite of benchmarks runs for many minutes on end.

Summary

Public constructors

Public functions

open Unit

Protected functions

open Unit

Ensures we've onStopped() all activities which were onStarted().

Inherited functions

From androidx.test.runner.AndroidJUnitRunner
Application
newApplication(cl: ClassLoader, className: String, context: Context)

Does initialization before creating the application.

Unit
onCreate(arguments: Bundle)

Sets up lifecycle monitoring, and argument registry.

Boolean
Unit

This implementation of onStart() will guarantee that the Application's onCreate method has completed when it returns.

Unit
sendStatus(resultCode: Int, results: Bundle)
From android.app.Instrumentation
TestLooperManager
Unit
Instrumentation.ActivityMonitor
addMonitor(
    cls: String,
    result: Instrumentation.ActivityResult,
    block: Boolean
)
Instrumentation.ActivityMonitor
addMonitor(
    filter: IntentFilter,
    result: Instrumentation.ActivityResult,
    block: Boolean
)
Unit
addResults(results: Bundle)
Unit
callActivityOnCreate(
    activity: Activity,
    icicle: Bundle,
    persistentState: PersistableBundle
)
Unit
callActivityOnNewIntent(activity: Activity, intent: Intent)
Unit
callActivityOnNewIntent(
    activity: Activity,
    intent: Intent,
    caller: ComponentCaller
)
Unit
Unit
callActivityOnPostCreate(activity: Activity, savedInstanceState: Bundle?)
Unit
callActivityOnPostCreate(
    activity: Activity,
    savedInstanceState: Bundle?,
    persistentState: PersistableBundle?
)
Unit
callActivityOnRestoreInstanceState(
    activity: Activity,
    savedInstanceState: Bundle
)
Unit
callActivityOnRestoreInstanceState(
    activity: Activity,
    savedInstanceState: Bundle?,
    persistentState: PersistableBundle?
)
Unit
Unit
callActivityOnSaveInstanceState(
    activity: Activity,
    outState: Bundle,
    outPersistentState: PersistableBundle
)
Unit
Boolean
Unit
UiAutomation
Boolean
invokeContextMenuAction(targetActivity: Activity, id: Int, flag: Int)
Boolean
invokeMenuActionSync(targetActivity: Activity, id: Int, flag: Int)
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Activity
startActivitySync(intent: Intent, options: Bundle?)
Unit
Unit
Unit
Unit
Unit
Unit
waitForIdle(recipient: Runnable)
Unit
Activity
Activity
From androidx.test.runner.MonitoringInstrumentation
Unit
callActivityOnCreate(activity: Activity, bundle: Bundle)
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
execStartActivities(
    who: Context,
    contextThread: IBinder,
    token: IBinder,
    target: Activity,
    intents: Array<Intent>,
    options: Bundle
)
Instrumentation.ActivityResult
execStartActivity(
    who: Context,
    contextThread: IBinder,
    token: IBinder,
    target: Activity,
    intent: Intent,
    requestCode: Int
)
Instrumentation.ActivityResult
execStartActivity(
    who: Context,
    contextThread: IBinder,
    token: IBinder,
    target: Activity,
    intent: Intent,
    requestCode: Int,
    options: Bundle
)
Instrumentation.ActivityResult
execStartActivity(
    who: Context,
    contextThread: IBinder,
    token: IBinder,
    target: Fragment,
    intent: Intent,
    requestCode: Int,
    options: Bundle
)
Instrumentation.ActivityResult
execStartActivity(
    who: Context,
    contextThread: IBinder,
    token: IBinder,
    target: String,
    intent: Intent,
    requestCode: Int,
    options: Bundle
)

This API was added in Android API 23 (M)

Instrumentation.ActivityResult
execStartActivity(
    who: Context,
    contextThread: IBinder,
    token: IBinder,
    target: Activity,
    intent: Intent,
    requestCode: Int,
    options: Bundle,
    user: UserHandle
)

This API was added in Android API 17 (JELLY_BEAN_MR1)

Unit
finish(resultCode: Int, results: Bundle)

Ensures all activities launched in this instrumentation are finished before the instrumentation exits.

Unit
Unit
installOldMultiDex(multidexClass: Class<*>)

Perform application MultiDex installation only when instrumentation installation is not available.

Unit
interceptActivityUsing(
    interceptingActivityFactory: InterceptingActivityFactory
)

Use the given InterceptingActivityFactory to create Activity instance in newActivity.

Boolean
isPrimaryInstrProcess(argsProcessName: String?)

This function is deprecated.

use isPrimaryInstrProcess()

Activity
newActivity(cl: ClassLoader, className: String, intent: Intent)
Activity
newActivity(
    clazz: Class<*>,
    context: Context,
    token: IBinder,
    application: Application,
    intent: Intent,
    info: ActivityInfo,
    title: CharSequence,
    parent: Activity,
    id: String,
    lastNonConfigurationInstance: Any
)
Unit
Unit

Posts a runnable to the main thread and blocks the caller's thread until the runnable is executed.

Unit
Boolean
Unit
Activity
Throwable

Unwraps an exception from certain wrapper types, e.g. RuntimeException.

Unit

Use default mechanism of creating activity instance in newActivity

Public constructors

AndroidBenchmarkRunner

Added in 1.0.0
AndroidBenchmarkRunner()

Public functions

onDestroy

@CallSuper
open fun onDestroy(): Unit

Protected functions

waitForActivitiesToComplete

@CallSuper
protected open fun waitForActivitiesToComplete(): Unit

Ensures we've onStopped() all activities which were onStarted().

According to Activity's contract, the process is not killable between onStart and onStop. Breaking this contract (which finish() will if you let it) can cause bad behaviour (including a full restart of system_server).

We give the app 2 seconds to stop all its activities, then we proceed.

This should never be run on the main thread.