BaselineProfileRule


@RequiresApi(value = 28)
class BaselineProfileRule : TestRule


A TestRule that collects Baseline Profiles to be embedded in your APK.

These rules are used at install time to partially pre-compile your application code.

BaselineProfileRule is only supported on Android 13 (API 33) and above, or if using a rooted device, Android P (API 28) and above.

Note that you can specify a filterPredicate to filter captured rules, for example, if you're generating rules for a library, and don't want to record profiles from outside that library.

See the Baseline Profile Guide for more information on creating Baseline Profiles.

import androidx.benchmark.macro.junit4.BaselineProfileRule
import androidx.test.ext.junit.runners.AndroidJUnit4

@RunWith(AndroidJUnit4::class)
class AppBaselineProfileGenerator {
    @get:Rule val baselineProfileRule = BaselineProfileRule()

    @Test
    fun startup() =
        baselineProfileRule.collect(packageName = "com.example.my.application.id") {
            pressHome()
            // This block defines the scenario for which profiles are captured.
            // Here we are interested in optimizing for app startup, but you
            // could also navigate and scroll through your most important UI.
            startActivityAndWait()
        }
}
import androidx.benchmark.macro.junit4.BaselineProfileRule
import androidx.test.ext.junit.runners.AndroidJUnit4

@RunWith(AndroidJUnit4::class)
class LibraryBaselineProfileGenerator {
    @get:Rule val baselineProfileRule = BaselineProfileRule()

    @Test
    fun libraryStartupRules() =
        baselineProfileRule.collect(
            packageName = "com.example.my.application.id",
            filterPredicate = {
                // Only capture rules in the library's package, excluding test app code
                // Rules are prefixed by tag characters, followed by JVM method signature,
                // e.g. `HSPLcom/mylibrary/LibraryClass;-><init>()V`, where `L`
                // signifies the start of the package/class, and '/' is divider instead of '.'
                val libPackagePrefix = "com.mylibrary"
                it.contains("^.*L${libPackagePrefix.replace(".", "/")}".toRegex())
            }
        ) {
            startActivityAndWait()
        }
}

Summary

Public constructors

Public functions

open Statement
apply(base: Statement, description: Description)
Unit
collect(
    packageName: String,
    maxIterations: Int,
    stableIterations: Int,
    outputFilePrefix: String?,
    includeInStartupProfile: Boolean,
    strictStability: Boolean,
    filterPredicate: (String) -> Boolean,
    profileBlock: MacrobenchmarkScope.() -> Unit
)

Collects baseline profiles for a critical user journey, while ensuring that the generated profiles are stable for a minimum of stableIterations.

BaselineProfileResult
collectWithResults(
    packageName: String,
    maxIterations: Int,
    stableIterations: Int,
    outputFilePrefix: String?,
    includeInStartupProfile: Boolean,
    strictStability: Boolean,
    filterPredicate: (String) -> Boolean,
    profileBlock: MacrobenchmarkScope.() -> Unit
)

Collects baseline profiles for a critical user journey, while ensuring that the generated profiles are stable for a minimum of stableIterations.

Public constructors

BaselineProfileRule

Added in 1.1.0
BaselineProfileRule()

Public functions

apply

Added in 1.1.0
open fun apply(base: Statement, description: Description): Statement

collect

Added in 1.2.0
fun collect(
    packageName: String,
    maxIterations: Int = 15,
    stableIterations: Int = 3,
    outputFilePrefix: String? = null,
    includeInStartupProfile: Boolean = false,
    strictStability: Boolean = false,
    filterPredicate: (String) -> Boolean = { true },
    profileBlock: MacrobenchmarkScope.() -> Unit
): Unit

Collects baseline profiles for a critical user journey, while ensuring that the generated profiles are stable for a minimum of stableIterations.

Parameters
packageName: String

Package name of the app for which profiles are to be generated.

maxIterations: Int = 15

Maximum number of iterations to run when collecting profiles.

stableIterations: Int = 3

Minimum number of iterations to observe as stable before assuming stability, and completing profile generation.

outputFilePrefix: String? = null

An optional file name prefix used when creating the output file with the contents of the human readable baseline profile. For example: outputFilePrefix-baseline-prof.txt

includeInStartupProfile: Boolean = false

determines whether the generated profile should be also used as a startup profile. A startup profile is utilized during the build process in order to determine which classes are needed in the primary dex to optimize the startup time. This flag should be used only for startup flows, such as main application startup pre and post login or other entry points of the app. Note that methods collected in a startup profiles are also utilized for baseline profiles.

strictStability: Boolean = false

Enforce if the generated profile was stable

filterPredicate: (String) -> Boolean = { true }

Function used to filter individual rules / lines of the baseline profile. By default, no filters are applied. Note that this works only when the target application's code is not obfuscated.

profileBlock: MacrobenchmarkScope.() -> Unit

defines the critical user journey.

collectWithResults

Added in 1.4.0-alpha07
fun collectWithResults(
    packageName: String,
    maxIterations: Int = 15,
    stableIterations: Int = 3,
    outputFilePrefix: String? = null,
    includeInStartupProfile: Boolean = false,
    strictStability: Boolean = false,
    filterPredicate: (String) -> Boolean = { true },
    profileBlock: MacrobenchmarkScope.() -> Unit
): BaselineProfileResult

Collects baseline profiles for a critical user journey, while ensuring that the generated profiles are stable for a minimum of stableIterations.

Parameters
packageName: String

Package name of the app for which profiles are to be generated.

maxIterations: Int = 15

Maximum number of iterations to run when collecting profiles.

stableIterations: Int = 3

Minimum number of iterations to observe as stable before assuming stability, and completing profile generation.

outputFilePrefix: String? = null

An optional file name prefix used when creating the output file with the contents of the human readable baseline profile. For example: outputFilePrefix-baseline-prof.txt

includeInStartupProfile: Boolean = false

determines whether the generated profile should be also used as a startup profile. A startup profile is utilized during the build process in order to determine which classes are needed in the primary dex to optimize the startup time. This flag should be used only for startup flows, such as main application startup pre and post login or other entry points of the app. Note that methods collected in a startup profiles are also utilized for baseline profiles.

strictStability: Boolean = false

Enforce if the generated profile was stable

filterPredicate: (String) -> Boolean = { true }

Function used to filter individual rules / lines of the baseline profile. By default, no filters are applied. Note that this works only when the target application's code is not obfuscated.

profileBlock: MacrobenchmarkScope.() -> Unit

defines the critical user journey.

Returns
BaselineProfileResult

BaselineProfileResult which can be used to determine the absolute paths of the collected baseline profiles.