diff --git a/acceptance/bin/check_init_time.py b/acceptance/bin/check_init_time.py new file mode 100755 index 00000000000..6e2412f11b4 --- /dev/null +++ b/acceptance/bin/check_init_time.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +""" +Report each Go package's init() clock time (GODEBUG=inittrace=1) on stderr and, on Linux, +fail if the total init time exceeds --max-ms. + +The total is judged by its minimum over several runs, since load from parallel tests +inflates individual runs but slow inits are slow on every run. While the total is over the +threshold, more runs are added (up to --max-runs) to ride out bursts of load. +""" + +import argparse +import os +import re +import subprocess +import sys + +INIT_LINE = re.compile(r"^init (\S+) @\S+ ms, (\S+) ms clock") + + +def measure(command): + env = dict(os.environ, GODEBUG="inittrace=1") + cp = subprocess.run(command, env=env, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True) + if cp.returncode != 0: + sys.exit(f"command failed with exit code {cp.returncode}:\n{cp.stderr}") + clocks = {m.group(1): float(m.group(2)) for m in map(INIT_LINE.match, cp.stderr.splitlines()) if m} + if not clocks: + sys.exit(f"no inittrace output from {command}:\n{cp.stderr}") + return clocks + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--runs", type=int, default=5) + parser.add_argument("--max-runs", type=int, default=20) + parser.add_argument("--max-ms", type=float, required=True) + parser.add_argument("command", nargs="+") + args = parser.parse_args() + + runs = [measure(args.command) for _ in range(args.runs)] + while len(runs) < args.max_runs and min(sum(r.values()) for r in runs) > args.max_ms: + runs.append(measure(args.command)) + + total = min(sum(r.values()) for r in runs) + clocks = {pkg: min(r[pkg] for r in runs) for pkg in runs[0]} + print(f"Minimum init() clock time in ms over {len(runs)} runs:", file=sys.stderr) + print(f"{total:5.2f} total", file=sys.stderr) + for pkg, ms in sorted(clocks.items(), key=lambda kv: (-kv[1], kv[0])): + print(f"{ms:5.2f} {pkg}", file=sys.stderr) + + # Clocks elsewhere are too coarse or noisy to enforce (on Windows they can be quantized + # to the ~15.6 ms system tick). + if total > args.max_ms and sys.platform == "linux": + print( + f"FAIL: package init() takes {total:.2f} ms in total, over {args.max_ms:g} ms; this delays every CLI invocation." + ) + print("To investigate:") + print(" GODEBUG=inittrace=1 databricks --version 2>&1 | sort -k5 -n -r | head") + print(" go mod why -m ") + sys.exit(1) + print(f"OK: total package init() time is within {args.max_ms:g} ms") + + +if __name__ == "__main__": + main() diff --git a/acceptance/inittime/out.test.toml b/acceptance/inittime/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/inittime/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/inittime/output.txt b/acceptance/inittime/output.txt new file mode 100644 index 00000000000..eb32da7ebc2 --- /dev/null +++ b/acceptance/inittime/output.txt @@ -0,0 +1 @@ +OK: total package init() time is within 10 ms diff --git a/acceptance/inittime/script b/acceptance/inittime/script new file mode 100644 index 00000000000..585dbc6dd80 --- /dev/null +++ b/acceptance/inittime/script @@ -0,0 +1,3 @@ +# Package init() runs on every CLI invocation, so slow inits (often arriving through a +# transitive dependency upgrade) delay every command. The total is ~5 ms on linux/amd64. +check_init_time.py --max-ms 10 -- $CLI --version 2> LOG.inittime