-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #827 from android/power-metric-benchmark
Adds PowerMetric benchmark to measure Light vs Dark theme
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...va/com/google/samples/apps/nowinandroid/interests/ScrollTopicListPowerMetricsBenchmark.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.samples.apps.nowinandroid.interests | ||
|
||
import android.os.Build.VERSION_CODES | ||
import androidx.annotation.RequiresApi | ||
import androidx.benchmark.macro.CompilationMode | ||
import androidx.benchmark.macro.ExperimentalMetricApi | ||
import androidx.benchmark.macro.FrameTimingMetric | ||
import androidx.benchmark.macro.PowerCategory | ||
import androidx.benchmark.macro.PowerCategoryDisplayLevel | ||
import androidx.benchmark.macro.PowerMetric | ||
import androidx.benchmark.macro.StartupMode | ||
import androidx.benchmark.macro.junit4.MacrobenchmarkRule | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.uiautomator.By | ||
import com.google.samples.apps.nowinandroid.PACKAGE_NAME | ||
import com.google.samples.apps.nowinandroid.allowNotifications | ||
import com.google.samples.apps.nowinandroid.foryou.forYouScrollFeedDownUp | ||
import com.google.samples.apps.nowinandroid.foryou.forYouSelectTopics | ||
import com.google.samples.apps.nowinandroid.foryou.forYouWaitForContent | ||
import com.google.samples.apps.nowinandroid.foryou.setAppTheme | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@OptIn(ExperimentalMetricApi::class) | ||
@RequiresApi(VERSION_CODES.Q) | ||
@RunWith(AndroidJUnit4::class) | ||
class ScrollTopicListPowerMetricsBenchmark { | ||
@get:Rule | ||
val benchmarkRule = MacrobenchmarkRule() | ||
|
||
private val categories = PowerCategory.values() | ||
.associateWith { PowerCategoryDisplayLevel.TOTAL } | ||
|
||
@Test | ||
fun benchmarkStateChangeCompilationLight() = | ||
benchmarkStateChangeWithTheme(CompilationMode.Partial(), false) | ||
|
||
@Test | ||
fun benchmarkStateChangeCompilationDark() = | ||
benchmarkStateChangeWithTheme(CompilationMode.Partial(), true) | ||
|
||
private fun benchmarkStateChangeWithTheme(compilationMode: CompilationMode, isDark: Boolean) = | ||
benchmarkRule.measureRepeated( | ||
packageName = PACKAGE_NAME, | ||
metrics = listOf(FrameTimingMetric(), PowerMetric(PowerMetric.Energy(categories))), | ||
compilationMode = compilationMode, | ||
iterations = 2, | ||
startupMode = StartupMode.WARM, | ||
setupBlock = { | ||
// Start the app | ||
pressHome() | ||
startActivityAndWait() | ||
allowNotifications() | ||
// Navigate to Settings | ||
device.findObject(By.desc("Settings")).click() | ||
device.waitForIdle() | ||
setAppTheme(isDark) | ||
}, | ||
) { | ||
forYouWaitForContent() | ||
forYouSelectTopics() | ||
repeat(3) { | ||
forYouScrollFeedDownUp() | ||
} | ||
} | ||
} |