Skip to content

Commit

Permalink
add macrobenchmark and trace generation
Browse files Browse the repository at this point in the history
  • Loading branch information
matejdro committed Dec 21, 2023
1 parent 38b2ff3 commit beb902c
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 20 deletions.
1 change: 1 addition & 0 deletions app/benchmark/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
56 changes: 56 additions & 0 deletions app/benchmark/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
plugins {
id("com.android.test")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "net.composegridperformance.benchmark"
compileSdk = 34

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

kotlinOptions {
jvmTarget = "17"
}

defaultConfig {
minSdk = 24
targetSdk = 34

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments["androidx.benchmark.perfettoSdkTracing.enable"] = "true"

}

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("release") {
isDebuggable = true
signingConfig = getByName("debug").signingConfig
matchingFallbacks += listOf("release")
}
}

targetProjectPath = ":app"
experimentalProperties["android.experimental.self-instrumenting"] = 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.2")
implementation("androidx.tracing:tracing-perfetto:1.0.0")
implementation("androidx.tracing:tracing-perfetto-binary:1.0.0")
}

androidComponents {
beforeVariants(selector().all()) {
it.enable = it.buildType == "release"
}
}
1 change: 1 addition & 0 deletions app/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,80 @@
package net.composegridperformance.benchmark

import android.util.TypedValue
import androidx.benchmark.macro.CompilationMode
import androidx.benchmark.macro.FrameTimingMetric
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.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Until
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.math.roundToInt

/**
* 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 `<profileable android:shell="true" />` to your app's manifest, within the `<application>` tag
*
* Run this benchmark from Studio to see startup measurements, and captured system traces
* for investigating your app's performance.
*/
@RunWith(AndroidJUnit4::class)
class ScrollBenchmark {
@get:Rule
val benchmarkRule = MacrobenchmarkRule()

@Test
fun scrollImageList() = benchmarkRule.measureRepeated(
packageName = "net.composegridperformance",
metrics = listOf(FrameTimingMetric()),
iterations = 5,
startupMode = StartupMode.WARM,
setupBlock = {
pressHome()
startActivityAndWait()

device.wait(Until.findObject(By.text("Image List (Coil)")), 10_000)
device.findObject(By.text("Image List (Coil)")).click()

Thread.sleep(500)
}

) {
val scrollOffset = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
SCROLL_LENGTH_DP,
InstrumentationRegistry.getInstrumentation().context.resources.displayMetrics
).roundToInt()

device.swipe(
/* startX = */ device.displayWidth / 2,
/* startY = */ device.displayHeight / 2 + scrollOffset / 2,
/* endX = */ device.displayWidth / 2,
/* endY = */ device.displayHeight / 2 - scrollOffset / 2,
/* steps = */ SCROLL_STEPS
)
Thread.sleep(SCROLL_SLEEP)

device.swipe(
/* startX = */ device.displayWidth / 2,
/* startY = */ device.displayHeight / 2 - scrollOffset / 2,
/* endX = */ device.displayWidth / 2,
/* endY = */ device.displayHeight / 2 + scrollOffset / 2,
/* steps = */ SCROLL_STEPS
)
Thread.sleep(SCROLL_SLEEP)
}
}

private const val SCROLL_SLEEP = 1000L
private const val SCROLL_STEPS = 3
private const val SCROLL_LENGTH_DP = 1000f
8 changes: 7 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ android {
"proguard-rules.pro"
)
}
create("benchmark") {
initWith(buildTypes.getByName("release"))
matchingFallbacks += listOf("release")
isDebuggable = false
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
Expand Down Expand Up @@ -70,6 +75,7 @@ dependencies {
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("androidx.profileinstaller:profileinstaller:1.3.1")
implementation("androidx.compose.runtime:runtime-tracing:1.0.0-beta01")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
Expand Down Expand Up @@ -98,4 +104,4 @@ dependencies {
// Network
implementation ("com.squareup.retrofit2:retrofit:2.9.0")
implementation ("com.squareup.retrofit2:converter-moshi:2.9.0")
}
}
39 changes: 20 additions & 19 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".MainApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ComposeGridPerformance"
tools:targetApi="31">

<profileable android:shell="true"/>
android:name=".MainApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ComposeGridPerformance"
tools:targetApi="31">
<profileable
android:shell="true"
tools:targetApi="29" />

<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.ComposeGridPerformance">
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.ComposeGridPerformance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -31,4 +32,4 @@
</activity>
</application>

</manifest>
</manifest>
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ dependencyResolutionManagement {
rootProject.name = "ComposeGridPerformance"
include(":app")
include(":app:baselineprofile")
include(":app:benchmark")

0 comments on commit beb902c

Please sign in to comment.