-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathbenchmark_multi_python.py
More file actions
executable file
·410 lines (336 loc) · 12.6 KB
/
benchmark_multi_python.py
File metadata and controls
executable file
·410 lines (336 loc) · 12.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
#!/usr/bin/env python3
"""
Multi-Python Benchmark: Compare json2xml performance across Python implementations.
Compares:
- CPython 3.14.2 (homebrew)
- CPython 3.15.0a4 (latest alpha)
- PyPy 3.10.16
- Go (json2xml-go)
Each Python version gets its own virtual environment with json2xml installed.
"""
from __future__ import annotations
import json
import os
import random
import shutil
import string
import subprocess
import sys
import tempfile
import time
from dataclasses import dataclass
from pathlib import Path
# Configuration
BASE_DIR = Path(__file__).resolve().parent
VENVS_DIR = BASE_DIR / ".benchmark_venvs"
GO_CLI = Path(os.environ.get("JSON2XML_GO_CLI", "json2xml-go"))
# Python implementations to benchmark
PYTHON_VERSIONS = [
{
"name": "CPython 3.14.2",
"python": "/opt/homebrew/bin/python3.14",
"venv_name": "venv_cpython314_2",
},
{
"name": "CPython 3.15.0a4",
"python": str(Path.home() / ".local/share/uv/python/cpython-3.15.0a4-macos-aarch64-none/bin/python3.15"),
"venv_name": "venv_cpython315a4",
},
{
"name": "PyPy 3.10.16",
"python": str(Path.home() / ".local/share/uv/python/pypy-3.10.19-macos-aarch64-none/bin/pypy3.10"),
"venv_name": "venv_pypy310",
},
]
# Colors for terminal output
class Colors:
RED = "\033[0;31m"
GREEN = "\033[0;32m"
BLUE = "\033[0;34m"
YELLOW = "\033[1;33m"
CYAN = "\033[0;36m"
MAGENTA = "\033[0;35m"
BOLD = "\033[1m"
NC = "\033[0m"
def colorize(text: str, color: str) -> str:
"""Wrap text in color codes."""
return f"{color}{text}{Colors.NC}"
def random_string(length: int = 10) -> str:
"""Generate a random string."""
return "".join(random.choices(string.ascii_letters, k=length))
def generate_test_json(num_records: int = 1000) -> str:
"""Generate a JSON file for benchmarking."""
data = []
for i in range(num_records):
item = {
"id": i,
"name": random_string(20),
"email": f"{random_string(8)}@example.com",
"active": random.choice([True, False]),
"score": round(random.uniform(0, 100), 2),
"tags": [random_string(5) for _ in range(5)],
"metadata": {
"created": "2024-01-15T10:30:00Z",
"updated": "2024-01-15T12:45:00Z",
"version": random.randint(1, 100),
"nested": {
"level1": {
"level2": {"value": random_string(10)}
}
},
},
}
data.append(item)
return json.dumps(data)
@dataclass
class BenchmarkResult:
"""Holds benchmark timing results."""
name: str
avg_ms: float
min_ms: float
max_ms: float
success: bool
error: str = ""
def setup_venv(python_path: str, venv_path: Path) -> bool:
"""Create a virtual environment and install json2xml."""
if not Path(python_path).exists():
print(f" {colorize('✗', Colors.RED)} Python not found: {python_path}")
return False
# Create venv if it doesn't exist or is broken
venv_python = venv_path / "bin" / "python"
if not venv_python.exists():
print(f" Creating venv at {venv_path}...")
result = subprocess.run(
[python_path, "-m", "venv", str(venv_path)],
capture_output=True,
text=True,
)
if result.returncode != 0:
print(f" {colorize('✗', Colors.RED)} Failed to create venv: {result.stderr}")
return False
# Install json2xml in the venv
print(f" Installing json2xml...")
pip_path = venv_path / "bin" / "pip"
result = subprocess.run(
[str(pip_path), "install", "-e", str(BASE_DIR), "-q"],
capture_output=True,
text=True,
cwd=str(BASE_DIR),
)
if result.returncode != 0:
print(f" {colorize('✗', Colors.RED)} Failed to install json2xml: {result.stderr}")
return False
return True
def run_benchmark(
cmd: list[str],
iterations: int = 10,
warmup: int = 2,
) -> BenchmarkResult:
"""Run a benchmark for the given command."""
times = []
# Check if command exists
if not Path(cmd[0]).exists() and not shutil.which(cmd[0]):
return BenchmarkResult(
name="",
avg_ms=0,
min_ms=0,
max_ms=0,
success=False,
error=f"Command not found: {cmd[0]}",
)
# Warmup runs
for _ in range(warmup):
result = subprocess.run(cmd, capture_output=True, check=False)
if result.returncode != 0:
return BenchmarkResult(
name="",
avg_ms=0,
min_ms=0,
max_ms=0,
success=False,
error=result.stderr.decode()[:100],
)
# Timed runs
for _ in range(iterations):
start = time.perf_counter()
result = subprocess.run(cmd, capture_output=True, check=False)
end = time.perf_counter()
if result.returncode != 0:
return BenchmarkResult(
name="",
avg_ms=0,
min_ms=0,
max_ms=0,
success=False,
error=result.stderr.decode()[:100],
)
duration_ms = (end - start) * 1000
times.append(duration_ms)
return BenchmarkResult(
name="",
avg_ms=sum(times) / len(times),
min_ms=min(times),
max_ms=max(times),
success=True,
)
def format_time(ms: float) -> str:
"""Format time in milliseconds."""
if ms < 1:
return f"{ms * 1000:.2f}µs"
elif ms < 1000:
return f"{ms:.2f}ms"
else:
return f"{ms / 1000:.2f}s"
def print_header(title: str) -> None:
"""Print a section header."""
print(colorize("=" * 70, Colors.BLUE))
print(colorize(f" {title}", Colors.BOLD))
print(colorize("=" * 70, Colors.BLUE))
def print_table_row(name: str, result: BenchmarkResult, baseline_ms: float | None = None) -> None:
"""Print a formatted table row."""
if not result.success:
print(f" {name:<35} {colorize('FAILED', Colors.RED)}: {result.error}")
return
speedup_str = ""
if baseline_ms and result.avg_ms > 0:
speedup = baseline_ms / result.avg_ms
if speedup > 1:
speedup_str = colorize(f" ({speedup:.1f}x faster)", Colors.GREEN)
else:
speedup_str = f" ({1/speedup:.1f}x slower)"
print(f" {name:<35} {format_time(result.avg_ms):>12} "
f"(min: {format_time(result.min_ms)}, max: {format_time(result.max_ms)}){speedup_str}")
def main() -> int:
"""Run the multi-Python benchmark suite."""
print_header("Multi-Python Benchmark: json2xml Performance")
print()
# Create venvs directory
VENVS_DIR.mkdir(exist_ok=True)
# Setup phase
print(colorize("Setting up Python environments...", Colors.YELLOW))
print()
active_pythons = []
for py_config in PYTHON_VERSIONS:
name = py_config["name"]
python_path = py_config["python"]
venv_name = py_config["venv_name"]
venv_path = VENVS_DIR / venv_name
print(f" {name}:")
if setup_venv(python_path, venv_path):
print(f" {colorize('✓', Colors.GREEN)} Ready")
active_pythons.append({
**py_config,
"venv_path": venv_path,
"cli_python": str(venv_path / "bin" / "python"),
})
else:
print(f" {colorize('✗', Colors.RED)} Skipped")
print()
# Check Go CLI
go_available = shutil.which(str(GO_CLI)) is not None or Path(GO_CLI).exists()
if go_available:
print(f" Go (json2xml-go): {colorize('✓', Colors.GREEN)} Ready")
else:
print(f" Go (json2xml-go): {colorize('✗', Colors.RED)} Not found at {GO_CLI}")
print(f" Set JSON2XML_GO_CLI env var or ensure json2xml-go is in PATH")
print()
if not active_pythons:
print(colorize("Error: No Python environments available!", Colors.RED))
return 1
# Generate test files
print(colorize("Generating test data...", Colors.YELLOW))
with tempfile.TemporaryDirectory() as tmpdir:
# Small JSON
small_json = '{"name": "John", "age": 30, "city": "New York"}'
# Medium JSON (existing file)
medium_json_file = BASE_DIR / "examples" / "bigexample.json"
# Large JSON
large_json = generate_test_json(1000)
large_json_file = Path(tmpdir) / "large.json"
large_json_file.write_text(large_json)
# Very large JSON
very_large_json = generate_test_json(5000)
very_large_json_file = Path(tmpdir) / "very_large.json"
very_large_json_file.write_text(very_large_json)
print(f" Small: {len(small_json):,} bytes")
print(f" Medium: {medium_json_file.stat().st_size:,} bytes")
print(f" Large: {large_json_file.stat().st_size:,} bytes (1000 records)")
print(f" Very Large: {very_large_json_file.stat().st_size:,} bytes (5000 records)")
print()
iterations = 10
all_results: dict[str, dict[str, BenchmarkResult]] = {}
# Benchmark each test case
test_cases = [
("Small JSON", ["-s", small_json]),
("Medium JSON", [str(medium_json_file)]),
("Large JSON (1K)", [str(large_json_file)]),
("Very Large JSON (5K)", [str(very_large_json_file)]),
]
for test_name, args in test_cases:
print_header(f"Benchmark: {test_name}")
print()
results: dict[str, BenchmarkResult] = {}
# Benchmark each Python version
for py_config in active_pythons:
name = py_config["name"]
cli_python = py_config["cli_python"]
cmd = [cli_python, "-m", "json2xml.cli"] + args
result = run_benchmark(cmd, iterations=iterations)
result.name = name
results[name] = result
# Benchmark Go
if go_available:
go_cmd = [str(GO_CLI)] + args
go_result = run_benchmark(go_cmd, iterations=iterations)
go_result.name = "Go (json2xml-go)"
results["Go"] = go_result
all_results[test_name] = results
# Find baseline (first successful Python result)
baseline_ms = None
for py_config in active_pythons:
if results.get(py_config["name"], BenchmarkResult("", 0, 0, 0, False)).success:
baseline_ms = results[py_config["name"]].avg_ms
break
# Print results
for py_config in active_pythons:
name = py_config["name"]
if name in results:
print_table_row(name, results[name], baseline_ms)
if go_available and "Go" in results:
print_table_row("Go (json2xml-go)", results["Go"], baseline_ms)
print()
# Summary
print_header("SUMMARY: Average Times Across All Tests")
print()
# Calculate averages
avg_times: dict[str, list[float]] = {}
for test_name, results in all_results.items():
for name, result in results.items():
if result.success:
if name not in avg_times:
avg_times[name] = []
avg_times[name].append(result.avg_ms)
# Print summary table
print(f" {'Implementation':<35} {'Avg Time':>12} {'vs CPython 3.14.2':>20}")
print(f" {'-' * 35} {'-' * 12} {'-' * 20}")
baseline_name = "CPython 3.14.2"
baseline_avg = sum(avg_times.get(baseline_name, [0])) / len(avg_times.get(baseline_name, [1]))
sorted_impls = sorted(avg_times.items(), key=lambda x: sum(x[1]) / len(x[1]))
for name, times in sorted_impls:
avg = sum(times) / len(times)
if baseline_avg > 0 and name != baseline_name:
speedup = baseline_avg / avg
if speedup > 1:
vs_baseline = colorize(f"{speedup:.2f}x faster", Colors.GREEN)
else:
vs_baseline = f"{1/speedup:.2f}x slower"
else:
vs_baseline = "baseline"
print(f" {name:<35} {format_time(avg):>12} {vs_baseline:>20}")
print()
print(colorize("=" * 70, Colors.BLUE))
print(colorize("Benchmark complete!", Colors.GREEN))
print(colorize("=" * 70, Colors.BLUE))
return 0
if __name__ == "__main__":
sys.exit(main())