4
0

run-tests.sh 5.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env bash
  2. # usage: scripts/run-tests.sh [-j N] [-s M] <lynx_tests> [lynx_tests arguments: FL Data folder, --filter ...]
  3. #
  4. # Runs the core tests in parallel (round speed185a): the tests are split into M shards (lynx_tests --shard k/M, default
  5. # M = 3 x N), N of them run at a time, then the timing-sensitive tests listed in tests/serial-tests.txt run alone, on an
  6. # idle machine, as they always did. Every test runs exactly once (the shards are a partition) and checks the same.
  7. #
  8. # The output is each shard's output one after the other (test names, their " FAILED file:line" lines and times), then
  9. # the slowest tests, then ONE "N checks, M failures" line for the whole run: the same format as a single lynx_tests run,
  10. # so the scripts that read it work unchanged. A shard that crashes adds a " FAILED" line under the test it was running.
  11. # Exit status: 0 only if every shard ran to its end with 0 failures. -j 1: the plain single run of lynx_tests.
  12. # The shard logs stay in <lynx_tests>.shards/ (LYNX_TEST_LOGS=<dir> to choose).
  13. # tests/test-weights.txt ("<name> <ms>", the slowest tests of a CI run and of a local run with FL's Data, the larger of
  14. # the two) balances the shards, heaviest first; refresh it with: cat <lynx_tests>.shards/*.times. It only changes which
  15. # shard runs a test, never whether it runs.
  16. set -u
  17. jobs="${LYNX_TEST_JOBS:-}" shards=""
  18. while [[ $# -gt 0 ]]; do
  19. case "$1" in
  20. -j) jobs="$2"; shift 2 ;;
  21. -j*) jobs="${1#-j}"; shift ;;
  22. -s) shards="$2"; shift 2 ;;
  23. *) break ;;
  24. esac
  25. done
  26. bin="${1:?usage: run-tests.sh [-j N] [-s M] <lynx_tests> [arguments]}"; shift
  27. [[ -z "$jobs" || "$jobs" == auto ]] && jobs="$(nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 4)"
  28. case "$jobs" in '' | *[!0-9]* | 0) echo "run-tests: -j wants a number or auto, not '$jobs'" >&2; exit 2 ;; esac
  29. (( jobs == 1 )) && exec "$bin" "$@"
  30. [[ -n "$shards" ]] || shards=$((jobs * 3))
  31. here="$(cd "$(dirname "$0")/.." && pwd)"
  32. serial="$(grep -v '^ *#' "$here/tests/serial-tests.txt" 2> /dev/null | tr -d ' \r' | grep . | paste -sd, -)"
  33. heavy=""
  34. if [[ "${LYNX_TEST_SKIP_HEAVY:-0}" == 1 ]]; then
  35. heavy="$(grep -v '^ *#' "$here/tests/heavy-tests.txt" 2> /dev/null | tr -d ' \r' | grep . | paste -sd, -)"
  36. [[ -n "$heavy" ]] && echo "run-tests: LYNX_TEST_SKIP_HEAVY=1: the tests of tests/heavy-tests.txt do NOT run"
  37. fi
  38. skip=(); [[ -n "$heavy" ]] && skip=(--exclude "$heavy")
  39. logs="${LYNX_TEST_LOGS:-$bin.shards}"
  40. mkdir -p "$logs" && rm -f "$logs"/*.log "$logs"/*.times "$logs"/*.rc
  41. weights=()
  42. [[ -f "$here/tests/test-weights.txt" ]] && weights=(--weights "$here/tests/test-weights.txt")
  43. started=$(date +%s)
  44. echo "run-tests: $shards shards, $jobs at a time$([[ -n "$serial" ]] && echo ", then the serial tests alone") (logs in $logs)"
  45. # one shard: <k> [arguments]; writes <k>.log, <k>.times and <k>.rc (xargs runs N at a time; works with macOS' bash 3.2)
  46. export bin logs shards serial heavy
  47. runshard() {
  48. local k="$1"; shift
  49. local ex=() skip=(); [[ -n "$serial" ]] && ex=(--exclude "$serial"); [[ -n "$heavy" ]] && skip=(--exclude "$heavy")
  50. "$bin" "$@" ${ex[@]+"${ex[@]}"} ${skip[@]+"${skip[@]}"} --shard "$k/$shards" --times "$logs/$k.times" > "$logs/$k.log" 2>&1
  51. echo $? > "$logs/$k.rc"
  52. }
  53. export -f runshard
  54. for ((k = 1; k <= shards; ++k)); do echo "$k"; done |
  55. xargs -P "$jobs" -I{} bash -c 'runshard "$@"' runshard {} "$@" ${weights[@]+"${weights[@]}"}
  56. para=$(( $(date +%s) - started ))
  57. if [[ -n "$serial" ]]; then
  58. "$bin" "$@" ${skip[@]+"${skip[@]}"} --filter "$serial" --times "$logs/serial.times" > "$logs/serial.log" 2>&1
  59. echo $? > "$logs/serial.rc"
  60. fi
  61. # the shards' output in order, without each one's own summary; then the totals
  62. checks=0 failures=0 bad=0
  63. parts=(); for ((k = 1; k <= shards; ++k)); do parts+=("$k"); done; [[ -n "$serial" ]] && parts+=(serial)
  64. for p in "${parts[@]}"; do
  65. f="$logs/$p.log" rc="$(cat "$logs/$p.rc" 2> /dev/null || echo none)"
  66. sum="$(grep -E '^[0-9]+ checks, [0-9]+ failures$' "$f" 2> /dev/null | tail -1)"
  67. # everything before the shard's summary block (the blank line before " N tests in" or before "N checks")
  68. awk '/^ [0-9]+ tests in .*slowest:$/ || /^[0-9]+ checks, [0-9]+ failures$/ { exit } { buf[n++] = $0 }
  69. END { while (n > 0 && buf[n - 1] == "") --n; for (i = 0; i < n; ++i) print buf[i] }' "$f" 2> /dev/null
  70. if [[ -n "$sum" ]]; then
  71. checks=$((checks + ${sum%% checks*}))
  72. f2="${sum#*, }"; failures=$((failures + ${f2%% failures}))
  73. fi
  74. if [[ -z "$sum" || ( "$rc" != 0 && "$rc" != 1 ) ]]; then
  75. echo " FAILED $f:0: shard $p of lynx_tests stopped (exit $rc) during the test above"
  76. failures=$((failures + 1)); bad=1
  77. elif [[ "$rc" != 0 && "${sum#*, }" == "0 failures" ]]; then
  78. bad=1
  79. fi
  80. done
  81. total=$(( $(date +%s) - started ))
  82. echo
  83. cat "$logs"/*.times 2> /dev/null | sort -k2,2nr | head -15 | awk '{ printf " %8.1f s %s\n", $2 / 1000.0, $1 }' > "$logs/slowest.txt"
  84. ntests=$(cat "$logs"/*.times 2> /dev/null | wc -l | tr -d ' ')
  85. echo " $ntests tests in $total s wall ($para s in $shards shards, $jobs at a time, $((total - para)) s serial); slowest:"
  86. cat "$logs/slowest.txt"
  87. echo
  88. echo "$checks checks, $failures failures"
  89. [[ $failures == 0 && $bad == 0 ]]