Skip to content

Commit

Permalink
Add baseline profile.
Browse files Browse the repository at this point in the history
  • Loading branch information
Swordfish90 committed May 9, 2024
1 parent aadde1a commit c74841d
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 27 deletions.
1 change: 1 addition & 0 deletions baselineprofile/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
50 changes: 50 additions & 0 deletions baselineprofile/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
plugins {
id("com.android.test")
id("org.jetbrains.kotlin.android")
id("androidx.baselineprofile")
}

android {
namespace = "com.swordfish.lemuroid.baselineprofile"
compileSdk = 34

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

kotlinOptions {
jvmTarget = "17"
}

defaultConfig {
minSdk = 28
targetSdk = 34

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

targetProjectPath = ":lemuroid-app"

flavorDimensions += listOf("opensource", "cores")
productFlavors {
create("free") { dimension = "opensource" }
create("play") { dimension = "opensource" }
create("bundle") { dimension = "cores" }
create("dynamic") { dimension = "cores" }
}

}

// 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 {
useConnectedDevices = true
}

dependencies {
implementation("androidx.test.ext:junit:1.1.5")
implementation("androidx.test.espresso:espresso-core:3.5.1")
implementation("androidx.test.uiautomator:uiautomator:2.2.0")
implementation("androidx.benchmark:benchmark-macro-junit4:1.2.3")
}
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,67 @@
package com.swordfish.lemuroid.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 :lemuroid-app: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.swordfish.lemuroid`."
rule.collect(
packageName = "com.swordfish.lemuroid",

// 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.swordfish.lemuroid.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.swordfish.lemuroid`."
rule.measureRepeated(
packageName = "com.swordfish.lemuroid",
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
}
)
}
}
27 changes: 8 additions & 19 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
import com.android.build.gradle.BaseExtension
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask

buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath(deps.plugins.android)
classpath(deps.plugins.navigationSafeArgs)
classpath(deps.plugins.kotlinGradlePlugin)
}
}

plugins {
plugins {
id("org.jetbrains.kotlin.jvm") version deps.versions.kotlin
id("com.github.ben-manes.versions") version "0.39.0"
id("org.jetbrains.kotlin.plugin.serialization") version "1.4.0"
id("org.jlleitschuh.gradle.ktlint") version "12.1.0"
id("com.android.test") version "8.4.0" apply false
id("org.jetbrains.kotlin.android") version deps.versions.kotlin apply false
id("androidx.baselineprofile") version "1.2.3" apply false
id("com.android.application") version "8.4.0" apply false
}

allprojects {
repositories {
google()
mavenCentral()
jcenter()
mavenLocal()
mavenCentral()
maven { setUrl("https://jitpack.io") }
}

Expand Down Expand Up @@ -85,20 +90,4 @@ tasks {
"clean"(Delete::class) {
delete(buildDir)
}

"dependencyUpdates"(DependencyUpdatesTask::class) {
resolutionStrategy {
componentSelection {
all {
val rejected =
listOf("alpha", "beta", "rc", "cr", "m")
.map { qualifier -> Regex("(?i).*[.-]$qualifier[.\\d-]*") }
.any { it.matches(candidate.version) }
if (rejected) {
reject("Release candidate")
}
}
}
}
}
}
4 changes: 3 additions & 1 deletion buildSrc/src/main/java/deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ object deps {
const val drawablePainter = "com.google.accompanist:accompanist-drawablepainter:${versions.accompanist}"
}
}
const val profileInstaller = "androidx.profileinstaller:profileinstaller:1.3.1"
}
object arch {
object work {
Expand Down Expand Up @@ -163,7 +164,8 @@ object deps {
}

object plugins {
const val android = "com.android.tools.build:gradle:8.2.2"
const val android = "com.android.tools.build:gradle:8.4.0"
const val navigationSafeArgs = "androidx.navigation:navigation-safe-args-gradle-plugin:${versions.navigation}"
const val kotlinGradlePlugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
}
}
5 changes: 1 addition & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ org.gradle.parallel=true
org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2560m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.caching=true

# https://github.com/gradle/kotlin-dsl/issues/311
org.gradle.configureondemand=false

# Kotlin
kotlin.incremental.usePreciseJavaTracking=true

# Android
android.builder.sdkDownload=true
android.enableD8.desugaring=true

# AndroidX
android.useAndroidX=true
android.enableJetifier=true
org.gradle.configuration-cache=true
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Mar 21 23:45:39 CET 2024
#Thu May 09 21:09:06 CEST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 4 additions & 0 deletions lemuroid-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id("kotlin-kapt")
id("androidx.navigation.safeargs.kotlin")
id("kotlinx-serialization")
id("androidx.baselineprofile")
}

android {
Expand Down Expand Up @@ -128,6 +129,9 @@ dependencies {
implementation(project(":lemuroid-metadata-libretro-db"))
implementation(project(":lemuroid-touchinput"))

"baselineProfile"(project(":baselineprofile"))
implementation(deps.libs.androidx.profileInstaller)

"bundleImplementation"(project(":bundled-cores"))

"freeImplementation"(project(":lemuroid-app-ext-free"))
Expand Down
11 changes: 10 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
@file:Suppress("ktlint")

pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}

include(
":retrograde-util",
":retrograde-app-shared",
Expand All @@ -8,7 +16,8 @@ include(
":lemuroid-metadata-libretro-db",
":lemuroid-app-ext-free",
":lemuroid-app-ext-play",
":bundled-cores"
":bundled-cores",
":baselineprofile"
)

project(":bundled-cores").projectDir = File("lemuroid-cores/bundled-cores")
Expand Down

0 comments on commit c74841d

Please sign in to comment.