我們的 CI 中有一個強大的服務器,我們希望利用它并在同一臺機器上并行化我們的 cypress 測驗套件。我們知道

我們正在盡最大努力避免將每個 cypress 實體作為一個新的 docker 容器運行,以避免額外的 CI 復雜性,但在必要時會深入研究。我們在這里遺漏了一些明顯的東西嗎?
這是完整的腳本供參考:
#!/bin/bash
nThreads=3
print_usage() {
printf "Usage:
./run_tests_parallel.sh -n <number of threads to use>
Defaults to $nThreads threads\n"
exit 0;
}
while true; do
case "$1" in
-n | --threads ) nThreads=$2; shift 2 ;;
-h | --help ) print_usage ; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
echo Using $nThreads threads
# Return non-zero if any of the subprocesses
# returns non-zero
set -eu
testFiles=`find . -name "*.test.ts" -not -path "./node_modules/*"`
# init testF
testFilesPerThread=()
for (( n=0; n<$nThreads; n )); do
testFilesPerThread =("")
done
i=0
for testFile in $testFiles; do
testFilesPerThread[$i]="${testFilesPerThread[$i]} $testFile"
i=$((($i 1)%$nThreads))
done
pids=()
for (( i=0; i<${#testFilesPerThread[@]}; i )); do
echo Thread $i has files: ${testFilesPerThread[$i]}
# strip string and join files with ","
specFiles=`echo ${testFilesPerThread[$i]} | xargs | tr -s "\ " ","`
port=$((30001 $i))
# run tests in background
npx cypress run --spec $specFiles --port $port --headless &
pids =($!)
echo "Spawned PID ${pids[${#pids[@]}-1]} for thread $i on port $port"
done
for pid in ${pids[@]} ; do
echo "Waiting for PID $pid."
wait $pid
done
echo DONE.
uj5u.com熱心網友回復:
我不是專家,但“意外的輸入結束”聽起來像是發生了檔案訪問沖突。也許兩個行程試圖寫入同一個測驗工件。
我聽說線??程數通常不應超過內核數 - 1. 在我的 4 核機器上,指定 3 個執行緒使我在超過 20 個規格的情況下吞吐量增加約 15%。
我使用 NodeJS 腳本呼叫 Cypress 模塊 API,它允許在每個執行緒的基礎上調整配置以避免檔案寫入沖突(請參閱reporterOptions)
const path = require('path')
const fs = require('fs-extra')
const cypress = require('cypress')
const walkSync = function(dir, filelist = []) {
const files = fs.readdirSync(dir);
files.forEach(function(item) {
const itemPath = path.join(dir, item)
if (fs.statSync(itemPath).isDirectory()) {
filelist = walkSync(itemPath, filelist);
}
else {
filelist.push(itemPath);
}
});
return filelist;
};
const files = walkSync(path.join(__dirname, '../cypress/integration'))
const groups= 3
const groupSize = Math.ceil(files.length / groups)
const groups = files.reduce((acc, file) => {
if (!acc.length || acc[acc.length-1].length === groupSize) acc.push([]);
acc[acc.length-1].push(file)
return acc
},[])
console.time('process-time')
const promises = groups.map((group, i) => {
return cypress.run({
config: {
video: false,
screenshotsFolder: `cypress/screenshots/group${i}`,
reporterOptions: {
mochaFile: `results/group${i}/my-test-output.xml`,
}
},
spec: group.join(','),
})
})
Promise.all(promises).then(results => {
console.timeEnd('process-time')
});
另請參閱是否有任何方法可以在使用cypress-parallel 的本地機器上運行并行 cypress 測驗。
uj5u.com熱心網友回復:
正如 Fody 在這個答案中提到的:
聽起來像是發生了檔案訪問沖突
所有這些錯誤都是由每個單獨的后臺行程轉換我們的打字稿代碼引起的競爭條件造成的。有時,cypress 實體會嘗試運行測驗,而另一個行程正在轉換代碼,這會導致各種錯誤,包括奇怪的語法錯誤。
我們現在決定在自己的容器中運行每組測驗,因為這是在自己的環境中隔離每個測驗套件的唯一方法
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/314559.html
