Skip to content

Commit dca6fae

Browse files
committed
microbench: refactor printing results
1 parent 0e24b86 commit dca6fae

File tree

6 files changed

+123
-19
lines changed

6 files changed

+123
-19
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,5 @@ docs/_build/
9494
microbench/pixi.lock
9595
microbench/.venv_*
9696
microbench/tmp_*
97+
microbench/profile_alloc/log
98+
microbench/profile_alloc/*.prof*

microbench/Makefile

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ uninstall:
1212
$(PYTHON) -m pip uninstall hpy.microbench --yes
1313

1414
test:
15-
$(PYTHON) -m pytest -v --purepy | tee tmp_results_$(shell $(PYTHON) -c "import sys; print(sys.implementation.name)").txt
15+
$(PYTHON) -m pytest -v --purepy | tee $(shell $(PYTHON) -c 'import util as u; u.print_file_name_results()')
1616

1717
bench: test
1818

1919
bench_hpy:
20-
$(PYTHON) -m pytest -v -m hpy | tee tmp_results_$(shell $(PYTHON) -c "import sys; print(sys.implementation.name)").txt
20+
$(PYTHON) -m pytest -v -m hpy | tee $(shell $(PYTHON) get_file_name_results.py)
2121

2222
clean:
2323
rm -f src/*.so src/hpy_simple.py
@@ -39,19 +39,16 @@ create_venv_graalpy:
3939
$(shell uv python find graalpy) -m venv .venv_graalpy
4040

4141
print_cpy:
42-
@echo =================================== CPython ====================================
43-
@tail tmp_results_cpython.txt -n 32
42+
@$(PYTHON) print_tmp_results.py cpy
4443

4544
print_pypy:
46-
@echo ==================================== PyPy ======================================
47-
@tail tmp_results_pypy.txt -n 32
45+
@$(PYTHON) print_tmp_results.py pypy
4846

4947
print_graalpy:
50-
@echo =================================== GraalPy ====================================
51-
@tail tmp_results_graalpy.txt -n 32
48+
@$(PYTHON) print_tmp_results.py graalpy
5249

5350
print_pypy_vs_cpy:
54-
@$(PYTHON) print_other_vs_cpy.py PyPy
51+
@$(PYTHON) print_other_vs_cpy.py pypy
5552

5653
print_graalpy_vs_cpy:
57-
@$(PYTHON) print_other_vs_cpy.py GraalPy
54+
@$(PYTHON) print_other_vs_cpy.py graalpy

microbench/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import re
22
import time
3+
import sys
34
from collections import defaultdict
45
import pytest
56
import _valgrind
67

8+
from util import get_short_prefix
9+
710
class Timer:
811

912
def __init__(self, nodeid):
@@ -72,6 +75,9 @@ def format_ratio(self, reference, value):
7275
def display_summary(self, tr):
7376
w = tr.write_line
7477
w('')
78+
tr.write_sep('=', "INFO", cyan=True)
79+
w(f"implementation: {sys.implementation.name}")
80+
w(f"short prefix: {get_short_prefix()}")
7581
tr.write_sep('=', 'BENCHMARKS', cyan=True)
7682
line = ' '*40 + ' cpy hpy'
7783
if 'purepy' in self.apis:

microbench/print_other_vs_cpy.py

100644100755
Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
#!/usr/bin/env python3
12
import sys
23

34
from pathlib import Path
45

6+
from util import capitalize_implementation, info_from_path, print_sep
7+
58
try:
6-
other = sys.argv[1]
9+
arg = sys.argv[1]
710
except IndexError:
8-
other = "PyPy"
11+
arg = "pypy"
12+
13+
path_result_cpy = Path("tmp_results_cpy.txt")
14+
15+
path_result_other = Path(arg)
916

10-
path_result_cpy = Path("tmp_results_cpython.txt")
11-
path_result_other = Path(f"tmp_results_{other.lower()}.txt")
17+
if not path_result_other.exists():
18+
path_result_other = Path(f"tmp_results_{arg}.txt")
1219

1320
assert path_result_cpy.exists()
1421
assert path_result_other.exists()
@@ -47,17 +54,24 @@ def data_from_path(path):
4754
return names, times_cpy, times_hpy
4855

4956

50-
names, times_cpy_cpy, times_cpy_hpy = data_from_path(path_result_cpy)
57+
info = info_from_path(path_result_other)
58+
59+
implementation = info["implementation"]
60+
61+
names_cpy, times_cpy_cpy, times_cpy_hpy = data_from_path(path_result_cpy)
5162
names, times_other_cpyext, times_other_hpy = data_from_path(path_result_other)
5263

64+
assert names_cpy == names
65+
5366
max_length_name = 45
5467
fmt_name = f"{{:{max_length_name}s}}"
5568

56-
out = f" {other} / CPy native (time ratio, smaller is better) "
57-
num_chars = 81
58-
num_equals = (num_chars - len(out)) // 2
69+
print_sep(
70+
f"{capitalize_implementation(implementation)} / CPy native (time ratio, smaller is better)"
71+
)
5972

60-
print(num_equals * "=" + out + num_equals * "=")
73+
if info["short prefix"] != implementation:
74+
print(f"short prefix: {info['short prefix']}")
6175

6276
if times_other_cpyext:
6377
print(max_length_name * " " + "cpyext HPy univ")

microbench/print_tmp_results.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
4+
from pathlib import Path
5+
6+
import util
7+
8+
try:
9+
arg = sys.argv[1]
10+
except IndexError:
11+
arg = "pypy"
12+
13+
path_result = Path(arg)
14+
15+
if not path_result.exists():
16+
path_result = Path(f"tmp_results_{arg}.txt")
17+
18+
assert path_result.exists()
19+
20+
info = util.info_from_path(path_result)
21+
implementation = info["implementation"]
22+
23+
util.print_sep(util.capitalize_implementation(implementation))
24+
if info["short prefix"] != implementation and info[
25+
"short prefix"
26+
] != util.short_implementation(implementation):
27+
print(f"short prefix: {info['short prefix']}")
28+
29+
txt = path_result.read_text()
30+
_, txt = txt.split(
31+
"================================== BENCHMARKS ==================================\n"
32+
)
33+
34+
print(txt.rstrip())

microbench/util.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import sys
2+
3+
from pathlib import Path
4+
5+
6+
def get_short_prefix():
7+
prefix = Path(sys.prefix)
8+
name = prefix.name
9+
parts = [".", "venv", "_", "-"]
10+
for part in parts:
11+
if name.startswith(part):
12+
name = name[len(part) :]
13+
return name
14+
15+
16+
def info_from_path(path):
17+
txt = path.read_text()
18+
_, txt = txt.split(
19+
"===================================== INFO ====================================="
20+
)
21+
txt, _ = txt.split(
22+
"================================== BENCHMARKS =================================="
23+
)
24+
result = {}
25+
for line in txt.splitlines():
26+
if not line:
27+
continue
28+
left, right = line.split(":")
29+
result[left] = right.strip()
30+
return result
31+
32+
33+
def capitalize_implementation(implementation):
34+
return implementation.capitalize().replace("py", "Py")
35+
36+
37+
def short_implementation(implementation):
38+
if implementation == "cpython":
39+
return "cpy"
40+
return implementation
41+
42+
43+
def print_sep(out, sep_char="=", num_chars=81):
44+
out = f" {out} "
45+
num_sep_chars = (num_chars - len(out)) // 2
46+
print(num_sep_chars * "=" + out + num_sep_chars * "=")
47+
48+
49+
def print_file_name_results():
50+
name = f"tmp_results_{get_short_prefix()}.txt"
51+
print(name)

0 commit comments

Comments
 (0)