Skip to content

Commit

Permalink
Tweak benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
mlykotom committed Apr 24, 2024
1 parent 318d7a6 commit 42cf1a5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
6 changes: 2 additions & 4 deletions coil-benchmark/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ setupTestModule(name = "coil.benchmark", config = true) {
minSdk = 23
buildConfigField("String", "PROJECT", "\"$targetProject\"")
// TODO temporary, should pass with run configuration
testInstrumentationRunnerArguments["androidx.benchmark.fullTracing.enable"] = "true"
// TODO just temporary
testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "LOW-BATTERY"
testInstrumentationRunnerArguments["androidx.benchmark.profiling.mode"] = "MethodTracing"
// testInstrumentationRunnerArguments["androidx.benchmark.fullTracing.enable"] = "true"
// testInstrumentationRunnerArguments["androidx.benchmark.profiling.mode"] = "MethodTracing"
}
buildTypes {
create("benchmark") {
Expand Down
51 changes: 36 additions & 15 deletions coil-benchmark/src/main/java/coil/benchmark/ScrollBenchmark.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.benchmark.macro.FrameTimingMetric
import androidx.benchmark.macro.StartupMode
import androidx.benchmark.macro.StartupTimingMetric
import androidx.benchmark.macro.TraceMetric
import androidx.benchmark.macro.TraceSectionMetric
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
import androidx.benchmark.perfetto.ExperimentalPerfettoTraceProcessorApi
import androidx.benchmark.perfetto.PerfettoTraceProcessor
Expand Down Expand Up @@ -45,18 +44,17 @@ class ScrollBenchmark {
metrics = listOf(
FrameTimingMetric(),
StartupTimingMetric(),
TraceSectionMetric(
MicroSecondsTraceSectionMetric(
"coil.compose.rememberAsyncImagePainter%",
TraceSectionMetric.Mode.Sum,
MicroMode.Sum,
),
// AverageTraceSectionMetric("coil.compose.rememberAsyncImagePainter%"),
TraceSectionMetric(
MicroSecondsTraceSectionMetric(
"AsyncImagePainter.onRemembered",
TraceSectionMetric.Mode.Sum,
MicroMode.Sum,
),
AverageTraceSectionMetric("AsyncImagePainter.onRemembered"),
MicroSecondsTraceSectionMetric("AsyncImagePainter.onRemembered", MicroMode.Average),
),
iterations = 3,
iterations = 10, // 20,
startupMode = StartupMode.COLD,
compilationMode = compilationMode,
measureBlock = {
Expand All @@ -71,9 +69,17 @@ class ScrollBenchmark {
}
}

// Benchmark to give average/sum in microseconds measurements

enum class MicroMode {
Sum,
Average
}

@OptIn(ExperimentalMetricApi::class)
class AverageTraceSectionMetric(
class MicroSecondsTraceSectionMetric(
private val sectionName: String,
private val mode: MicroMode,
private val label: String = sectionName,
private val targetPackageOnly: Boolean = true,
) : TraceMetric() {
Expand All @@ -89,11 +95,26 @@ class AverageTraceSectionMetric(
packageName = if (targetPackageOnly) captureInfo.targetPackageName else null,
)

return listOf(
Measurement(
name = label + "AverageMs",
data = slices.sumOf { it.dur } / 1_000_000.0 / slices.size,
),
)
return when (mode) {
MicroMode.Sum -> listOf(
Measurement(
name = sectionName + "_µs",
// note, this duration assumes non-reentrant slices
data = slices.sumOf { it.dur } / 1_000.0,
),
Measurement(
name = sectionName + "Count",
data = slices.size.toDouble(),
),
)

MicroMode.Average -> listOf(
Measurement(
name = label + "Average_µs",
data = slices.sumOf { it.dur } / 1_000.0 / slices.size,
),
)
}

}
}

0 comments on commit 42cf1a5

Please sign in to comment.