Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature/#719] Add Benchmark Module and baseline profile generator #1011

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ plugins {
id("droidkaigi.primitive.android.roborazzi")
id("droidkaigi.primitive.kover.entrypoint")
id("droidkaigi.primitive.android.osslicenses")
alias(libs.plugins.androidx.baselineprofile)
}

val keystorePropertiesFile = file("keystore.properties")
Expand Down Expand Up @@ -89,7 +90,8 @@ android {
signingConfig = signingConfigs.getByName("debug")
matchingFallbacks += listOf("release")
isDebuggable = false
proguardFiles("benchmark-rules.pro")
isMinifyEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
}
Expand All @@ -102,6 +104,8 @@ dependencies {
implementation(projects.feature.sponsors)
implementation(projects.feature.floorMap)
implementation(projects.feature.achievements)
baselineProfile(projects.baselineprofile)
implementation(libs.profileinstaller)
implementation(projects.feature.staff)
implementation(projects.core.model)
implementation(projects.core.data)
Expand Down
25,891 changes: 25,891 additions & 0 deletions app-android/src/devRelease/generated/baselineProfiles/baseline-prof.txt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I'm going to create this by myself without committing so could you remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's deleted

Large diffs are not rendered by default.

20,425 changes: 20,425 additions & 0 deletions app-android/src/prodRelease/generated/baselineProfiles/baseline-prof.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions baselineprofile/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
77 changes: 77 additions & 0 deletions baselineprofile/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import com.android.build.api.dsl.ManagedVirtualDevice

plugins {
alias(libs.plugins.androidTest)
alias(libs.plugins.kotlinGradlePlugin)
alias(libs.plugins.androidx.baselineprofile)
}

android {
namespace = "io.github.droidkaigi.confsched2023.baselineprofile"
compileSdk = 34

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = "17"
}

defaultConfig {
minSdk = 28
targetSdk = 34

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "EMULATOR"
}

targetProjectPath = ":app-android"

flavorDimensions += listOf("network")
productFlavors {
create("dev") {
dimension = "network"
buildConfigField(
"String",
"APP_FLAVOR_SUFFIX",
"\".dev\""
)
}
create("prod") {
dimension = "network"
buildConfigField(
"String",
"APP_FLAVOR_SUFFIX",
"\"\""
)
}
}

testOptions.managedDevices.devices {
create<ManagedVirtualDevice>("pixel6Api34") {
device = "Pixel 6"
apiLevel = 34
systemImageSource = "google"
}
}

buildFeatures {
buildConfig = true
}
}

// 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 {
managedDevices += "pixel6Api34"
useConnectedDevices = false
}

dependencies {
implementation(libs.androidxTestExtJunit)
implementation(libs.androidxTestEspressoEspressoCore)
implementation(libs.uiautomator)
implementation(libs.benchmark.macro.junit4)
}
5 changes: 5 additions & 0 deletions baselineprofile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<queries>
<package android:name="io.github.droidkaigi.confsched2023" />
</queries>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.droidkaigi.confsched2023.baselineprofile

