Skip to content

Commit d15d269

Browse files
authored
Merge pull request #775 from android/tm/fix-benchmarks
Fix benchmarks
2 parents 5b8453b + 4fcc867 commit d15d269

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed

benchmarks/src/main/java/com/google/samples/apps/nowinandroid/Utils.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

1717
package com.google.samples.apps.nowinandroid
1818

19+
import androidx.test.uiautomator.BySelector
1920
import androidx.test.uiautomator.Direction
2021
import androidx.test.uiautomator.UiDevice
2122
import androidx.test.uiautomator.UiObject2
23+
import androidx.test.uiautomator.Until
2224
import com.google.samples.apps.nowinandroid.benchmarks.BuildConfig
25+
import java.io.ByteArrayOutputStream
2326

2427
/**
2528
* Convenience parameter to use proper package name with regards to build type and build flavor.
@@ -38,3 +41,24 @@ fun UiDevice.flingElementDownUp(element: UiObject2) {
3841
waitForIdle()
3942
element.fling(Direction.UP)
4043
}
44+
45+
/**
46+
* Waits until an object with [selector] if visible on screen and returns the object.
47+
* If the element is not available in [timeout], throws [AssertionError]
48+
*/
49+
fun UiDevice.waitAndFindObject(selector: BySelector, timeout: Long): UiObject2 {
50+
if (!wait(Until.hasObject(selector), timeout)) {
51+
throw AssertionError("Element not found on screen in ${timeout}ms (selector=$selector)")
52+
}
53+
54+
return findObject(selector)
55+
}
56+
57+
/**
58+
* Helper to dump window hierarchy into a string.
59+
*/
60+
fun UiDevice.dumpWindowHierarchy(): String {
61+
val buffer = ByteArrayOutputStream()
62+
dumpWindowHierarchy(buffer)
63+
return buffer.toString()
64+
}

benchmarks/src/main/java/com/google/samples/apps/nowinandroid/baselineprofile/BaselineProfileGenerator.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.google.samples.apps.nowinandroid.baselineprofile
1919
import androidx.benchmark.macro.ExperimentalBaselineProfilesApi
2020
import androidx.benchmark.macro.junit4.BaselineProfileRule
2121
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
22+
import com.google.samples.apps.nowinandroid.allowNotifications
2223
import com.google.samples.apps.nowinandroid.bookmarks.goToBookmarksScreen
2324
import com.google.samples.apps.nowinandroid.foryou.forYouScrollFeedDownUp
2425
import com.google.samples.apps.nowinandroid.foryou.forYouSelectTopics
@@ -41,7 +42,7 @@ class BaselineProfileGenerator {
4142
// This block defines the app's critical user journey. Here we are interested in
4243
// optimizing for app startup. But you can also navigate and scroll
4344
// through your most important UI.
44-
45+
allowNotifications()
4546
pressHome()
4647
startActivityAndWait()
4748

benchmarks/src/main/java/com/google/samples/apps/nowinandroid/foryou/ForYouActions.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ import androidx.test.uiautomator.By
2121
import androidx.test.uiautomator.Until
2222
import androidx.test.uiautomator.untilHasChildren
2323
import com.google.samples.apps.nowinandroid.flingElementDownUp
24+
import com.google.samples.apps.nowinandroid.waitAndFindObject
2425

2526
fun MacrobenchmarkScope.forYouWaitForContent() {
2627
// Wait until content is loaded by checking if topics are loaded
2728
device.wait(Until.gone(By.res("loadingWheel")), 5_000)
2829
// Sometimes, the loading wheel is gone, but the content is not loaded yet
2930
// So we'll wait here for topics to be sure
30-
val obj = device.findObject(By.res("forYou:topicSelection"))
31+
val obj = device.waitAndFindObject(By.res("forYou:topicSelection"), 10_000)
3132
// Timeout here is quite big, because sometimes data loading takes a long time!
3233
obj.wait(untilHasChildren(), 60_000)
3334
}

benchmarks/src/main/java/com/google/samples/apps/nowinandroid/foryou/ScrollForYouFeedBenchmark.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ScrollForYouFeedBenchmark {
4343
metrics = listOf(FrameTimingMetric()),
4444
compilationMode = compilationMode,
4545
iterations = 10,
46-
startupMode = StartupMode.COLD,
46+
startupMode = StartupMode.WARM,
4747
setupBlock = {
4848
// Start the app
4949
pressHome()

benchmarks/src/main/java/com/google/samples/apps/nowinandroid/startup/StartupBenchmark.kt

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,24 @@ package com.google.samples.apps.nowinandroid.startup
1919
import androidx.benchmark.macro.BaselineProfileMode.Disable
2020
import androidx.benchmark.macro.BaselineProfileMode.Require
2121
import androidx.benchmark.macro.CompilationMode
22-
import androidx.benchmark.macro.StartupMode
2322
import androidx.benchmark.macro.StartupMode.COLD
24-
import androidx.benchmark.macro.StartupMode.HOT
25-
import androidx.benchmark.macro.StartupMode.WARM
2623
import androidx.benchmark.macro.StartupTimingMetric
2724
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
2825
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
2926
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
27+
import com.google.samples.apps.nowinandroid.allowNotifications
3028
import com.google.samples.apps.nowinandroid.foryou.forYouWaitForContent
3129
import org.junit.Rule
3230
import org.junit.Test
3331
import org.junit.runner.RunWith
3432

3533
/**
34+
* Enables app startups from various states of baseline profile or [CompilationMode]s.
3635
* Run this benchmark from Studio to see startup measurements, and captured system traces
3736
* for investigating your app's performance from a cold state.
3837
*/
3938
@RunWith(AndroidJUnit4ClassRunner::class)
40-
class ColdStartupBenchmark : AbstractStartupBenchmark(COLD)
41-
42-
/**
43-
* Run this benchmark from Studio to see startup measurements, and captured system traces
44-
* for investigating your app's performance from a warm state.
45-
*/
46-
@RunWith(AndroidJUnit4ClassRunner::class)
47-
class WarmStartupBenchmark : AbstractStartupBenchmark(WARM)
48-
49-
/**
50-
* Run this benchmark from Studio to see startup measurements, and captured system traces
51-
* for investigating your app's performance from a hot state.
52-
*/
53-
@RunWith(AndroidJUnit4ClassRunner::class)
54-
class HotStartupBenchmark : AbstractStartupBenchmark(HOT)
55-
56-
/**
57-
* Base class for benchmarks with different startup modes.
58-
* Enables app startups from various states of baseline profile or [CompilationMode]s.
59-
*/
60-
abstract class AbstractStartupBenchmark(private val startupMode: StartupMode) {
39+
class StartupBenchmark {
6140
@get:Rule
6241
val benchmarkRule = MacrobenchmarkRule()
6342

@@ -80,9 +59,10 @@ abstract class AbstractStartupBenchmark(private val startupMode: StartupMode) {
8059
metrics = listOf(StartupTimingMetric()),
8160
compilationMode = compilationMode,
8261
iterations = 10,
83-
startupMode = startupMode,
62+
startupMode = COLD,
8463
setupBlock = {
8564
pressHome()
65+
allowNotifications()
8666
},
8767
) {
8868
startActivityAndWait()

0 commit comments

Comments
 (0)