|
| 1 | +/* |
| 2 | + * Copyright 2026 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.reply.benchmark |
| 18 | + |
| 19 | +import android.content.Intent |
| 20 | +import androidx.benchmark.macro.CompilationMode |
| 21 | +import androidx.benchmark.macro.StartupMode |
| 22 | +import androidx.benchmark.macro.StartupTimingMetric |
| 23 | +import androidx.benchmark.macro.junit4.MacrobenchmarkRule |
| 24 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 25 | +import androidx.test.platform.app.InstrumentationRegistry |
| 26 | +import androidx.test.uiautomator.By |
| 27 | +import androidx.test.uiautomator.Until |
| 28 | +import org.junit.Rule |
| 29 | +import org.junit.Test |
| 30 | +import org.junit.runner.RunWith |
| 31 | + |
| 32 | +@RunWith(AndroidJUnit4::class) |
| 33 | +class ExampleStartupBenchmark { |
| 34 | + @get:Rule |
| 35 | + val benchmarkRule = MacrobenchmarkRule() |
| 36 | + |
| 37 | + private val targetPackageName = "com.example.reply" |
| 38 | + |
| 39 | + @Test |
| 40 | + fun startupCold() = benchmarkRule.measureRepeated( |
| 41 | + packageName = targetPackageName, |
| 42 | + metrics = listOf(StartupTimingMetric()), |
| 43 | + compilationMode = CompilationMode.None(), |
| 44 | + startupMode = StartupMode.COLD, |
| 45 | + iterations = 5 |
| 46 | + ) { |
| 47 | + pressHome() |
| 48 | + val context = InstrumentationRegistry.getInstrumentation().context |
| 49 | + val intent = context.packageManager.getLaunchIntentForPackage(packageName)!! |
| 50 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) |
| 51 | + context.startActivity(intent) |
| 52 | + device.wait(Until.hasObject(By.pkg(packageName).depth(0)), 15000) |
| 53 | + } |
| 54 | +} |
0 commit comments