-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_bcf_benchmarks.py
More file actions
executable file
·212 lines (189 loc) · 7.4 KB
/
Copy pathrun_bcf_benchmarks.py
File metadata and controls
executable file
·212 lines (189 loc) · 7.4 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
#!/usr/bin/env python3
"""Run isolated, repeatable BCF timing and peak-RSS benchmarks."""
import argparse
import hashlib
import importlib.metadata
import json
import os
import platform
import statistics
import subprocess
import sys
from pathlib import Path
import psutil
from benchmarks.common import BCF_PATH
RUNNERS = {
"polars-bio": "benchmarks.bench_bcf_polars_bio",
"snputils": "benchmarks.bench_bcf_snputils",
}
SNPUTILS_REF = "bdb1a56b52a6b16210d60e347d33d023dc98352f"
def parse_result(output: str, prefix: str) -> dict:
for line in output.splitlines():
if line.startswith(prefix):
return json.loads(line.removeprefix(prefix))
raise RuntimeError(f"process did not emit {prefix!r}:\n{output}")
def run_module(
python: str,
module: str,
env: dict[str, str],
*,
result_prefix: str = "BENCHMARK_RESULT:",
) -> dict:
completed = subprocess.run(
[python, "-m", module],
check=False,
capture_output=True,
text=True,
env=env,
)
print(completed.stdout, end="")
if completed.stderr:
print(completed.stderr, file=sys.stderr, end="")
if completed.returncode != 0:
raise subprocess.CalledProcessError(
completed.returncode,
completed.args,
output=completed.stdout,
stderr=completed.stderr,
)
return parse_result(completed.stdout, result_prefix)
def file_sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(8 * 1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def summarize(results: list[dict]) -> dict:
times = [result["time_seconds"] for result in results]
memories = [result["peak_rss_mb"] for result in results]
return {
"runs": len(results),
"time_seconds_median": round(statistics.median(times), 3),
"time_seconds_mean": round(statistics.mean(times), 3),
"time_seconds_stdev": round(statistics.stdev(times), 3)
if len(times) > 1
else 0.0,
"peak_rss_mb_median": round(statistics.median(memories), 1),
"peak_rss_mb_mean": round(statistics.mean(memories), 1),
"peak_rss_mb_stdev": round(statistics.stdev(memories), 1)
if len(memories) > 1
else 0.0,
"raw": results,
}
def compare_summaries(summary: dict[str, dict]) -> dict:
polars = summary["polars-bio"]
snputils = summary["snputils"]
polars_time = polars["time_seconds_median"]
snputils_time = snputils["time_seconds_median"]
polars_memory = polars["peak_rss_mb_median"]
snputils_memory = snputils["peak_rss_mb_median"]
return {
"polars_bio_time_speedup": round(snputils_time / polars_time, 3),
"polars_bio_time_reduction_percent": round(
100 * (1 - polars_time / snputils_time), 1
),
"polars_bio_peak_rss_advantage": round(snputils_memory / polars_memory, 3),
"polars_bio_peak_rss_reduction_percent": round(
100 * (1 - polars_memory / snputils_memory), 1
),
}
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--runs", type=int, default=3)
parser.add_argument(
"--threads",
type=int,
default=1,
help=(
"polars-bio DataFusion target partitions and thread caps; "
"the pinned snputils BCF reader remains serial"
),
)
parser.add_argument("--python", default=sys.executable)
parser.add_argument("--skip-verify", action="store_true")
parser.add_argument("--output", default="results/bcf_benchmark_results.json")
args = parser.parse_args()
if args.runs < 1 or args.threads < 1:
parser.error("--runs and --threads must be positive")
bcf_path = Path(BCF_PATH).expanduser().resolve()
if not bcf_path.is_file():
parser.error(f"BCF file does not exist: {bcf_path}")
env = os.environ.copy()
env.update(
{
"BCF_PATH": str(bcf_path),
"BENCH_VARIANT": "dosage",
"THREAD_NUM": str(args.threads),
"POLARS_MAX_THREADS": str(args.threads),
"RAYON_NUM_THREADS": str(args.threads),
"OMP_NUM_THREADS": str(args.threads),
"OPENBLAS_NUM_THREADS": str(args.threads),
"MKL_NUM_THREADS": str(args.threads),
"VECLIB_MAXIMUM_THREADS": str(args.threads),
"NUMEXPR_NUM_THREADS": str(args.threads),
"TQDM_DISABLE": "1",
}
)
equivalence = None
if not args.skip_verify:
equivalence = run_module(
args.python,
"benchmarks.verify_bcf_equivalence",
env,
result_prefix="BCF_EQUIVALENCE:",
)
raw: dict[str, list[dict]] = {name: [] for name in RUNNERS}
names = list(RUNNERS)
for round_index in range(args.runs):
order = names if round_index % 2 == 0 else list(reversed(names))
for order_index, name in enumerate(order, start=1):
print(f"\nRound {round_index + 1}/{args.runs}: {name}")
result = run_module(args.python, RUNNERS[name], env)
result["round"] = round_index + 1
result["order_in_round"] = order_index
raw[name].append(result)
summary = {name: summarize(results) for name, results in raw.items()}
metadata = {
"format": "BCF",
"variant": "dosage",
"path": str(bcf_path),
"file_size_bytes": bcf_path.stat().st_size,
"file_sha256": file_sha256(bcf_path),
"threads": args.threads,
"polars_bio_target_partitions": args.threads,
"snputils_bcf_parallelism": "serial; reader exposes no thread-count option",
"python": platform.python_version(),
"platform": platform.platform(),
"machine": platform.machine(),
"logical_cpu_count": psutil.cpu_count(logical=True),
"physical_cpu_count": psutil.cpu_count(logical=False),
"memory_total_bytes": psutil.virtual_memory().total,
"polars_bio_version": importlib.metadata.version("polars-bio"),
"polars_version": importlib.metadata.version("polars"),
"pyarrow_version": importlib.metadata.version("pyarrow"),
"numpy_version": importlib.metadata.version("numpy"),
"snputils_version": importlib.metadata.version("snputils"),
"snputils_ref": SNPUTILS_REF,
"polars_bio_ref": os.environ.get("POLARS_BIO_REF"),
"datafusion_bio_formats_ref": os.environ.get("DATAFUSION_BIO_FORMATS_REF"),
"polars_bio_build_profile": os.environ.get("POLARS_BIO_BUILD_PROFILE"),
"polars_bio_rustflags": os.environ.get("POLARS_BIO_RUSTFLAGS"),
"cache_state": "warm; full equivalence scan precedes timed rounds",
"timing_scope": "read, decode, dosage conversion, and materialization; imports excluded",
"memory_metric": "fresh-process peak RSS including retained materialized output",
}
payload = {
"metadata": metadata,
"equivalence": equivalence,
"results": summary,
"comparison": compare_summaries(summary),
}
output_path = Path(args.output)
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(
json.dumps(payload, indent=2, sort_keys=True) + "\n", encoding="utf-8"
)
print(f"\nBCF_BENCHMARK_SUMMARY:{json.dumps(payload, sort_keys=True)}")
print(f"Wrote {output_path}")
if __name__ == "__main__":
main()