Skip to content

Commit

Permalink
build: baseline-profile plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyroid committed Oct 25, 2024
1 parent 6af3406 commit 274ed31
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 181 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Android CI
name: Android SNAPSHOT CI

on:
push:
Expand All @@ -11,7 +11,7 @@ on:
- 'fastlane/**'
- '!.github/workflows/**'
pull_request:
branches: [ "master", "ci-1.14.0-rc04-benchmark" ]
branches: [ "master" ]
workflow_dispatch:

jobs:
Expand All @@ -32,10 +32,7 @@ jobs:
run: chmod +x gradlew

- name: Generate baseline profile
run: ./gradlew :benchmark:Pixel5Api31StableChannelRichCodecBenchmarkAndroidTest --rerun-tasks -P android.testInstrumentationRunnerArguments.class=com.m3u.benchmark.BaselineProfileGenerator

- name: Copy and Rename baseline profile
run: cp benchmark/build/outputs/managed_device_android_test_additional_output/benchmark/flavours/stableChannelRichCodec/Pixel5Api31/BaselineProfileGenerator_generateBaselineProfile-baseline-prof.txt androidApp/src/main/baseline-prof.txt
run: ./gradlew :androidApp:generateSnapshotChannelRichCodecReleaseBaselineProfile

- name: Build with Gradle
run: ./gradlew :androidApp:assembleSnapshotChannelRichCodecRelease
Expand Down

This file was deleted.

5 changes: 4 additions & 1 deletion androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ android {
versionName = "1.14.0-rc03"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments["androidx.benchmark.profiling.mode"] = "MethodTracing"
}
flavorDimensions += setOf("channel", "codec")
productFlavors {
Expand Down Expand Up @@ -131,6 +130,9 @@ dependencies {
implementation(project(":feature:playlist-configuration"))
implementation(project(":feature:crash"))

implementation(libs.androidx.profileinstaller)
baselineProfile(project(":baselineprofile"))

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.activity.compose)
Expand All @@ -143,6 +145,7 @@ dependencies {
implementation(libs.androidx.core.splashscreen)

implementation(libs.google.dagger.hilt)
baselineProfile(project(":baselineprofile"))
ksp(libs.google.dagger.hilt.compiler)
implementation(libs.androidx.hilt.navigation.compose)

Expand Down
15 changes: 14 additions & 1 deletion annotation/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
kotlin("jvm")
alias(libs.plugins.org.jetbrains.kotlin.jvm)
}

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

kotlin {
compilerOptions {
jvmTarget = JvmTarget.JVM_17
}
}

dependencies {
Expand Down
File renamed without changes.
61 changes: 61 additions & 0 deletions baselineprofile/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
@file:Suppress("UnstableApiUsage")

import com.android.build.api.dsl.ManagedVirtualDevice

plugins {
alias(libs.plugins.com.android.test)
alias(libs.plugins.org.jetbrains.kotlin.android)
alias(libs.plugins.androidx.baselineprofile)
}

android {
namespace = "com.m3u.baselineprofile"
compileSdk = 34
defaultConfig {
minSdk = 28
targetSdk = 34
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
kotlinOptions {
jvmTarget = "17"
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

targetProjectPath = ":androidApp"

flavorDimensions += listOf("channel", "codec")
productFlavors {
create("stableChannel") { dimension = "channel" }
create("snapshotChannel") { dimension = "channel" }
create("richCodec") { dimension = "codec" }
create("liteCodec") { dimension = "codec" }
}

// This code creates the gradle managed device used to generate baseline profiles.
// To use GMD please invoke generation through the command line:
// ./gradlew :androidApp:generateBaselineProfile
testOptions.managedDevices.devices {
create<ManagedVirtualDevice>("pixel6Api34") {
device = "Pixel 6"
apiLevel = 34
systemImageSource = "aosp"
}
}
}

// 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.androidx.test.ext.junit)
implementation(libs.androidx.test.espresso.espresso.core)
implementation(libs.androidx.test.uiautomator.uiautomator)
implementation(libs.androidx.benchmark.benchmark.macro.junit4)
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.m3u.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 in Android Studio or
* the equivalent `generateBaselineProfile` gradle task:
* ```
* ./gradlew :androidApp:generateReleaseBaselineProfile
* ```
* 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.
*
* When using this class to generate a baseline profile, only API 33+ or rooted API 28+ are supported.
*
* The minimum required version of androidx.benchmark to generate a baseline profile is 1.2.0.
**/
@RunWith(AndroidJUnit4::class)
@LargeTest
class BaselineProfileGenerator {

@get:Rule
val rule = BaselineProfileRule()

@Test
fun generate() {
// This example works only with the variant with application id `com.m3u.androidApp.snapshot`."
rule.collect(
packageName = "com.m3u.androidApp",

// See: https://d.android.com/topic/performance/baselineprofiles/dex-layout-optimizations
includeInStartupProfile = true
) {
// 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,75 @@
package com.m3u.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 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) {
// This example works only with the variant with application id `com.m3u.androidApp.snapshot`."
rule.measureRepeated(
packageName = "com.m3u.androidApp.snapshot",
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
}
)
}
}
87 changes: 0 additions & 87 deletions benchmark/build.gradle.kts

This file was deleted.

Loading

0 comments on commit 274ed31

Please sign in to comment.