forked from SRSWTI/bodega-inference-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_llm.py
More file actions
1208 lines (1050 loc) · 48 KB
/
benchmark_llm.py
File metadata and controls
1208 lines (1050 loc) · 48 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Bodega LLM Performance Benchmark
=================================
Comprehensive language-model benchmark for the Bodega Inference Engine.
Communicates exclusively through the HTTP API at localhost:44468 — no internal
engine imports required.
Measures
TTFT Time to First Token (ms) — latency until the model starts generating
TPOT Time Per Output Token (ms) — time between each generated token
Gen TPS Output tokens/second (generation phase only, excludes first token)
Proc TPS Input tokens/second — how fast the prompt was ingested (prompt_tokens / TTFT)
Latency End-to-end wall time per request
Throughput (input + output) tokens / total wall time
System TPS Total output tokens / wall time across ALL concurrent requests
Two modes:
batched Load with continuous_batching=True, fire all requests concurrently.
This is the preferred mode for throughput benchmarks.
sequential Load with continuous_batching=False; the server queues and serialises
requests. Use --compare to run both and see the speedup.
Usage:
# Quick start — loads, benchmarks, unloads
python benchmark_llm.py --model srswti/bodega-orion-0.6b
# More prompts and tokens
python benchmark_llm.py --model srswti/bodega-raptor-0.9b --prompts 10 --max-tokens 256
# Concurrency sweep (mirrors the README tables)
python benchmark_llm.py --model srswti/bodega-raptor-0.9b --concurrencies 4,8,16,32
# CB vs sequential side-by-side comparison
python benchmark_llm.py --model srswti/bodega-raptor-8b --compare
# Use a model already loaded in the server (skip auto load/unload)
python benchmark_llm.py --model-id bodega-raptor-8b --no-manage
# Save full structured JSON results
python benchmark_llm.py --model srswti/bodega-raptor-0.9b --output results.json
# Test against a different host
python benchmark_llm.py --model srswti/bodega-raptor-0.9b --base-url http://192.168.1.10:44468
Environment:
BODEGA_SKIP_TELEMETRY=1 Skip the mactop telemetry window.
"""
from __future__ import annotations
import argparse
import asyncio
import json
import os
import platform
import shutil
import statistics
import subprocess
import sys
import time
from dataclasses import asdict, dataclass, field
from typing import Any, Optional
import httpx
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
DEFAULT_BASE_URL = "http://localhost:44468"
# 10-prompt suite: 3 short · 3 medium · 4 long )
PROMPTS: list[str] = [
# Short (~5-15 tokens) — 3 prompts
"Hello, how are you?",
"What is 2+2?",
"Say hello in Spanish.",
# Medium (~30-60 tokens) — 3 prompts
"What is the capital of France and why is it historically significant? Include some interesting facts about the city.",
"Write a Python function to calculate fibonacci numbers using memoization. Explain how it works.",
"Explain the difference between a list and a tuple in Python. When should you use each one?",
# Long (100+ tokens) — 4 prompts
(
"Explain quantum computing in comprehensive detail. Cover all of the following thoroughly:\n"
"1. What are qubits and how do they differ from classical bits?\n"
"2. What is quantum superposition and how does it enable parallel computation?\n"
"3. What is quantum entanglement and why is it crucial for quantum algorithms?\n"
"4. What are the most promising applications in cryptography, drug discovery, and optimisation?\n"
"5. What are the current hardware limitations, error correction challenges, and decoherence problems?\n"
"6. Compare the approaches of IBM, Google, and other major players in quantum computing research."
),
(
"Write a comprehensive guide to building a production-ready REST API with Python Flask. Include:\n"
"1. Project structure with blueprints, configuration management, and environment variables\n"
"2. Creating routes and endpoints following RESTful conventions with proper HTTP methods\n"
"3. Handling JSON requests and responses with validation using marshmallow or pydantic\n"
"4. Authentication and authorisation with JWT tokens and role-based access control\n"
"5. Error handling best practices with custom exception handlers\n"
"6. Writing tests with pytest — unit tests and integration tests\n"
"7. Logging, monitoring, and API documentation with Swagger/OpenAPI"
),
(
"Describe the complete process of photosynthesis with scientific detail. Cover:\n"
"1. Light-dependent reactions in the thylakoid membrane, including photosystems I and II\n"
"2. The electron transport chain and chemiosmosis for ATP synthesis\n"
"3. The Calvin cycle (light-independent reactions) and carbon fixation by RuBisCO\n"
"4. The role of chlorophyll a, chlorophyll b, and accessory pigments like carotenoids\n"
"5. How light intensity, CO2 concentration, and temperature affect the photosynthesis rate\n"
"6. The importance of photosynthesis for life on Earth and its role in the carbon cycle\n"
"7. C3, C4, and CAM photosynthesis adaptations in different plant species"
),
(
"You are a senior software architect. Design a microservices architecture for a large-scale "
"e-commerce platform handling millions of users. Include:\n"
"1. Service breakdown: User, Product catalog, Inventory, Order, Payment, Notification\n"
"2. Database choices per service with justification (PostgreSQL vs MongoDB vs Redis)\n"
"3. Inter-service communication: synchronous REST/gRPC vs asynchronous message queues\n"
"4. API gateway design with rate limiting, authentication, and request routing\n"
"5. Caching strategy with Redis for sessions, product data, and search results\n"
"6. Message queue architecture with RabbitMQ or Kafka for event-driven communication\n"
"7. Kubernetes deployment with horizontal pod autoscaling and rolling updates\n"
"8. CI/CD pipeline with GitHub Actions, testing stages, and blue-green deployments"
),
]
# ---------------------------------------------------------------------------
# Resource metrics
# ---------------------------------------------------------------------------
@dataclass
class ResourceMetrics:
"""Hardware/memory snapshot during a benchmark run.
Fields map to what the Bodega engine exposes via GET /v1/admin/loaded-models
(metal_* fields) and mactop --headless (power/temp/system-RAM fields).
The names are kept compatible with the ResourceMetrics dataclass
so JSON output is directly comparable.
"""
# From GET /v1/admin/loaded-models (per-model Metal Unified Memory)
process_memory_gb: float = 0.0 # model subprocess RSS
mlx_active_memory_gb: float = 0.0 # metal_active_mb (in-flight compute)
mlx_cache_gb: float = 0.0 # metal_cache_mb (retained KV cache)
mlx_peak_memory_gb: float = 0.0 # metal_peak_mb (high-water mark)
# From mactop --headless (system-level)
system_memory_used_gb: float = 0.0
system_memory_total_gb: float = 0.0
gpu_power_w: float = 0.0
cpu_power_w: float = 0.0
system_power_w: float = 0.0
gpu_temp_c: float = 0.0
class ResourceMonitor:
"""Poll the Bodega HTTP API and mactop during benchmark runs.
Mirrors the interface ofResourceMonitor so callers work the
same way, but all data comes from HTTP rather than in-process mlx calls.
"""
def __init__(self, client: httpx.AsyncClient, base_url: str, model_id: str):
self._client = client
self._base_url = base_url.rstrip("/")
self._model_id = model_id
self.samples: list[ResourceMetrics] = []
async def sample(self) -> ResourceMetrics:
metrics = ResourceMetrics()
# -- Metal memory from /v1/admin/loaded-models ---------------------
try:
resp = await self._client.get(
f"{self._base_url}/v1/admin/loaded-models", timeout=5.0
)
if resp.status_code == 200:
data = resp.json().get("data", [])
for m in data:
if m.get("id") == self._model_id:
mem = m.get("memory", {})
metrics.process_memory_gb = mem.get("rss_mb", 0) / 1024
metrics.mlx_active_memory_gb = mem.get("metal_active_mb", 0) / 1024
metrics.mlx_cache_gb = mem.get("metal_cache_mb", 0) / 1024
metrics.mlx_peak_memory_gb = mem.get("metal_peak_mb", 0) / 1024
break
except Exception:
pass
# -- System RAM + power from mactop --------------------------------
try:
res = subprocess.run(
["mactop", "--headless", "--count", "1"],
capture_output=True, text=True, timeout=3.0,
)
if res.returncode == 0 and res.stdout.strip():
mdata = json.loads(res.stdout)
if isinstance(mdata, list) and mdata:
sm = mdata[0].get("soc_metrics", {})
mem = mdata[0].get("memory", {})
metrics.gpu_power_w = sm.get("gpu_power", 0.0)
metrics.cpu_power_w = sm.get("cpu_power", 0.0)
metrics.system_power_w = sm.get("system_power", 0.0)
metrics.gpu_temp_c = sm.get("gpu_temp", 0.0)
metrics.system_memory_used_gb = mem.get("used", 0) / (1024 ** 3)
metrics.system_memory_total_gb = mem.get("total", 0) / (1024 ** 3)
except Exception:
pass
self.samples.append(metrics)
return metrics
def get_summary(self) -> ResourceMetrics:
if not self.samples:
return ResourceMetrics()
peak = ResourceMetrics(
process_memory_gb=max(s.process_memory_gb for s in self.samples),
mlx_active_memory_gb=max(s.mlx_active_memory_gb for s in self.samples),
mlx_cache_gb=max(s.mlx_cache_gb for s in self.samples),
mlx_peak_memory_gb=max(s.mlx_peak_memory_gb for s in self.samples),
gpu_power_w=max(s.gpu_power_w for s in self.samples),
cpu_power_w=max(s.cpu_power_w for s in self.samples),
system_power_w=max(s.system_power_w for s in self.samples),
gpu_temp_c=max(s.gpu_temp_c for s in self.samples),
)
latest = self.samples[-1]
peak.system_memory_used_gb = latest.system_memory_used_gb
peak.system_memory_total_gb = latest.system_memory_total_gb
return peak
# ---------------------------------------------------------------------------
# Per-request result
# ---------------------------------------------------------------------------
@dataclass
class BenchmarkResult:
"""Metrics for a single request.
Derived fields are computed automatically in __post_init__ using the same
arithmetic — TPOT and generation_tps exclude the
first token (already captured by TTFT), and processing_tps measures prompt
ingestion speed.
"""
prompt: str
prompt_tokens: int
generated_tokens: int
# Primary timing (seconds)
ttft: float # Time to First Token
total_time: float # End-to-end latency
# Derived (filled by __post_init__)
tpot: float = 0.0 # Time Per Output Token (s)
generation_tps: float = 0.0 # Output tok/s (excludes first token)
processing_tps: float = 0.0 # Prompt tok/s = prompt_tokens / TTFT
error: Optional[str] = None
def __post_init__(self) -> None:
if self.error:
return
if self.generated_tokens > 1:
# Exclude the first token from generation speed — it is already
# captured by TTFT.
generation_time = self.total_time - self.ttft
self.tpot = (
generation_time / (self.generated_tokens - 1)
if self.generated_tokens > 1 else 0.0
)
self.generation_tps = (
(self.generated_tokens - 1) / generation_time
if generation_time > 0 else 0.0
)
if self.ttft > 0:
self.processing_tps = self.prompt_tokens / self.ttft
# ---------------------------------------------------------------------------
# Aggregate summary
# ---------------------------------------------------------------------------
@dataclass
class BenchmarkSummary:
"""Aggregate statistics across all requests in a benchmark run."""
model_name: str
mode: str # "batched" | "sequential"
concurrency: int
num_runs: int
total_prompt_tokens: int
total_generated_tokens: int
total_time: float # wall time for the entire batch
# TTFT distribution (seconds)
ttft_mean: float
ttft_min: float
ttft_max: float
ttft_p50: float
ttft_p95: float
# TPOT distribution (seconds)
tpot_mean: float
tpot_min: float
tpot_max: float
# Token throughput
generation_tps_mean: float # mean per-request generation speed
generation_tps_max: float
processing_tps_mean: float # mean prompt ingestion speed
# End-to-end latency distribution (seconds)
latency_mean: float
latency_min: float
latency_max: float
latency_p50: float
latency_p95: float
# System-level throughput
total_throughput_tps: float # (input + output) tok / wall time
system_throughput_tps: float # output tok / wall time (CB key metric)
requests_per_second: float
# Hardware (populated from mactop / platform at run time)
hardware_chip: str = ""
hardware_memory_gb: float = 0.0
# Resource snapshot
resources: ResourceMetrics = field(default_factory=ResourceMetrics)
# Raw per-request results (for per-run breakdown table)
results: list[BenchmarkResult] = field(default_factory=list)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def calculate_percentile(data: list[float], percentile: float) -> float:
"""Percentile helper"""
if not data:
return 0.0
sorted_data = sorted(data)
index = int(len(sorted_data) * percentile / 100)
index = min(index, len(sorted_data) - 1)
return sorted_data[index]
def build_summary(
model_name: str,
mode: str,
concurrency: int,
results: list[BenchmarkResult],
wall_time: float,
resources: ResourceMetrics,
hardware_chip: str = "",
hardware_memory_gb: float = 0.0,
) -> BenchmarkSummary:
"""Aggregate a list of BenchmarkResult into a BenchmarkSummary."""
ok = [r for r in results if r.error is None]
if not ok:
return BenchmarkSummary(
model_name=model_name, mode=mode, concurrency=concurrency,
num_runs=0, total_prompt_tokens=0, total_generated_tokens=0,
total_time=wall_time, ttft_mean=0, ttft_min=0, ttft_max=0,
ttft_p50=0, ttft_p95=0, tpot_mean=0, tpot_min=0, tpot_max=0,
generation_tps_mean=0, generation_tps_max=0, processing_tps_mean=0,
latency_mean=0, latency_min=0, latency_max=0, latency_p50=0,
latency_p95=0, total_throughput_tps=0, system_throughput_tps=0,
requests_per_second=0, hardware_chip=hardware_chip,
hardware_memory_gb=hardware_memory_gb, resources=resources,
results=results,
)
ttfts = [r.ttft for r in ok]
tpots = [r.tpot for r in ok if r.tpot > 0]
latencies = [r.total_time for r in ok]
gen_tps = [r.generation_tps for r in ok if r.generation_tps > 0]
proc_tps = [r.processing_tps for r in ok if r.processing_tps > 0]
total_prompt = sum(r.prompt_tokens for r in ok)
total_gen = sum(r.generated_tokens for r in ok)
return BenchmarkSummary(
model_name=model_name,
mode=mode,
concurrency=concurrency,
num_runs=len(ok),
total_prompt_tokens=total_prompt,
total_generated_tokens=total_gen,
total_time=wall_time,
# TTFT
ttft_mean=statistics.mean(ttfts),
ttft_min=min(ttfts),
ttft_max=max(ttfts),
ttft_p50=calculate_percentile(ttfts, 50),
ttft_p95=calculate_percentile(ttfts, 95),
# TPOT
tpot_mean=statistics.mean(tpots) if tpots else 0.0,
tpot_min=min(tpots) if tpots else 0.0,
tpot_max=max(tpots) if tpots else 0.0,
# TPS
generation_tps_mean=statistics.mean(gen_tps) if gen_tps else 0.0,
generation_tps_max=max(gen_tps) if gen_tps else 0.0,
processing_tps_mean=statistics.mean(proc_tps) if proc_tps else 0.0,
# Latency
latency_mean=statistics.mean(latencies),
latency_min=min(latencies),
latency_max=max(latencies),
latency_p50=calculate_percentile(latencies, 50),
latency_p95=calculate_percentile(latencies, 95),
# Throughput
total_throughput_tps=(total_prompt + total_gen) / wall_time if wall_time > 0 else 0,
system_throughput_tps=total_gen / wall_time if wall_time > 0 else 0,
requests_per_second=len(ok) / wall_time if wall_time > 0 else 0,
hardware_chip=hardware_chip,
hardware_memory_gb=hardware_memory_gb,
resources=resources,
results=results,
)
# ---------------------------------------------------------------------------
# Hardware detection
# ---------------------------------------------------------------------------
def _detect_hardware_from_mactop() -> tuple[str, float]:
"""Return (chip_name, total_memory_gb) from mactop --headless."""
try:
res = subprocess.run(
["mactop", "--headless", "--count", "1"],
capture_output=True, text=True, timeout=4.0,
)
if res.returncode == 0 and res.stdout.strip():
data = json.loads(res.stdout)
if isinstance(data, list) and data:
sm = data[0].get("soc_metrics", {})
mem = data[0].get("memory", {})
chip = sm.get("chip_name", "")
total_gb = mem.get("total", 0) / (1024 ** 3)
return chip, total_gb
except Exception:
pass
return platform.processor() or "Apple Silicon", 0.0
# ---------------------------------------------------------------------------
# Model lifecycle via admin API
# ---------------------------------------------------------------------------
class IncompatibleModelError(Exception):
pass
def _model_type_from_config(model_path: str) -> str:
"""Detect model_type from config.json (lm | multimodal | whisper | embeddings)."""
from detect_model_type import detect_model_type
return detect_model_type(model_path)
async def _check_mlx_tag(client: httpx.AsyncClient, model_path: str) -> None:
"""Verify model has MLX tag on HuggingFace. Raises IncompatibleModelError if not."""
if "/" not in model_path:
return
try:
resp = await client.get(
f"https://huggingface.co/api/models/{model_path}", timeout=8.0
)
if resp.status_code == 200:
tags = [t.lower() for t in resp.json().get("tags", [])]
if not any("mlx" in t for t in tags):
raise IncompatibleModelError(
f"'{model_path}' has no MLX tag on HuggingFace — only MLX-format "
"models are compatible with the Bodega Inference Engine."
)
except IncompatibleModelError:
raise
except Exception:
pass
async def load_model(
client: httpx.AsyncClient,
base_url: str,
model_path: str,
model_id: str,
continuous_batching: bool,
cb_max_num_seqs: int,
cb_prefill_batch_size: int,
cb_completion_batch_size: int,
cb_chunked_prefill_tokens: int,
context_length: int,
) -> bool:
url = f"{base_url}/v1/admin/load-model"
try:
await _check_mlx_tag(client, model_path)
except IncompatibleModelError as exc:
print(f"\n ✗ {exc}")
return False
model_type = _model_type_from_config(model_path)
payload: dict[str, Any] = {
"model_path": model_path,
"model_id": model_id,
"model_type": model_type,
"context_length": context_length,
}
if continuous_batching:
payload.update(
continuous_batching=True,
cb_max_num_seqs=cb_max_num_seqs,
cb_prefill_batch_size=cb_prefill_batch_size,
cb_completion_batch_size=cb_completion_batch_size,
cb_chunked_prefill_tokens=cb_chunked_prefill_tokens,
cb_enable_prefix_cache=False,
)
resp = await client.post(url, json=payload, timeout=180.0)
if resp.status_code == 409:
print(" (already loaded)", flush=True)
return True
if resp.status_code in (200, 201):
print(f" (loaded as {model_type})", flush=True)
return True
try:
msg = resp.json().get("error", {}).get("message", resp.text[:120])
except Exception:
msg = resp.text[:120]
print(f" ✗ load failed ({resp.status_code}): {msg}")
return False
async def unload_model(
client: httpx.AsyncClient, base_url: str, model_id: str
) -> None:
try:
await client.delete(
f"{base_url}/v1/admin/unload-model/{model_id}", timeout=30.0
)
except Exception:
pass
# ---------------------------------------------------------------------------
# Single streaming request
# ---------------------------------------------------------------------------
async def _stream_one(
client: httpx.AsyncClient,
base_url: str,
model_id: str,
prompt: str,
max_tokens: int,
temperature: float,
) -> BenchmarkResult:
"""Stream a single chat completion and measure TTFT/total_time precisely."""
url = f"{base_url}/v1/chat/completions"
payload = {
"model": model_id,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": max_tokens,
"temperature": temperature,
"stream": True,
# Request usage in the final chunk — honoured by Bodega and newer
# OpenAI-compatible servers (LM Studio, etc.). Ignored if unsupported.
"stream_options": {"include_usage": True},
}
t0 = time.perf_counter()
ttft: Optional[float] = None
prompt_tokens = 0
completion_tokens = 0
truncated_prompt = prompt[:50] + "..." if len(prompt) > 50 else prompt
try:
async with client.stream("POST", url, json=payload, timeout=300.0) as resp:
if resp.status_code != 200:
body = await resp.aread()
return BenchmarkResult(
prompt=truncated_prompt, prompt_tokens=0, generated_tokens=0,
ttft=0.0, total_time=time.perf_counter() - t0,
error=f"HTTP {resp.status_code}: {body.decode()[:60]}",
)
buffer = ""
async for chunk in resp.aiter_text():
buffer += chunk
while "\n" in buffer:
line, buffer = buffer.split("\n", 1)
line = line.strip()
if not line or not line.startswith("data: "):
continue
data_str = line[6:]
if data_str == "[DONE]":
break
try:
data = json.loads(data_str)
except json.JSONDecodeError:
continue
choices = data.get("choices", [])
if choices:
delta = choices[0].get("delta", {})
# Accept both standard content and reasoning_content
# (reasoning models like bodega-raptor emit the latter).
token_text = delta.get("content") or delta.get("reasoning_content")
if token_text:
if ttft is None:
ttft = time.perf_counter() - t0
completion_tokens += 1
# Usage chunk — present in Bodega; present in LM Studio only
# when stream_options.include_usage=true is sent.
usage = data.get("usage")
if usage:
pt = usage.get("prompt_tokens")
ct = usage.get("completion_tokens")
if pt is not None and pt > 0:
prompt_tokens = pt
# Prefer server count over our increment counter
if ct is not None and ct > 0:
completion_tokens = ct
total_time = time.perf_counter() - t0
if ttft is None:
ttft = total_time
return BenchmarkResult(
prompt=truncated_prompt,
prompt_tokens=prompt_tokens,
generated_tokens=completion_tokens,
ttft=ttft,
total_time=total_time,
)
except Exception as exc:
return BenchmarkResult(
prompt=truncated_prompt, prompt_tokens=0, generated_tokens=0,
ttft=0.0, total_time=time.perf_counter() - t0,
error=str(exc)[:80],
)
# ---------------------------------------------------------------------------
# Benchmark runners
# ---------------------------------------------------------------------------
async def run_benchmark(
base_url: str,
model_path: str,
model_id: str,
prompts: list[str],
concurrency: int,
max_tokens: int,
temperature: float,
warmup_runs: int,
continuous_batching: bool,
manage_model_lifecycle: bool,
cb_max_num_seqs: int,
cb_prefill_batch_size: int,
cb_completion_batch_size: int,
cb_chunked_prefill_tokens: int,
context_length: int,
) -> BenchmarkSummary:
mode = "batched" if continuous_batching else "sequential"
chip, mem_gb = _detect_hardware_from_mactop()
sep = "─" * 64
print(f"\n{sep}")
print(f" [{mode.upper()}] {model_path or model_id}")
print(f" concurrency={concurrency} max_tokens={max_tokens} prompts={len(prompts)}")
print(sep)
async with httpx.AsyncClient() as client:
# ── health check ───────────────────────────────────────────────────
try:
hc = await client.get(f"{base_url}/health", timeout=5.0)
if hc.status_code != 200:
print(" ⚠ Health check returned non-200; proceeding anyway.")
except Exception as exc:
print(f" ✗ Cannot reach server at {base_url}: {exc}")
sys.exit(1)
# ── model load ─────────────────────────────────────────────────────
if manage_model_lifecycle:
print(" Loading model via API...", end=" ", flush=True)
t_load = time.perf_counter()
ok = await load_model(
client, base_url, model_path, model_id, continuous_batching,
cb_max_num_seqs, cb_prefill_batch_size, cb_completion_batch_size,
cb_chunked_prefill_tokens, context_length,
)
if not ok:
return BenchmarkSummary(
model_name=model_path, mode=mode, concurrency=concurrency,
num_runs=0, total_prompt_tokens=0, total_generated_tokens=0,
total_time=0, ttft_mean=0, ttft_min=0, ttft_max=0,
ttft_p50=0, ttft_p95=0, tpot_mean=0, tpot_min=0, tpot_max=0,
generation_tps_mean=0, generation_tps_max=0, processing_tps_mean=0,
latency_mean=0, latency_min=0, latency_max=0, latency_p50=0,
latency_p95=0, total_throughput_tps=0, system_throughput_tps=0,
requests_per_second=0, hardware_chip=chip,
hardware_memory_gb=mem_gb,
)
print(f" Model load time: {time.perf_counter()-t_load:.1f}s")
monitor = ResourceMonitor(client, base_url, model_id)
# ── warmup ─────────────────────────────────────────────────────────
if warmup_runs > 0:
print(f" Warmup ({warmup_runs} run(s))...", end=" ", flush=True)
for _ in range(warmup_runs):
await _stream_one(
client, base_url, model_id, "Hello, how are you?",
max_tokens=20, temperature=temperature,
)
await monitor.sample()
print("done")
# ── build prompt list ──────────────────────────────────────────────
test_prompts = (prompts * ((concurrency // len(prompts)) + 1))[:concurrency]
print(f"\n Prompt distribution ({len(test_prompts)} requests):")
short_n = sum(1 for p in test_prompts if len(p) < 100)
medium_n = sum(1 for p in test_prompts if 100 <= len(p) < 500)
long_n = sum(1 for p in test_prompts if len(p) >= 500)
print(f" Short (<100 chars): {short_n}")
print(f" Medium (100-500): {medium_n}")
print(f" Long (500+): {long_n}")
print()
# ── concurrent requests ────────────────────────────────────────────
print(f" Firing {len(test_prompts)} concurrent request(s)...", flush=True)
t_start = time.perf_counter()
raw_results = await asyncio.gather(
*[
_stream_one(client, base_url, model_id, p, max_tokens, temperature)
for p in test_prompts
]
)
wall_time = time.perf_counter() - t_start
# ── resource snapshot ──────────────────────────────────────────────
await monitor.sample()
# ── mactop live telemetry line ─────────────────────────────────────
_try_print_mactop_line()
# ── unload model ───────────────────────────────────────────────────
if manage_model_lifecycle:
print(" Unloading model...", end=" ", flush=True)
await unload_model(client, base_url, model_id)
print("done.")
return build_summary(
model_name=model_path or model_id,
mode=mode,
concurrency=concurrency,
results=list(raw_results),
wall_time=wall_time,
resources=monitor.get_summary(),
hardware_chip=chip,
hardware_memory_gb=mem_gb,
)
# ---------------------------------------------------------------------------
# Mactop telemetry helper
# ---------------------------------------------------------------------------
def _try_print_mactop_line() -> None:
if os.environ.get("BODEGA_SKIP_TELEMETRY") == "1":
return
if not shutil.which("mactop"):
return
try:
res = subprocess.run(
["mactop", "--headless", "--count", "1"],
capture_output=True, text=True, timeout=3.0,
)
if res.returncode == 0 and res.stdout.strip():
data = json.loads(res.stdout)
if isinstance(data, list) and data:
sm = data[0].get("soc_metrics", {})
mem = data[0].get("memory", {})
ram_used = mem.get("used", 0) / (1024 ** 3)
ram_tot = mem.get("total", 0) / (1024 ** 3)
print(
f" [Telemetry] RAM {ram_used:.1f}/{ram_tot:.0f} GB "
f"| Pwr {sm.get('system_power',0):.1f}W "
f"(CPU {sm.get('cpu_power',0):.1f}W "
f"GPU {sm.get('gpu_power',0):.1f}W) "
f"| GPU {sm.get('gpu_temp',0):.0f}°C"
)
except Exception:
pass
def open_mactop_window() -> None:
if os.environ.get("BODEGA_SKIP_TELEMETRY") == "1":
return
if not shutil.which("mactop"):
return
script = '''tell application "Terminal"
do script "mactop"
activate
end tell'''
os.system(f"osascript -e '{script}' >/dev/null 2>&1")
# ---------------------------------------------------------------------------
# Print functions
# ---------------------------------------------------------------------------
def print_summary(s: BenchmarkSummary) -> None:
sep = "═" * 66
ok = [r for r in s.results if r.error is None]
err = [r for r in s.results if r.error is not None]
print(f"\n{sep}")
print(" BENCHMARK RESULTS")
print(sep)
# Overview
rows = [
("Model", s.model_name),
("Mode", f"{s.mode.upper()} (concurrency={s.concurrency})"),
("Hardware", f"{s.hardware_chip} ({s.hardware_memory_gb:.0f} GB)" if s.hardware_chip else "—"),
("Requests", f"{len(ok)}/{s.num_runs + len(err)} succeeded ({len(err)} failed)"),
("Input tokens", f"{s.total_prompt_tokens:,}"),
("Output tokens", f"{s.total_generated_tokens:,}"),
("Wall time", f"{s.total_time:.2f}s"),
]
for label, val in rows:
print(f" {label:<20} {val}")
if not ok:
print(sep)
return
# Performance metrics
print(f"\n {'─'*62}")
print(" Performance Metrics")
print(f" {'─'*62}")
perf_rows = [
("TTFT mean", f"{s.ttft_mean*1000:.1f} ms",
f"p50={s.ttft_p50*1000:.1f}ms p95={s.ttft_p95*1000:.1f}ms"),
("TTFT range", f"{s.ttft_min*1000:.1f}ms → {s.ttft_max*1000:.1f}ms", ""),
("TPOT mean", f"{s.tpot_mean*1000:.2f} ms/tok",
f"max={s.tpot_max*1000:.2f}ms"),
("Gen TPS mean", f"{s.generation_tps_mean:.1f} tok/s",
f"max={s.generation_tps_max:.1f} tok/s"),
("Proc TPS mean", f"{s.processing_tps_mean:.1f} tok/s", "input ingestion"),
("Latency mean", f"{s.latency_mean:.3f}s",
f"p50={s.latency_p50:.3f}s p95={s.latency_p95:.3f}s"),
]
for label, val, note in perf_rows:
note_str = f" ← {note}" if note else ""
print(f" {label:<22} {val:<22}{note_str}")
print(f"\n {'─'*62}")
print(" Throughput")
print(f" {'─'*62}")
print(f" {'System TPS':<22} {s.system_throughput_tps:.1f} tok/s ← output tokens / wall time")
print(f" {'Total TPS':<22} {s.total_throughput_tps:.1f} tok/s ← (input+output) / wall time")
print(f" {'Req/sec':<22} {s.requests_per_second:.2f}")
# Resource usage
res = s.resources
if res.mlx_peak_memory_gb > 0 or res.system_memory_total_gb > 0:
print(f"\n {'─'*62}")
print(" Resource Usage")
print(f" {'─'*62}")
if res.mlx_peak_memory_gb > 0:
print(f" {'Metal peak':<22} {res.mlx_peak_memory_gb:.2f} GB")
if res.mlx_active_memory_gb > 0:
print(f" {'Metal active':<22} {res.mlx_active_memory_gb:.2f} GB")
if res.mlx_cache_gb > 0:
print(f" {'Metal cache (KV)':<22} {res.mlx_cache_gb:.2f} GB")
if res.process_memory_gb > 0:
print(f" {'Process RSS':<22} {res.process_memory_gb:.2f} GB")
if res.system_memory_total_gb > 0:
pct = (res.system_memory_used_gb / res.system_memory_total_gb) * 100
print(f" {'System RAM':<22} {res.system_memory_used_gb:.1f} / {res.system_memory_total_gb:.0f} GB ({pct:.0f}%)")
if res.system_power_w > 0:
print(f" {'Power (peak)':<22} {res.system_power_w:.1f}W (CPU {res.cpu_power_w:.1f}W GPU {res.gpu_power_w:.1f}W {res.gpu_temp_c:.0f}°C)")
# Per-request breakdown
print(f"\n {'─'*62}")
print(" Per-Request Breakdown")
print(f" {'─'*62}")
print(f" {'#':<4} {'In':>6} {'Out':>6} {'TTFT':>10} {'TPOT':>10} {'Gen TPS':>9} Status")
for i, r in enumerate(s.results, 1):
if r.error:
print(f" {i:<4} {'—':>6} {'—':>6} {'—':>10} {'—':>10} {'—':>9} ✗ {r.error[:36]}")
else:
print(
f" {i:<4} {r.prompt_tokens:>6} {r.generated_tokens:>6} "
f"{r.ttft*1000:>9.1f}ms {r.tpot*1000:>9.2f}ms "
f"{r.generation_tps:>8.1f} ✓"
)
print(sep)
def print_comparison(batched: BenchmarkSummary, sequential: BenchmarkSummary) -> None:
sep = "═" * 66
bt = batched.total_time or 1
st = sequential.total_time or 1
wall_ratio = st / bt
tp_ratio = (batched.system_throughput_tps / sequential.system_throughput_tps
if sequential.system_throughput_tps > 0 else 0)
print(f"\n{sep}")
print(" BATCHED vs SEQUENTIAL — COMPARISON")
print(sep)
print(f" {'Metric':<32} {'Sequential':>14} {'Batched (CB)':>14}")
print(f" {'─'*60}")
def row(label: str, seq_val: str, bat_val: str) -> None:
print(f" {label:<32} {seq_val:>14} {bat_val:>14}")
row("Wall time",
f"{sequential.total_time:.2f}s",
f"{batched.total_time:.2f}s")
row("Mean TTFT",
f"{sequential.ttft_mean*1000:.0f}ms",
f"{batched.ttft_mean*1000:.0f}ms")
row("P95 TTFT",
f"{sequential.ttft_p95*1000:.0f}ms",
f"{batched.ttft_p95*1000:.0f}ms")
row("Mean TPOT",
f"{sequential.tpot_mean*1000:.2f}ms",
f"{batched.tpot_mean*1000:.2f}ms")
row("Mean Gen TPS (per-req)",
f"{sequential.generation_tps_mean:.1f} tok/s",
f"{batched.generation_tps_mean:.1f} tok/s")
row("System throughput",
f"{sequential.system_throughput_tps:.1f} tok/s",
f"{batched.system_throughput_tps:.1f} tok/s")
print(f" {'─'*60}")
faster = "faster" if wall_ratio >= 1 else "slower"
print(f" Wall time speedup: {wall_ratio:.2f}x (CB is {faster})")
print(f" Throughput gain: {tp_ratio:.2f}x")
print(sep)
def print_concurrency_table(summaries: list[BenchmarkSummary]) -> None:
sep = "═" * 80
print(f"\n{sep}")
print(" CONTINUOUS BATCHING — CONCURRENCY SWEEP")
print(f" {summaries[0].model_name}")
print(sep)
print(
f" {'Concurrency':<14} {'Wall Time':>12} {'Sys TPS':>12} "
f"{'Mean TTFT':>12} {'P95 TTFT':>10} {'Req/s':>8}"
)
print(f" {'─'*76}")
for s in summaries:
print(
f" {s.concurrency:<14} {s.total_time:>11.2f}s "
f"{s.system_throughput_tps:>11.1f} "
f"{s.ttft_mean*1000:>11.0f}ms "
f"{s.ttft_p95*1000:>9.0f}ms "
f"{s.requests_per_second:>7.2f}"
)
if len(summaries) >= 2:
base = summaries[0]
print(f"\n Throughput scaling vs concurrency={base.concurrency}:")
for s in summaries[1:]:
gain = (s.system_throughput_tps / base.system_throughput_tps
if base.system_throughput_tps > 0 else 0)
print(f" concurrency {s.concurrency:>3}: {gain:.2f}x throughput")
print(sep)
# ---------------------------------------------------------------------------
# JSON serialisation
# ---------------------------------------------------------------------------
def _metrics_to_dict(r: ResourceMetrics) -> dict[str, Any]:
return {
"process_memory_gb": r.process_memory_gb,
"mlx_active_memory_gb": r.mlx_active_memory_gb,
"mlx_cache_gb": r.mlx_cache_gb,
"mlx_peak_memory_gb": r.mlx_peak_memory_gb,
"system_memory_used_gb": r.system_memory_used_gb,
"system_memory_total_gb":r.system_memory_total_gb,
"gpu_power_w": r.gpu_power_w,
"cpu_power_w": r.cpu_power_w,
"system_power_w": r.system_power_w,
"gpu_temp_c": r.gpu_temp_c,
}
def _result_to_dict(r: BenchmarkResult) -> dict[str, Any]:
return {
"prompt": r.prompt,
"prompt_tokens": r.prompt_tokens,
"generated_tokens":r.generated_tokens,
"ttft_ms": r.ttft * 1000,
"total_time_s": r.total_time,