|
| 1 | +package com.dongchyeon.baselineprofile |
| 2 | + |
| 3 | +import android.content.ComponentName |
| 4 | +import android.content.Intent |
| 5 | +import androidx.benchmark.macro.CompilationMode |
| 6 | +import androidx.benchmark.macro.FrameTimingMetric |
| 7 | +import androidx.benchmark.macro.StartupMode |
| 8 | +import androidx.benchmark.macro.junit4.MacrobenchmarkRule |
| 9 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 10 | +import androidx.test.filters.LargeTest |
| 11 | +import androidx.test.platform.app.InstrumentationRegistry |
| 12 | +import androidx.test.uiautomator.By |
| 13 | +import androidx.test.uiautomator.Until |
| 14 | +import org.junit.Rule |
| 15 | +import org.junit.Test |
| 16 | +import org.junit.runner.RunWith |
| 17 | + |
| 18 | +@RunWith(AndroidJUnit4::class) |
| 19 | +@LargeTest |
| 20 | +class OrbitPickerBenchmarks { |
| 21 | + |
| 22 | + @get:Rule |
| 23 | + val rule = MacrobenchmarkRule() |
| 24 | + |
| 25 | + @Test |
| 26 | + fun orbitPickerScrollCompilationNone() = benchmark(CompilationMode.None()) |
| 27 | + |
| 28 | + @Test |
| 29 | + fun orbitPickerScrollCompilationBaselineProfile() = benchmark(CompilationMode.Partial()) |
| 30 | + |
| 31 | + private fun benchmark(compilationMode: CompilationMode) { |
| 32 | + val targetPackage = InstrumentationRegistry.getArguments().getString("targetAppId") |
| 33 | + ?: throw IllegalStateException("targetAppId not passed as instrumentation runner arg") |
| 34 | + |
| 35 | + rule.measureRepeated( |
| 36 | + packageName = targetPackage, |
| 37 | + metrics = listOf(FrameTimingMetric()), |
| 38 | + compilationMode = compilationMode, |
| 39 | + startupMode = StartupMode.COLD, |
| 40 | + iterations = 10, |
| 41 | + setupBlock = { |
| 42 | + killProcess() |
| 43 | + pressHome() |
| 44 | + }, |
| 45 | + measureBlock = { |
| 46 | + val intent = Intent().apply { |
| 47 | + component = ComponentName(targetPackage, BENCHMARK_ACTIVITY_NAME) |
| 48 | + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 49 | + putExtra(BENCHMARK_SCREEN_EXTRA, ORBIT_PICKER_SCREEN_KEY) |
| 50 | + } |
| 51 | + |
| 52 | + startActivityAndWait(intent) |
| 53 | + |
| 54 | + device.wait(Until.hasObject(By.desc(ORBIT_PICKER_SEMANTICS)), 5_000) |
| 55 | + val picker = device.findObject(By.desc(ORBIT_PICKER_SEMANTICS)) |
| 56 | + ?: error("OrbitPicker root not found") |
| 57 | + |
| 58 | + val bounds = picker.visibleBounds |
| 59 | + val x = bounds.centerX() + bounds.width() / 4 |
| 60 | + val startY = bounds.centerY() + bounds.height() / 4 |
| 61 | + val endY = bounds.centerY() - bounds.height() / 4 |
| 62 | + |
| 63 | + device.swipe(x, startY, x, endY, 30) |
| 64 | + device.waitForIdle() |
| 65 | + }, |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + private companion object { |
| 70 | + private const val BENCHMARK_ACTIVITY_NAME = |
| 71 | + "com.yapp.orbit.benchmark.BenchmarkHostActivity" |
| 72 | + private const val BENCHMARK_SCREEN_EXTRA = "benchmark_screen_key" |
| 73 | + private const val ORBIT_PICKER_SCREEN_KEY = "orbit_picker" |
| 74 | + private const val ORBIT_PICKER_SEMANTICS = "orbit_picker_root" |
| 75 | + } |
| 76 | +} |
0 commit comments