Skip to content

A fully functional Android app built entirely with Kotlin and Jetpack Compose

Ads PowerMetric benchmark which scrolls through the topics list in Light vs Dark theme

Murat Yener7dc03367117a
diff --git a/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/interests/InterestsActions.kt b/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/interests/InterestsActions.kt
index e94369ce2..7bc1feaae 100644
--- a/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/interests/InterestsActions.kt
+++ b/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/interests/InterestsActions.kt
@@ -48,3 +48,17 @@ fun MacrobenchmarkScope.interestsToggleBookmarked() {
checkable.click()
device.waitForIdle()
}
+
+fun MacrobenchmarkScope.setAppTheme(isDark: Boolean) {
+ when (isDark){
+ true -> device.findObject(By.text("Dark")).click()
+ false -> device.findObject(By.text("Light")).click()
+ }
+ device.waitForIdle()
+ device.findObject(By.text("OK")).click()
+
+ // Wait until interests are shown on screen
+ device.wait(Until.hasObject(By.res("niaTopAppBar")), 2_000)
+ val topAppBar = device.findObject(By.res("niaTopAppBar"))
+ topAppBar.wait(Until.hasObject(By.text("Now in Android")), 2_000)
+}
diff --git a/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/interests/ScrollTopicListPowerMetricsBenchmark.kt b/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/interests/ScrollTopicListPowerMetricsBenchmark.kt
new file mode 100644
index 000000000..4a996a9d6
--- /dev/null
+++ b/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/interests/ScrollTopicListPowerMetricsBenchmark.kt
@@ -0,0 +1,82 @@
+/*
+ * 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 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 interests screen
+ device.findObject(By.desc("Settings")).click()
+ device.waitForIdle()
+ setAppTheme(isDark)
+ },
+ ) {
+ forYouWaitForContent()
+ forYouSelectTopics()
+ repeat(3) {
+ forYouScrollFeedDownUp()
+ }
+ }
+}