-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_latency.py
More file actions
executable file
·396 lines (330 loc) · 11.9 KB
/
benchmark_latency.py
File metadata and controls
executable file
·396 lines (330 loc) · 11.9 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
#!/usr/bin/env python3
"""
Benchmark latency comparison: Self-hosted LLM vs Commercial APIs
Measures: TTFT, TPS, Total latency, Cost per token
"""
import asyncio
import json
import os
import statistics
import subprocess
import time
from dataclasses import dataclass, field
from datetime import datetime
from pathlib import Path
from typing import Optional
from openai import AsyncOpenAI
def _get_service_url():
"""Automatically retrieve the Cloud Run service URL if not set."""
try:
region = os.getenv("GCP_REGION", "asia-east2")
result = subprocess.run(
[
"gcloud",
"run",
"services",
"describe",
"llm-api",
"--region",
region,
"--format",
"value(status.url)",
],
capture_output=True,
text=True,
timeout=5,
)
if result.returncode == 0:
return result.stdout.strip()
except Exception:
pass
return None
@dataclass
class BenchmarkConfig:
"""Benchmark configuration."""
self_hosted_url: str = (
os.getenv("SELF_HOSTED_URL", "")
or os.getenv("LLM_API_URL", "")
or _get_service_url()
or ""
)
openai_api_key: str = os.getenv("OPENAI_API_KEY", "")
num_runs: int = 10
warmup_runs: int = 2
max_tokens: int = 256
temperature: float = 0.7
prompts: list = field(
default_factory=lambda: [
"What is 2+2?",
"Explain the concept of machine learning in 3 sentences.",
"Write a comparison of Python and JavaScript for web development.",
]
)
@dataclass
class LatencyResult:
"""Single request latency measurements."""
provider: str
model: str
prompt_tokens: int
completion_tokens: int
ttft_ms: float
total_ms: float
tps: float
cost_usd: float
error: Optional[str] = None
PRICING = {
"gpt-4o-mini": {"input": 0.15 / 1_000_000, "output": 0.60 / 1_000_000},
"gpt-4o": {"input": 2.50 / 1_000_000, "output": 10.00 / 1_000_000},
"self-hosted": {"input": 0, "output": 0},
}
async def benchmark_openai(
client: AsyncOpenAI,
model: str,
prompt: str,
config: BenchmarkConfig,
) -> LatencyResult:
"""Benchmark OpenAI API."""
start_time = time.perf_counter()
ttft = None
tokens_received = 0
try:
stream = await client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
max_tokens=config.max_tokens,
temperature=config.temperature,
stream=True,
)
async for chunk in stream:
if ttft is None:
ttft = (time.perf_counter() - start_time) * 1000
if chunk.choices[0].delta.content:
tokens_received += 1
total_time = (time.perf_counter() - start_time) * 1000
prompt_tokens = len(prompt) // 4
pricing = PRICING.get(model, PRICING["gpt-4o-mini"])
cost = (prompt_tokens * pricing["input"]) + (
tokens_received * pricing["output"]
)
return LatencyResult(
provider="OpenAI",
model=model,
prompt_tokens=prompt_tokens,
completion_tokens=tokens_received,
ttft_ms=ttft or total_time,
total_ms=total_time,
tps=tokens_received / (total_time / 1000) if total_time > 0 else 0,
cost_usd=cost,
)
except Exception as e:
return LatencyResult(
provider="OpenAI",
model=model,
prompt_tokens=0,
completion_tokens=0,
ttft_ms=0,
total_ms=0,
tps=0,
cost_usd=0,
error=str(e),
)
async def benchmark_self_hosted(
client: AsyncOpenAI,
model: str,
prompt: str,
config: BenchmarkConfig,
) -> LatencyResult:
"""Benchmark self-hosted vLLM API."""
start_time = time.perf_counter()
ttft = None
tokens_received = 0
try:
stream = await client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
max_tokens=config.max_tokens,
temperature=config.temperature,
stream=True,
)
async for chunk in stream:
if ttft is None:
ttft = (time.perf_counter() - start_time) * 1000
if chunk.choices[0].delta.content:
tokens_received += 1
total_time = (time.perf_counter() - start_time) * 1000
prompt_tokens = len(prompt) // 4
return LatencyResult(
provider="Self-Hosted",
model=model,
prompt_tokens=prompt_tokens,
completion_tokens=tokens_received,
ttft_ms=ttft or total_time,
total_ms=total_time,
tps=tokens_received / (total_time / 1000) if total_time > 0 else 0,
cost_usd=0,
)
except Exception as e:
return LatencyResult(
provider="Self-Hosted",
model=model,
prompt_tokens=0,
completion_tokens=0,
ttft_ms=0,
total_ms=0,
tps=0,
cost_usd=0,
error=str(e),
)
def calculate_percentile(values: list[float], percentile: float) -> float:
"""Calculate percentile of a list of values."""
if not values:
return 0
sorted_values = sorted(values)
index = int(len(sorted_values) * percentile / 100)
return sorted_values[min(index, len(sorted_values) - 1)]
def aggregate_results(results: list[LatencyResult]) -> dict:
"""Aggregate individual results into a report."""
successful = [r for r in results if r.error is None]
if not successful:
return {
"provider": results[0].provider if results else "Unknown",
"model": results[0].model if results else "Unknown",
"num_requests": len(results),
"error_rate": 1.0,
}
ttft_values = [r.ttft_ms for r in successful]
total_values = [r.total_ms for r in successful]
tps_values = [r.tps for r in successful]
return {
"provider": successful[0].provider,
"model": successful[0].model,
"num_requests": len(results),
"avg_ttft_ms": statistics.mean(ttft_values),
"p50_ttft_ms": calculate_percentile(ttft_values, 50),
"p95_ttft_ms": calculate_percentile(ttft_values, 95),
"avg_total_ms": statistics.mean(total_values),
"p50_total_ms": calculate_percentile(total_values, 50),
"p95_total_ms": calculate_percentile(total_values, 95),
"avg_tps": statistics.mean(tps_values),
"total_cost_usd": sum(r.cost_usd for r in successful),
"error_rate": (len(results) - len(successful)) / len(results),
}
async def run_benchmark(config: BenchmarkConfig) -> dict:
"""Run full benchmark suite."""
results = {
"timestamp": datetime.now().isoformat(),
"config": {
"num_runs": config.num_runs,
"max_tokens": config.max_tokens,
"prompts": config.prompts,
},
"reports": [],
}
clients = {}
if config.openai_api_key:
clients["openai"] = AsyncOpenAI(api_key=config.openai_api_key)
if config.self_hosted_url:
clients["self_hosted"] = AsyncOpenAI(
base_url=f"{config.self_hosted_url}/v1",
api_key="dummy",
)
for prompt in config.prompts:
print(f"\n{'='*60}")
print(f"Prompt: {prompt[:50]}...")
print(f"{'='*60}")
if "openai" in clients:
for model in ["gpt-4o-mini"]:
print(f"\nBenchmarking OpenAI {model}...")
model_results = []
for _ in range(config.warmup_runs):
await benchmark_openai(clients["openai"], model, prompt, config)
for i in range(config.num_runs):
result = await benchmark_openai(
clients["openai"], model, prompt, config
)
model_results.append(result)
print(
f" Run {i+1}: TTFT={result.ttft_ms:.0f}ms, "
f"Total={result.total_ms:.0f}ms, TPS={result.tps:.1f}"
)
report = aggregate_results(model_results)
report["prompt_preview"] = prompt[:50]
results["reports"].append(report)
if "self_hosted" in clients:
print("\nBenchmarking Self-Hosted...")
model_results = []
model_name = os.getenv("MODEL_NAME", "qwen3-8b-awq")
for _ in range(config.warmup_runs):
await benchmark_self_hosted(
clients["self_hosted"], model_name, prompt, config
)
for i in range(config.num_runs):
result = await benchmark_self_hosted(
clients["self_hosted"], model_name, prompt, config
)
model_results.append(result)
print(
f" Run {i+1}: TTFT={result.ttft_ms:.0f}ms, "
f"Total={result.total_ms:.0f}ms, TPS={result.tps:.1f}"
)
report = aggregate_results(model_results)
report["prompt_preview"] = prompt[:50]
results["reports"].append(report)
return results
def print_summary(results: dict):
"""Print benchmark summary."""
print("\n" + "=" * 80)
print("BENCHMARK SUMMARY")
print("=" * 80)
by_provider = {}
for report in results["reports"]:
provider = report["provider"]
if provider not in by_provider:
by_provider[provider] = []
by_provider[provider].append(report)
for provider, reports in by_provider.items():
print(f"\n{provider}:")
print("-" * 40)
valid_reports = [r for r in reports if "avg_ttft_ms" in r]
if valid_reports:
avg_ttft = statistics.mean(r["avg_ttft_ms"] for r in valid_reports)
avg_total = statistics.mean(r["avg_total_ms"] for r in valid_reports)
avg_tps = statistics.mean(r["avg_tps"] for r in valid_reports)
total_cost = sum(r["total_cost_usd"] for r in valid_reports)
print(f" Avg TTFT: {avg_ttft:>8.1f} ms")
print(f" Avg Total: {avg_total:>8.1f} ms")
print(f" Avg TPS: {avg_tps:>8.1f} tokens/sec")
print(f" Total Cost: ${total_cost:>7.4f}")
def save_results(results: dict, output_dir: str = "benchmarks/results"):
"""Save benchmark results to JSON."""
Path(output_dir).mkdir(parents=True, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_file = Path(output_dir) / f"benchmark_{timestamp}.json"
with open(output_file, "w") as f:
json.dump(results, f, indent=2)
print(f"\nResults saved to: {output_file}")
async def main():
"""Main entry point."""
config = BenchmarkConfig()
if not config.self_hosted_url and not config.openai_api_key:
print("Error: Could not determine API URL or API key.")
print("\nFor self-hosted LLM, set:")
print(" export SELF_HOSTED_URL=https://llm-api-xxx.run.app")
print(" # or")
print(" export LLM_API_URL=https://llm-api-xxx.run.app")
print("\nTo find your service URL, run:")
cmd = (
"gcloud run services describe llm-api --region "
"asia-southeast1 --format 'value(status.url)'"
)
print(f" {cmd}")
print("\nFor OpenAI comparison, also set:")
print(" export OPENAI_API_KEY=sk-...")
return
print("Starting LLM Latency Benchmark...")
print(f"Configuration: {config.num_runs} runs, {config.max_tokens} max tokens")
results = await run_benchmark(config)
print_summary(results)
save_results(results)
if __name__ == "__main__":
asyncio.run(main())