Skip to content

Commit

Permalink
Add baseline profile module
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Oct 5, 2023
1 parent eda8b9d commit cc1183c
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ plugins {
id(libs.plugins.android.application.get().pluginId)
id(libs.plugins.kotlin.android.get().pluginId)
id(libs.plugins.kotlin.parcelize.get().pluginId)
id(libs.plugins.androidx.baselineprofile.get().pluginId)
}

android {
Expand Down Expand Up @@ -58,4 +59,7 @@ dependencies {
implementation(libs.androidx.material)
implementation(libs.androidx.constraint)
implementation(libs.glide)
implementation(libs.profileinstaller)

baselineProfile(project(":baselineprofile"))
}
1 change: 1 addition & 0 deletions baselineprofile/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
53 changes: 53 additions & 0 deletions baselineprofile/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import com.android.build.api.dsl.ManagedVirtualDevice

@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.android.test)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.androidx.baselineprofile)
}

android {
namespace = "com.skydoves.transformationlayout.benchmark"
compileSdk = 34

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

kotlinOptions {
jvmTarget = "1.8"
}

defaultConfig {
minSdk = 28
targetSdk = 34

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

targetProjectPath = ":app"

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

// 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.junit)
implementation(libs.espresso.core)
implementation(libs.uiautomator)
implementation(libs.benchmark.macro.junit4)
}
1 change: 1 addition & 0 deletions baselineprofile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest />
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.skydoves.transformationlayout.benchmark

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: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("com.skydoves.transformationlayoutdemo") {
// 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 com.skydoves.transformationlayout.benchmark

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 = "com.skydoves.transformationlayoutdemo",
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
}
)
}
}
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.android.test) apply false
alias(libs.plugins.androidx.baselineprofile) apply false
alias(libs.plugins.nexus.plugin)
alias(libs.plugins.spotless)
alias(libs.plugins.dokka)
Expand Down
14 changes: 13 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ androidxMaterial = "1.9.0"
constraintLayout = "2.1.4"
glide = "4.16.0"
spotless = "6.21.0"
junit = "1.1.5"
espresso-core = "3.5.1"
uiautomator = "2.2.0"
benchmark-macro-junit4 = "1.2.0-rc01"
androidx-baselineprofile = "1.2.0-beta05"
profileinstaller = "1.3.1"

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand All @@ -20,11 +26,17 @@ dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
nexus-plugin = { id = "com.vanniktech.maven.publish", version.ref = "nexusPlugin" }
kotlin-binary-compatibility = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "kotlinBinaryCompatibility" }
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
androidx-baselineprofile = { id = "androidx.baselineprofile", version.ref = "androidx-baselineprofile" }

[libraries]
agp = { module = "com.android.tools.build:gradle", version.ref = "agp" }
kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" }

androidx-material = { module = "com.google.android.material:material", version.ref = "androidxMaterial" }
androidx-constraint = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintLayout" }
glide = { module = "com.github.bumptech.glide:glide", version.ref = "glide" }
glide = { module = "com.github.bumptech.glide:glide", version.ref = "glide" }
junit = { group = "androidx.test.ext", name = "junit", version.ref = "junit" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espresso-core" }
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" }
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@file:Suppress("UnstableApiUsage")

include(":baselineprofile")


// Designed and developed by 2020 skydoves (Jaewoong Eum)
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
10 changes: 9 additions & 1 deletion transformationlayout/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ plugins {
id(libs.plugins.kotlin.android.get().pluginId)
id(libs.plugins.kotlin.parcelize.get().pluginId)
id(libs.plugins.nexus.plugin.get().pluginId)
id(libs.plugins.androidx.baselineprofile.get().pluginId)
}

apply(from = "${rootDir}/scripts/publish-module.gradle.kts")
Expand All @@ -45,7 +46,6 @@ android {
compileSdk = Configuration.compileSdk
defaultConfig {
minSdk = Configuration.minSdk
targetSdk = Configuration.targetSdk
}

resourcePrefix = "transformation"
Expand All @@ -68,6 +68,12 @@ android {
}
}

baselineProfile {
filter {
include("com.skydoves.transformationlayout.**")
}
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.freeCompilerArgs += listOf(
"-Xexplicit-api=strict"
Expand All @@ -76,4 +82,6 @@ tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {

dependencies {
implementation(libs.androidx.material)

baselineProfile(project(":baselineprofile"))
}

0 comments on commit cc1183c

Please sign in to comment.