From b4a306ef794e69d2b94a94b361633e6047476fdb Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Thu, 26 Oct 2023 14:58:33 +0100 Subject: [PATCH] =?UTF-8?q?Optional=20challange=20-=20P01=20=E2=80=93=20Me?= =?UTF-8?q?asure=20the=20performance=20of=20the=20app?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- androidApp/build.gradle.kts | 5 ++ androidApp/src/main/AndroidManifest.xml | 6 ++- benchmark/.gitignore | 1 + benchmark/build.gradle.kts | 53 +++++++++++++++++++ benchmark/src/main/AndroidManifest.xml | 1 + .../benchmark/ExampleStartupBenchmark.kt | 39 ++++++++++++++ gradle/libs.versions.toml | 8 +++ settings.gradle.kts | 2 + 8 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 benchmark/.gitignore create mode 100644 benchmark/build.gradle.kts create mode 100644 benchmark/src/main/AndroidManifest.xml create mode 100644 benchmark/src/main/java/com/bumble/appyx/livemosaic/android/benchmark/ExampleStartupBenchmark.kt diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index ad8f1707..287d110d 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -24,6 +24,11 @@ android { isMinifyEnabled = true signingConfig = signingConfigs.getByName("debug") } + create("benchmark") { + initWith(buildTypes.getByName("release")) + matchingFallbacks += listOf("release") + isDebuggable = false + } } compileOptions { sourceCompatibility = JavaVersion.VERSION_17 diff --git a/androidApp/src/main/AndroidManifest.xml b/androidApp/src/main/AndroidManifest.xml index 0a4b4f42..d93f57c1 100644 --- a/androidApp/src/main/AndroidManifest.xml +++ b/androidApp/src/main/AndroidManifest.xml @@ -7,13 +7,17 @@ android:supportsRtl="true" android:theme="@style/AppTheme" tools:ignore="DataExtractionRules"> + + + - + + \ No newline at end of file diff --git a/benchmark/.gitignore b/benchmark/.gitignore new file mode 100644 index 00000000..42afabfd --- /dev/null +++ b/benchmark/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/benchmark/build.gradle.kts b/benchmark/build.gradle.kts new file mode 100644 index 00000000..c8864d51 --- /dev/null +++ b/benchmark/build.gradle.kts @@ -0,0 +1,53 @@ +@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed +plugins { + id("com.android.test") + kotlin("android") +} + +android { + namespace = "com.bumble.appyx.livemosaic.android.benchmark" + compileSdk = 34 + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + + kotlinOptions { + jvmTarget = "1.8" + } + + defaultConfig { + minSdk = 23 + targetSdk = 34 + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + // This benchmark buildType is used for benchmarking, and should function like your + // release build (for example, with minification on). It"s signed with a debug key + // for easy local/CI testing. + create("benchmark") { + isDebuggable = true + signingConfig = getByName("debug").signingConfig + matchingFallbacks += listOf("release") + } + } + + targetProjectPath = ":androidApp" + experimentalProperties["android.experimental.self-instrumenting"] = true +} + +dependencies { + implementation(libs.androidx.test.ext.junit) + implementation(libs.espresso.core) + implementation(libs.uiautomator) + implementation(libs.benchmark.macro.junit4) +} + +androidComponents { + beforeVariants(selector().all()) { + it.enable = it.buildType == "benchmark" + } +} \ No newline at end of file diff --git a/benchmark/src/main/AndroidManifest.xml b/benchmark/src/main/AndroidManifest.xml new file mode 100644 index 00000000..227314ee --- /dev/null +++ b/benchmark/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/benchmark/src/main/java/com/bumble/appyx/livemosaic/android/benchmark/ExampleStartupBenchmark.kt b/benchmark/src/main/java/com/bumble/appyx/livemosaic/android/benchmark/ExampleStartupBenchmark.kt new file mode 100644 index 00000000..ad4089b5 --- /dev/null +++ b/benchmark/src/main/java/com/bumble/appyx/livemosaic/android/benchmark/ExampleStartupBenchmark.kt @@ -0,0 +1,39 @@ +package com.bumble.appyx.livemosaic.android.benchmark + +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 org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +/** + * This is an example startup benchmark. + * + * It navigates to the device's home screen, and launches the default activity. + * + * Before running this benchmark: + * 1) switch your app's active build variant in the Studio (affects Studio runs only) + * 2) add `` to your app's manifest, within the `` tag + * + * Run this benchmark from Studio to see startup measurements, and captured system traces + * for investigating your app's performance. + */ +@RunWith(AndroidJUnit4::class) +class ExampleStartupBenchmark { + @get:Rule + val benchmarkRule = MacrobenchmarkRule() + + @Test + fun startup() = benchmarkRule.measureRepeated( + packageName = "com.bumble.appyx.livemosaic.android", + metrics = listOf(StartupTimingMetric()), + iterations = 5, + startupMode = StartupMode.COLD + ) { + pressHome() + startActivityAndWait() + } +} \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a16fc303..7068315c 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -25,6 +25,10 @@ resources = "0.22.3" serialization-json = "1.5.0" ktor = "2.3.4" uuid = "9.0.0" +androidxtestext = "1.1.5" +espresso-core = "3.5.1" +uiautomator = "2.2.0" +benchmark-macro-junit4 = "1.2.0-beta01" [libraries] androidx-activity-compose = "androidx.activity:activity-compose:1.7.2" @@ -91,6 +95,10 @@ detekt-compose = "io.nlopez.compose.rules:detekt:0.3.0" toolargetool = "com.gu.android:toolargetool:0.3.0" symbol-processing-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" } +androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidxtestext" } +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" } [plugins] detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" } diff --git a/settings.gradle.kts b/settings.gradle.kts index 0b4cb3b2..ad0b7309 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -8,6 +8,7 @@ pluginManagement { plugins { kotlin("multiplatform") id("org.jetbrains.compose") + id("com.android.test") } } @@ -27,3 +28,4 @@ include(":androidApp") include(":desktopApp") include(":webApp") include(":shared") +include(":benchmark")