android{defaultConfig{// must be one of: 'None', 'StackSampling', or 'MethodTracing'testInstrumentationRunnerArguments["androidx.benchmark.profiling.mode"]='StackSampling'}}
Kotlin
android{defaultConfig{// must be one of: 'None', 'StackSampling', or 'MethodTracing'testInstrumentationRunnerArguments["androidx.benchmark.profiling.mode"]="StackSampling"}}
当您对基准测试进行性能分析后,系统会将输出 .trace 文件连同 JSON 结果一起复制到主机的目录下。如需在 Android Studio 中检查性能分析结果,请在微基准测试结果中选择方法跟踪或堆栈采样跟踪链接。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Profile a Microbenchmark\n\nBy default, Microbenchmarks give you information about the timing and\nallocations of the executed code. If you want to investigate why the measured\ncode is running slowly, inspect the method trace---captured by default on\n[supported OS versions](#method-tracing-default-support)---or select other\nprofiling configurations.\n\nTo select the profiler configuration, add the instrumentation runner argument\n`androidx.benchmark.profiling.mode` with one of\n[`MethodTracing`](#method-tracing) (default),\n[`StackSampling`](#stack-sampling), or [`None`](#none) argument, as shown in the\nfollowing snippet.\n\nFor more information about the options, see [Record Java/Kotlin methods](/studio/profile/record-java-kotlin-methods).\n`MethodTracing` is the equivalent of tracing, and `StackSampling` is the\nequivalent of sampling as defined in that document. \n\n### Groovy\n\n```groovy\nandroid {\n defaultConfig {\n // must be one of: 'None', 'StackSampling', or 'MethodTracing'\n testInstrumentationRunnerArguments[\"androidx.benchmark.profiling.mode\"]= 'StackSampling'\n }\n}\n```\n\n### Kotlin\n\n```kotlin\nandroid {\n defaultConfig {\n // must be one of: 'None', 'StackSampling', or 'MethodTracing'\n testInstrumentationRunnerArguments[\"androidx.benchmark.profiling.mode\"] = \"StackSampling\"\n }\n}\n```\n\nWhen you profile a benchmark, an output `.trace` file is copied to the host in\nthe directory [alongside JSON results](/topic/performance/benchmarking/microbenchmark-write#benchmark-results). To inspect profiling results in\nAndroid Studio, select the **Method Trace** or **Stack Sampling Trace** link\nin the microbenchmark results.\n| **Note:** The Microbenchmark library automatically captures system tracing, but with custom tracing sections disabled---it ignores calls to `Trace.beginSection and endSection`---to prevent interference from tracing overhead.\n\nMethodTracing\n-------------\n\nMethod tracing is useful when you are trying to optimize your code because it\ncan help you identify the methods that take longer to run than others. You can\nthen focus on optimizing the methods that have the most impact on performance.\n\nProfiling occurs in sequence after code measurement, so your test outputs both\naccurate timing and profiling results.\n\nMethod tracing is on by default. \n**Note:** On some Android OS and ART versions, method tracing is off by default. In these cases, Android Studio outputs a warning.\n\nStackSampling\n-------------\n\nSample tracing can also help identify expensive methods without the\nperformance overhead of method tracing. However, if your app enters a method\nafter a call stack has been captured and the method exits before the next\ncapture, then the method call is not logged. To properly track methods with\nshort life cycles, use method tracing instead of sample tracing.\n\nWith stack sampling, the benchmark samples call stacks after the warmup is\ncomplete. You can control sampling behavior such as the\n[sample frequency](#profiling-samplefrequency)\nand [duration of sampling](#profiling-sampleduration) using instrumentation\narguments.\n\nOn Android 10 (API 29) and higher, stack sampling uses [Simpleperf](/ndk/guides/simpleperf) to sample\napp callstacks, including C++ code. On Android 9 (API 28) and lower, it uses\n[`Debug.startMethodTracingSampling`](/reference/kotlin/android/os/Debug#startMethodTracingSampling(kotlin.String,%20kotlin.Int,%20kotlin.Int)) to capture stack samples.\n\nYou can configure this profiling mode by adding another instrumentation\narguments:\n\n- `androidx.benchmark.profiling.sampleFrequency`\n\n - Number of stack samples to capture per second.\n - Argument type: integer\n - Defaults to 1000 samples per second.\n- `androidx.benchmark.profiling.sampleDurationSeconds`\n\n - Duration of benchmark to run.\n - Argument type: integer\n - Defaults to 5 seconds.\n- `androidx.benchmark.profiling.skipWhenDurationRisksAnr`\n\n - Skips method tracing when it's likely to cause an ANR. You should keep this enabled for CI runs, since ANRs can cause problems during long CI runs.\n - Argument type: boolean\n - Defaults to `true`\n\nNone\n----\n\nThis argument doesn't capture a profiling file. Information about timing and\nallocations are still measured.\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Microbenchmark Instrumentation Arguments](/topic/performance/benchmarking/microbenchmark-instrumentation-args)\n- [Run benchmarks in Continuous Integration](/topic/performance/benchmarking/benchmarking-in-ci)"]]