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 中檢查剖析結果,請選取微基準測試結果中的「方法追蹤記錄」或「堆疊取樣追蹤記錄」連結。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-21 (世界標準時間)。"],[],[],null,["By 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\nGroovy \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\nKotlin \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\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\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\nThis argument doesn't capture a profiling file. Information about timing and\nallocations are still measured.\n\nRecommended for you\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)"]]