Skip to content

Commit dd407db

Browse files
committed
使用BaselineProfiles改善性能
1 parent 8f903e8 commit dd407db

9 files changed

+12999
-3
lines changed

app/build.gradle.kts

+7-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ android {
2323
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2424

2525
ndk {
26-
// abiFilters "arm64-v8a", "armeabi-v7a", "armeabi", "x86", "x86_64"
2726
abiFilters.add("arm64-v8a")
2827
abiFilters.add("armeabi-v7a")
2928
abiFilters.add("armeabi")
@@ -40,6 +39,12 @@ android {
4039
"proguard-rules.pro"
4140
)
4241
}
42+
create("benchmark") {
43+
initWith(buildTypes.getByName("release"))
44+
signingConfig = signingConfigs.getByName("debug")
45+
matchingFallbacks += listOf("release")
46+
isDebuggable = false
47+
}
4348
}
4449

4550
fun createManifestPlaceholders(
@@ -96,6 +101,7 @@ dependencies {
96101
implementation(Libraries.navigationFragment)
97102
implementation(Libraries.navigationUi)
98103
implementation(Libraries.media)
104+
implementation("androidx.profileinstaller:profileinstaller:1.3.1")
99105

100106
implementation(Libraries.kotlinxCoroutinesAndroid)
101107
implementation(Libraries.kodeinDi) // 依赖注入
@@ -141,7 +147,6 @@ dependencies {
141147
// 弹幕引擎
142148
implementation(project(":DanmakuFlameMaster"))
143149

144-
145150
// 百度统计
146151
implementation(Libraries.baiduMobstat)
147152

app/src/main/baseline-prof.txt

+12,856
Large diffs are not rendered by default.

benchmark/.gitignore

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

benchmark/build.gradle.kts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
plugins {
2+
id("com.android.test")
3+
id("org.jetbrains.kotlin.android")
4+
}
5+
6+
android {
7+
namespace = "cn.a10miaomiao.bilimiao.benchmark"
8+
compileSdk = 33
9+
10+
compileOptions {
11+
sourceCompatibility = JavaVersion.VERSION_1_8
12+
targetCompatibility = JavaVersion.VERSION_1_8
13+
}
14+
15+
kotlinOptions {
16+
jvmTarget = "1.8"
17+
}
18+
19+
defaultConfig {
20+
minSdk = 23
21+
targetSdk = 33
22+
23+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
24+
25+
// testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "EMULATOR,DEBUGGABLE"
26+
}
27+
28+
buildTypes {
29+
// This benchmark buildType is used for benchmarking, and should function like your
30+
// release build (for example, with minification on). It"s signed with a debug key
31+
// for easy local/CI testing.
32+
create("benchmark") {
33+
isDebuggable = true
34+
signingConfig = getByName("debug").signingConfig
35+
matchingFallbacks += listOf("release")
36+
}
37+
}
38+
39+
flavorDimensions += listOf("default")
40+
productFlavors {
41+
create("coolapk") { dimension = "default" }
42+
create("github") { dimension = "default" }
43+
create("gitee") { dimension = "default" }
44+
create("qq") { dimension = "default" }
45+
create("miao") { dimension = "default" }
46+
}
47+
48+
targetProjectPath = ":app"
49+
experimentalProperties["android.experimental.self-instrumenting"] = true
50+
}
51+
52+
dependencies {
53+
implementation("androidx.test.ext:junit:1.1.5")
54+
implementation("androidx.test.espresso:espresso-core:3.5.1")
55+
implementation("androidx.test.uiautomator:uiautomator:2.2.0")
56+
implementation("androidx.benchmark:benchmark-macro-junit4:1.1.1")
57+
}
58+
59+
androidComponents {
60+
beforeVariants(selector().all()) {
61+
it.enable = it.buildType == "benchmark"
62+
}
63+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<manifest />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.a10miaomiao.bilimiao.benchmark
2+
3+
import androidx.benchmark.macro.ExperimentalBaselineProfilesApi
4+
import androidx.benchmark.macro.junit4.BaselineProfileRule
5+
import androidx.test.ext.junit.runners.AndroidJUnit4
6+
import org.junit.Rule
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
10+
@RunWith(AndroidJUnit4::class)
11+
@OptIn(ExperimentalBaselineProfilesApi::class)
12+
class BaselineProfileGenerator {
13+
14+
@get:Rule
15+
val rule = BaselineProfileRule()
16+
17+
@Test
18+
fun generate() {
19+
rule.collectBaselineProfile(
20+
packageName = "com.a10miaomiao.bilimiao",
21+
) {
22+
pressHome()
23+
startActivityAndWait()
24+
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cn.a10miaomiao.bilimiao.benchmark
2+
3+
import androidx.benchmark.macro.CompilationMode
4+
import androidx.benchmark.macro.StartupMode
5+
import androidx.benchmark.macro.StartupTimingMetric
6+
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
7+
import androidx.test.ext.junit.runners.AndroidJUnit4
8+
import org.junit.Rule
9+
import org.junit.Test
10+
import org.junit.runner.RunWith
11+
12+
/**
13+
* This is an example startup benchmark.
14+
*
15+
* It navigates to the device's home screen, and launches the default activity.
16+
*
17+
* Before running this benchmark:
18+
* 1) switch your app's active build variant in the Studio (affects Studio runs only)
19+
* 2) add `<profileable android:shell="true" />` to your app's manifest, within the `<application>` tag
20+
*
21+
* Run this benchmark from Studio to see startup measurements, and captured system traces
22+
* for investigating your app's performance.
23+
*/
24+
@RunWith(AndroidJUnit4::class)
25+
class ExampleStartupBenchmark {
26+
@get:Rule
27+
val benchmarkRule = MacrobenchmarkRule()
28+
29+
@Test
30+
fun startup() = benchmarkRule.measureRepeated(
31+
packageName = "com.a10miaomiao.bilimiao",
32+
metrics = listOf(StartupTimingMetric()),
33+
iterations = 5,
34+
startupMode = StartupMode.COLD
35+
) {
36+
pressHome()
37+
startActivityAndWait()
38+
}
39+
40+
}

gradle.properties

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ android.enableJetifier=true
2121
kotlin.code.style=official
2222
android.defaults.buildfeatures.buildconfig=true
2323
android.nonTransitiveRClass=false
24-
android.nonFinalResIds=false
24+
android.nonFinalResIds=false
25+
# Enable adding baseline-prof.txt files to AAR artifacts, and binary profiles to APKs
26+
android.experimental.enableArtProfiles=true

settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ include(":app")
2121
include(":miao-binding", ":miao-binding-android")
2222
include(":bilimiao-comm", ":bilimiao-cover", ":bilimiao-download", "bilimiao-appwidget", "bilimiao-compose")
2323
include(":DanmakuFlameMaster")
24+
include(":benchmark")

0 commit comments

Comments
 (0)