Rahul Ravikumar | d651ee2 | 2021-09-21 17:22:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | * 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 androidx.benchmark.macro.junit4 |
| 18 | |
| 19 | import android.Manifest |
| 20 | import androidx.annotation.RequiresApi |
Rahul Ravikumar | 6da9b7f27 | 2021-10-20 13:52:42 -0700 | [diff] [blame] | 21 | import androidx.benchmark.macro.ExperimentalBaselineProfilesApi |
Rahul Ravikumar | d651ee2 | 2021-09-21 17:22:48 -0700 | [diff] [blame] | 22 | import androidx.benchmark.macro.MacrobenchmarkScope |
| 23 | import androidx.benchmark.macro.collectBaselineProfile |
| 24 | import androidx.test.rule.GrantPermissionRule |
| 25 | import org.junit.rules.RuleChain |
| 26 | import org.junit.rules.TestRule |
| 27 | import org.junit.runner.Description |
| 28 | import org.junit.runners.model.Statement |
| 29 | |
| 30 | /** |
Rahul Ravikumar | 6da9b7f27 | 2021-10-20 13:52:42 -0700 | [diff] [blame] | 31 | * A [TestRule] that collects Baseline Profiles to be embedded in your APK. |
Rahul Ravikumar | d651ee2 | 2021-09-21 17:22:48 -0700 | [diff] [blame] | 32 | * |
Rahul Ravikumar | 6da9b7f27 | 2021-10-20 13:52:42 -0700 | [diff] [blame] | 33 | * These rules are used at install time to partially pre-compile your application code. |
Rahul Ravikumar | d651ee2 | 2021-09-21 17:22:48 -0700 | [diff] [blame] | 34 | */ |
| 35 | @RequiresApi(28) |
Rahul Ravikumar | 6da9b7f27 | 2021-10-20 13:52:42 -0700 | [diff] [blame] | 36 | @ExperimentalBaselineProfilesApi |
Rahul Ravikumar | d651ee2 | 2021-09-21 17:22:48 -0700 | [diff] [blame] | 37 | class BaselineProfileRule : TestRule { |
| 38 | private lateinit var currentDescription: Description |
| 39 | |
| 40 | override fun apply(base: Statement, description: Description): Statement { |
| 41 | return RuleChain |
| 42 | .outerRule(GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)) |
| 43 | .around(::applyInternal) |
| 44 | .apply(base, description) |
| 45 | } |
| 46 | |
| 47 | private fun applyInternal(base: Statement, description: Description) = object : Statement() { |
| 48 | override fun evaluate() { |
| 49 | currentDescription = description |
| 50 | base.evaluate() |
| 51 | } |
| 52 | } |
| 53 | |
Rahul Ravikumar | 6da9b7f27 | 2021-10-20 13:52:42 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Collects baseline profiles for a critical user journey. |
| 56 | * @param packageName Package name of the app for which profiles are to be generated. |
Rahul Ravikumar | 6da9b7f27 | 2021-10-20 13:52:42 -0700 | [diff] [blame] | 57 | * @param [profileBlock] defines the critical user journey. |
| 58 | */ |
Rahul Ravikumar | d651ee2 | 2021-09-21 17:22:48 -0700 | [diff] [blame] | 59 | public fun collectBaselineProfile( |
| 60 | packageName: String, |
Rahul Ravikumar | d651ee2 | 2021-09-21 17:22:48 -0700 | [diff] [blame] | 61 | profileBlock: MacrobenchmarkScope.() -> Unit |
| 62 | ) { |
| 63 | collectBaselineProfile( |
| 64 | currentDescription.toUniqueName(), |
| 65 | packageName = packageName, |
Rahul Ravikumar | d651ee2 | 2021-09-21 17:22:48 -0700 | [diff] [blame] | 66 | profileBlock = profileBlock |
| 67 | ) |
| 68 | } |
| 69 | |
| 70 | private fun Description.toUniqueName() = testClass.simpleName + "_" + methodName |
| 71 | } |