|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 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 | + * http://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.google.android.fhir.engine.macrobenchmark |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import androidx.benchmark.macro.ExperimentalMetricApi |
| 21 | +import androidx.benchmark.macro.MacrobenchmarkScope |
| 22 | +import androidx.benchmark.macro.TraceSectionMetric |
| 23 | +import androidx.benchmark.macro.junit4.MacrobenchmarkRule |
| 24 | +import androidx.test.core.app.ApplicationProvider |
| 25 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 26 | +import androidx.test.uiautomator.By |
| 27 | +import androidx.test.uiautomator.Until |
| 28 | +import org.junit.Assert.fail |
| 29 | +import org.junit.Rule |
| 30 | +import org.junit.Test |
| 31 | +import org.junit.runner.RunWith |
| 32 | + |
| 33 | +/** |
| 34 | + * This is an example startup benchmark. |
| 35 | + * |
| 36 | + * It navigates to the device's home screen, and launches the default activity. |
| 37 | + * |
| 38 | + * Before running this benchmark: |
| 39 | + * 1) switch your app's active build variant in the Studio (affects Studio runs only) |
| 40 | + * 2) add `<profileable android:shell="true" />` to your app's manifest, within the `<application>` |
| 41 | + * tag |
| 42 | + * |
| 43 | + * Run this benchmark from Studio to see startup measurements, and captured system traces for |
| 44 | + * investigating your app's performance. |
| 45 | + */ |
| 46 | +@OptIn(ExperimentalMetricApi::class) |
| 47 | +@RunWith(AndroidJUnit4::class) |
| 48 | +class ExampleBenchmark { |
| 49 | + |
| 50 | + @get:Rule val benchmarkRule = MacrobenchmarkRule() |
| 51 | + |
| 52 | + private val applicationContext = ApplicationProvider.getApplicationContext<Context>() |
| 53 | + |
| 54 | + @Test |
| 55 | + fun tracingCreate() { |
| 56 | + benchmarkRule.measureRepeated( |
| 57 | + packageName = TARGET_PACKAGE, |
| 58 | + metrics = listOf(TraceSectionMetric("Create API")), |
| 59 | + iterations = DEFAULT_ITERATIONS, |
| 60 | + startupMode = null, |
| 61 | + setupBlock = { startActivityAndWait() }, |
| 62 | + ) { |
| 63 | + clickOnId("create") |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private fun MacrobenchmarkScope.clickOnId(resourceId: String) { |
| 68 | + val selector = By.res(TARGET_PACKAGE, resourceId) |
| 69 | + if (!device.wait(Until.hasObject(selector), 2_500)) { |
| 70 | + fail("Did not find object with id $resourceId") |
| 71 | + } |
| 72 | + |
| 73 | + device.findObject(selector).click() |
| 74 | + // Chill to ensure we capture the end of the click span in the trace. |
| 75 | + Thread.sleep(100) |
| 76 | + } |
| 77 | +} |
0 commit comments