Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit f6def17

Browse files
committed
Implement Startup Macrobenchmark
1 parent 9beb208 commit f6def17

File tree

11 files changed

+216
-13
lines changed

11 files changed

+216
-13
lines changed

build.gradle.kts

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ allprojects {
5757
maven {
5858
url = uri("${project.rootDir}/../../../prebuilts/fullsdk/linux/extras/support/m2repository")
5959
}
60+
// snapshot repository for androidx - not necessary once alpha is public
61+
maven {
62+
url = uri("https://androidx.dev/snapshots/builds/7352378/artifacts/repository")
63+
}
6064

6165
flatDir {
6266
dirs = setOf(file("libs"), project(":ar").file("libs"))

macrobenchmark/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

macrobenchmark/build.gradle

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
plugins {
2+
id 'com.android.test'
3+
id 'kotlin-android'
4+
}
5+
6+
android {
7+
compileSdkVersion 30
8+
defaultConfig {
9+
minSdkVersion 29
10+
targetSdkVersion 30
11+
versionCode 1
12+
versionName "1.0"
13+
14+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15+
}
16+
17+
compileOptions {
18+
sourceCompatibility JavaVersion.VERSION_1_8
19+
targetCompatibility JavaVersion.VERSION_1_8
20+
}
21+
kotlinOptions {
22+
jvmTarget = '1.8'
23+
}
24+
25+
buildTypes {
26+
// declare a build type (release) to match the target app's build type
27+
release {
28+
debuggable = true
29+
signingConfig = debug.signingConfig
30+
}
31+
}
32+
33+
targetProjectPath = ":mobile"
34+
properties["android.experimental.self-instrumenting"] = true
35+
}
36+
37+
androidComponents {
38+
beforeVariants(selector().all()) {
39+
// enable only the release buildType, since we only want to measure
40+
// release build performance
41+
enable = buildType == 'release'
42+
}
43+
}
44+
45+
dependencies {
46+
//noinspection GradleDependency
47+
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.0"
48+
implementation 'androidx.test.ext:junit:1.1.2'
49+
implementation 'androidx.test.espresso:espresso-core:3.3.0'
50+
implementation 'androidx.test.uiautomator:uiautomator:2.2.0'
51+
implementation 'androidx.benchmark:benchmark-macro-junit4:1.1.0-SNAPSHOT'
52+
}

macrobenchmark/consumer-rules.pro

Whitespace-only changes.

macrobenchmark/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
~ Copyright 2021 Google LLC
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+
~ https://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+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:tools="http://schemas.android.com/tools"
19+
package="com.google.samples.apps.iosched.macrobenchmark">
20+
21+
<uses-permission
22+
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
23+
tools:ignore="ScopedStorage" />
24+
25+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2021 Google LLC
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+
* https://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.google.samples.apps.iosched.macrobenchmark
18+
19+
import androidx.benchmark.macro.StartupMode
20+
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
21+
import androidx.test.filters.LargeTest
22+
import org.junit.Rule
23+
import org.junit.Test
24+
import org.junit.runner.RunWith
25+
import org.junit.runners.Parameterized
26+
27+
@LargeTest
28+
@RunWith(Parameterized::class)
29+
class StartupBenchmark(private val startupMode: StartupMode) {
30+
@get:Rule
31+
val benchmarkRule = MacrobenchmarkRule()
32+
33+
@Test
34+
fun startup() = benchmarkRule.measureStartup(
35+
profileCompiled = false,
36+
startupMode = startupMode,
37+
iterations = 3
38+
) {
39+
action = "com.google.samples.apps.iosched.STARTUP_ACTIVITY"
40+
}
41+
42+
companion object {
43+
@Parameterized.Parameters(name = "mode={0}")
44+
@JvmStatic
45+
fun parameters(): List<Array<Any>> {
46+
return listOf(StartupMode.COLD, StartupMode.WARM, StartupMode.HOT)
47+
.map { arrayOf(it) }
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2021 The Android Open Source Project
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.google.samples.apps.iosched.macrobenchmark
18+
19+
import android.content.Intent
20+
import androidx.benchmark.macro.CompilationMode
21+
import androidx.benchmark.macro.StartupMode
22+
import androidx.benchmark.macro.StartupTimingMetric
23+
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
24+
25+
const val TARGET_PACKAGE = "com.google.samples.apps.iosched"
26+
27+
fun MacrobenchmarkRule.measureStartup(
28+
profileCompiled: Boolean,
29+
startupMode: StartupMode,
30+
iterations: Int = 3,
31+
setupIntent: Intent.() -> Unit = {}
32+
) = measureRepeated(
33+
packageName = TARGET_PACKAGE,
34+
metrics = listOf(StartupTimingMetric()),
35+
compilationMode = if (profileCompiled) {
36+
CompilationMode.SpeedProfile(warmupIterations = 3)
37+
} else {
38+
CompilationMode.None
39+
},
40+
iterations = iterations,
41+
startupMode = startupMode
42+
) {
43+
pressHome()
44+
val intent = Intent()
45+
intent.setPackage(TARGET_PACKAGE)
46+
setupIntent(intent)
47+
startActivityAndWait(intent)
48+
}

mobile/src/main/AndroidManifest.xml

+11-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@
5858
</activity>
5959

6060
<activity
61-
android:name=".ui.MainActivity" />
61+
android:name=".ui.MainActivity" >
62+
<intent-filter>
63+
<action android:name="com.google.samples.apps.iosched.STARTUP_ACTIVITY" />
64+
<category android:name="android.intent.category.DEFAULT" />
65+
</intent-filter>
66+
</activity>
6267

6368
<activity
6469
android:name=".ui.onboarding.OnboardingActivity"
@@ -99,11 +104,14 @@
99104
<meta-data
100105
android:name="com.google.samples.apps.iosched.util.initializers.StrictModeInitializer"
101106
android:value="androidx.startup" />
102-
<!-- Initalize TimberInitializer after the Application's onCreate is called -->
103107
<meta-data
104108
android:name="com.google.samples.apps.iosched.util.initializers.TimberInitializer"
105-
tools:node="remove" />
109+
android:value="androidx.startup" />
106110
</provider>
111+
<!-- enable profiling by macrobenchmark -->
112+
<profileable
113+
android:shell="true"
114+
tools:targetApi="q" />
107115
</application>
108116

109117
</manifest>

mobile/src/main/java/com/google/samples/apps/iosched/MainApplication.kt

+2-9
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
package com.google.samples.apps.iosched
1818

1919
import android.app.Application
20-
import androidx.startup.AppInitializer
2120
import com.google.samples.apps.iosched.shared.analytics.AnalyticsHelper
22-
import com.google.samples.apps.iosched.util.initializers.TimberInitializer
2321
import dagger.hilt.android.HiltAndroidApp
2422
import javax.inject.Inject
2523

@@ -30,11 +28,6 @@ import javax.inject.Inject
3028
class MainApplication : Application() {
3129

3230
// Even if the var isn't used, needs to be initialized at application startup.
33-
@Inject lateinit var analyticsHelper: AnalyticsHelper
34-
35-
override fun onCreate() {
36-
super.onCreate()
37-
AppInitializer.getInstance(this)
38-
.initializeComponent(TimberInitializer::class.java)
39-
}
31+
@Inject
32+
lateinit var analyticsHelper: AnalyticsHelper
4033
}

settings.gradle.kts

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ include(
2222
":androidTest-shared",
2323
"ar",
2424
"benchmark",
25-
"depconstraints"
25+
"depconstraints",
26+
":macrobenchmark"
2627
)

0 commit comments

Comments
 (0)