編寫測試執行程式時,請務必考量擴充性。提問 「如果測試執行器必須執行 20 萬個測試案例」需要多久時間?
分割是 Trade Federation 提供的其中一個答案。這項操作需要將執行程式需要的所有測試分割成可並行執行的多個區塊。
本頁說明如何將跑者可分割用於交易。
要實作的介面
要實作的單一最重要介面,可視為可分割
TF 是
IShardableTest、
其中包含兩個方法:split(int numShard)
和 split()
。
如果區塊切割作業取決於要求的區塊數量,您應實作 split(int numShard)
。否則,請實作 split()
。
當 TF 測試指令搭配使用區隔參數 --shard-count
和 --shard-index
執行時,TF 會逐一檢查所有 IRemoteTest
,尋找實作 IShardableTest
的 IRemoteTest
。如果找到,就會呼叫 split
取得新的 IRemoteTest
物件,為特定分割區執行部分測試案例。
請問我應該瞭解哪些關於分割導入方式的資訊?
- 跑者只能在某些情況下進行資料分割;這樣會傳回
null
找到的資料 - 請盡量多分得: 將跑者分成一根 合理的方式執行程式碼這確實取決於跑者。適用對象 範例: HostTest 在類別層級進行資料分割,每個測試類別都會放在獨立的資料分割中。
- 如果可行,請新增一些選項,進一步控制資料分割。
例如:AndroidJUnitTest 有
ajur-max-shard
,可指定可分割的切片數量上限,無論要求的數量為何。
詳細實作範例
以下是導入 IShardableTest
的程式碼片段範例
參照。如需完整的程式碼,請前往
(https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/main/test_framework/com/android/tradefed/testtype/InstalledInstrumentationsTest.java)
/**
* Runs all instrumentation found on current device.
*/
@OptionClass(alias = "installed-instrumentation")
public class InstalledInstrumentationsTest
implements IDeviceTest, IResumableTest, IShardableTest {
...
/** {@inheritDoc} */
@Override
public Collection<IRemoteTest> split(int shardCountHint) {
if (shardCountHint > 1) {
Collection<IRemoteTest> shards = new ArrayList<>(shardCountHint);
for (int index = 0; index < shardCountHint; index++) {
shards.add(getTestShard(shardCountHint, index));
}
return shards;
}
// Nothing to shard
return null;
}
private IRemoteTest getTestShard(int shardCount, int shardIndex) {
InstalledInstrumentationsTest shard = new InstalledInstrumentationsTest();
try {
OptionCopier.copyOptions(this, shard);
} catch (ConfigurationException e) {
CLog.e("failed to copy instrumentation options: %s", e.getMessage());
}
shard.mShardIndex = shardIndex;
shard.mTotalShards = shardCount;
return shard;
}
...
}
這個範例只會建立一個本身的新例項,並設定資料分割 參數。不過,分割邏輯可能與 進行測試;只要確定性不定 即使只有少數幾個子集也沒問題
獨立
資料分割必須獨立!實作
執行器中的 split
不應彼此依附性或共用
再複習一下,機構節點
是所有 Google Cloud Platform 資源的根節點
分割區塊必須是確定性的!這也是必要條件,在相同條件下,split
方法應一律傳回完全相同的切片清單,且順序也相同。
注意事項:由於每個資料分割都能在不同的 TF 執行個體上執行,因此請務必
確保 split
邏輯會產生互斥的子集,且
收集完整資料
在本機分割測試
如要在本機 TF 上分割測試,只需將 --shard-count
選項新增至
建立服務帳戶
tf >run host --class com.android.tradefed.UnitTests --shard-count 3
接著,TF 會自動為每個分割區產生指令並執行。
tf >l i
Command Id Exec Time Device State
3 0m:03 [null-device-2] running stub on build 0 (shard 1 of 3)
3 0m:03 [null-device-1] running stub on build 0 (shard 0 of 3)
3 0m:03 [null-device-3] running stub on build 0 (shard 2 of 3)
測試結果匯總
由於 TF 不會針對分割的叫用作業進行任何測試結果匯總,因此您必須確認報表服務支援這項功能。