Share
Embed
起動時間で差をつけろ! アプリ起動パフォーマンス改善!
Yanmar
September 11, 2024
Transcript
53 slides
-
自己紹介 yanmar - 所属:チャリチャリ株式会社 (Charichari, Inc.) - 職種:Androidチームリード兼テックリード - 経歴:
2018.03 早稲田大学商学部 卒 2018.04~ 東京海上日動火災保険 2020.01~ レンサ 2022.05~ フラー 2024.02~ チャリチャリ 2 -
アジェンダ Agenda 1 . アプリの起動時間とは What is application startup time?
2 . パフォーマンス検査 Performance Inspection 3 . パフォーマンス向上 Performance Improvements 4 . パフォーマンスモニタリング Performance Monitoring 3 -
起動の方法 Ways to launch ・コールドスタート Cold start ・ウォームスタート Warm start
・ホットスタート Hot start 5 -
コールドスタート Cold start ・デバイスの起動後に初めてアプリを起動 Launch the app for the first
time after device activation ・システムがアプリを強制終了した後で起動 Launch after the system has killed the application ※ アプリキルからの起動 ⊂ コールドスタート Booting from app kill is part of the cold start 6 -
ウォームスタート Warm start ・アプリを終了 → 再び起動(プロセスは生きている) Exit application -> Start
again (process is still running) ・メモリからアプリを削除 → 再び起動(状態バンドル 利⽤可) Remove app from memory -> Start again (state bundle available) 7 -
ホットスタート Hot start ・アプリのプロセスがバックグラウンドで実行されて いる場合 If the app process is
still running in the background すべてのアクティビティがまだメモリに存在していれば初期 化処理を行わなくて良いため、高速起動できる Fast startup if all activity still exists in memory 8 -
・TTID (Time To Initial Display) - 初期表示までの時間 - プロセスの初期化〜最初のフレーム表示 Process
initialization - first frame generation 起動時間の指標 Startup time index ・TTFD (Time To Full Display) - 完全表示までの時間 - コンテンツが表示され、ユーザー操作できるまで Time until content is displayed and can be manipulated by the user 10 -
前提 Assumption 計測はReleaseビルドで行う Measure with Release build 本番アプリでの改善が目的であり 環境を一致させなければ意味がない The goal
is to improve on the production application, and if the environment does not match, there is no point. 13 -
パフォーマンス検査 Performance Inspection ・Macrobenchmark - アプリ操作によって直接アプリを制御して計測 Direct app control and
measurement by app operation - スクロールやアニメーションの計測にも使える It can be used to measure scrolling and animation. → 定期的な観測で、不要な変更が入っていないか確認できる Regular observation to check for unwanted changes. 14 -
パフォーマンス検査 Performance Inspection ・CPU Profiler - ボトルネックの特定とパフォーマンス問題の追跡 Identify bottlenecks and
track performance issues - Android Studio内で完結 Complete within Android Studio → どこの処理で時間がかかっているのかが視覚的にわかる Visual indication of where the process is taking time 15 -
buildTypes { release { isMinifyEnabled = false isShrinkResources = true
proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } create("benchmark") { initWith(buildTypes.getByName("release")) signingConfig = signingConfigs.getByName("debug") matchingFallbacks += listOf("release") isDebuggable = false } } 17 Macrobenchmark -
@RunWith(AndroidJUnit4::class) class ExampleStartupBenchmark { @get:Rule val benchmarkRule = MacrobenchmarkRule() @Test
fun startup() = benchmarkRule.measureRepeated( packageName = "com.example.performance", metrics = listOf(StartupTimingMetric()), iterations = 10, startupMode = StartupMode.COLD ) { pressHome() startActivityAndWait() } } 18 Macrobenchmark -
Macrobenchmark Activity.reportFullyDrawn() UIが完全に描画され、重要なデータが反映されたタイミング で呼び出す Call when the UI is fully
drawn and important data is reflected 20 -
CPU Profiler 起動時間計測のための設定 Configuration for startup time measurement
Tool Bar ↓ Run ↓ Edit Configurations 21 -
CPU Profiler app ↓ Profiling ↓ Check Start this recording
on startup ↓ CPU activity Java/Kotlin Method Sample 22 -
CPU Profiler ・Profile 'app' with low overhead:CPU、メモリプロファイラー CPU and Memory
profilers ・Profile 'app' with complete data:CPU、メモリ、エネルギープロファイラー CPU, Memory, and Energy profilers. 23 -
ツールとライブラリ Tools & Libraries 1 . App Startupライブラリ App
Startup Library 2 . ベースラインプロファイル Baseline Profiles 3 . 起動プロファイル Startup Profiles 27 -
App Startup アプリ起動時のコンポーネント初期化ライブラリ Component initialization library at app startup ・単一のコンテンツプロバイダで複数のInitializer共有ができる
Multiple Initializer shares with a single content provider ・初期化の順序を制御できる Can control the order of initialization 28 -
App Startup ContentProviderでの個別初期化 → App Startup Initialization with ContentProvider
→ App Startup Applicationクラスでの初期化 → App Startup Initialization in Application class → App Startup App Startupで起動時間改善するには... To improve startup time with App Startup... 29 -
App Startup dependencies { implementation "androidx.startup:startup-runtime:1.1.1" } build.gradle class ExampleInitializer
: Initializer<Unit> { override fun create(context: Context) { // initialization process } override fun dependencies(): List<Class<out Initializer<*>>> { // No dependencies on other libraries. return emptyList() } } ExampleInitializer.kt 32 -
App Startup <provider android:name="androidx.startup.InitializationProvider" android:authorities="${applicationId}.androidx-startup" android:exported="false" tools:node="merge"> <meta-data android:name="com.example.HogeInitializer" android:value="androidx.startup"
/> <meta-data android:name="com.example.FugaInitializer" android:value="androidx.startup" /> </provider> AndroidManifest.kt 33 -
ベースラインプロファイル Baseline Profiles 起動や画面遷移、スクロールなど重要な機能を提供するガイド Guide role in providing important
functions such as startup, screen transitions, scrolling, etc. 1 . 主要な起動パスや重要なメソッドを含むプロファイルを生成 Generate a profile containing the main launch paths and important methods for your app 2 . APKにプロファイルを埋め込む Embed profiles in APKs 3 . ART(Android runtime)による最適化 Optimization by ART (Android runtime) 34 -
ベースラインプロファイル Baseline Profiles @RunWith(AndroidJUnit4::class) @LargeTest class BaselineProfileGenerator { @get:Rule val
rule = BaselineProfileRule() @Test fun generate() { rule.collect( packageName = InstrumentationRegistry.getArguments().getString("targetAppId") ?: throw Exception("targetAppId not passed as instrumentation runner arg"), includeInStartupProfile = true ) { pressHome() startActivityAndWait() } } } 37 -
起動プロファイル Startup Profiles isMinifyEnabled = true baselineProfile { dexLayoutOptimization =
true } build.gradle Settings 40 -
起動プロファイル Startup Profiles @RunWith(AndroidJUnit4::class) @LargeTest class BaselineProfileGenerator { @get:Rule val
rule = BaselineProfileRule() @Test fun generate() { rule.collect( packageName = InstrumentationRegistry.getArguments().getString("targetAppId") ?: throw Exception("targetAppId not passed as instrumentation runner arg"), includeInStartupProfile = true ) { Generator Settings ※ 起動フローでのみ使用すること To be used only in the startup flow 41 -
起動プロファイル Startup Profiles @Test fun generate() { rule.collect( packageName =
InstrumentationRegistry .getArguments() .getString("targetAppId") ?: throw Exception("targetAppId not passed as instrumentation runner arg"), includeInStartupProfile = true ) { pressHome() startActivityAndWait(Intent().apply { setPackage(packageName) setAction("com.example.app.NEWS_FEED") }) } } Generator Settings 42 -
プロファイル Profiles @Test fun startupCompilationNone() = benchmark(CompilationMode.None()) @Test fun startupCompilationBaselineProfiles()
= benchmark(CompilationMode.Partial(BaselineProfileMode.Require)) private fun benchmark(compilationMode: CompilationMode) { rule.measureRepeated( packageName = InstrumentationRegistry.getArguments().getString("targetAppId") ?: throw Exception("targetAppId not passed as instrumentation runner arg"), metrics = listOf(StartupTimingMetric()), compilationMode = compilationMode, StartupBenchmarks 44 -
パフォーマンスモニタリング Performance Monitoring Firebase Performance Monitoring ・起動時間やネットワークリクエストなどを自動測定 Automatic measurement of
startup time and network requests → FirebasePerfProvider.onCreate 〜 first Activity.onResume ・アラートによるパフォーマンス低下の検知 Detection of performance degradation through alerts → 指定したパーセンタイル値の閾値を超える Exceeds the specified percentile value threshold 47 -
パフォーマンスモニタリング Performance Monitoring plugins { alias(libs.plugins.firebase.perf) apply false {project}/build.gradle dependencies
{ implementation(platform(libs.firebase.bom)) implementation(libs.firebase.perf) } app/build.gradle plugins { alias(libs.plugins.firebase.perf) … 48 -
まとめ Summary 1 . アプリの起動時間とは What is application startup time?
- Cold Start, Warm Start, Hot Start - TTID, TTFD 2 . パフォーマンス検査 Performance Inspection - Macrobenchmark, CPU Profiler 3 . パフォーマンス向上 Performance Improvements - App Startup, Baseline Profiles, Startup Profiles 4 . パフォーマンスモニタリング Performance Monitoring - Firebase Performance Monitoring 50 -
参照 • アプリの起動 <https://developer.android.com/topic/performance/vitals/launch-time?hl=j a> • アプリのスタートアップ <https://developer.android.com/topic/libraries/app-startup?hl=ja#startup-i nitialize> •
App Startupについて <https://medium.com/@jmatsu.drm/app-startup-%E3%81%AB%E3%81%A4%E 3%81%84%E3%81%A6-b11cc31850de> • Android 向け Performance Monitoring を使ってみる <https://firebase.google.com/docs/perf-mon/get-started-android?hl=ja> 51 -
参照 • パフォーマンス監視ツールの活用 <https://speakerdeck.com/recruitengineers/pahuomansujian-shi-turunohu o-yong> • performance-samples <https://github.com/android/performance-samples/tree/main> • いかにしてアプリの起動時間を改善するか
<https://speakerdeck.com/haru067/ikanisiteapurifalseqi-dong-shi-jian-wo gai-shan-suruka> • Firebase Performance Monitoring <https://firebase.google.com/docs/perf-mon?hl=ja> 52