Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions acceptance/bin/check_init_time.py
Original file line number Diff line number Diff line change
@@ -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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just run it here and record output? For historical runs. Can also drop sort and head.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYM for historical runs?

We already capture the timing in the log output.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do it in a follow up if we need to.

print(" go mod why -m <module>")
sys.exit(1)
print(f"OK: total package init() time is within {args.max_ms:g} ms")


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions acceptance/inittime/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cloud = false
1 change: 1 addition & 0 deletions acceptance/inittime/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OK: total package init() time is within 10 ms
3 changes: 3 additions & 0 deletions acceptance/inittime/script
Original file line number Diff line number Diff line change
@@ -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
Loading