Skip to content

Commit

Permalink
Merge pull request #988 from jakepurple13/baseline_test
Browse files Browse the repository at this point in the history
Starting to add benchmarks
  • Loading branch information
jakepurple13 authored Oct 30, 2023
2 parents 39076d1 + 2b0746e commit 7bd81aa
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 12 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions MangaWorldbaselineprofile/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
13 changes: 13 additions & 0 deletions MangaWorldbaselineprofile/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
id("otaku-benchmark")
}

android {
namespace = "com.programmersbox.mangaworldbaselineprofile"
targetProjectPath = ":mangaworld"
}

dependencies {

}
1 change: 1 addition & 0 deletions MangaWorldbaselineprofile/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,59 @@
package com.programmersbox.mangaworldbaselineprofile

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 :mangaworld: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.programmersbox.mangaworld") {
// 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()
device.click(40, 40)
Thread.sleep(5000)

// 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,71 @@
package com.programmersbox.mangaworldbaselineprofile

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 :MangaWorldbaselineprofile: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.programmersbox.mangaworld",
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
}
)
}
}
11 changes: 0 additions & 11 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ buildscript {
}
}

allprojects {
repositories {
google()
gradlePluginPortal()
mavenCentral()
maven("https://jitpack.io")
//maven { url "https://dl.bintray.com/piasy/maven" }
maven("https://oss.sonatype.org/content/repositories/snapshots")
}
}

subprojects {
afterEvaluate {
when {
Expand Down
6 changes: 6 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ gradlePlugin {
id = "otaku-multiplatform"
implementationClass = "plugins.MultiplatformLibraryPlugin"
}

register("otaku-benchmark") {
id = "otaku-benchmark"
implementationClass = "plugins.BenchmarkPlugin"
}
}
}

Expand All @@ -45,4 +50,5 @@ dependencies {
implementation(libs.gradle)
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${libs.versions.kotlin.get()}")
implementation(libs.easylauncher)
implementation(libs.androidx.baselineprofile.gradle.plugin)
}
69 changes: 69 additions & 0 deletions buildSrc/src/main/kotlin/plugins/BenchmarkPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package plugins

import AppInfo
import androidx.baselineprofile.gradle.producer.BaselineProfileProducerExtension
import com.android.build.api.dsl.BaselineProfile
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.TestExtension
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.plugins
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.util.Locale
import kotlin.reflect.KClass

class BenchmarkPlugin : Plugin<Project> {

override fun apply(target: Project) {
target.pluginManager.apply {
apply("kotlin-android")
apply("com.android.test")
apply("androidx.baselineprofile")
}
target.tasks.withType<KotlinCompile> { kotlinOptions { jvmTarget = "1.8" } }
target.configureAndroidBase()
}

private fun Project.configureAndroidBase() {
extensions.findByType(TestExtension::class)?.apply {
compileSdk = AppInfo.compileVersion

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

defaultConfig {
minSdk = 28
targetSdk = 34

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

flavorDimensions += listOf("version")
productFlavors {
create("noFirebase") { dimension = "version" }
create("full") { dimension = "version" }
}

dependencies {
implementation(libs.findLibrary("junit").get())
implementation(libs.findLibrary("uiautomator").get())
implementation(libs.findLibrary("espresso-core").get())
implementation(libs.findLibrary("benchmark-macro-junit4").get())
}
}
extensions.findByType(BaselineProfileProducerExtension::class)?.apply {
// This is the configuration block for the Baseline Profile plugin.
// You can specify to run the generators on a managed devices or connected devices.
useConnectedDevices = true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class MultiplatformLibraryPlugin : Plugin<Project> {
dependencyHandling: DependencyHandling,
) {
targetHierarchy.default()
android {
androidTarget {
compilations.all {
kotlinOptions { jvmTarget = "1.8" }
}
Expand Down
14 changes: 14 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[versions]
androidxBaselineprofileGradlePlugin = "1.2.0"
easylauncher = "6.2.0"
firebaseCrashlyticsGradle = "2.9.9"
googleServices = "4.4.0"
Expand Down Expand Up @@ -37,13 +38,21 @@ piasy = "1.8.1"
autoBinding = "1.1-beta04"
media3Version = "1.1.1"

junit = "1.1.5"
espresso-core = "3.5.1"
uiautomator = "2.2.0"
benchmark-macro-junit4 = "1.2.0"
androidx-baselineprofile = "1.2.0"
profileinstaller = "1.3.1"

[plugins]
kotlinGradle = { id = "org.jetbrains.kotlin:kotlin.gradle.plugin", version.ref = "kotlin" }
kotlinSerializationGradle = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
navSafeArgsGradle = { id = "androidx.navigation:navigation-safe-args-gradle-plugin", version.ref = "navVersion" }
ksp = { id = "com.google.devtools.ksp", version = "1.9.10-1.0.13" }

[libraries]
androidx-baselineprofile-gradle-plugin = { module = "androidx.baselineprofile:androidx.baselineprofile.gradle.plugin", version.ref = "androidx-baselineprofile" }
easylauncher = { module = "com.project.starter:easylauncher", version.ref = "easylauncher" }
firebase-crashlytics-gradle = { module = "com.google.firebase:firebase-crashlytics-gradle", version.ref = "firebaseCrashlyticsGradle" }
google-services = { module = "com.google.gms:google-services", version.ref = "googleServices" }
Expand Down Expand Up @@ -268,6 +277,11 @@ sketch-compose = { module = "io.github.panpf.sketch3:sketch-compose", version.re
sketch-zoom = { module = "io.github.panpf.sketch3:sketch-zoom", version.ref = "sketchVersion" }
sketch-extensions = { module = "io.github.panpf.sketch3:sketch-extensions", version.ref = "sketchVersion" }
sketch-gif = { module = "io.github.panpf.sketch3:sketch-gif-koral", version.ref = "sketchVersion" }
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" }

[bundles]
compose = [
Expand Down
3 changes: 3 additions & 0 deletions mangaworld/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
id("com.mikepenz.aboutlibraries.plugin")
id("otaku-easylauncher")
alias(libs.plugins.ksp)
id("androidx.baselineprofile")
}

android {
Expand All @@ -28,6 +29,7 @@ dependencies {
implementation(libs.swiperefresh)
implementation(libs.recyclerview)
implementation(libs.googlePlayAds)
implementation(libs.profileinstaller)
testImplementation(TestDeps.junit)
androidTestImplementation(TestDeps.androidJunit)
androidTestImplementation(TestDeps.androidEspresso)
Expand All @@ -46,6 +48,7 @@ dependencies {
implementation(libs.bundles.ziplineLibs)

implementation(libs.glide)
baselineProfile(projects.mangaWorldbaselineprofile)
ksp(libs.glideCompiler)
// Excludes the support library because it"s already included by Glide.
implementation(libs.glideRecyclerview) { isTransitive = false }
Expand Down
23 changes: 23 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
maven("https://jitpack.io")
//maven { url "https://dl.bintray.com/piasy/maven" }
maven("https://oss.sonatype.org/content/repositories/snapshots")
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven("https://www.jitpack.io")
maven("https://oss.sonatype.org/content/repositories/snapshots/")
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
}

enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")

include(
Expand Down Expand Up @@ -26,3 +48,4 @@ include(
)
include(":manga_sources:defaultmangasources")
include(":anime_sources:defaultanimesources")
include(":MangaWorldbaselineprofile")

0 comments on commit 7bd81aa

Please sign in to comment.