forked from PauliusVal/ComposeGridPerformance
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add macrobenchmark and trace generation
- Loading branch information
Showing
7 changed files
with
166 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<manifest /> |
80 changes: 80 additions & 0 deletions
80
app/benchmark/src/main/java/net/composegridperformance/benchmark/ScrollBenchmark.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters