askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_fast.sh · 5.3 KB · 117 lines
| 1 | #!/usr/bin/env bash |
| 2 | # Run the whole default test suite across several pytest processes at once. |
| 3 | # |
| 4 | # scripts/test_fast.sh [SHARDS] default: one per logical CPU, at most 12 |
| 5 | # |
| 6 | # The test files are split into SHARDS groups of about equal measured time |
| 7 | # (scripts/test_fast_weights.tsv), each group runs in its own pytest process |
| 8 | # with its own --basetemp, and the result is one summary line. The plain |
| 9 | # command (`pytest`) is unchanged and remains the reference. |
| 10 | # |
| 11 | # It fails if any shard fails, or if the shards together ran a different number |
| 12 | # of tests than a plain `pytest --collect-only` selects (a shard can never |
| 13 | # silently drop a test). The deselected count is the plain collect's: those |
| 14 | # tests are never handed to a shard, so a shard cannot report them. |
| 15 | # |
| 16 | # Environment: |
| 17 | # PYTHON interpreter to use (default: python3) |
| 18 | # TEST_FAST_LOGS where each shard's log goes (default: build/test-fast) |
| 19 | # TEST_FAST_KEEP=1 keep the per-shard basetemp directories on success |
| 20 | set -u |
| 21 | cd "$(dirname "$0")/.." |
| 22 | export TZ=UTC |
| 23 | PY="${PYTHON:-python3}" |
| 24 | LOGS="${TEST_FAST_LOGS:-build/test-fast}" |
| 25 | |
| 26 | default_shards() { |
| 27 | local n |
| 28 | n=$(sysctl -n hw.ncpu 2>/dev/null || nproc 2>/dev/null || echo 4) |
| 29 | [ "$n" -gt 12 ] && n=12 |
| 30 | echo "$n" |
| 31 | } |
| 32 | SHARDS="${1:-$(default_shards)}" |
| 33 | case "$SHARDS" in ''|*[!0-9]*|0) echo "usage: $0 [SHARDS] (SHARDS is a positive integer)" >&2; exit 2;; esac |
| 34 | |
| 35 | rm -rf "$LOGS"; mkdir -p "$LOGS" |
| 36 | # Per-shard --basetemp lives OUTSIDE the repository: the record layer refuses |
| 37 | # a check-object override root that sits inside the repo, so a basetemp under |
| 38 | # build/ makes every test that stages locked checks fail. |
| 39 | BASE="$(mktemp -d "${TMPDIR:-/tmp}/test-fast.XXXXXX")" |
| 40 | START=$(date +%s) |
| 41 | |
| 42 | # Collect exactly what plain `pytest` would run (same addopts, same -m filter). |
| 43 | "$PY" -m pytest --collect-only -qq -p no:cacheprovider > "$LOGS/collect.txt" 2>&1 |
| 44 | CRC=$? |
| 45 | if [ "$CRC" -ne 0 ]; then |
| 46 | echo "collection failed (exit $CRC); see $LOGS/collect.txt" >&2; tail -20 "$LOGS/collect.txt" >&2; exit 1 |
| 47 | fi |
| 48 | |
| 49 | "$PY" - "$SHARDS" "$LOGS" scripts/test_fast_weights.tsv <<'PYEOF' || exit 1 |
| 50 | import collections, re, sys |
| 51 | shards, logs, weights_path = int(sys.argv[1]), sys.argv[2], sys.argv[3] |
| 52 | counts = collections.Counter() |
| 53 | collected = None |
| 54 | for line in open(f"{logs}/collect.txt"): |
| 55 | if "::" in line and line.startswith("tests/"): |
| 56 | counts[line.split("::", 1)[0]] += 1 |
| 57 | m = re.match(r"(\d+)(?:/\d+)? tests? collected(?: \((\d+) deselected\))?", line) |
| 58 | if m: |
| 59 | collected = (int(m.group(1)), int(m.group(2) or 0)) |
| 60 | if collected is None or sum(counts.values()) != collected[0]: |
| 61 | sys.exit(f"could not account for the collected tests: ids={sum(counts.values())} summary={collected}") |
| 62 | weights = {} |
| 63 | for line in open(weights_path): |
| 64 | if line.strip() and not line.startswith("#"): |
| 65 | name, secs = line.split("\t"); weights[name] = float(secs) |
| 66 | cost = {f: weights.get(f, n * 0.06) for f, n in counts.items()} |
| 67 | buckets = [[0.0, []] for _ in range(shards)] |
| 68 | for f in sorted(counts, key=lambda f: -cost[f]): # longest first, onto the lightest shard |
| 69 | b = min(buckets, key=lambda b: b[0]); b[0] += cost[f]; b[1].append(f) |
| 70 | planned = 0 |
| 71 | for i, (_, files) in enumerate(buckets, 1): |
| 72 | if files: |
| 73 | open(f"{logs}/shard-{i}.files", "w").write("\n".join(sorted(files)) + "\n") |
| 74 | planned += sum(counts[f] for f in files) |
| 75 | assert planned == collected[0] |
| 76 | open(f"{logs}/expected", "w").write(f"{collected[0]} {collected[1]}\n") |
| 77 | PYEOF |
| 78 | |
| 79 | pids=() |
| 80 | for list in "$LOGS"/shard-*.files; do |
| 81 | n=${list##*shard-}; n=${n%.files} |
| 82 | # shellcheck disable=SC2046 |
| 83 | ( "$PY" -m pytest -q -p no:cacheprovider -o tmp_path_retention_policy=failed --basetemp="$BASE/$n" $(cat "$list") > "$LOGS/shard-$n.log" 2>&1 |
| 84 | echo $? > "$LOGS/shard-$n.rc" ) & |
| 85 | pids+=($!) |
| 86 | done |
| 87 | wait "${pids[@]}" |
| 88 | |
| 89 | "$PY" - "$LOGS" "$START" <<'PYEOF' |
| 90 | import glob, re, sys, time |
| 91 | logs, start = sys.argv[1], int(sys.argv[2]) |
| 92 | tot = dict(passed=0, failed=0, skipped=0, deselected=0, error=0, xfailed=0, xpassed=0) |
| 93 | bad = [] |
| 94 | for rc_path in sorted(glob.glob(f"{logs}/shard-*.rc")): |
| 95 | n = re.search(r"shard-(\d+)\.rc", rc_path).group(1) |
| 96 | rc = int(open(rc_path).read().strip() or 1) |
| 97 | text = open(f"{logs}/shard-{n}.log", errors="replace").read().splitlines() |
| 98 | last = next((l for l in reversed(text) if re.search(r" in [\d.]+s", l)), "") |
| 99 | for num, word in re.findall(r"(\d+) (passed|failed|skipped|deselected|errors?|xfailed|xpassed)", last): |
| 100 | tot["error" if word.startswith("error") else word] += int(num) |
| 101 | if rc != 0 or not last: |
| 102 | bad.append(f"shard {n} exit {rc} ({logs}/shard-{n}.log)") |
| 103 | exp_run, exp_desel = map(int, open(f"{logs}/expected").read().split()) |
| 104 | ran = tot["passed"] + tot["failed"] + tot["skipped"] + tot["error"] + tot["xfailed"] + tot["xpassed"] |
| 105 | if ran != exp_run: |
| 106 | bad.append(f"ran {ran} tests, a plain collect selects {exp_run}") |
| 107 | status = "FAIL" if bad or tot["failed"] or tot["error"] else "OK" |
| 108 | print(f"{status}: {tot['passed']} passed, {tot['failed']} failed, {tot['skipped']} skipped, " |
| 109 | f"{exp_desel} deselected (by the default -m filter), {tot['error']} errors in {int(time.time()) - start}s " |
| 110 | f"across {len(glob.glob(f'{logs}/shard-*.rc'))} shards (logs: {logs}/shard-*.log)") |
| 111 | for b in bad: |
| 112 | print(" " + b, file=sys.stderr) |
| 113 | sys.exit(0 if status == "OK" else 1) |
| 114 | PYEOF |
| 115 | RC=$? |
| 116 | if [ "$RC" -eq 0 ] && [ "${TEST_FAST_KEEP:-0}" != 1 ]; then rm -rf "$BASE"; else echo "basetemp kept: $BASE"; fi |
| 117 | exit "$RC" |