kmp-project-template icon indicating copy to clipboard operation
kmp-project-template copied to clipboard

feat: integrate Baseline Profiles for performance optimization

Open markrizkalla opened this issue 8 months ago • 3 comments

  • Create a new :baselineprofile module to handle profile generation and benchmarking.
  • Implement BaselineProfileGenerator to capture app startup flows using BaselineProfileRule.
  • Implement StartupBenchmarks using MacrobenchmarkRule to measure and verify startup performance improvements.
  • Configure the androidx.baselineprofile plugin across the project and link the new module to :cmp-android.
  • Update dependencies in libs.versions.toml to include benchmark-macro-junit4, uiautomator, and espresso-core.
  • Register the :baselineprofile module in settings.gradle.kts.

Changes:

  • Add baselineprofile module with profile generator and startup benchmarks
  • Configure cmp-android to consume baseline profiles
  • Enable DEX layout optimization for additional startup gains
  • Auto-detect package name via InstrumentationRegistry (generic for template)

Usage: Generate: ./gradlew :cmp-android:generateProdReleaseBaselineProfile Benchmark: ./gradlew :baselineprofile:connectedProdBenchmarkReleaseAndroidTest"

The metrics is calculated after 10 iterations

Without Baseline Profile (CV = 23%): Run 1: 391ms ████████████████████████████████████████ Run 2: 444ms █████████████████████████████████████████████ Run 3: 405ms █████████████████████████████████████████ Run 4: 345ms ███████████████████████████████████ Run 5: 348ms ███████████████████████████████████ Run 6: 241ms ████████████████████████ Run 7: 255ms █████████████████████████ Run 8: 328ms █████████████████████████████████ Run 9: 253ms █████████████████████████ Run 10: 238ms ████████████████████████

With Baseline Profile (CV = 10%): Run 1: 240ms ████████████████████████ Run 2: 278ms ████████████████████████████ Run 3: 306ms ██████████████████████████████ Run 4: 258ms ██████████████████████████ Run 5: 249ms █████████████████████████ Run 6: 224ms ██████████████████████ Run 7: 231ms ███████████████████████ Run 8: 233ms ███████████████████████ Run 9: 240ms ████████████████████████ Run 10: 260ms ██████████████████████████

┌──────────────────────────────────────────────────────────────────┐ │ │ │ 📊 Startup Benchmark Results — Pixel 6a (Android 16) │ │ │ │ ┌────────────────────┬──────────────┬──────────────┬─────────┐ │ │ │ Metric │ No Profile │ With Profile │ Change │ │ │ ├────────────────────┼──────────────┼──────────────┼─────────┤ │ │ │ Median Startup │ 336.34 ms │ 244.74 ms │ -27.2% │ │ │ │ Minimum Startup │ 237.95 ms │ 224.41 ms │ -5.7% │ │ │ │ Maximum Startup │ 444.33 ms │ 306.02 ms │ -31.1% │ │ │ │ Consistency (CV) │ 23.10% │ 9.89% │ 2.3x ⬆️ │ │ │ └────────────────────┴──────────────┴──────────────┴─────────┘ │ │ │ │ No Profile: ████████████████████████████████████ 336ms │ │ With Profile: █████████████████████████ 245ms │ │ ^^^^^^^^^^^ │ │ 91ms SAVED │ │ 27% FASTER │ │ │ └──────────────────────────────────────────────────────────────────┘

Summary by CodeRabbit

  • New Features

    • Added baseline profile support to optimize app startup and runtime performance.
  • Chores

    • Integrated performance benchmarking infrastructure for monitoring startup performance with and without baseline profile optimizations.

markrizkalla avatar Mar 24 '26 15:03 markrizkalla

📝 Walkthrough

Walkthrough

This PR introduces a new baseline profile module to the project for performance measurement and optimization. It adds instrumentation tests for generating baseline profiles and benchmarking startup performance, along with necessary Gradle configuration, dependencies, and manifest setup.

Changes

Cohort / File(s) Summary
New Baseline Profile Module
baselineProfile/.gitignore, baselineProfile/src/main/AndroidManifest.xml, baselineProfile/build.gradle.kts
Module setup with Gradle build configuration, namespace declaration, SDK level targeting, and Android/Kotlin plugin application for baseline profile generation.
Instrumentation Test Classes
baselineProfile/src/main/java/org/mifos/baselineprofile/BaselineProfileGenerator.kt, baselineProfile/src/main/java/org/mifos/baselineprofile/StartupBenchmarks.kt
Two instrumentation test classes: BaselineProfileGenerator collects startup baseline profiles via BaselineProfileRule, and StartupBenchmarks measures startup performance under different compilation modes using MacrobenchmarkRule with cold-start iterations.
Root Build Configuration
build.gradle.kts, settings.gradle.kts
Added baselineprofile plugin to root plugins block and registered :baselineprofile as a new Gradle subproject.
Dependency and Version Management
gradle/libs.versions.toml, cmp-android/build.gradle.kts
Declared three new library versions (benchmarkMacroJunit4, uiautomator, espressoCore) and their coordinates, then added baselineProfile project dependency to the main app module.
Build Property Formatting
gradle.properties
Removed whitespace around equals sign in android.testOptions.unitTests.isIncludeAndroidResources property.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 With baselines drawn and benchmarks run,
Performance measured, tests all done,
Gradle scripts compiled with care,
Startup optimized, metrics fair!
A hopping progress, swift and true. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: integrating Baseline Profiles for performance optimization, which is the core objective of adding the new baselineprofile module and related infrastructure.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Mar 24 '26 15:03 coderabbitai[bot]

@markrizkalla It looks like the scope of this PR is limited to Android app only. I think it should be mentioned in the PR title and description.

Edit: I noticed that the description mentions cmp-android.

Are there any metrics related to the optimization and runtime performance that can be showcased here?

biplab1 avatar Mar 24 '26 15:03 biplab1

Are there any metrics related to the optimization and runtime performance that can be showcased here?

I will provide more details

markrizkalla avatar Mar 24 '26 16:03 markrizkalla