diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 7bb6185..0a8ffe3 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -25,6 +25,7 @@ plugins { alias(libs.plugins.kotlin.compose) alias(libs.plugins.kotlin.serialization) alias(libs.plugins.ksp) + alias(libs.plugins.baselineprofile) } tasks.withType(Test::class) { @@ -124,6 +125,8 @@ dependencies { implementation(libs.androidx.room.runtime) implementation(libs.androidx.room.ktx) + implementation(libs.androidx.profileinstaller) + "baselineProfile"(project(":baselineprofile")) ksp(libs.androidx.room.compiler) "playImplementation"(libs.revenuecat.purchases) diff --git a/baselineprofile/.gitignore b/baselineprofile/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/baselineprofile/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/baselineprofile/build.gradle.kts b/baselineprofile/build.gradle.kts new file mode 100644 index 0000000..a72ed40 --- /dev/null +++ b/baselineprofile/build.gradle.kts @@ -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 . + */ + +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 } + ) + } +} \ No newline at end of file diff --git a/baselineprofile/src/main/AndroidManifest.xml b/baselineprofile/src/main/AndroidManifest.xml new file mode 100644 index 0000000..2d815d5 --- /dev/null +++ b/baselineprofile/src/main/AndroidManifest.xml @@ -0,0 +1,18 @@ + + + \ No newline at end of file diff --git a/baselineprofile/src/main/java/org/nsh07/baselineprofile/BaselineProfileGenerator.kt b/baselineprofile/src/main/java/org/nsh07/baselineprofile/BaselineProfileGenerator.kt new file mode 100644 index 0000000..6296f6a --- /dev/null +++ b/baselineprofile/src/main/java/org/nsh07/baselineprofile/BaselineProfileGenerator.kt @@ -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 . + */ + +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() + } +} \ No newline at end of file diff --git a/baselineprofile/src/main/java/org/nsh07/baselineprofile/StartupBenchmarks.kt b/baselineprofile/src/main/java/org/nsh07/baselineprofile/StartupBenchmarks.kt new file mode 100644 index 0000000..cbd41bf --- /dev/null +++ b/baselineprofile/src/main/java/org/nsh07/baselineprofile/StartupBenchmarks.kt @@ -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 . + */ + +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 + ) + } + ) + } +} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index f370cb1..2dbaeca 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,20 @@ +/* + * 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 . + */ + // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { alias(libs.plugins.android.application) apply false @@ -5,4 +22,6 @@ plugins { alias(libs.plugins.kotlin.compose) apply false alias(libs.plugins.kotlin.serialization) apply false alias(libs.plugins.ksp) apply false + alias(libs.plugins.android.test) apply false + alias(libs.plugins.baselineprofile) apply false } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 20e2a01..8224a81 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,11 +1,19 @@ -# Project-wide Gradle settings. -# IDE (e.g. Android Studio) users: -# Gradle settings configured through the IDE *will override* -# any settings specified in this file. -# For more details on how to configure your build environment visit -# http://www.gradle.org/docs/current/userguide/build_environment.html -# Specifies the JVM arguments used for the daemon process. -# The setting is particularly useful for tweaking memory settings. +# +# 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 . +# org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. For more details, visit @@ -20,4 +28,5 @@ kotlin.code.style=official # Enables namespacing of each library's R class so that its R class includes only the # resources declared in the library itself and none from the library's dependencies, # thereby reducing the size of the R class for that library -android.nonTransitiveRClass=true \ No newline at end of file +android.nonTransitiveRClass=true +org.gradle.configuration-cache=true \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1eb1d0b..2479ed8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -15,6 +15,10 @@ navigation3 = "1.0.0" revenuecat = "9.15.2" room = "2.8.4" vico = "2.3.6" +uiautomator = "2.4.0-alpha07" +benchmarkMacroJunit4 = "1.4.1" +baselineprofile = "1.4.1" +profileinstaller = "1.4.1" [libraries] androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" } @@ -43,6 +47,9 @@ material-kolor = { module = "com.materialkolor:material-kolor", version.ref = "m revenuecat-purchases = { module = "com.revenuecat.purchases:purchases", version.ref = "revenuecat" } revenuecat-purchases-ui = { module = "com.revenuecat.purchases:purchases-ui", version.ref = "revenuecat" } vico-compose-m3 = { group = "com.patrykandpatrick.vico", name = "compose-m3", version.ref = "vico" } +androidx-uiautomator = { group = "androidx.test.uiautomator", name = "uiautomator", version.ref = "uiautomator" } +androidx-benchmark-macro-junit4 = { group = "androidx.benchmark", name = "benchmark-macro-junit4", version.ref = "benchmarkMacroJunit4" } +androidx-profileinstaller = { group = "androidx.profileinstaller", name = "profileinstaller", version.ref = "profileinstaller" } [plugins] android-application = { id = "com.android.application", version.ref = "agp" } @@ -50,3 +57,5 @@ kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } +android-test = { id = "com.android.test", version.ref = "agp" } +baselineprofile = { id = "androidx.baselineprofile", version.ref = "baselineprofile" } diff --git a/settings.gradle.kts b/settings.gradle.kts index b04cb7a..ca9580e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,3 +1,20 @@ +/* + * 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 . + */ + pluginManagement { repositories { google { @@ -21,3 +38,4 @@ dependencyResolutionManagement { rootProject.name = "Tomato" include(":app") +include(":baselineprofile")