性能测试
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
应用运行时性能可分为本地测试和现场测试。请注意,这两个区域提供的结果和指标不同。只要结果本身就有结论,这种差异是可以接受的。
现场测试
实地测试有助于您了解应用在真实用户和真实环境下的表现。这是一个重要的领域,有助于了解应用在现场的表现。您可以使用 Google Play Vitals 和 Firebase Performance Monitoring 等工具从用户那里获取现场指标。
您可以使用 AndroidX Tracing 库添加跟踪点,以便为现场指标提供更多背景信息和数据洞见。
您还可以使用 ApplicationStartInfo
和 ApplicationExitInfo
获取有关应用启动和退出的更多详细信息。
AndroidX JankStats 库可汇总和报告缓慢帧和丢帧,以便进一步分析。
本地测试
为了在本地测试应用的运行时性能,我们提供了基准测试库。它分为 macrobenchmark 库(用于测试整个用户流的性能)和 microbenchmark 库(用于分析应用或库的热循环性能)。
所有性能测试都应在实体设备上运行。只有这样,才能确保您衡量的是设备上实际发生的性能。运行时性能测试的结果会因其运行设备和设备的繁忙程度而异。
应用性能可能会下降。为了避免出现回归问题,请务必经常运行性能测试。在理想情况下,每当有新功能或代码合并到主分支时,系统都会对应用进行基准测试。最基本的性能监控是基准测试候选版本,并验证主要用户历程中的启动时间和帧时间是否会出现回归。我们建议您尽可能运行基准测试,例如在将功能合并到主分支之前或针对每夜 build 运行基准测试。
使用结果
性能测试是一个持续的过程。我们建议您以便于对比不同时间段的性能测试结果的方式存储性能测试结果。
您可以通过多种方式使用性能测试结果。
- 效果提升 - 使用衡量结果确定优先提升效果的方面
- 避免回归 - 确保新版本不会出现性能回归
- 生产监控 - 了解是否存在您在开发期间未发现的问题
如需详细了解 Android 运行时性能测试,请参阅应用性能指南。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Performance tests\n\nApp runtime performance can be divided into local testing and field testing.\nKeep in mind that both of these areas provide different results and\nmetrics. As long as the results are in itself conclusive, that divergence is\nacceptable.\n\nField testing\n-------------\n\nField testing helps you to understand how an app performs with real users under\nreal world conditions. It is an important area and helps to understand how an\napp performs in the field. You can use tools such as [Google Play Vitals](/topic/performance/vitals) and\n[Firebase Performance Monitoring](https://firebase.google.com/docs/perf-mon) to get field metrics from\nusers.\n\nYou can use the [AndroidX Tracing](/jetpack/androidx/releases/tracing) library to add trace points which\nprovides more context and insights to field metrics.\n\nYou can also use [`ApplicationStartInfo`](/reference/android/app/ApplicationStartInfo) and [`ApplicationExitInfo`](/reference/android/app/ApplicationExitInfo) to\nget more detailed information on application start and exit from users.\n\nThe [AndroidX JankStats](/topic/performance/jankstats) library enables aggregating and reporting of slow\nand dropped frames for further analysis.\n\nLocal testing\n-------------\n\nTo locally test the runtime performance of an app we provide the benchmarking\nlibrary. It is divided into the [macrobenchmark](/topic/performance/benchmarking/macrobenchmark-overview) library, which can be used\nto test the performance of entire user flows and the [microbenchmark](/topic/performance/benchmarking/microbenchmark-overview)\nlibrary, which is used to analyze hot loop performance of an application or\nlibrary.\n\nAll performance tests should run on a physical device. This is the only way to\nensure that the performance you're measuring is the actual performance\noccurring on a device. Runtime performance tests will produce different results\ndepending on the device they run on and how busy the device is.\n\nApplication performance can regress. To avoid regressions it's important to run\nperformance tests frequently. In an ideal scenario an app is benchmarked every\ntime a new feature is added or code is merged into the main branch. The bare\nminimum of performance monitoring is to benchmark release candidates and verify\nthat startup time and frame timing does not regress for major user journeys.\nWe recommend you run benchmarks whenever possible, such as before merging a\nfeature to the main branch or for nightly builds.\n\nUse the results\n---------------\n\nPerformance testing is an ongoing process. We recommend that you store results\nof performance tests in a way that they can be compared over time.\n\nYou can use results of performance tests in several ways.\n\n- Performance improvement - Use measurement results to prioritize performance improvement\n- Regression avoidance - Ensure there are no performance regressions with new releases\n- Production monitoring - Understand whether there are issues you're not seeing during development\n\nTo learn more about Android runtime performance testing, see the\n[guide to app performance](/performance)."]]