对您的应用进行基准测试
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
基准测试是检查和监控应用性能的一种方式。您可以定期运行基准测试,以分析和调试性能问题,并帮助确保近期的更改不会引起性能下降。
Android 提供了以下两种进行基准测试的库和方法,供您分析和测试应用在不同情况下的性能表现:Macrobenchmark 和 Microbenchmark。
Macrobenchmark
Macrobenchmark 库可衡量更大规模的最终用户互动,例如启动、与界面交互和动画。此库可让您直接控制受测试的性能环境。借助它,您还可以通过控制编译、启动和停止应用来直接衡量实际的应用启动或滚动。
Macrobenchmark 库可在外部从您通过测试构建的测试应用注入事件并监控结果。因此,您在编写基准测试时,不要直接调用应用代码,而要像用户那样在应用中导航。
Microbenchmark
借助 Microbenchmark 库,您可以直接在一个循环中对应用代码进行基准测试。该库旨在衡量 CPU 工作情况,衡量结果将用于评估最佳情况下的性能(例如,即时 [JIT] 编译已预热,磁盘访问已缓存),使用内部循环或特定热函数时就可能获得这种最佳性能。此外,该库只能衡量您可以直接单独调用的代码。
如果您的应用需要处理复杂的数据结构,或者采用了一些会在应用运行期间进行多次调用的特定计算密集型算法,就可能适合进行基准测试。您还可以衡量界面的各个部分。例如,您可以衡量绑定一个 RecyclerView
项目的成本有多高、膨胀布局需要花多长时间,以及从性能的角度来看,View
类的“布局和衡量”遍历的要求有多高。
不过,您无法衡量进行基准测试的用例对整体用户体验所产生的影响。在某些情况下,您无法通过基准测试判断卡顿或应用启动时间等瓶颈是否得到了改善。因此,首先使用 Android 性能分析器找出这类瓶颈至关重要。找到要调查和优化的代码后,您便可以快速且更轻松地反复运行会进行基准测试的循环,并生成噪声较少的结果,让您能够专注于需要改进的方面。
Microbenchmark 库只能报告应用的相关信息,而非整个系统的相关信息。因此,它最适合用来分析应用在特定情况下的性能,而不是分析系统的整体性能问题。
基准测试库之间的对比
|
Macrobenchmark |
Microbenchmark |
API 版本 |
23 及更高版本 |
14 及更高版本 |
函数
|
衡量高级别入口点或交互,例如 activity 启动或列表滚动。 |
衡量各个函数。
|
范围
|
对整个应用进行进程外测试。 |
对 CPU 工作情况进行进程内测试。
|
速度
|
迭代速度中等。可能超过 1 分钟。 |
迭代速度快。通常不到 10 秒。 |
跟踪
|
结果包含性能分析轨迹。 |
(可选)方法采样和跟踪。 |
为您推荐
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Benchmark your app\n\nBenchmarking is a way to inspect and monitor the performance of your app. You\ncan regularly run benchmarks to analyze and debug performance problems and help\nensure that you don't introduce regressions in recent changes.\n\nAndroid offers two benchmarking libraries and approaches for analyzing and\ntesting different kinds of situations in your app: Macrobenchmark and\nMicrobenchmark.\n\nMacrobenchmark\n--------------\n\nThe [Macrobenchmark](/studio/profile/macrobenchmark) library measures larger end-user interactions, such as\nstartup, interacting with the UI, and animations. The library provides direct\ncontrol over the performance environment you're testing. It lets you control\ncompiling and lets you start and stop your app to directly measure actual app\nstartup or scrolling.\n\nThe Macrobenchmark library injects events and monitors results externally from a\ntest app that is built with your tests. Therefore, when writing the benchmarks,\nyou don't call your app code directly and instead navigate within your app as a\nuser.\n\nMicrobenchmark\n--------------\n\nThe [Microbenchmark](/studio/profile/benchmark) library lets you benchmark app code directly in a loop.\nThis is designed for measuring CPU work that assesses best-case performance---such\nas warmed up Just in Time (JIT) and disk accesses cached---that you might see with\nan inner-loop or a specific hot function. The library can only measure the\ncode that you can call directly in isolation.\n\nIf your app needs to process a complex data structure, or have some specific\ncomputation-heavy algorithm that is called multiple times during the app run,\nthese might be good cases for benchmarking. You can also measure parts of your\nUI. For example, you can measure the cost of the `RecyclerView` item binding,\nhow long it takes to inflate a layout, or how demanding the layout-and-measure\npass of your `View` class is from a performance perspective.\n\nHowever, you aren't able to measure how the benchmarked cases contribute to the\noverall user experience. In some scenarios, benchmarking doesn't tell you if\nyou're improving a bottleneck like jank or app startup time. For this reason,\nit's crucial to identify those bottlenecks first with the [Android Profiler](/studio/profile).\nAfter you find the code you want to investigate and optimize, the benchmarked\nloop can run repeatedly in a quick and easier fashion to create less noisy\nresults, which lets you focus on one area of improvement.\n\nThe Microbenchmark library only reports information about your app, not about\nthe system overall. Therefore, it's best at analyzing performance of situations\nspecific to the app, not ones that might relate to overall system issues.\n\nBenchmark library comparison\n----------------------------\n\n| | Macrobenchmark | Microbenchmark |\n|-------------|-----------------------------------------------------------------------------------------------|---------------------------------------------------|\n| API version | 23 and later | 14 and later |\n| Function | Measure high-level entry points or interactions, such as activity launch or scrolling a list. | Measure individual functions. |\n| Scope | Out-of-process test of full app. | In-process test of CPU work. |\n| Speed | Medium iteration speed. It can exceed a minute. | Fast iteration speed. Often less than 10 seconds. |\n| Tracing | Results come with profiling traces. | Optional method sampling and tracing. |\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Create Baseline Profiles {:#creating-profile-rules}](/topic/performance/baselineprofiles/create-baselineprofile)\n- [JankStats Library](/topic/performance/jankstats)\n- [Overview of measuring app performance](/topic/performance/measuring-performance)"]]