Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 18 minutes ago History
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
20set -u
21cd "$(dirname "$0")/.."
22export TZ=UTC
23PY="${PYTHON:-python3}"
24LOGS="${TEST_FAST_LOGS:-build/test-fast}"
25
26default_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}
32SHARDS="${1:-$(default_shards)}"
33case "$SHARDS" in ''|*[!0-9]*|0) echo "usage: $0 [SHARDS] (SHARDS is a positive integer)" >&2; exit 2;; esac
34
35rm -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.
39BASE="$(mktemp -d "${TMPDIR:-/tmp}/test-fast.XXXXXX")"
40START=$(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
44CRC=$?
45if [ "$CRC" -ne 0 ]; then
46 echo "collection failed (exit $CRC); see $LOGS/collect.txt" >&2; tail -20 "$LOGS/collect.txt" >&2; exit 1
47fi
48
49"$PY" - "$SHARDS" "$LOGS" scripts/test_fast_weights.tsv <<'PYEOF' || exit 1
50import collections, re, sys
51shards, logs, weights_path = int(sys.argv[1]), sys.argv[2], sys.argv[3]
52counts = collections.Counter()
53collected = None
54for 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))
60if 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}")
62weights = {}
63for line in open(weights_path):
64 if line.strip() and not line.startswith("#"):
65 name, secs = line.split("\t"); weights[name] = float(secs)
66cost = {f: weights.get(f, n * 0.06) for f, n in counts.items()}
67buckets = [[0.0, []] for _ in range(shards)]
68for 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)
70planned = 0
71for 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)
75assert planned == collected[0]
76open(f"{logs}/expected", "w").write(f"{collected[0]} {collected[1]}\n")
77PYEOF
78
79pids=()
80for 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+=($!)
86done
87wait "${pids[@]}"
88
89"$PY" - "$LOGS" "$START" <<'PYEOF'
90import glob, re, sys, time
91logs, start = sys.argv[1], int(sys.argv[2])
92tot = dict(passed=0, failed=0, skipped=0, deselected=0, error=0, xfailed=0, xpassed=0)
93bad = []
94for 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)")
103exp_run, exp_desel = map(int, open(f"{logs}/expected").read().split())
104ran = tot["passed"] + tot["failed"] + tot["skipped"] + tot["error"] + tot["xfailed"] + tot["xpassed"]
105if ran != exp_run:
106 bad.append(f"ran {ran} tests, a plain collect selects {exp_run}")
107status = "FAIL" if bad or tot["failed"] or tot["error"] else "OK"
108print(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)")
111for b in bad:
112 print(" " + b, file=sys.stderr)
113sys.exit(0 if status == "OK" else 1)
114PYEOF
115RC=$?
116if [ "$RC" -eq 0 ] && [ "${TEST_FAST_KEEP:-0}" != 1 ]; then rm -rf "$BASE"; else echo "basetemp kept: $BASE"; fi
117exit "$RC"