自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
設定區塊
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本頁面說明如何透過區隔,為套件模組 (AndroidTest.xml
) 進行調整,並在實驗室中持續執行時獲得最佳速度效能。我們會嘗試以一般方式說明這些選項,並說明使用每個選項的理由。
在實驗室中持續執行套件時,套件通常會分散到多部裝置上,以縮短整體完成時間。通常,測試套件會嘗試平衡各個分割區的執行時間,以便縮短整體完成時間 (當最後一個分割區完成時);但由於某些測試的性質,我們不一定有足夠的內省功能,因此需要模組擁有者調整某些行為。
是否可分割?
您可以使用 <option name="not-shardable" value="true" />
標記模組 (AndroidTest.xml
),通知系統不應分割該模組。
在一般模組中,讓控制台分割模組 (預設行為) 是正確的做法。但在某些情況下,您可能需要覆寫該行為:
分割模組的結果是,每個相關裝置可能會執行一次準備作業 (安裝 APK、推送檔案等)。如果模組設定耗時且成本高昂,且與測試執行階段相比,不值得複製,則應將模組標記為不可分割。
分割模組的結果是,所有測試案例可能會在不同的裝置上獨立執行。這與第一點有關;如果測試次數偏低,某些分割區可能就會只執行單一測試或不執行任何測試,這會讓任何準備步驟變得相當昂貴。舉例來說,為單一測試案例安裝 APK 通常不值得。
檢測設備測試:資料分割數量上限?
透過 AndroidJUnitTest 執行的檢測設備測試,在實際安裝及執行 APK 之前,不會向測試套件公開檢測設備的測試數量。這些作業成本高昂,且無法在分割時間執行套件中所有模組的部分。
測試套件可能會過度分割檢測設備測試,導致部分分割區為空白;如果在六個分割區中分割包含五個測試的檢測設備測試,則會產生五個包含一個測試的分割區,以及一個不含測試的分割區。每個分割區都需要耗費大量成本的 APK 安裝作業。
因此,如果檢測設備測試 APK 中的測試數量偏低,將模組標記為 <option name="not-shardable" value="true" />
可讓測試套件知道不值得對該模組進行分割。
AndroidJUnitTest
執行程式具有特殊選項,可讓其指定可分割的區塊數量上限:<option name="ajur-max-shard" value="5" />
。
這樣一來,您就能指定可分割檢測工具的次數上限,無論在叫用層級要求的分割數量為何。根據預設,這項檢測會被切割成呼叫所要求的分割區數量。
舉例來說,如果檢測工具測試 APK 只包含兩個測試案例,但您仍想將其分割,將 ajur-max-shard
值設為 2
可確保您不會建立空白分割區。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","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-07-27 (世界標準時間)。"],[],[],null,["# Configure sharding\n\nThis page describes what is possible to tune for a suite module\n(`AndroidTest.xml`) via sharding and get the best speed performance during\ncontinuous execution in the lab. We will attempt to describe the options in a\ngeneric manner with the rationale for using each.\n\nWhen running continuously a suite in the lab, the suite is usually sharded\nacross several devices to reduce the overall completion time. The harness\ntypically attempts to balance the execution time of each shard to minimize the\noverall completion time (when the last shard finishes); but due to the nature of\nsome tests, we do not always have enough introspection and need the module owner\nto tune some behavior.\n\nShardable or not shardable?\n---------------------------\n\nIt is possible to tag a module (`AndroidTest.xml`) with\n`\u003coption name=\"not-shardable\" value=\"true\" /\u003e` to notify the harness that it\nshould not be sharded.\n\nIn a typical module, letting the harness shard your module\n(the default behavior) is the right thing to do. But in some cases, you might\nwant to override that behavior:\n\n- When the setup of your module is expensive:\n\nSharding a module results in the preparation (install APK, push file, etc.)\npossibly run once per device involved. If your module setup is long and\nexpensive and not worth being replicated compared to the test's runtime, you\nshould tag your module as not-shardable.\n\n- When the number of tests in your module is low:\n\nSharding a module results in all the test cases possibly executing independently\non different devices. This relates to the first point; if your number of tests\nis low, you might end up with a single test or no test in some shards, which\nwould make any preparation step quite expensive. Installing an APK for a single\ntest case is usually not worth it, for example.\n\nInstrumentation tests: Max number of shards?\n--------------------------------------------\n\nAn instrumentation test running through [AndroidJUnitTest](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/test_framework/com/android/tradefed/testtype/AndroidJUnitTest.java)\ndoes not expose to the harness how many tests are part of the instrumentation\nuntil we actually install and run the APK. These operations are costly and\ncannot be executed at sharding time for all the modules part of the suite.\n\nThe harness might over-shard the instrumentation test and end up with some\nempty shards; sharding an instrumentation test with five tests in six shards\nresults in five shards with one test and one shard with no tests. Each of these\nshards would require a costly APK installation.\n\nSo when the number of tests in the instrumentation test APK is low, tagging the\nmodule with `\u003coption name=\"not-shardable\" value=\"true\" /\u003e` would allow the\nharness to know sharding that module is not worth it.\n\nThe `AndroidJUnitTest` runner has a special option allowing it to specify the\nmax number of shards it is allowed to shard into:\n`\u003coption name=\"ajur-max-shard\" value=\"5\" /\u003e`.\n\nThis allows you to specify a maximum number of times the instrumentation can be\nsharded regardless of the number of shards requested at the invocation level. By\ndefault, the instrumentation will be sharded into the number of shards requested\nfor the invocation.\n\nFor example, if your instrumentation test APK contains only two test cases but\nyou still want to shard it, having a `ajur-max-shard` value of `2` would ensure\nyou are not creating empty shards."]]