-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconftest.py
More file actions
1177 lines (1002 loc) · 41.6 KB
/
Copy pathconftest.py
File metadata and controls
1177 lines (1002 loc) · 41.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import os
import re
import subprocess
import sys
import tempfile
import types
from datetime import datetime, timezone
from pathlib import Path
import pytest
import pytest_benchmark.utils as _bm_utils
from pytest_benchmark.fixture import BenchmarkFixture
from pytest_benchmark.utils import NameWrapper
# Patch pytest-benchmark to support a "throughput" column before argparse runs.
if "throughput" not in _bm_utils.ALLOWED_COLUMNS:
_bm_utils.ALLOWED_COLUMNS.append("throughput")
_TEST_FN_RE = re.compile(r"^def\s+(test_\w+)\s*\(", re.MULTILINE)
_BENCH_FN_RE = re.compile(r"^def\s+(bench_\w+)\s*\(", re.MULTILINE)
class MojoRunner:
"""Builds and executes Mojo test/benchmark commands."""
@staticmethod
def find_asan_lib():
"""Locate the upstream LLVM ASAN runtime shared library.
Searches in order:
1. $CONDA_PREFIX/lib (pixi/conda environment)
2. clang resource dirs reported by any clang on PATH
Returns the path as a string, or None if not found.
"""
is_macos = sys.platform == "darwin"
lib_names = (
["libclang_rt.asan_osx_dynamic.dylib"]
if is_macos
else ["libclang_rt.asan-x86_64.so", "libclang_rt.asan.so"]
)
candidates = []
conda_prefix = os.environ.get("CONDA_PREFIX")
if conda_prefix:
for name in lib_names:
candidates.append(Path(conda_prefix) / "lib" / name)
for clang in ["clang", "clang-18", "clang-17", "clang-16"]:
try:
result = subprocess.run(
[clang, "--print-runtime-dir"],
capture_output=True,
text=True,
timeout=5,
)
if result.returncode == 0:
for name in lib_names:
candidates.append(Path(result.stdout.strip()) / name)
except (FileNotFoundError, subprocess.TimeoutExpired):
pass
for path in candidates:
if path.exists():
return str(path)
return None
@staticmethod
def asan_flags(config):
"""Return ASAN-related compiler flags, or an empty list if not requested."""
if not config.getoption("--asan"):
return []
asan_lib = MojoRunner.find_asan_lib()
if asan_lib is None:
pytest.exit(
"ASAN requested but no compatible ASAN runtime found. "
"Install libcompiler-rt via conda-forge.",
returncode=1,
)
# --shared-libasan is a Clang-only flag; on Linux the system `cc` is
# GCC and rejects it. The explicit -Xlinker path is sufficient there.
flags = ["--sanitize", "address"]
if sys.platform == "darwin":
flags += ["--shared-libasan"]
# Ensure the conda env's lib dir appears first in the binary's
# rpath so dyld resolves libclang_rt.asan_osx_dynamic.dylib from
# the pixi env rather than the incompatible Xcode toolchain copy.
lib_dir = str(Path(asan_lib).parent)
flags += ["-Xlinker", "-rpath", "-Xlinker", lib_dir]
flags += ["-Xlinker", asan_lib]
return flags
@staticmethod
def build_cmd(config, fspath, test_names=None):
"""Return the command to run a Mojo source file with optional test filtering.
Always compiles to a binary with `mojo build` so that crashes produce
symbolicated stack traces. Binaries are content-hash-cached under
.test_runners/ so repeated runs skip recompilation.
When *test_names* is provided, appends `--only name1 name2 ...` so that
TestSuite skips unselected tests.
"""
benchmark = config.getoption("--benchmark")
opt = "-O3" if benchmark else "-O1"
assert_flag = [] if benchmark else ["-D", "ASSERT=all"]
asan = MojoRunner.asan_flags(config)
src = Path(fspath)
# Always build a binary so crashes produce symbolicated stack traces.
# ASAN requires a binary because `mojo run` cannot resolve sanitizer
# symbols at runtime; for non-ASAN runs a binary is also needed because
# `mojo run` does not honour -g1 for crash symbolication in practice.
runners_dir = Path(config.rootpath) / ".test_runners"
runners_dir.mkdir(exist_ok=True)
binary = runners_dir / src.stem
# -lm: mojo build on Linux doesn't auto-link libm (needed for
# log10f etc.); harmless on macOS where libm is part of libSystem.
lm = [] if sys.platform == "darwin" else ["-Xlinker", "-lm"]
build_cmd = (
["mojo", "build", opt, "-g1", "-I", "."] + assert_flag + asan + lm + [str(src), "-o", str(binary)]
)
result = subprocess.run(
build_cmd, cwd=config.rootpath, capture_output=True, text=True
)
if result.returncode != 0:
raise RuntimeError(f"mojo build failed for {src}:\n{result.stderr}")
cmd = [str(binary)]
if test_names:
cmd += ["--only"] + list(test_names)
return cmd
@staticmethod
def run_tests(config, fspath, test_names):
"""Run a Mojo test file with ``--json`` and return {name: (status, error)}."""
cmd = MojoRunner.build_cmd(config, fspath, test_names)
cmd.append("--json")
result = subprocess.run(
cmd,
cwd=config.rootpath,
capture_output=True,
text=True,
)
if result.returncode != 0:
# Try to parse JSON even on failure (tests may have run partially).
try:
entries = json.loads(result.stdout)
except (json.JSONDecodeError, ValueError):
# Couldn't parse — mark all requested tests as failed.
return {name: ("FAIL", result.stderr) for name in test_names}
parsed = {}
for entry in entries:
parsed[entry["name"]] = (entry["status"], entry.get("error", ""))
# Mark any missing test names as failed.
for name in test_names:
if name not in parsed:
parsed[name] = ("FAIL", result.stderr)
return parsed
try:
entries = json.loads(result.stdout)
except (json.JSONDecodeError, ValueError):
return {
name: ("FAIL", f"failed to parse JSON:\n{result.stdout}")
for name in test_names
}
return {
entry["name"]: (entry["status"], entry.get("error", ""))
for entry in entries
}
@staticmethod
def run_benches(config, fspath, bench_names=None):
"""Run a Mojo benchmark file with ``--json`` and return parsed entries.
When *bench_names* is provided, passes ``--only name1 name2 ...`` so that
BenchSuite skips unselected benchmarks (same pattern as tests).
"""
cmd = MojoRunner.build_cmd(config, fspath, test_names=bench_names)
cmd.append("--json")
result = subprocess.run(
cmd,
cwd=config.rootpath,
capture_output=True,
text=True,
)
if result.returncode != 0:
detail = (
"\n".join(
part for part in (result.stderr, result.stdout) if part.strip()
)
or f"exit code {result.returncode}"
)
return {"_error": detail}
if result.stderr:
sys.stderr.write(result.stderr)
try:
entries = json.loads(result.stdout)
except (json.JSONDecodeError, ValueError):
return {"_error": f"failed to parse JSON output:\n{result.stdout}"}
return {e["name"]: e for e in entries}
def _to_seconds(value, unit):
"""Convert a benchmark timing value to seconds."""
if unit == "ns":
return value / 1e9
if unit == "us":
return value / 1e6
if unit == "ms":
return value / 1e3
return value
# Strips the leading "[n=NNN]" or "[n=NNN-" prefix from a parametrized pytest
# test ID. The n fixture always comes first: "[n=10000]" or "[n=10000-case]".
_N_PREFIX_RE = re.compile(r"\[n=\d+(-|\])")
class CompetitionReport:
"""Side-by-side benchmark comparison table for all measured libraries."""
@staticmethod
def _parse(bench):
"""Return ``(lib, operation, n)`` using ``extra_info``."""
ei = bench.get("extra_info", {})
lib = ei.get("lib")
n_val = ei.get("n")
if not (lib and n_val is not None):
return None, None, None
name = bench["name"]
prefix = f"test_{lib}_"
op = name[len(prefix) :] if name.startswith(prefix) else name
# "[n=10000]" → "" (fixture-only, no mark suffix)
# "[n=10000-inner]" → "[inner]" (fixture + mark suffix)
op = _N_PREFIX_RE.sub(lambda m: "[" if m.group(1) == "-" else "", op)
return lib, op, n_val
@staticmethod
def _fmt(seconds):
ns = seconds * 1e9
if ns < 1_000:
return f"{ns:.1f} ns"
if ns < 1_000_000:
return f"{ns / 1_000:.2f} µs"
if ns < 1_000_000_000:
return f"{ns / 1_000_000:.2f} ms"
return f"{ns / 1_000_000_000:.2f} s"
@classmethod
def display(cls, tr, benchmarks):
from rich.console import Console
from rich.table import Table
from rich import box
# Keys from extra_info that are not shown as columns (internal bookkeeping).
_hidden = frozenset({"lib", "n", _THROUGHPUT_KEY})
# Collect (op, n) → {lib: mean_seconds} and metadata per (op, n).
data: dict[tuple, dict[str, float]] = {}
meta: dict[tuple, dict] = {}
for b in benchmarks:
lib, op, n = cls._parse(b)
if lib is None:
continue
data.setdefault((op, n), {})[lib] = b["mean"]
ei = b.get("extra_info", {})
row_meta = {k: v for k, v in ei.items() if k not in _hidden}
if row_meta:
meta.setdefault((op, n), {}).update(row_meta)
# Discover all libs and metadata keys in stable insertion order.
libs: list[str] = []
meta_keys: list[str] = []
for lib_data in data.values():
for lib in lib_data:
if lib not in libs:
libs.append(lib)
for row_meta in meta.values():
for k in row_meta:
if k not in meta_keys:
meta_keys.append(k)
if not libs:
tr.write_line("No benchmarks with lib metadata found.")
return
# Build rows: only include (op, n) pairs that have at least two libs.
rows = []
for (op, n), lib_data in sorted(data.items()):
if len(lib_data) < 2:
continue
best_t = min(lib_data.values())
best_lib = min(lib_data, key=lib_data.get)
rows.append((op, n, lib_data, best_lib, best_t))
if not rows:
tr.write_line("No operations with multiple libs measured.")
return
# Win counters per lib.
wins = {lib: 0 for lib in libs}
ties = 0
for op, n, lib_data, best_lib, best_t in rows:
present = [l for l in libs if l in lib_data]
if len(present) < 2:
continue
times = [lib_data[l] for l in present]
fastest_t = min(times)
ratio = max(times) / fastest_t if fastest_t > 0 else 1.0
if ratio < 1.05:
ties += 1
else:
wins[best_lib] += 1
table = Table(title="Competition", box=box.SIMPLE_HEAD, show_footer=True)
table.add_column("Operation", no_wrap=True)
table.add_column("n", justify="right")
table.add_column("", no_wrap=True) # before first lib
for i, lib in enumerate(libs):
if i > 0:
table.add_column("", no_wrap=True)
footer = f"[bold green]{wins[lib]} wins[/]" if wins[lib] else ""
table.add_column(
lib.capitalize(), justify="right", footer=footer, no_wrap=True
)
table.add_column("", no_wrap=True) # after last lib
table.add_column("Fastest", justify="right", footer=f"[dim]{ties} ties[/]")
for k in meta_keys:
table.add_column(k.capitalize(), justify="right", no_wrap=True)
current_group = None
for op, n, lib_data, best_lib, best_t in rows:
grp = op.split("[")[0]
if grp != current_group:
if current_group is not None:
table.add_section()
current_group = grp
present = {lib: t for lib in libs if (t := lib_data.get(lib)) is not None}
fastest_t = min(present.values()) if present else None
if len(present) >= 2 and fastest_t and fastest_t > 0:
worst_t = max(present.values())
ratio = worst_t / fastest_t
is_tie = ratio < 1.05
else:
ratio = 1.0
is_tie = True
if is_tie:
fastest_markup = "[dim]~tie[/dim]"
else:
fastest_markup = f"[bold green]{best_lib} {ratio:.1f}x[/bold green]"
sep = "[dim]│[/]"
row = [op.replace("[", "\\["), f"{n:,}", sep]
for i, lib in enumerate(libs):
if i > 0:
row.append(sep)
t = present.get(lib)
if t is None:
row.append("—")
elif is_tie:
row.append(cls._fmt(t))
elif t == fastest_t:
row.append(f"[bold green]{cls._fmt(t)} 1.0x[/bold green]")
else:
rel = t / fastest_t
row.append(f"{cls._fmt(t)} {rel:.1f}x")
row.append(sep)
row.append(fastest_markup)
row_meta = meta.get((op, n), {})
for k in meta_keys:
v = row_meta.get(k)
row.append(str(v) if v is not None else "")
table.add_row(*row)
console = Console(highlight=False, width=220)
tr.ensure_newline()
tr.write_line("")
with console.capture() as cap:
console.print(table)
for line in cap.get().splitlines():
tr.write_line(line)
class MojoTestFailure(Exception):
pass
def pytest_addoption(parser):
parser.addoption(
"--mojo", action="store_true", default=False, help="Select Mojo tests"
)
parser.addoption(
"--no-mojo", action="store_true", default=False, help="Exclude Mojo tests"
)
parser.addoption(
"--python", action="store_true", default=False, help="Select Python tests"
)
parser.addoption(
"--no-python", action="store_true", default=False, help="Exclude Python tests"
)
parser.addoption(
"--cpu",
action="store_true",
default=False,
help="Select CPU tests (non-GPU Mojo + Python)",
)
parser.addoption(
"--gpu", action="store_true", default=False, help="Select GPU tests"
)
parser.addoption(
"--no-gpu", action="store_true", default=False, help="Exclude GPU tests"
)
parser.addoption(
"--benchmark",
action="store_true",
default=False,
help="Include benchmarks (Python pytest-benchmark and Mojo bench_*.mojo); skipped by default",
)
parser.addoption(
"--asan",
action="store_true",
default=False,
help="Run Mojo tests under AddressSanitizer (ASAN)",
)
parser.addoption(
"--competition",
action="store_true",
default=False,
help="After benchmarks, print a side-by-side comparison table for all measured libs.",
)
parser.addoption(
"--save-benchmarks",
metavar="DIR",
default=None,
help="Save benchmark results as a JSON envelope to DIR/<commit>.json (implies --benchmark).",
)
parser.addoption(
"--benchmark-history",
metavar="FILE",
default=None,
help="Path to the rolling benchmark history JSON file (default: benchmarks/data.json).",
)
def _python_excluded(config) -> bool:
"""Return True if Python tests/files should be excluded from this session."""
if config.getoption("--no-python"):
return True
sel_mojo = config.getoption("--mojo")
sel_gpu = config.getoption("--gpu")
sel_python = config.getoption("--python")
sel_cpu = config.getoption("--cpu")
if (sel_mojo or sel_gpu) and not (sel_python or sel_cpu):
return True
# Specific paths given → check whether any lead to Python test files.
if config.args:
for arg in config.args:
path_str = str(arg).split("::")[0]
p = Path(path_str)
if not p.is_absolute():
p = config.rootpath / p
if p.is_file():
if p.suffix == ".py":
return False
elif p.is_dir():
if any(p.rglob("test_*.py")) or any(p.rglob("bench_*.py")):
return False
return True
return False
def pytest_ignore_collect(collection_path, config):
"""Skip collecting Python test/bench files when Python tests are not needed."""
if collection_path.suffix == ".py" and collection_path.name.startswith(
("test_", "bench_")
):
if _python_excluded(config):
return True
# Python bench files are only collected when --benchmark is active,
# mirroring the behaviour of Mojo bench_*.mojo files.
if collection_path.name.startswith("bench_") and not config.getoption(
"--benchmark"
):
return True
def pytest_sessionstart(session):
"""Rebuild python/marrow.so before the session when Python tests will run."""
config = session.config
# xdist workers inherit the already-built library from the controller.
if hasattr(config, "workerinput"):
return
# Skip build when Python tests are excluded.
if _python_excluded(config):
return
print("building python/marrow.so ...", flush=True)
benchmark = config.getoption("--benchmark")
opt = "-O3" if benchmark else "-O1"
cmd = (
["mojo", "build", opt, "-I", "."]
+ MojoRunner.asan_flags(config)
+ ["python/lib.mojo", "--emit", "shared-lib", "-o", "python/marrow.so"]
)
result = subprocess.run(cmd, cwd=config.rootpath)
if result.returncode != 0:
pytest.exit("Failed to build python/marrow.so", returncode=1)
print("python/marrow.so built successfully", flush=True)
def pytest_collection_modifyitems(config, items):
sel_cpu = config.getoption("--cpu")
sel_mojo = config.getoption("--mojo")
sel_python = config.getoption("--python")
sel_gpu = config.getoption("--gpu")
no_mojo = config.getoption("--no-mojo")
no_python = config.getoption("--no-python")
no_gpu = config.getoption("--no-gpu")
run_benchmark = config.getoption("--benchmark")
# --cpu implies both --mojo and --python (all non-GPU tests)
if sel_cpu:
sel_mojo = True
sel_python = True
selective = sel_mojo or sel_python or sel_gpu
for item in items:
is_gpu = "gpu" in item.keywords
is_mojo = "mojo" in item.keywords and not is_gpu
is_python = "python" in item.keywords
is_benchmark = "benchmark" in item.keywords
if is_benchmark and not run_benchmark:
item.add_marker(
pytest.mark.skip(
reason="benchmarks excluded; pass --benchmark to include"
)
)
elif is_gpu and (no_gpu or not sel_gpu):
item.add_marker(
pytest.mark.skip(reason="GPU tests excluded; pass --gpu to include")
)
elif (no_mojo and is_mojo) or (selective and is_mojo and not sel_mojo):
item.add_marker(
pytest.mark.skip(reason="Mojo tests excluded; pass --mojo to include")
)
elif (no_python and is_python) or (selective and is_python and not sel_python):
item.add_marker(
pytest.mark.skip(
reason="Python tests excluded; pass --python to include"
)
)
def pytest_collect_file(parent, file_path):
if file_path.suffix == ".mojo" and file_path.name.startswith("test_"):
return MojoTestFile.from_parent(parent, path=file_path)
if file_path.suffix == ".mojo" and file_path.name.startswith("bench_"):
return MojoBenchFile.from_parent(parent, path=file_path)
def pytest_itemcollected(item):
if item.fspath.ext == ".py":
item.add_marker(pytest.mark.python)
if item.fspath.basename.startswith("bench_"):
item.add_marker(pytest.mark.benchmark)
def pytest_collection_finish(session):
"""Pre-compute per-file groups for tests and benchmarks."""
# Test groups (existing).
file_groups = {}
for item in session.items:
if isinstance(item, MojoTestItem) and not any(
m.name == "skip" for m in item.iter_markers()
):
key = str(item.fspath)
if key not in file_groups:
file_groups[key] = []
file_groups[key].append(item.name)
session.config._mojo_file_groups = file_groups
session.config._mojo_results = {}
# Benchmark groups — collect non-skipped bench names per file.
bench_groups = {}
for item in session.items:
if isinstance(item, MojoBenchItem) and not any(
m.name == "skip" for m in item.iter_markers()
):
key = str(item.fspath)
if key not in bench_groups:
bench_groups[key] = []
bench_groups[key].append(item.name)
session.config._mojo_bench_groups = bench_groups
session.config._mojo_bench_results = {}
def pytest_configure(config):
config.addinivalue_line("markers", "mojo: Mojo language tests")
config.addinivalue_line("markers", "python: Python tests")
config.addinivalue_line("markers", "gpu: requires GPU hardware")
config.addinivalue_line(
"markers",
"benchmark: performance benchmarks (skipped by default, run with --benchmark)",
)
# --save-benchmarks implies --benchmark.
if config.getoption("--save-benchmarks", default=None):
config.option.benchmark = True
class MojoTestFile(pytest.File):
def collect(self):
is_gpu = self.path.stem.endswith("_gpu")
source = self.path.read_text()
test_names = _TEST_FN_RE.findall(source)
for name in test_names:
yield MojoTestItem.from_parent(self, name=name, is_gpu=is_gpu)
class MojoTestItem(pytest.Item):
def __init__(self, name, parent, is_gpu=False):
super().__init__(name, parent)
self.is_gpu = is_gpu
self.add_marker(pytest.mark.mojo)
if is_gpu:
self.add_marker(pytest.mark.gpu)
def runtest(self):
results = self.config._mojo_results
fspath = str(self.fspath)
if fspath not in results:
names = self.config._mojo_file_groups.get(fspath, [self.name])
results[fspath] = MojoRunner.run_tests(self.config, fspath, names)
file_results = results[fspath]
if self.name not in file_results:
raise MojoTestFailure(f"{self.name} did not appear in test runner output")
status, error = file_results[self.name]
if status == "FAIL":
raise MojoTestFailure(error)
def repr_failure(self, excinfo):
return str(excinfo.value)
def reportinfo(self):
return self.fspath, 0, f"mojo::{self.name}"
class MojoBenchFile(pytest.File):
"""Collect individual benchmark items from a bench_*.mojo file.
Files using BenchSuite yield one item per ``def bench_*(mut b: Bencher)``
function discovered in the source (mirroring MojoTestFile). Files without
discoverable bench functions fall back to a single item per file.
The Mojo file is compiled and executed once per file; results are cached
and individual timings injected into pytest-benchmark.
"""
def collect(self):
source = self.path.read_text()
bench_names = _BENCH_FN_RE.findall(source)
if bench_names:
for name in bench_names:
yield MojoBenchItem.from_parent(self, name=name)
else:
# Fallback for old-style files without discoverable bench_* fns.
yield MojoBenchItem.from_parent(self, name=self.path.stem)
class MojoBenchItem(pytest.Item):
def __init__(self, name, parent):
super().__init__(name, parent)
self.add_marker(pytest.mark.mojo)
self.add_marker(pytest.mark.benchmark)
def runtest(self):
# Run the bench file once per file, cache results (same as MojoTestItem).
results = self.config._mojo_bench_results
fspath = str(self.fspath)
if fspath not in results:
bench_names = self.config._mojo_bench_groups.get(fspath, None)
results[fspath] = MojoRunner.run_benches(self.config, fspath, bench_names)
entries = results[fspath]
if "_error" in entries:
raise MojoTestFailure(entries["_error"])
# Look up this benchmark's entry.
entry = entries.get(self.name)
if entry is not None:
self._inject_one(self.name, entry)
return
# Not found by exact name. For old-style files (dump_report output),
# the first item for the file injects all entries; subsequent items
# from the same file become no-ops.
injected_key = fspath + ":_injected"
if injected_key not in results and entries:
results[injected_key] = True
self._inject_all(entries)
def _inject_one(self, bench_name, entry):
"""Inject pre-measured timings into pytest-benchmark.
When the entry contains a ``runs`` list (from BenchSuite), each run
is injected as a separate round so pytest-benchmark computes proper
min/max/stddev statistics. Otherwise falls back to a single mean.
"""
bs = self.config._benchmarksession
if bs.disabled:
return
runs = entry.get("runs")
unit = entry.get("unit", "ns")
if runs:
# Multiple per-iteration measurements — inject each as a round.
durations_s = [_to_seconds(v, unit) for v in runs]
else:
# Legacy single-value format.
durations_s = [_to_seconds(entry["value"], unit)]
# Build a fake timer that yields (0, d1, 0, d2, ...) for each round.
# pedantic() calls timer() twice per round: start then end.
timer_seq = []
for d in durations_s:
timer_seq.append(0.0)
timer_seq.append(d)
timer_it = iter(timer_seq)
fake_timer = NameWrapper(lambda: next(timer_it))
node = types.SimpleNamespace(name=bench_name, _nodeid=self._nodeid)
noop = lambda *_: None
fixture = BenchmarkFixture(
node=node,
add_stats=bs.benchmarks.append,
logger=noop,
warner=noop,
disabled=bs.disabled,
timer=fake_timer,
disable_gc=False,
min_rounds=1,
min_time=0,
max_time=0,
calibration_precision=10,
warmup=False,
warmup_iterations=0,
cprofile=False,
cprofile_loops=None,
cprofile_dump=None,
)
fixture.pedantic(
lambda: None,
rounds=len(durations_s),
iterations=1,
warmup_rounds=0,
)
# Compute and attach throughput if the entry has metric data.
tp_count = entry.get("throughput_count")
if tp_count and fixture.stats:
mean_s = fixture.stats.stats.mean
if mean_s > 0:
metric_name = entry.get("throughput_metric", "throughput")
metric_unit = entry.get("throughput_unit", "GElems/s")
rate = tp_count * 1e-9 / mean_s
fixture.extra_info[f"{metric_name} ({metric_unit})"] = round(rate, 4)
def _inject_all(self, entries):
"""Inject all entries (fallback for old-style files)."""
for bench_name, entry in entries.items():
self._inject_one(bench_name, entry)
def repr_failure(self, excinfo):
return str(excinfo.value)
def reportinfo(self):
return self.fspath, 0, f"mojo::bench::{self.name}"
_THROUGHPUT_KEY = "throughput (GElems/s)"
def pytest_benchmark_group_stats(
config, benchmarks, group_by
): # config: required by pytest hook signature
"""Group benchmarks by the native benchmark group marker for display.
Within each group, benchmarks are sorted by ``(n, name, mean)`` so rows
are ordered by size then by operation name. Throughput is computed and
injected into ``extra_info`` for each benchmark that has an ``n`` value.
Only activates for the default ``group_by="group"``; custom
``--benchmark-group-by`` values are passed through unchanged.
"""
if group_by != "group":
return None # honour explicit --benchmark-group-by choices
groups: dict[str, list] = {}
for bench in benchmarks:
key = bench.get("group") or bench["name"].split("[")[0]
groups.setdefault(key, []).append(bench)
for group_benchmarks in groups.values():
group_benchmarks.sort(
key=lambda b: (
b.get("extra_info", {}).get("n", 0),
b["name"],
b["mean"],
)
)
for bench in group_benchmarks:
ei = bench.get("extra_info", {})
n_val = ei.get("n")
mean_s = bench.get("mean", 0)
if n_val and mean_s > 0 and _THROUGHPUT_KEY not in ei:
ei[_THROUGHPUT_KEY] = round(n_val / mean_s / 1e9, 4)
return sorted(groups.items(), key=lambda pair: pair[0] or "")
@pytest.hookimpl(trylast=True)
def pytest_terminal_summary(
terminalreporter, exitstatus, config
): # exitstatus: required by pytest hook signature
if not config.getoption("--competition", default=False):
return
bs = getattr(config, "_benchmarksession", None)
if bs is None or not bs.benchmarks:
return
CompetitionReport.display(terminalreporter, bs.benchmarks)
# ---------------------------------------------------------------------------
# --save-benchmarks: write result envelope + update rolling history
# ---------------------------------------------------------------------------
class BenchmarkHistory:
"""Rolling history of benchmark runs, persisted as JSON.
Each run is an envelope: ``{commit, timestamp, ref, results: [...]}``.
The history merges envelopes into ``benchmarks/data.json`` and keeps
individual per-commit snapshots under a results directory.
"""
MAX_RUNS = 200
def __init__(self, root, results_dir, history_file=None):
self._root = Path(root)
self._results_dir = Path(results_dir)
self._history_file = Path(history_file) if history_file else self._root / "benchmarks" / "data.json"
# -- git metadata -------------------------------------------------------
@staticmethod
def _git(root, *args):
try:
return (
subprocess.run(
["git", "-C", str(root)] + list(args),
capture_output=True,
text=True,
).stdout.strip()
or "unknown"
)
except Exception:
return "unknown"
# -- envelope -----------------------------------------------------------
def _make_envelope(self, benchmarks):
"""Convert pytest-benchmark Metadata objects into a result envelope."""
commit = self._git(self._root, "rev-parse", "HEAD")
ref = self._git(self._root, "rev-parse", "--abbrev-ref", "HEAD")
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
results = []
for b in benchmarks:
# bs.benchmarks contains Metadata objects (not flat dicts).
stats = b.stats
mean_s = stats.mean if stats else 0.0
throughput = b.extra_info.get(_THROUGHPUT_KEY)
# Extract source file from fullname (pytest node ID)
# e.g. "python/tests/bench_compute.py::test_marrow_add[...]" → "bench_compute.py"
fullname = getattr(b, "fullname", "") or ""
file = fullname.split("::")[0].rsplit("/", 1)[-1] if "::" in fullname else None
# Collect extra_info fields (excluding internal keys)
extra = {}
for k, v in b.extra_info.items():
if k != _THROUGHPUT_KEY:
extra[k] = v
result = {
"name": b.name,
"file": file,
"mean_ns": mean_s * 1e9,
"throughput_gelems_s": throughput,
}
if stats:
result["min_ns"] = stats.min * 1e9
result["max_ns"] = stats.max * 1e9
result["median_ns"] = stats.median * 1e9
result["stddev_ns"] = stats.stddev * 1e9
result["rounds"] = stats.rounds
if extra:
result["extra_info"] = extra
results.append(result)
return {
"commit": commit,
"timestamp": timestamp,
"ref": ref,
"results": results,
}
# -- persistence --------------------------------------------------------
def _write_envelope(self, envelope):
"""Write per-commit result file and latest.json."""
self._results_dir.mkdir(parents=True, exist_ok=True)
out_file = self._results_dir / f"{envelope['commit']}.json"
for path in [out_file, self._results_dir / "latest.json"]:
with path.open("w") as f:
json.dump(envelope, f, indent=2)
f.write("\n")
return out_file
def _update_history(self, envelope):
"""Merge envelope into the rolling history JSON file."""
data_file = self._history_file
if data_file.exists():
with data_file.open() as f:
history = json.load(f)
else:
history = {"runs": [], "operations": []}
existing_commits = {r["commit"] for r in history["runs"]}
if envelope["commit"] not in existing_commits:
run_results = {}
for r in envelope["results"]:
entry = {
"mean_ns": r["mean_ns"],
"throughput_gelems_s": r["throughput_gelems_s"],
}
for key in ("file", "min_ns", "max_ns", "median_ns", "stddev_ns", "rounds", "extra_info"):
if key in r:
entry[key] = r[key]
run_results[r["name"]] = entry
history["runs"].append(
{
"commit": envelope["commit"],
"short_commit": envelope["commit"][:7],
"timestamp": envelope["timestamp"],
"ref": envelope["ref"],
"results": run_results,
}
)
history["runs"].sort(key=lambda r: r.get("timestamp", ""), reverse=True)
history["runs"] = history["runs"][: self.MAX_RUNS]
seen = {}
for run in history["runs"]:
for name in run.get("results", {}):
seen[name] = None
history["operations"] = list(seen)
data_file.parent.mkdir(parents=True, exist_ok=True)
with data_file.open("w") as f:
json.dump(history, f, indent=2)
f.write("\n")
return len(history["runs"])
# -- public API ---------------------------------------------------------
def save(self, benchmarks):
"""Build envelope from pytest-benchmark results, write files, update history."""
envelope = self._make_envelope(benchmarks)
out_file = self._write_envelope(envelope)
total = self._update_history(envelope)