#!/bin/sh # Rewrites .amadan/test-weights.tsv, the measured weights .amadan/test-gate.sh # deals the split packages' tests by. # # go test -count=1 -json ./internal/console/ >console.json # go test -count=1 -json . >root.json # .amadan/test-weights.sh console.json root.json # # Measure on a quiet machine with the temp root on tmpfs, as CI has it, or # the fsync-heavy tests weigh several times what they do there. # # Only the packages in the given files are rewritten; rows for other # packages are kept. A top-level test of a quarter-second or more gets a # row. A test that called t.Parallel (its JSON has a "pause") overlaps # with its neighbours, so its weight is its time divided by 8, roughly # what it adds to its shard's wall clock. Everything unlisted is dealt as # short (test-gate.sh counts it as 0.05 s), which is what it is: host's # 2,500 small serial tests average about 0.025 s. set -eu [ $# -gt 0 ] || { echo "usage: .amadan/test-weights.sh go-test-json-file..." >&2 exit 2 } out=.amadan/test-weights.tsv tab=$(printf '\t') new=$(mktemp "${TMPDIR:-/tmp}/test-weights-XXXXXX") trap 'rm -f "$new" "$out.tmp"' EXIT cat "$@" | awk ' /"Test":"/ { if (!match($0, /"Test":"[^"]*"/)) next test = substr($0, RSTART + 8, RLENGTH - 9) if (index(test, "/")) next if (!match($0, /"Package":"[^"]*"/)) next pkg = substr($0, RSTART + 11, RLENGTH - 12) key = pkg "\t" test if ($0 ~ /"Action":"pause"/) { parallel[key] = 1; next } if ($0 !~ /"Action":"(pass|fail)"/) next if (!match($0, /"Elapsed":[0-9.]+/)) next secs[key] = substr($0, RSTART + 10, RLENGTH - 10) + 0 } END { for (k in secs) { w = (k in parallel) ? secs[k] / 8 : secs[k] if (w >= 0.25) printf "%s\t%.2f\n", k, w } # A package line with no weighted test still marks its rows as replaced. for (k in secs) { split(k, f, "\t"); seen[f[1]] = 1 } for (p in seen) printf "#pkg\t%s\n", p } ' >"$new" { printf '# package\ttest\tseconds (.amadan/test-weights.sh, %s)\n' "$(date -u +%Y-%m-%d)" { grep -v '^#pkg' "$new" if [ -f "$out" ]; then awk -F "$tab" -v newf="$new" ' BEGIN { while ((getline l < newf) > 0) { split(l, f, "\t"); if (f[1] == "#pkg") replaced[f[2]] = 1 } } /^#/ { next } !($1 in replaced) ' "$out" fi } | sort -t "$tab" -k1,1 -k3,3gr } >"$out.tmp" mv "$out.tmp" "$out" echo "wrote $out: $(($(wc -l <"$out") - 1)) weighted tests"