-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealistic_workload.py
More file actions
305 lines (251 loc) · 9.59 KB
/
realistic_workload.py
File metadata and controls
305 lines (251 loc) · 9.59 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
#!/usr/bin/env python3
"""Benchmark: Realistic workload comparison.
This benchmark tests SDK overhead on realistic API workloads:
1. Typical read endpoint (~5-10ms baseline)
2. Typical write endpoint (~15-25ms baseline)
3. Realistic mixed workload (~10-20ms baseline)
These simulate production APIs that do actual work (I/O, validation, processing).
"""
from __future__ import annotations
import json
import os
import subprocess
import sys
from pathlib import Path
def run_benchmark_subprocess(mode: str, sampling_rate: float = 1.0) -> dict | None:
"""Run benchmark in a subprocess to ensure clean SDK state."""
script = f'''
import os
import sys
import time
import shutil
import statistics
from pathlib import Path
# Configure SDK mode
os.environ["TUSK_DRIFT_MODE"] = "{mode}"
sys.path.insert(0, "{os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))}")
import requests
TRACE_DIR = Path("{Path(__file__).parent.parent / ".benchmark-traces-realistic"}")
if TRACE_DIR.exists():
shutil.rmtree(TRACE_DIR)
if "{mode}" == "RECORD":
from drift import TuskDrift
from drift.core.tracing.adapters.filesystem import FilesystemSpanAdapter
sdk = TuskDrift.initialize(
api_key="benchmark-test-key",
env="benchmark",
sampling_rate={sampling_rate},
log_level="warn",
)
if sdk.span_exporter:
sdk.span_exporter.clear_adapters()
adapter = FilesystemSpanAdapter(base_directory=TRACE_DIR)
sdk.span_exporter.add_adapter(adapter)
sdk.mark_app_as_ready()
from benchmarks.server.test_server import TestServer
# Start server
server = TestServer()
server_info = server.start()
server_url = server_info["url"]
# Wait for server
time.sleep(0.5)
session = requests.Session()
# Warmup
for _ in range(10):
session.get(f"{{server_url}}/api/typical-read")
session.post(f"{{server_url}}/api/typical-write", json={{"name": "test"}})
session.post(f"{{server_url}}/api/realistic", json={{"userId": "u1", "query": "test"}})
# Benchmark parameters (configurable via BENCHMARK_ITERATIONS env var)
try:
iterations = int(os.environ.get("BENCHMARK_ITERATIONS", "200"))
except ValueError:
iterations = 200 # Fall back to default on invalid input
results = {{}}
# Test 1: Typical Read (~5-10ms baseline)
latencies = []
for _ in range(iterations):
start = time.perf_counter_ns()
response = session.get(f"{{server_url}}/api/typical-read")
response.json()
latencies.append(time.perf_counter_ns() - start)
results["typical_read"] = {{
"mean_ms": statistics.mean(latencies) / 1_000_000,
"p50_ms": statistics.median(latencies) / 1_000_000,
"p99_ms": sorted(latencies)[int(len(latencies) * 0.99)] / 1_000_000,
"samples": len(latencies),
}}
# Test 2: Typical Write (~15-25ms baseline)
latencies = []
for i in range(iterations):
start = time.perf_counter_ns()
response = session.post(f"{{server_url}}/api/typical-write", json={{"name": f"item-{{i}}"}})
response.json()
latencies.append(time.perf_counter_ns() - start)
results["typical_write"] = {{
"mean_ms": statistics.mean(latencies) / 1_000_000,
"p50_ms": statistics.median(latencies) / 1_000_000,
"p99_ms": sorted(latencies)[int(len(latencies) * 0.99)] / 1_000_000,
"samples": len(latencies),
}}
# Test 3: Realistic Mixed (~10-20ms baseline)
latencies = []
for i in range(iterations):
start = time.perf_counter_ns()
response = session.post(
f"{{server_url}}/api/realistic",
json={{"userId": f"user-{{i}}", "query": "search query", "email": "test@example.com"}},
)
response.json()
latencies.append(time.perf_counter_ns() - start)
results["realistic_mixed"] = {{
"mean_ms": statistics.mean(latencies) / 1_000_000,
"p50_ms": statistics.median(latencies) / 1_000_000,
"p99_ms": sorted(latencies)[int(len(latencies) * 0.99)] / 1_000_000,
"samples": len(latencies),
}}
# Cleanup
session.close()
server.stop()
if "{mode}" == "RECORD":
sdk.shutdown()
import json
print("RESULTS_JSON:" + json.dumps(results))
'''
result = subprocess.run(
[sys.executable, "-c", script],
capture_output=True,
text=True,
cwd=os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
timeout=120,
)
if result.returncode != 0:
print(f"Error running benchmark ({mode}):")
print(result.stderr)
return None
# Extract results from output
for line in result.stdout.split("\n"):
if line.startswith("RESULTS_JSON:"):
return json.loads(line.replace("RESULTS_JSON:", ""))
print(f"Could not find results in output for {mode}")
print(result.stdout[-500:])
return None
def format_comparison(baseline: dict, sdk_on: dict) -> dict:
"""Calculate overhead percentages."""
comparison = {}
for key in baseline:
b_mean = baseline[key]["mean_ms"]
s_mean = sdk_on[key]["mean_ms"]
b_p99 = baseline[key]["p99_ms"]
s_p99 = sdk_on[key]["p99_ms"]
comparison[key] = {
"baseline_mean_ms": b_mean,
"sdk_mean_ms": s_mean,
"mean_overhead_ms": s_mean - b_mean,
"mean_overhead_pct": ((s_mean - b_mean) / b_mean) * 100 if b_mean > 0 else 0,
"baseline_p99_ms": b_p99,
"sdk_p99_ms": s_p99,
"p99_overhead_ms": s_p99 - b_p99,
"p99_overhead_pct": ((s_p99 - b_p99) / b_p99) * 100 if b_p99 > 0 else 0,
}
return comparison
def main():
print("=" * 60)
print("Realistic Workload Benchmark")
print("=" * 60)
print()
# Run baseline (SDK disabled)
print("Running baseline (SDK disabled)...")
baseline = run_benchmark_subprocess("DISABLED")
if not baseline:
print("Failed to run baseline benchmark")
return
print(
f" Typical Read: {baseline['typical_read']['mean_ms']:.2f}ms mean, {baseline['typical_read']['p99_ms']:.2f}ms p99"
)
print(
f" Typical Write: {baseline['typical_write']['mean_ms']:.2f}ms mean, {baseline['typical_write']['p99_ms']:.2f}ms p99"
)
print(
f" Realistic Mixed: {baseline['realistic_mixed']['mean_ms']:.2f}ms mean, {baseline['realistic_mixed']['p99_ms']:.2f}ms p99"
)
print()
# Run with SDK (100% sampling)
print("Running with SDK (100% sampling)...")
sdk_100 = run_benchmark_subprocess("RECORD", sampling_rate=1.0)
if not sdk_100:
print("Failed to run SDK benchmark")
return
print(
f" Typical Read: {sdk_100['typical_read']['mean_ms']:.2f}ms mean, {sdk_100['typical_read']['p99_ms']:.2f}ms p99"
)
print(
f" Typical Write: {sdk_100['typical_write']['mean_ms']:.2f}ms mean, {sdk_100['typical_write']['p99_ms']:.2f}ms p99"
)
print(
f" Realistic Mixed: {sdk_100['realistic_mixed']['mean_ms']:.2f}ms mean, {sdk_100['realistic_mixed']['p99_ms']:.2f}ms p99"
)
print()
# Run with SDK (10% sampling)
print("Running with SDK (10% sampling)...")
sdk_10 = run_benchmark_subprocess("RECORD", sampling_rate=0.1)
if not sdk_10:
print("Failed to run SDK 10% benchmark")
return
print(
f" Typical Read: {sdk_10['typical_read']['mean_ms']:.2f}ms mean, {sdk_10['typical_read']['p99_ms']:.2f}ms p99"
)
print(
f" Typical Write: {sdk_10['typical_write']['mean_ms']:.2f}ms mean, {sdk_10['typical_write']['p99_ms']:.2f}ms p99"
)
print(
f" Realistic Mixed: {sdk_10['realistic_mixed']['mean_ms']:.2f}ms mean, {sdk_10['realistic_mixed']['p99_ms']:.2f}ms p99"
)
print()
# Calculate and display comparisons
comparison_100 = format_comparison(baseline, sdk_100)
comparison_10 = format_comparison(baseline, sdk_10)
print("=" * 60)
print("Results Summary")
print("=" * 60)
print()
print("## SDK Overhead on Realistic Workloads")
print()
print("| Endpoint | Baseline | SDK (100%) | Overhead | SDK (10%) | Overhead |")
print("|----------|----------|------------|----------|-----------|----------|")
for key, label in [
("typical_read", "Read API (~5ms)"),
("typical_write", "Write API (~15ms)"),
("realistic_mixed", "Mixed API (~10ms)"),
]:
c100 = comparison_100[key]
c10 = comparison_10[key]
print(
f"| {label} | {c100['baseline_mean_ms']:.1f}ms | {c100['sdk_mean_ms']:.1f}ms | +{c100['mean_overhead_ms']:.1f}ms ({c100['mean_overhead_pct']:+.1f}%) | {c10['sdk_mean_ms']:.1f}ms | +{c10['mean_overhead_ms']:.1f}ms ({c10['mean_overhead_pct']:+.1f}%) |"
)
print()
print("## P99 Latency Impact")
print()
print("| Endpoint | Baseline p99 | SDK (100%) p99 | SDK (10%) p99 |")
print("|----------|--------------|----------------|---------------|")
for key, label in [("typical_read", "Read API"), ("typical_write", "Write API"), ("realistic_mixed", "Mixed API")]:
c100 = comparison_100[key]
c10 = comparison_10[key]
print(
f"| {label} | {c100['baseline_p99_ms']:.1f}ms | {c100['sdk_p99_ms']:.1f}ms (+{c100['p99_overhead_pct']:.1f}%) | {c10['sdk_p99_ms']:.1f}ms (+{c10['p99_overhead_pct']:.1f}%) |"
)
print()
# Save results
results_dir = Path(__file__).parent.parent / "results"
results_dir.mkdir(exist_ok=True)
results = {
"baseline": baseline,
"sdk_100_percent": sdk_100,
"sdk_10_percent": sdk_10,
"comparison_100": comparison_100,
"comparison_10": comparison_10,
}
with open(results_dir / "realistic-workload.json", "w") as f:
json.dump(results, f, indent=2)
print(f"Results saved to {results_dir / 'realistic-workload.json'}")
if __name__ == "__main__":
main()