Skip to content

Commit f5cf16f

Browse files
authored
Merge pull request #77 from skydoves/improve/baseline-profile
Improve generating baseline profiles scenario
2 parents 4904ceb + 1d9c41d commit f5cf16f

File tree

6 files changed

+6954
-8784
lines changed

6 files changed

+6954
-8784
lines changed

app/src/main/baseline-prof.txt

Lines changed: 6891 additions & 8777 deletions
Large diffs are not rendered by default.

app/src/main/res/layout/activity_detail.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
</data>
3131

3232
<androidx.core.widget.NestedScrollView
33+
android:id="@+id/nestedScroll"
3334
android:layout_width="match_parent"
3435
android:layout_height="match_parent"
3536
android:background="@color/background">

benchmark/src/main/kotlin/com/github/skydoves/benchmark/BaselineProfileGenerator.kt

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,65 @@
1616

1717
package com.github.skydoves.benchmark
1818

19-
import androidx.benchmark.macro.ExperimentalBaselineProfilesApi
19+
import androidx.benchmark.macro.ExperimentalStableBaselineProfilesApi
2020
import androidx.benchmark.macro.junit4.BaselineProfileRule
21+
import androidx.test.uiautomator.By
22+
import androidx.test.uiautomator.BySelector
23+
import androidx.test.uiautomator.Direction
24+
import androidx.test.uiautomator.UiDevice
25+
import androidx.test.uiautomator.UiObject2
26+
import androidx.test.uiautomator.Until
2127
import org.junit.Rule
2228
import org.junit.Test
2329

2430
/**
2531
* Generates a baseline profile which can be copied to `app/src/main/baseline-prof.txt`.
2632
*/
27-
@ExperimentalBaselineProfilesApi
2833
class BaselineProfileGenerator {
2934
@get:Rule
3035
val baselineProfileRule = BaselineProfileRule()
3136

3237
@Test
38+
@OptIn(ExperimentalStableBaselineProfilesApi::class)
3339
fun startup() =
34-
baselineProfileRule.collectBaselineProfile(
35-
packageName = "com.skydoves.pokedex"
40+
baselineProfileRule.collectStableBaselineProfile(
41+
packageName = packageName,
42+
stableIterations = 2,
43+
maxIterations = 8
3644
) {
3745
pressHome()
3846
// This block defines the app's critical user journey. Here we are interested in
3947
// optimizing for app startup. But you can also navigate and scroll
4048
// through your most important UI.
4149
startActivityAndWait()
4250
device.waitForIdle()
51+
52+
// Navigate to the details screen
53+
device.testDiscover() || return@collectStableBaselineProfile
54+
device.navigateFromMainToDetails()
55+
device.pressBack()
4356
}
4457
}
58+
59+
private fun UiDevice.testDiscover(): Boolean {
60+
// UI automator library has an issue about scrolling down.
61+
// waitForObject(By.res(packageName, "recyclerView")).scroll(Direction.DOWN, 1f).
62+
return wait(Until.hasObject(By.res(packageName, "transformationLayout")), 1_000)
63+
}
64+
65+
private fun UiDevice.navigateFromMainToDetails() {
66+
// Open a show from one of the carousels
67+
waitForObject(By.res(packageName, "transformationLayout")).click()
68+
wait(Until.hasObject(By.res(packageName, "nestedScroll")), 1_000)
69+
waitForObject(By.res(packageName, "nestedScroll")).scroll(Direction.DOWN, 1f)
70+
waitForIdle()
71+
pressBack()
72+
}
73+
74+
private fun UiDevice.waitForObject(selector: BySelector, timeout: Long = 5_000): UiObject2 {
75+
if (wait(Until.hasObject(selector), timeout)) {
76+
return findObject(selector)
77+
}
78+
79+
error("Object with selector [$selector] not found")
80+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Designed and developed by 2022 skydoves (Jaewoong Eum)
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 com.github.skydoves.benchmark
18+
19+
internal const val packageName: String = "com.skydoves.pokedex"

benchmark/src/main/kotlin/com/github/skydoves/benchmark/StartupBenchmark.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ abstract class AbstractStartupBenchmark(private val startupMode: StartupMode) {
7171
fun startupFullCompilation() = startup(CompilationMode.Full())
7272

7373
private fun startup(compilationMode: CompilationMode) = benchmarkRule.measureRepeated(
74-
packageName = "com.skydoves.pokedex",
74+
packageName = packageName,
7575
metrics = listOf(StartupTimingMetric()),
7676
compilationMode = compilationMode,
7777
iterations = 5,

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ progressView = "1.1.3"
3030
rainbow = "1.0.3"
3131
timber = "5.0.1"
3232
baselineProfiles = "1.3.0"
33-
macroBenchmark = "1.1.0"
34-
uiAutomator = "2.2.0"
33+
macroBenchmark = "1.2.0-alpha12"
34+
uiAutomator = "2.3.0-alpha02"
3535
truth = "1.0.1"
3636
junit = "4.13.1"
3737
turbine = "0.6.0"

0 commit comments

Comments
 (0)