istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tools/go-stress-test (about) 1 #!/usr/bin/env bash 2 3 # Copyright Istio Authors 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 # This script runs go tests in a package, but each test is run individually in a loop. This helps 18 # determine which tests are flaky. 19 # This requires https://github.com/howardjohn/golang-tools/tree/master/cmd/stress to be installed in $PATH. 20 21 # Usage: `go test -p 1 -exec $PWD/tools/go-stress-test ./...` 22 # Supported `go test` flags: -v, -race, -run, -count. All others are ignored (-timeout, etc) 23 # Addition flags: 24 # * -stress.runs: exit successfully after this many runs. Default 1000. 25 # * -stress.time: exit successfully after this much time has passed. Default 10s. 26 27 set -u 28 29 red='\e[0;31m' 30 green='\e[0;32m' 31 yellow='\e[0;33m' 32 clr='\e[0m' 33 34 binary="${1}" 35 RUN="." 36 COUNT=1 37 RUNS=1000 38 TIME=10s 39 while [[ "$#" -gt 0 ]]; do 40 case $1 in 41 -test.v=true) VERBOSE=true ;; 42 -test.run=*) RUN="${1#-test.run=}" ;; 43 -test.count=*) COUNT="${1#-test.count=}" ;; 44 -stress.runs=*) RUNS="${1#-stress.runs=}" ;; 45 -stress.runs) RUNS="${2}"; shift ;; 46 -stress.time=*) TIME="${1#-stress.time=}" ;; 47 -stress.time) TIME="${2}"; shift ;; 48 esac 49 shift 50 done 51 52 RESULTS=/tmp/test-results"$(dirname ${binary})" 53 mkdir -p "${RESULTS}" 54 code=0 55 56 for testname in $("${binary}" -test.list "${RUN}" | grep '^Test'); do 57 stress -f --max-time "${TIME:-10s}" --max-runs "${RUNS:-1000}" "${binary}" -test.run '^'"${testname}"'$' -test.count "${COUNT}" -test.v &> "${RESULTS}/${testname}" 58 # shellcheck disable=SC2181 59 if [[ $? != 0 ]]; then 60 echo -e "--- ${red}FAIL:${clr} ${testname}. See ${RESULTS}/${testname} for full logs" 61 code=1 62 elif [[ "${VERBOSE:-}" == true ]]; then 63 echo -e "--- ${green}PASS:${clr} ${testname}. $(cat ${RESULTS}/${testname} | tail -n2 | head -n1)" 64 fi 65 done 66 67 exit ${code}