Skip to content

Commit

Permalink
feat(baselineprofile): Add BaseLineProfile && Benchmark
Browse files Browse the repository at this point in the history
feat(baselineprofile): Add Warm Hot BenchMark

feat(baselineprofile): Remove Test Device
  • Loading branch information
kez-lab authored and l2hyunwoo committed Apr 22, 2024
1 parent 4752e72 commit e836aec
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 4 deletions.
19 changes: 16 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ plugins {
alias(libs.plugins.secret)
alias(libs.plugins.sentry)
alias(libs.plugins.app.distribution)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.androidx.baselineprofile)
}

val properties =
Expand Down Expand Up @@ -73,14 +73,14 @@ android {
}

buildTypes {
getByName("debug") {
val debug by getting {
applicationIdSuffix = ".debug"
firebaseAppDistribution {
artifactType = "APK"
groups = "app-team"
}
}
release {
val release by getting {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(
Expand All @@ -89,6 +89,17 @@ android {
)
signingConfig = signingConfigs.getByName("release")
}
val benchmark by creating {
initWith(release)
matchingFallbacks.add("release")
signingConfig = signingConfigs.getByName("debug")
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
isMinifyEnabled = true
isDebuggable = false
}
}
applicationVariants.all {
val variant = this
Expand Down Expand Up @@ -159,6 +170,8 @@ dependencies {
debugImplementation(libs.bundles.compose.android.test)

implementation(libs.coil.core)
implementation(libs.profileinstaller)
baselineProfile(projects.benchmark)
}

secrets {
Expand Down
1 change: 1 addition & 0 deletions benchmark/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
53 changes: 53 additions & 0 deletions benchmark/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.androidTest)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.androidx.baselineprofile)
}

android {
namespace = "org.sopt.official.benchmark"
compileSdk = 34

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

kotlinOptions {
jvmTarget = "17"
}

defaultConfig {
minSdk = 28
targetSdk = 34

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

buildTypes {
val benchmark by creating {
isDebuggable = false
signingConfig = signingConfigs.getByName("debug")
matchingFallbacks.add("release")
}
}

targetProjectPath = ":app"
experimentalProperties["android.experimental.self-instrumenting"] = true
}

dependencies {
implementation(libs.androidx.test.junit)
implementation(libs.androidx.test.espresso)
implementation(libs.androidx.uiautomator)
implementation(libs.benchmark.macro.junit4)
}

androidComponents {
beforeVariants {
it.enable = it.buildType == "benchmark"
}
}
1 change: 1 addition & 0 deletions benchmark/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,24 @@
package org.sopt.official.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

@RunWith(AndroidJUnit4::class)
@LargeTest
class BaselineProfileGenerator {

@get:Rule
val rule = BaselineProfileRule()

@Test
fun generate() {
rule.collect(PACKAGE_NAME) {
pressHome()
startActivityAndWait()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.sopt.official.benchmark

internal const val PACKAGE_NAME: String = "org.sopt.official"
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.sopt.official.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.internal.runner.junit4.AndroidJUnit4ClassRunner
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4ClassRunner::class)
class ColdStartupBenchmark : AbstractStartupBenchmark(StartupMode.COLD)

/**
* Run this benchmark from Studio to see startup measurements, and captured system traces
* for investigating your app's performance from a warm state.
*/
@RunWith(AndroidJUnit4ClassRunner::class)
class WarmStartupBenchmark : AbstractStartupBenchmark(StartupMode.WARM)

/**
* Run this benchmark from Studio to see startup measurements, and captured system traces
* for investigating your app's performance from a hot state.
*/
@RunWith(AndroidJUnit4ClassRunner::class)
class HotStartupBenchmark : AbstractStartupBenchmark(StartupMode.HOT)

/**
* Base class for benchmarks with different startup modes.
* Enables app startups from various states of baseline profile or [CompilationMode]s.
*/
abstract class AbstractStartupBenchmark(private val startupMode: StartupMode) {
@get:Rule
val benchmarkRule = MacrobenchmarkRule()

@Test
fun startupNoCompilation() = startup(CompilationMode.None())

@Test
fun startupBaselineProfileDisabled() = startup(
CompilationMode.Partial(
baselineProfileMode = BaselineProfileMode.Disable,
warmupIterations = 3,
),
)

@Test
fun startupBaselineProfile() =
startup(CompilationMode.Partial(baselineProfileMode = BaselineProfileMode.Require))

@Test
fun startupFullCompilation() = startup(CompilationMode.Full())

private fun startup(compilationMode: CompilationMode) = benchmarkRule.measureRepeated(
packageName = PACKAGE_NAME,
metrics = listOf(StartupTimingMetric()),
compilationMode = compilationMode,
iterations = 5,
startupMode = startupMode,
setupBlock = {
pressHome()
},
measureBlock = {
startActivityAndWait()
}
)
}
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ plugins {
alias(libs.plugins.app.distribution) apply false
alias(libs.plugins.spotless) apply false
alias(libs.plugins.org.jetbrains.kotlin.jvm) apply false
alias(libs.plugins.androidTest) apply false
alias(libs.plugins.androidx.baselineprofile) apply false
}

subprojects {
Expand Down
10 changes: 9 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ process-pheonix = "3.0.0"
amplitude = "1.16.7"
spotless = "6.25.0"
constraintlayout = "2.1.4"
org-jetbrains-kotlin-jvm = "1.9.20"
benchmark-macro-junit4 = "1.2.0"
androidx-baselineprofile = "1.2.0"
profileinstaller = "1.3.1"

[libraries]
agp = { module = "com.android.tools.build:gradle", version.ref = "gradleplugin" }
Expand Down Expand Up @@ -176,6 +180,8 @@ androidx-lifecycle-process = { group = "androidx.lifecycle", name = "lifecycle-p
amplitude-android = { module = "com.amplitude:analytics-android", version.ref = "amplitude" }
android-lottie = { module = "com.airbnb.android:lottie", version.ref = "lottie" }
constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
benchmark-macro-junit4 = { group = "androidx.benchmark", name = "benchmark-macro-junit4", version.ref = "benchmark-macro-junit4" }
profileinstaller = { group = "androidx.profileinstaller", name = "profileinstaller", version.ref = "profileinstaller" }

[bundles]
compose = [
Expand Down Expand Up @@ -219,4 +225,6 @@ sentry = { id = "io.sentry.android.gradle", version.ref = "sentry" }
junit5 = { id = "de.mannodermaus.android-junit5", version.ref = "junit5-plugin" }
app-distribution = { id = "com.google.firebase.appdistribution", version.ref = "app-distribution" }
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
org-jetbrains-kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
org-jetbrains-kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "org-jetbrains-kotlin-jvm" }
androidTest = { id = "com.android.test", version.ref = "gradleplugin" }
androidx-baselineprofile = { id = "androidx.baselineprofile", version.ref = "androidx-baselineprofile" }
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ include(
":feature:mypage",
":feature:soptamp",
":feature:poke",
":benchmark"
)

0 comments on commit e836aec

Please sign in to comment.