forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBc327308Tahinli25 days ago1014 commits

heavy_tests.py97 lines, 3.3 KiBHistoryRawDownload
1#!/usr/bin/env python3
2"""Print the nextest filterset that keeps the heavy tests out of the `fast` profile.
3
4Usage:
5 python3 scripts/heavy_tests.py <nextest.log> [threshold_seconds=10]
6
7Reads PASS/FAIL result lines from a full nextest run log, dedupes them on
8(binary id, test name), and prints one TOML literal string for
9`default-filter` in the `[profile.fast]` section of .config/nextest.toml.
10
11Rerun it after a full run when slow tests come or go.
12"""
13import re
14import sys
15from collections import defaultdict
16
17RESULT = re.compile(
18 r"^\s*(?:PASS|FAIL)\s+\[\s*([\d.]+)s\]\s+\(\s*\d+/\d+\)\s+(\S+)\s+(\S.*?)\s*$"
19)
20ANSI = re.compile(r"\x1b\[[0-9;]*m")
21
22# Integration binaries of `engine` other than the merged `it` binary are heavy.
23ENGINE_PKG = "engine"
24# Binaries whose tests are grouped by the first `::` segment of the name.
25MODULE_BINARIES = ("engine::it", "edith_mcp::it")
26MODULE_SHARE = 0.6
27
28
29def load(path):
30 tests = {}
31 with open(path, encoding="utf-8", errors="replace") as f:
32 for line in f:
33 m = RESULT.match(ANSI.sub("", line))
34 if m:
35 secs, binary, name = m.groups()
36 tests.setdefault((binary, name), float(secs))
37 return tests
38
39
40def in_engine_rule(binary):
41 """Term (a): integration test binaries of engine, except `it`."""
42 pkg, sep, rest = binary.partition("::")
43 return pkg == ENGINE_PKG and bool(sep) and rest != "it" and not rest.startswith("bin/")
44
45
46def build_terms(tests, threshold):
47 # Group module-binary tests by module (only names that have a `::`).
48 groups = defaultdict(lambda: defaultdict(list))
49 for (binary, name), secs in tests.items():
50 if binary in MODULE_BINARIES and "::" in name:
51 groups[binary][name.split("::", 1)[0]].append((name, secs))
52
53 heavy_modules = set() # (binary, module)
54 for binary, mods in groups.items():
55 for module, items in mods.items():
56 heavy = sum(1 for _, s in items if s >= threshold)
57 if heavy / len(items) >= MODULE_SHARE:
58 heavy_modules.add((binary, module))
59
60 a_terms = [f"(package({ENGINE_PKG}) and kind(test) and not binary(it))"]
61 b_terms = []
62 for binary, module in sorted(heavy_modules):
63 b_terms.append(f"(binary_id({binary}) and test(/^{re.escape(module)}::/))")
64
65 c_terms = []
66 for (binary, name), secs in sorted(tests.items()):
67 if secs < threshold:
68 continue
69 if in_engine_rule(binary):
70 continue
71 if (binary, name.split("::", 1)[0]) in heavy_modules and "::" in name:
72 continue
73 c_terms.append(f"(binary_id({binary}) and test(={name}))")
74
75 return a_terms, b_terms, c_terms
76
77
78def toml_literal(expr):
79 if "'" in expr:
80 return "'''" + expr + "'''"
81 return "'" + expr + "'"
82
83
84def main():
85 if len(sys.argv) not in (2, 3):
86 sys.exit("usage: heavy_tests.py <nextest.log> [threshold_seconds=10]")
87 threshold = float(sys.argv[2]) if len(sys.argv) == 3 else 10.0
88 tests = load(sys.argv[1])
89 a_terms, b_terms, c_terms = build_terms(tests, threshold)
90 expr = "not (" + " or ".join(a_terms + b_terms + c_terms) + ")"
91 print(f"{len(tests)} distinct tests, {len(a_terms)} (a), {len(b_terms)} (b), "
92 f"{len(c_terms)} (c)", file=sys.stderr)
93 print(toml_literal(expr))
94
95
96if __name__ == "__main__":
97 main()