feat(build): add baseline profile generation test

and related build system changes
This commit is contained in:
Nishant Mishra
2025-12-20 14:09:15 +05:30
parent 09639fd5e0
commit 2ce3801728
10 changed files with 407 additions and 9 deletions

1
baselineprofile/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,81 @@
/*
* Copyright (c) 2025 Nishant Mishra
*
* This file is part of Tomato - a minimalist pomodoro timer for Android.
*
* Tomato is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Tomato is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tomato.
* If not, see <https://www.gnu.org/licenses/>.
*/
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins {
alias(libs.plugins.android.test)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.baselineprofile)
}
android {
namespace = "org.nsh07.baselineprofile"
compileSdk {
version = release(36)
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlin {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_17) // Use the enum for target JVM version
}
}
defaultConfig {
minSdk = 28
targetSdk = 36
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
targetProjectPath = ":app"
flavorDimensions += listOf("version")
productFlavors {
create("foss") { dimension = "version" }
create("play") { dimension = "version" }
}
}
// This is the configuration block for the Baseline Profile plugin.
// You can specify to run the generators on a managed devices or connected devices.
baselineProfile {
useConnectedDevices = true
}
dependencies {
implementation(libs.androidx.junit)
implementation(libs.androidx.espresso.core)
implementation(libs.androidx.uiautomator)
implementation(libs.androidx.benchmark.macro.junit4)
}
androidComponents {
onVariants { v ->
val artifactsLoader = v.artifacts.getBuiltArtifactsLoader()
v.instrumentationRunnerArguments.put(
"targetAppId",
v.testedApks.map { artifactsLoader.load(it)?.applicationId }
)
}
}

View File

@@ -0,0 +1,18 @@
<!--
~ Copyright (c) 2025 Nishant Mishra
~
~ This file is part of Tomato - a minimalist pomodoro timer for Android.
~
~ Tomato is free software: you can redistribute it and/or modify it under the terms of the GNU
~ General Public License as published by the Free Software Foundation, either version 3 of the
~ License, or (at your option) any later version.
~
~ Tomato is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
~ the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
~ Public License for more details.
~
~ You should have received a copy of the GNU General Public License along with Tomato.
~ If not, see <https://www.gnu.org/licenses/>.
-->
<manifest />

View File