val PACKAGE_NAME = buildString {
append("io.github.droidkaigi.confsched2023")
append(BuildConfig.APP_FLAVOR_SUFFIX)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.github.droidkaigi.confsched2023.baselineprofile

import androidx.benchmark.macro.junit4.BaselineProfileRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* This test class generates a basic startup baseline profile for the target package.
*
* We recommend you start with this but add important user flows to the profile to improve their performance.
* Refer to the [baseline profile documentation](https://d.android.com/topic/performance/baselineprofiles)
* for more information.
*
* You can run the generator with the Generate Baseline Profile run configuration,
* or directly with `generateBaselineProfile` Gradle task:
* ```
* ./gradlew :app-android:generateReleaseBaselineProfile -Pandroid.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=BaselineProfile
* ```
* The run configuration runs the Gradle task and applies filtering to run only the generators.
*
* Check [documentation](https://d.android.com/topic/performance/benchmarking/macrobenchmark-instrumentation-args)
* for more information about available instrumentation arguments.
*
* After you run the generator, you can verify the improvements running the [StartupBenchmarks] benchmark.
**/
@RunWith(AndroidJUnit4::class)
@LargeTest
class BaselineProfileGenerator {

@get:Rule
val rule = BaselineProfileRule()

@Test
fun generate() {
rule.collect(PACKAGE_NAME) {
// This block defines the app's critical user journey. Here we are interested in
// optimizing for app startup. But you can also navigate and scroll
// through your most important UI.

// Start default activity for your app
pressHome()
startActivityAndWait()

// TODO Write more interactions to optimize advanced journeys of your app.
// For example:
// 1. Wait until the content is asynchronously loaded
// 2. Scroll the feed content
// 3. Navigate to detail screen

// Check UiAutomator documentation for more information how to interact with the app.
// https://d.android.com/training/testing/other-components/ui-automator
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.github.droidkaigi.confsched2023.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 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 with this Gradle task:
* ```
* ./gradlew :baselineprofile:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=Macrobenchmark
* ```
*
* 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) {
rule.measureRepeated(
packageName = PACKAGE_NAME,
metrics = listOf(StartupTimingMetric()),
compilationMode = compilationMode,
startupMode = StartupMode.COLD,
iterations = 10,
setupBlock = {
pressHome()
},
measureBlock = {
startActivityAndWait()

// TODO Add interactions to wait for when your app is fully drawn.
// The app is fully drawn when Activity.reportFullyDrawn is called.
// For Jetpack Compose, you can use ReportDrawn, ReportDrawnWhen and ReportDrawnAfter
// from the AndroidX Activity library.

// Check the UiAutomator documentation for more information on how to
// interact with the app.
// https://d.android.com/training/testing/other-components/ui-automator
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class KmpPlugin : Plugin<Project> {
with(pluginManager) {
apply("org.jetbrains.kotlin.multiplatform")
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class.java) {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class) {
kotlinOptions.jvmTarget = "11"
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink>().configureEach {
Expand Down
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ plugins {
alias(libs.plugins.androidGradleLibraryPlugin) apply false
alias(libs.plugins.kotlinGradlePlugin) apply false
alias(libs.plugins.kotlinxKover) apply false
alias(libs.plugins.androidTest) apply false
alias(libs.plugins.androidx.baselineprofile) apply false
}

tasks.register("clean", Delete::class) {
Expand All @@ -19,4 +21,4 @@ buildscript {
}
}
}
}
}
9 changes: 9 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ twitterComposeRule = "0.0.26"
lottie = "6.1.0"
kover = "0.7.3"
androidxLifecycleProcess = "2.6.2"
uiautomator = "2.2.0"
benchmark-macro-junit4 = "1.2.4"
androidx-baselineprofile = "1.2.4"
profileinstaller = "1.3.1"

[libraries]
androidGradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" }
Expand Down Expand Up @@ -154,6 +158,9 @@ roborazziCompose = { module = "io.github.takahirom.roborazzi:roborazzi-compose",
roborazziRule = { module = "io.github.takahirom.roborazzi:roborazzi-junit-rule", version.ref = "roborazzi" }
showkaseRuntime = { group = "com.airbnb.android", name = "showkase", version.ref = "showkase" }
showkaseProcessor = { group = "com.airbnb.android", name = "showkase-processor", version.ref = "showkase" }
uiautomator = { group = "androidx.test.uiautomator", name = "uiautomator", version.ref = "uiautomator" }
benchmark-macro-junit4 = { group = "androidx.benchmark", name = "benchmark-macro-junit4", version.ref = "benchmark-macro-junit4" }
profileinstaller = { group = "androidx.profileinstaller", name = "profileinstaller", version.ref = "profileinstaller" }

[plugins]
androidGradlePlugin = { id = "com.android.application", version.ref = "androidGradlePlugin" }
Expand All @@ -165,6 +172,8 @@ kspGradlePlugin = { id = "com.google.devtools.ksp", version.ref = "ksp" }
kotlinxKover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kover" }
detektGradlePlugin = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
ossLicensesPlugin = { id = "com.google.android.gms.oss-licenses-plugin", version.ref = "ossLicensesPlugin" }
androidTest = { id = "com.android.test", version.ref = "androidGradlePlugin" }
androidx-baselineprofile = { id = "androidx.baselineprofile", version.ref = "androidx-baselineprofile" }

[bundles]
plugins = [
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rootProject.name = "conference-app-2023"
include(
":app-android",
":app-ios-shared",
":baselineprofile",
":feature:main",
":feature:sessions",
":feature:about",
Expand Down
Loading