设置环境以进行性能测试
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
记录短时间内的设备活动情况并收集应用启动期间的跟踪记录有助于查出潜在瓶颈并提高应用的整体性能。本页介绍如何设置环境以进行性能测试。
使用 Macrobenchmark 库
Macrobenchmark 库可衡量更大规模的最终用户互动,例如启动、与界面互动和动画。此库可让您直接控制受测试的性能环境。借助它,您还可以通过控制编译、启动和停止应用来直接衡量精确的应用启动时间,并最大限度地减少多次测试运行之间的噪声和差异。
使用中端设备发现潜在的性能问题
请在关注的每类设备上测试性能。在组件运行速度较快的高端设备上,有可能无法察觉在较旧、运行速度较慢或 RAM 配置较低的设备上存在的性能问题。低端设备在加载数据或运行代码时可能需要更长时间,因此在这类设备上更容易发现瓶颈。针对低端设备优化性能通常还有助于针对高端设备的优化。
降低噪声
- 网络:以稳定的互联网 Wi-Fi 速度测试应用(或进程)。如果应用启动时间包含网络请求,这是可能出现变化的地方,请特别注意。
- RAM 使用量:在测试应用启动性能时,不要让任何其他应用在后台运行。
- 电池:确保您的设备电量充足,以避免特定硬件因电量低而受到性能限制。
在发布 build 上进行测试
使用发布 build 测试性能。调试 build 不适合用于性能调试,因为它们不提供编译优化,而这会对性能产生重大影响。
但是,可以使用未经混淆处理的发布 build 来识别类和操作名称。具体来说,我们建议您通过 ProGuard 文件中的 -dontobfuscate
启用 minify (R8) 并停用混淆处理功能。如果 build 不经过混淆处理,布局、资产和资源更容易识别。
请务必在清单中添加 profileable 标志,以便在不可调试的 build 中可以看到您的自定义事件。此标志适用于 Android 10(API 级别 29)及更高版本。
为应用操作添加自定义跟踪记录
在您的应用中添加自定义跟踪记录,以便更轻松地了解与其他库相比,应用执行了哪些操作。这有助于您全面了解应用在任何时间的活动情况。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Set up your environment for performance testing\n\nYou can identify potential bottlenecks and improve overall app performance by\nrecording device activity over a short period of time and [collecting traces of\nyour app's startup period](/topic/performance/tracing). This page shows how to\nset up your environment for performance testing.\n\nUse the Macrobenchmark library\n------------------------------\n\nThe [Macrobenchmark\nlibrary](/topic/performance/benchmarking/macrobenchmark-overview) measures\nlarger end-user interactions, such as startup, interacting with the UI, and\nanimations. The library provides direct control over the performance environment\nyou're testing. It lets you control compiling, starting, and stopping your app\nto directly measure precise app startup time. It also works to minimize the\nnoise and differences between test runs.\n\nUse mid-range devices to identify potential performance issues\n--------------------------------------------------------------\n\nTest performance on each device type you care about. High-end devices with fast\ncomponents can hide performance problems on earlier, slower, or low RAM devices.\nLower-end devices can take longer to load data or run code, making it easier to\nidentify bottlenecks. Optimizing performance for low-end devices usually also\nbenefits optimization for high-end devices.\n\nReduce noise\n------------\n\n- Network: test your apps or processes with strong and stable internet Wi-Fi speeds. If the app startup time includes a network request, note this as a place where variability might occur.\n- RAM usage: don't have any other apps running in the background of your device while testing app startup performance.\n- Battery: ensure your device is charged to avoid any hardware-specific low power performance throttling.\n\nTest on release builds\n----------------------\n\nUse release builds to test performance. Debug builds are [unsuitable for\nperformance\ndebugging](/studio/profile/measuring-performance#apk-considerations), as they\ndon't provide compilation optimization and significantly impact performance.\n\nHowever, it's okay to use an unobfuscated release build to identify classes and\noperation names. Specifically, we recommend enabling [minify\n(R8)](/studio/build/shrink-code) and disabling obfuscation, with\n[`-dontobfuscate`](/studio/build/shrink-code#obfuscate) in the proguard file.\nIt's easier to identify layouts, assets, and resources if the build is\nunobfuscated.\n\nMake sure you include the\n[profileable](/guide/topics/manifest/profileable-element) flag in the manifest\nso that your custom events are visible in non-debuggable builds. This flag is\navailable on Android 10 (API level 29) and later.\n\nAdd custom traces to your app operations\n----------------------------------------\n\nAdd [custom traces](/topic/performance/tracing/custom-events) within your app to\nmake it easier to identify what operations are performed by your app compared to\nother libraries. This helps give you more context about what the app is doing at\nall times."]]