Upgrade to Pro — share decks privately, control downloads, hide ads and more …

起動時間で差をつけろ! アプリ起動パフォーマンス改善!

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for Yanmar Yanmar
September 11, 2024
2k

起動時間で差をつけろ! アプリ起動パフォーマンス改善!

Avatar for Yanmar

Yanmar

September 11, 2024
Tweet

Transcript

  1. 自己紹介 yanmar - 所属:チャリチャリ株式会社 (Charichari, Inc.) - 職種:Androidチームリード兼テックリード - 経歴:

    2018.03 早稲田大学商学部 卒 2018.04~ 東京海上日動火災保険 2020.01~ レンサ 2022.05~ フラー 2024.02~ チャリチャリ 2
  2. アジェンダ Agenda 1 . アプリの起動時間とは
 What is application startup time?

    2 . パフォーマンス検査
   Performance Inspection 3 . パフォーマンス向上
   Performance Improvements 4 . パフォーマンスモニタリング
   Performance Monitoring 3
  3. コールドスタート 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
  4. ウォームスタート Warm start ・アプリを終了 → 再び起動(プロセスは生きている)
  Exit application -> Start

    again (process is still running) ・メモリからアプリを削除 → 再び起動(状態バンドル  利⽤可)
  Remove app from memory -> Start again (state  bundle available) 7
  5. ホットスタート Hot start ・アプリのプロセスがバックグラウンドで実行されて
  いる場合
  If the app process is

    still running in the background すべてのアクティビティがまだメモリに存在していれば初期 化処理を行わなくて良いため、高速起動できる
 Fast startup if all activity still exists in memory
 8
  6. ・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
  7. パフォーマンス検査  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
  8. パフォーマンス検査  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
  9. 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
  10. @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
  11. CPU Profiler app ↓ Profiling ↓ Check Start this recording

    on startup ↓ CPU activity Java/Kotlin Method Sample 22
  12. CPU Profiler ・Profile 'app' with low overhead:CPU、メモリプロファイラー
  CPU and Memory

    profilers ・Profile 'app' with complete data:CPU、メモリ、エネルギープロファイラー
  CPU, Memory, and Energy profilers. 23
  13. ツールとライブラリ Tools & Libraries 1 . App Startupライブラリ
   App

    Startup Library 2 . ベースラインプロファイル
   Baseline Profiles 3 . 起動プロファイル
   Startup Profiles 27
  14. App Startup アプリ起動時のコンポーネント初期化ライブラリ
 Component initialization library at app startup ・単一のコンテンツプロバイダで複数のInitializer共有ができる


     Multiple Initializer shares with a single content provider ・初期化の順序を制御できる
  Can control the order of initialization 28
  15. 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
  16. 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
  17. ベースラインプロファイル 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
  18. ベースラインプロファイル 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
  19. 起動プロファイル 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
  20. 起動プロファイル 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
  21. プロファイル 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
  22. パフォーマンスモニタリング 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
  23. パフォーマンスモニタリング 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
  24. まとめ 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
  25. 参照
 • アプリの起動 <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