Android Performance Samples

repository·main·Indexed 23 days ago

https://github.com/android/performance-samples

A collection of Android samples demonstrating the practical application of performance-related libraries, including Macrobenchmark, Microbenchmark, and JankStats. The repository provides guidance on per-frame performance logging with JankStats, CI integration for Jetpack Macrobenchmarks using Firebase Test Lab and Google Cloud Functions, and specific project structures for consistent Microbenchmark results.

Tokens
9K
Snippets
19
Records
45
Agent score
80%

What's inside android-performance-samples

  1. Overview of the Microbenchmark Sample

    main

    This sample project demonstrates how to use the Jetpack Microbenchmark library. It provides several benchmark examples to illustrate different performance measurement scenarios:

    • AutoBoxingBenchmark: Measures the cost of allocating Integer() objects.
    • BitmapBenchmark: Measures the cost of accessing Bitmap pixels, demonstrating JNI call overhead.
    • SortingBenchmarks: Demonstrates working with random data and using runWithTimingDisabled sections to measure different sorting algorithms.
    • RecyclerViewBenchmark: A complex UI benchmark measuring the cost of scrolling a RecyclerView.
  2. Overview of Android Performance Samples

    main
    This repository is a collection of samples demonstrating how to use various Android performance libraries. It provides practical implementations for measuring application startup, runtime performance, code execution, and frame stability (jank).
  3. Overview of Jetpack Macrobenchmarks CI integration

    main

    To run Jetpack Macrobenchmarks on Firebase Test Lab as a post-submit action and track performance regressions, you must configure three integrated components:

    1. GitHub Workflow: Responsible for building the application under test and the macrobenchmark APK, then submitting both to Firebase Test Lab.
    2. Google Cloud Function: Automatically retrieves benchmark results from Firebase Test Lab and generates custom metrics within Google Cloud Monitoring.
    3. Google Cloud Monitoring Dashboards: Used to visualize performance data via custom dashboards and configure alerts for regressions.

    This setup allows you to track app performance over time and detect regressions automatically during your CI process.

  4. Manage Baseline Profiles with AGP 8.0.0+

    main

    Starting with Android Gradle Plugin (AGP) 8.0.0, Baseline Profiles can be stored in the src/main/baselineProfiles folder.

    This approach allows for:

    • Storing multiple profile files in one location.
    • Granular updates: You can update or create specific profiles without needing to re-generate the entire Baseline Profile.
    • Separate generator classes: You can define separate classes for different user journeys and a separate one for app startup (e.g., using the baselineprofile package).
  5. Understand the Microbenchmark Project Structure

    main

    To produce consistent results, Microbenchmarks require a specific module separation. The project is split into three distinct modules to ensure the benchmark runner does not interfere with the application being measured:

    1. :microbenchmark: Contains only the Microbenchmark tests. This module serves as the entrypoint and has no dependencies on other modules to prevent it from being merged into the built application.
    2. :benchmarkable: Contains the actual code that is intended to be called and measured by the Microbenchmarks.
    3. :app: Contains general application code. Code in this module cannot be called from Microbenchmarks.

    Key Rule: Ensure the code you want to benchmark resides in the :benchmarkable module, not the :app module.

  6. Run Microbenchmarks

    main

    You can run the microbenchmarks using Android Studio or the command line.

    Using Android Studio

    Open the project in Android Studio Arctic Fox 2020.3.1 or later. Run the benchmarks as you would any other test using the keyboard shortcut:

    • Windows/Linux: Ctrl-Shift-F10
    • macOS: Ctrl-Shift-R

    Using Command Line

    Execute the following Gradle command to run the connected checks:

    ./gradlew benchmark:connectedCheck