From 7dc03367117ae153a3a9a3584d0f8daebbdd9b70 Mon Sep 17 00:00:00 2001 From: Murat Yener Date: Thu, 29 Jun 2023 15:04:12 -0700 Subject: [PATCH 1/3] Ads PowerMetric benchmark which scrolls through the topics list in Light vs Dark theme --- .../interests/InterestsActions.kt | 14 ++++ .../ScrollTopicListPowerMetricsBenchmark.kt | 82 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 benchmarks/src/main/java/com/google/samples/apps/nowinandroid/interests/ScrollTopicListPowerMetricsBenchmark.kt 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 e94369ce25..7bc1feaae9 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 0000000000..4a996a9d61 --- /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() + } + } +} From f79b19c3779d6dd683f51b72ffa2c66431219080 Mon Sep 17 00:00:00 2001 From: Murat Yener Date: Thu, 29 Jun 2023 15:10:42 -0700 Subject: [PATCH 2/3] Spotless fixes --- .../interests/InterestsActions.kt | 2 +- .../ScrollTopicListPowerMetricsBenchmark.kt | 20 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) 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 7bc1feaae9..a6420a7fdf 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 @@ -50,7 +50,7 @@ fun MacrobenchmarkScope.interestsToggleBookmarked() { } fun MacrobenchmarkScope.setAppTheme(isDark: Boolean) { - when (isDark){ + when (isDark) { true -> device.findObject(By.text("Dark")).click() false -> device.findObject(By.text("Light")).click() } 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 index 4a996a9d61..8ede75e4be 100644 --- 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 @@ -1,17 +1,17 @@ /* - * Copyright 2023 The Android Open Source Project + * Copyright 2022 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 + * 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 + * 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. + * 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 From e18f7cb3764cbaf30658ed2944230f10d2f2c258 Mon Sep 17 00:00:00 2001 From: Murat Yener Date: Mon, 10 Jul 2023 16:18:30 -0700 Subject: [PATCH 3/3] addressed comments --- .../apps/nowinandroid/foryou/ForYouActions.kt | 14 ++++++++++++++ .../nowinandroid/interests/InterestsActions.kt | 14 -------------- .../ScrollTopicListPowerMetricsBenchmark.kt | 5 +++-- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/foryou/ForYouActions.kt b/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/foryou/ForYouActions.kt index 5e4952fbb1..8599bddd03 100644 --- a/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/foryou/ForYouActions.kt +++ b/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/foryou/ForYouActions.kt @@ -88,3 +88,17 @@ fun MacrobenchmarkScope.forYouScrollFeedDownUp() { val feedList = device.findObject(By.res("forYou:feed")) device.flingElementDownUp(feedList) } + +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 the top app bar is visible 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/InterestsActions.kt b/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/interests/InterestsActions.kt index a6420a7fdf..e94369ce25 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,17 +48,3 @@ 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 index 8ede75e4be..13c6f55e32 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright 2022 The Android Open Source Project + * 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. @@ -33,6 +33,7 @@ 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 @@ -67,7 +68,7 @@ class ScrollTopicListPowerMetricsBenchmark { pressHome() startActivityAndWait() allowNotifications() - // Navigate to interests screen + // Navigate to Settings device.findObject(By.desc("Settings")).click() device.waitForIdle() setAppTheme(isDark)