@@ -0,0 +1,148 @@
/*
* Copyright (c) 2025 Nishant Mishra
*
* This file is part of Tomato - a minimalist pomodoro timer for Android.
*
* Tomato is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Tomato is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tomato.
* If not, see <https://www.gnu.org/licenses/>.
*/
package org.nsh07.baselineprofile
import androidx.benchmark.macro.junit4.BaselineProfileRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiAutomatorTestScope
import androidx.test.uiautomator.textAsString
import androidx.test.uiautomator.uiAutomator
import androidx.test.uiautomator.watcher.PermissionDialog
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.util.Locale
@RunWith(AndroidJUnit4::class)
@LargeTest
class BaselineProfileGenerator {
@get:Rule
val rule = BaselineProfileRule()
@Test
fun generate() {
// The application id for the running build variant is read from the instrumentation arguments.
val packageName = InstrumentationRegistry.getArguments().getString("targetAppId")
?: throw Exception("targetAppId not passed as instrumentation runner arg")
rule.collect(
packageName = packageName,
// See: https://d.android.com/topic/performance/baselineprofiles/dex-layout-optimizations
includeInStartupProfile = true
) {
pressHome()
startActivityAndWait()
uiAutomator {
onElement { contentDescription == "Play" }.click()
watchFor(PermissionDialog) {
clickAllow() // Allow notification permission
}
waitForAppToBeVisible(packageName)
onElement { contentDescription == "Restart" }.click()
onElement { contentDescription == "Stats" }.click()
waitForStableInActiveWindow()
scrollThroughContent()
onElement { textAsString() == "Last week" }.click()
waitForStableInActiveWindow()
scrollThroughContent()
onElement { contentDescription == "Back" }.click()
waitForStableInActiveWindow()
onElement { textAsString() == "Last month" }.click()
waitForStableInActiveWindow()
scrollThroughContent()
onElement { contentDescription == "Back" }.click()
waitForStableInActiveWindow()
fling(500, 1500, "UP")
waitForStableInActiveWindow()
onElement { textAsString() == "Last year" }.click()
waitForStableInActiveWindow()
scrollThroughContent()
onElement { contentDescription == "Back" }.click()
waitForStableInActiveWindow()
onElement { contentDescription == "Settings" }.click()
waitForStableInActiveWindow()
onElement { textAsString() == "Timer" }.click()
waitForStableInActiveWindow()
scrollThroughContent()
onElement { contentDescription == "Back" }.click()
waitForStableInActiveWindow()
onElement { textAsString() == "Appearance" }.click()
waitForStableInActiveWindow()
scrollThroughContent()
onElement { contentDescription == "Back" }.click()
waitForStableInActiveWindow()
onElement { textAsString() == "Alarm" }.click()
waitForStableInActiveWindow()
scrollThroughContent()
onElement { contentDescription == "Back" }.click()
waitForStableInActiveWindow()
onElement { textAsString() == "About" }.click()
waitForStableInActiveWindow()
scrollThroughContent()
onElement { contentDescription == "Back" }.click()
}
}
}
private fun UiAutomatorTestScope.fling(startX: Int, startY: Int, direction: String) {
val screenHeight = device.displayHeight
val screenWidth = device.displayWidth
val steps = 5 // Fast speed for fling
var endX = startX
var endY = startY
when (direction.uppercase(Locale.getDefault())) {
"UP" -> endY = startY - (screenHeight * 0.3).toInt()
"DOWN" -> endY = startY + (screenHeight * 0.3).toInt()
"LEFT" -> endX = startX - (screenWidth * 0.3).toInt()
"RIGHT" -> endX = startX + (screenWidth * 0.3).toInt()
}
device.swipe(startX, startY, endX, endY, steps)
}
private fun UiAutomatorTestScope.scrollThroughContent() {
fling(500, 1500, "UP")
waitForStableInActiveWindow()
fling(500, 1500, "DOWN")
waitForStableInActiveWindow()
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright (c) 2025 Nishant Mishra
*
* This file is part of Tomato - a minimalist pomodoro timer for Android.
*
* Tomato is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Tomato is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tomato.
* If not, see <https://www.gnu.org/licenses/>.
*/
package org.nsh07.baselineprofile
import androidx.benchmark.macro.BaselineProfileMode
import androidx.benchmark.macro.CompilationMode
import androidx.benchmark.macro.StartupMode
import androidx.benchmark.macro.StartupTimingMetric
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Until
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
/**
* This test class benchmarks the speed of app startup.
* Run this benchmark to verify how effective a Baseline Profile is.
* It does this by comparing [CompilationMode.None], which represents the app with no Baseline
* Profiles optimizations, and [CompilationMode.Partial], which uses Baseline Profiles.
*
* Run this benchmark to see startup measurements and captured system traces for verifying
* the effectiveness of your Baseline Profiles. You can run it directly from Android
* Studio as an instrumentation test, or run all benchmarks for a variant, for example benchmarkRelease,
* with this Gradle task:
* ```
* ./gradlew :baselineprofile:connectedBenchmarkReleaseAndroidTest
* ```
*
* You should run the benchmarks on a physical device, not an Android emulator, because the
* emulator doesn't represent real world performance and shares system resources with its host.
*
* For more information, see the [Macrobenchmark documentation](https://d.android.com/macrobenchmark#create-macrobenchmark)
* and the [instrumentation arguments documentation](https://d.android.com/topic/performance/benchmarking/macrobenchmark-instrumentation-args).
**/
@RunWith(AndroidJUnit4::class)
@LargeTest
class StartupBenchmarks {
@get:Rule
val rule = MacrobenchmarkRule()
@Test
fun startupCompilationNone() =
benchmark(CompilationMode.None())
@Test
fun startupCompilationBaselineProfiles() =
benchmark(CompilationMode.Partial(BaselineProfileMode.Require))
private fun benchmark(compilationMode: CompilationMode) {
// The application id for the running build variant is read from the instrumentation arguments.
rule.measureRepeated(
packageName = InstrumentationRegistry.getArguments().getString("targetAppId")
?: throw Exception("targetAppId not passed as instrumentation runner arg"),
metrics = listOf(StartupTimingMetric()),
compilationMode = compilationMode,
startupMode = StartupMode.COLD,
iterations = 10,
setupBlock = {
pressHome()
},
measureBlock = {
startActivityAndWait()
// This ensures the full UI rendering is included in the measurement.
device.wait(
Until.hasObject(By.text("25:00")),
5_000 // Wait for a maximum of 5 seconds for the content to appear
)
}
)
}
}