-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathbenchmark_all.py
More file actions
337 lines (276 loc) · 11.3 KB
/
benchmark_all.py
File metadata and controls
337 lines (276 loc) · 11.3 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
#!/usr/bin/env python3
"""
Comprehensive benchmark comparing all json2xml implementations:
- Pure Python (json2xml)
- Rust-powered Python (json2xml-rs)
- Go CLI (json2xml-go)
- Zig CLI (json2xml-zig)
Run with: python benchmark_all.py
"""
from __future__ import annotations
import json
import os
import random
import shutil
import string
import subprocess
import sys
import tempfile
import time
from pathlib import Path
# Add the json2xml module to path
sys.path.insert(0, str(Path(__file__).parent))
from json2xml import dicttoxml as py_dicttoxml
# Try to import Rust implementation
try:
from json2xml_rs import dicttoxml as rust_dicttoxml
RUST_AVAILABLE = True
except ImportError:
RUST_AVAILABLE = False
# Check for CLI tools
GO_AVAILABLE = shutil.which("json2xml-go") is not None
ZIG_AVAILABLE = shutil.which("json2xml-zig") is not None
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:
return f"{color}{text}{Colors.NC}"
def random_string(length: int = 10) -> str:
return "".join(random.choices(string.ascii_letters, k=length))
def generate_test_data(num_records: int) -> list[dict]:
"""Generate test data with various types."""
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 data
def benchmark_python(data, iterations: int = 10, warmup: int = 2) -> dict:
"""Benchmark pure Python implementation."""
# Warmup
for _ in range(warmup):
py_dicttoxml.dicttoxml(data, attr_type=True)
times = []
for _ in range(iterations):
start = time.perf_counter()
result = py_dicttoxml.dicttoxml(data, attr_type=True)
end = time.perf_counter()
times.append((end - start) * 1000)
return {
"avg": sum(times) / len(times),
"min": min(times),
"max": max(times),
"result_size": len(result),
}
def benchmark_rust(data, iterations: int = 10, warmup: int = 2) -> dict | None:
"""Benchmark Rust implementation."""
if not RUST_AVAILABLE:
return None
# Warmup
for _ in range(warmup):
rust_dicttoxml(data, attr_type=True)
times = []
for _ in range(iterations):
start = time.perf_counter()
result = rust_dicttoxml(data, attr_type=True)
end = time.perf_counter()
times.append((end - start) * 1000)
return {
"avg": sum(times) / len(times),
"min": min(times),
"max": max(times),
"result_size": len(result),
}
def benchmark_cli(cmd: str, json_file: str, iterations: int = 10, warmup: int = 2) -> dict | None:
"""Benchmark CLI tool (Go or Zig)."""
# Warmup
for _ in range(warmup):
subprocess.run([cmd, "-f", json_file], capture_output=True)
times = []
result_size = 0
for _ in range(iterations):
start = time.perf_counter()
result = subprocess.run([cmd, "-f", json_file], capture_output=True)
end = time.perf_counter()
times.append((end - start) * 1000)
result_size = len(result.stdout)
return {
"avg": sum(times) / len(times),
"min": min(times),
"max": max(times),
"result_size": result_size,
}
def format_time(ms: float) -> str:
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 run_benchmark(name: str, data: dict | list, iterations: int = 10):
"""Run benchmark for all implementations."""
print(colorize(f"\n{'=' * 70}", Colors.BLUE))
print(colorize(f" {name}", Colors.BOLD))
print(colorize(f"{'=' * 70}", Colors.BLUE))
results = {}
# Create temp file for CLI tools
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump(data, f)
json_file = f.name
try:
# Python implementation
py_result = benchmark_python(data, iterations)
results["python"] = py_result
print(f" {colorize('Python:', Colors.YELLOW):20} {format_time(py_result['avg']):>12} avg "
f"(min: {format_time(py_result['min'])}, max: {format_time(py_result['max'])})")
# Rust implementation
if RUST_AVAILABLE:
rust_result = benchmark_rust(data, iterations)
results["rust"] = rust_result
speedup = py_result["avg"] / rust_result["avg"]
print(f" {colorize('Rust:', Colors.GREEN):20} {format_time(rust_result['avg']):>12} avg "
f"(min: {format_time(rust_result['min'])}, max: {format_time(rust_result['max'])}) "
f"[{colorize(f'{speedup:.1f}x faster', Colors.GREEN)}]")
else:
print(f" {colorize('Rust:', Colors.RED):20} NOT AVAILABLE")
# Go implementation
if GO_AVAILABLE:
go_result = benchmark_cli("json2xml-go", json_file, iterations)
results["go"] = go_result
speedup = py_result["avg"] / go_result["avg"]
color = Colors.GREEN if speedup > 1 else Colors.RED
print(f" {colorize('Go:', Colors.CYAN):20} {format_time(go_result['avg']):>12} avg "
f"(min: {format_time(go_result['min'])}, max: {format_time(go_result['max'])}) "
f"[{colorize(f'{speedup:.1f}x vs Python', color)}]")
else:
print(f" {colorize('Go:', Colors.RED):20} NOT AVAILABLE")
# Zig implementation
if ZIG_AVAILABLE:
zig_result = benchmark_cli("json2xml-zig", json_file, iterations)
results["zig"] = zig_result
speedup = py_result["avg"] / zig_result["avg"]
color = Colors.GREEN if speedup > 1 else Colors.RED
print(f" {colorize('Zig:', Colors.MAGENTA):20} {format_time(zig_result['avg']):>12} avg "
f"(min: {format_time(zig_result['min'])}, max: {format_time(zig_result['max'])}) "
f"[{colorize(f'{speedup:.1f}x vs Python', color)}]")
else:
print(f" {colorize('Zig:', Colors.RED):20} NOT AVAILABLE")
finally:
os.unlink(json_file)
return results
def main():
print(colorize("=" * 70, Colors.BLUE))
print(colorize(" COMPREHENSIVE JSON2XML BENCHMARK", Colors.BOLD))
print(colorize(" Python vs Rust vs Go vs Zig", Colors.BOLD))
print(colorize("=" * 70, Colors.BLUE))
print(colorize("\nAvailable implementations:", Colors.YELLOW))
print(f" • Python: {colorize('✓', Colors.GREEN)} (pure Python)")
print(f" • Rust: {colorize('✓', Colors.GREEN) if RUST_AVAILABLE else colorize('✗', Colors.RED)} (json2xml-rs)")
print(f" • Go: {colorize('✓', Colors.GREEN) if GO_AVAILABLE else colorize('✗', Colors.RED)} (json2xml-go CLI)")
print(f" • Zig: {colorize('✓', Colors.GREEN) if ZIG_AVAILABLE else colorize('✗', Colors.RED)} (json2xml-zig CLI)")
# Test data
small_data = {"name": "John", "age": 30, "city": "New York"}
medium_data = generate_test_data(10)
large_data = generate_test_data(100)
very_large_data = generate_test_data(1000)
# Load bigexample.json if available
examples_dir = Path(__file__).parent / "examples"
bigexample_file = examples_dir / "bigexample.json"
if bigexample_file.exists():
with open(bigexample_file) as f:
bigexample_data = json.load(f)
else:
bigexample_data = None
# Run benchmarks
all_results = {}
all_results["small"] = run_benchmark(
f"Small JSON ({len(json.dumps(small_data))} bytes)",
small_data,
iterations=50,
)
all_results["medium"] = run_benchmark(
f"Medium JSON ({len(json.dumps(medium_data))} bytes, 10 records)",
medium_data,
iterations=30,
)
if bigexample_data:
all_results["bigexample"] = run_benchmark(
f"bigexample.json ({len(json.dumps(bigexample_data))} bytes)",
bigexample_data,
iterations=20,
)
all_results["large"] = run_benchmark(
f"Large JSON ({len(json.dumps(large_data))} bytes, 100 records)",
large_data,
iterations=15,
)
all_results["very_large"] = run_benchmark(
f"Very Large JSON ({len(json.dumps(very_large_data))} bytes, 1000 records)",
very_large_data,
iterations=10,
)
# Summary table
print(colorize("\n" + "=" * 70, Colors.BLUE))
print(colorize(" SUMMARY TABLE", Colors.BOLD))
print(colorize("=" * 70, Colors.BLUE))
print(f"\n{'Test Case':<25} {'Python':>12} {'Rust':>12} {'Go':>12} {'Zig':>12}")
print("-" * 73)
for name, results in all_results.items():
py_time = format_time(results.get("python", {}).get("avg", 0))
rust_time = format_time(results.get("rust", {}).get("avg", 0)) if results.get("rust") else "N/A"
go_time = format_time(results.get("go", {}).get("avg", 0)) if results.get("go") else "N/A"
zig_time = format_time(results.get("zig", {}).get("avg", 0)) if results.get("zig") else "N/A"
print(f"{name:<25} {py_time:>12} {rust_time:>12} {go_time:>12} {zig_time:>12}")
# Speedup comparison
print(colorize("\n" + "=" * 70, Colors.BLUE))
print(colorize(" SPEEDUP vs PYTHON", Colors.BOLD))
print(colorize("=" * 70, Colors.BLUE))
print(f"\n{'Test Case':<25} {'Rust':>12} {'Go':>12} {'Zig':>12}")
print("-" * 61)
for name, results in all_results.items():
py_avg = results.get("python", {}).get("avg", 1)
rust_speedup = f"{py_avg / results['rust']['avg']:.1f}x" if results.get("rust") else "N/A"
go_speedup = f"{py_avg / results['go']['avg']:.1f}x" if results.get("go") else "N/A"
zig_speedup = f"{py_avg / results['zig']['avg']:.1f}x" if results.get("zig") else "N/A"
print(f"{name:<25} {rust_speedup:>12} {go_speedup:>12} {zig_speedup:>12}")
# Overall winner
print(colorize("\n" + "=" * 70, Colors.BLUE))
print(colorize(" NOTES", Colors.BOLD))
print(colorize("=" * 70, Colors.BLUE))
print("""
• Python: Pure Python implementation (always available)
• Rust: Native extension via PyO3 (library call, no process overhead)
• Go: CLI tool (includes process spawn overhead)
• Zig: CLI tool (includes process spawn overhead)
Note: CLI tools (Go, Zig) include process spawning overhead which makes
them appear slower for small inputs. For larger data, this overhead
becomes negligible. Rust extension has no such overhead since it's
called directly from Python.
""")
print(colorize("=" * 70, Colors.BLUE))
print(colorize(" Benchmark complete!", Colors.GREEN))
print(colorize("=" * 70, Colors.BLUE))
if __name__ == "__main__":
main()