-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_test_engine.py
More file actions
520 lines (467 loc) · 19.3 KB
/
Copy pathload_test_engine.py
File metadata and controls
520 lines (467 loc) · 19.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
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
#!/usr/bin/env python3
"""LLMStorm concurrent load-test engine.
The engine deliberately keeps API keys in memory only. It supports the two
dominant relay protocols (OpenAI Chat Completions and Anthropic Messages) plus
Gemini's native streaming endpoint.
"""
from __future__ import annotations
import asyncio
import ipaddress
import json
import math
import socket
import ssl
import time
from collections import Counter
from collections.abc import Awaitable, Callable
from dataclasses import asdict, dataclass
from typing import Any
from urllib.parse import urlsplit
import aiohttp
from aiohttp.abc import AbstractResolver, ResolveResult
from llmstorm import __version__
from llmstorm.policy import (
DEFAULT_LOCAL_MAX_CONCURRENCY,
DEFAULT_RAMP_RATIOS,
DEFAULT_SUCCESS_THRESHOLD,
MAX_CAPTURED_OUTPUT_CHARS,
MAX_OUTPUT_TOKENS,
MAX_UPSTREAM_ERROR_BYTES,
)
from llmstorm.providers import SUPPORTED_PROVIDERS, RequestContext, get_adapter
ProgressCallback = Callable[["RequestResult"], Awaitable[None]]
@dataclass(slots=True)
class TestConfig:
url: str
provider: str
api_key: str
model: str
max_concurrency: int
timeout: float = 60.0
max_tokens: int = 64
prompt: str = "请用一句话说明并发测试的意义。"
insecure: bool = False
locale: str = "zh"
max_concurrency_limit: int = DEFAULT_LOCAL_MAX_CONCURRENCY
allow_private_targets: bool = True
allow_insecure: bool = True
success_threshold: float = DEFAULT_SUCCESS_THRESHOLD
ramp_ratios: tuple[float, ...] = DEFAULT_RAMP_RATIOS
@dataclass(slots=True)
class RequestResult:
index: int
success: bool
status: int | str
latency: float
ttft: float | None
output_chars: int
error: str | None = None
preview: str = ""
input_tokens: int = 0
output_tokens: int = 0
cache_read_tokens: int = 0
cache_write_tokens: int = 0
MESSAGES = {
"zh": {
"invalid_url": "请输入有效的 HTTP/HTTPS 接口地址",
"https_required": "公网模式仅允许访问 HTTPS 接口",
"url_credentials": "接口地址不能包含用户名或密码",
"unsupported_provider": "不支持的大模型协议",
"select_model": "请选择模型",
"concurrency_range": "最大并发数必须在 1 到 {maximum} 之间",
"timeout_range": "超时时间必须在 5 到 300 秒之间",
"token_range": "最大输出 Token 必须在 1 到 {maximum} 之间",
"insecure_disabled": "公网模式不允许跳过 HTTPS 证书校验",
"private_target": "公网模式禁止访问私有、回环、链路本地或保留地址",
"request_suffix": "测试编号:{index}。请直接回答,不要复述编号。",
"unknown_upstream": "上游返回未知错误",
"empty_output": "响应成功,但没有解析到模型输出;请检查厂商协议和 URL 是否匹配",
"timeout": "请求超过 {timeout} 秒未完成",
"recommendation": "测试阶段成功率不低于 {threshold}% 的最高并发档位",
},
"en": {
"invalid_url": "Enter a valid HTTP or HTTPS endpoint URL",
"https_required": "Public mode only allows HTTPS upstream endpoints",
"url_credentials": "The endpoint URL must not contain a username or password",
"unsupported_provider": "Unsupported model provider or protocol",
"select_model": "Select or enter a model ID",
"concurrency_range": "Maximum concurrency must be between 1 and {maximum}",
"timeout_range": "Timeout must be between 5 and 300 seconds",
"token_range": "Maximum output tokens must be between 1 and {maximum}",
"insecure_disabled": "HTTPS certificate verification cannot be disabled in public mode",
"private_target": "Public mode blocks private, loopback, link-local, and reserved targets",
"request_suffix": (
"Test request {index}. Answer directly without repeating the request number."
),
"unknown_upstream": "The upstream returned an unknown error",
"empty_output": (
"The request succeeded, but no model output was parsed. "
"Check that the provider protocol matches the URL."
),
"timeout": "The request did not complete within {timeout} seconds",
"recommendation": (
"Highest tested concurrency stage with a success rate of at least {threshold}%"
),
},
}
def message(config: TestConfig, key: str, **values: Any) -> str:
locale = config.locale if config.locale in MESSAGES else "zh"
return MESSAGES[locale][key].format(**values)
class PublicTargetResolver(AbstractResolver):
"""Resolve hosts while rejecting private and special-purpose addresses."""
def __init__(self) -> None:
self._resolver = aiohttp.DefaultResolver()
async def resolve(
self,
host: str,
port: int = 0,
family: socket.AddressFamily = socket.AF_INET,
) -> list[ResolveResult]:
results = await self._resolver.resolve(host, port, family)
for result in results:
address = ipaddress.ip_address(str(result["host"]).split("%", 1)[0])
if not address.is_global:
raise OSError(
"Public mode blocks private, loopback, link-local, and reserved targets"
)
return results
async def close(self) -> None:
await self._resolver.close()
def percentile(values: list[float], p: float) -> float | None:
if not values:
return None
ordered = sorted(values)
position = (len(ordered) - 1) * p
lower = math.floor(position)
upper = math.ceil(position)
if lower == upper:
return ordered[lower]
weight = position - lower
return ordered[lower] * (1 - weight) + ordered[upper] * weight
def concurrency_levels(
max_concurrency: int,
ratios: tuple[float, ...] = DEFAULT_RAMP_RATIOS,
) -> list[int]:
"""Create a short, increasing ramp that always includes 1 and the maximum."""
if max_concurrency == 1:
return [1]
candidates = {1, max_concurrency}
candidates.update(max(1, math.ceil(max_concurrency * ratio)) for ratio in ratios)
return sorted(candidates)
def resolve_endpoint(raw_url: str, provider: str, model: str) -> str:
"""Accept either a full endpoint or a conventional API/base URL."""
return get_adapter(provider).resolve_endpoint(raw_url, model)
def validate_config(config: TestConfig) -> None:
parsed = urlsplit(config.url)
if parsed.scheme not in {"http", "https"} or not parsed.netloc:
raise ValueError(message(config, "invalid_url"))
if not config.allow_private_targets and parsed.scheme != "https":
raise ValueError(message(config, "https_required"))
if parsed.username or parsed.password:
raise ValueError(message(config, "url_credentials"))
if not config.allow_private_targets and parsed.hostname:
try:
literal_address = ipaddress.ip_address(parsed.hostname.split("%", 1)[0])
except ValueError:
literal_address = None
if literal_address is not None and not literal_address.is_global:
raise ValueError(message(config, "private_target"))
if config.provider not in SUPPORTED_PROVIDERS:
raise ValueError(message(config, "unsupported_provider"))
if not config.model.strip():
raise ValueError(message(config, "select_model"))
if not 1 <= config.max_concurrency <= config.max_concurrency_limit:
raise ValueError(message(config, "concurrency_range", maximum=config.max_concurrency_limit))
if not 5 <= config.timeout <= 300:
raise ValueError(message(config, "timeout_range"))
if not 1 <= config.max_tokens <= MAX_OUTPUT_TOKENS:
raise ValueError(message(config, "token_range", maximum=MAX_OUTPUT_TOKENS))
if not 0 < config.success_threshold <= 100:
raise ValueError("Success threshold must be between 0 and 100")
if any(not 0 < ratio < 1 for ratio in config.ramp_ratios):
raise ValueError("Ramp ratios must be between 0 and 1")
if config.insecure and not config.allow_insecure:
raise ValueError(message(config, "insecure_disabled"))
def request_spec(config: TestConfig, index: int) -> tuple[dict[str, str], dict[str, Any]]:
prompt = f"{config.prompt}\n{message(config, 'request_suffix', index=index)}"
return get_adapter(config.provider).build_request(
RequestContext(
api_key=config.api_key,
model=config.model,
prompt=prompt,
max_tokens=config.max_tokens,
user_agent=f"LLMStorm/{__version__}",
)
)
def extract_stream_text(provider: str, chunk: dict[str, Any]) -> str:
return get_adapter(provider).extract_text(chunk)
def extract_stream_usage(provider: str, chunk: dict[str, Any]) -> dict[str, int]:
return get_adapter(provider).extract_usage(chunk)
def extract_error(body: str, reason: str | None = None, fallback: str = "Upstream error") -> str:
compact = body.strip()
try:
value = json.loads(compact)
error = value.get("error") if isinstance(value, dict) else None
if isinstance(error, dict):
compact = str(error.get("message") or error.get("type") or compact)
elif isinstance(error, str):
compact = error
elif isinstance(value, dict) and value.get("message"):
compact = str(value["message"])
except (json.JSONDecodeError, TypeError):
pass
return (compact or reason or fallback)[:500]
def redact_secret(value: str, secret: str) -> str:
return value.replace(secret, "[REDACTED]") if secret else value
async def run_request(
session: aiohttp.ClientSession,
config: TestConfig,
endpoint: str,
index: int,
) -> RequestResult:
headers, payload = request_spec(config, index)
started = time.perf_counter()
ttft: float | None = None
parts: list[str] = []
output_chars_seen = 0
output_limit_reached = False
status: int | str = "EXCEPTION"
usage = {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}
def update_usage(chunk: dict[str, Any]) -> None:
for key, value in extract_stream_usage(config.provider, chunk).items():
if key in usage and isinstance(value, int) and value >= 0:
usage[key] = max(usage[key], value)
try:
async with session.post(endpoint, headers=headers, json=payload) as response:
status = response.status
if response.status < 200 or response.status >= 300:
body = (await response.content.read(MAX_UPSTREAM_ERROR_BYTES)).decode(
"utf-8", errors="replace"
)
error = extract_error(
body,
response.reason,
message(config, "unknown_upstream"),
)
return RequestResult(
index,
False,
status,
time.perf_counter() - started,
None,
0,
redact_secret(error, config.api_key),
)
buffer = ""
async for raw in response.content.iter_any():
buffer += raw.decode("utf-8", errors="ignore")
if len(buffer) > MAX_CAPTURED_OUTPUT_CHARS and "\n" not in buffer:
output_limit_reached = True
break
while "\n" in buffer:
line, buffer = buffer.split("\n", 1)
line = line.strip()
if not line or line.startswith(":"):
continue
data = line[5:].strip() if line.startswith("data:") else line
if not data or data == "[DONE]":
continue
try:
chunk = json.loads(data)
except json.JSONDecodeError:
continue
update_usage(chunk)
text = extract_stream_text(config.provider, chunk)
if text:
if ttft is None:
ttft = time.perf_counter() - started
parts.append(text)
output_chars_seen += len(text)
if output_chars_seen >= MAX_CAPTURED_OUTPUT_CHARS:
output_limit_reached = True
break
if output_limit_reached:
break
# Handle a non-streaming JSON body without a trailing newline.
tail = buffer.strip()
if tail:
data = tail[5:].strip() if tail.startswith("data:") else tail
try:
chunk = json.loads(data)
update_usage(chunk)
text = extract_stream_text(config.provider, chunk)
if text:
if ttft is None:
ttft = time.perf_counter() - started
parts.append(text)
except json.JSONDecodeError:
pass
latency = time.perf_counter() - started
output = "".join(parts)
if not output:
return RequestResult(
index,
False,
status,
latency,
ttft,
0,
message(config, "empty_output"),
)
return RequestResult(
index,
True,
status,
latency,
ttft,
len(output),
preview=output[:160],
input_tokens=usage["input"],
output_tokens=usage["output"],
cache_read_tokens=usage["cacheRead"],
cache_write_tokens=usage["cacheWrite"],
)
except TimeoutError:
return RequestResult(
index,
False,
status,
time.perf_counter() - started,
ttft,
0,
message(config, "timeout", timeout=f"{config.timeout:.0f}"),
)
except aiohttp.ClientError as exc:
error = redact_secret(f"{type(exc).__name__}: {exc}", config.api_key)
return RequestResult(
index,
False,
status,
time.perf_counter() - started,
ttft,
0,
error,
)
except Exception as exc: # Keep one upstream failure from aborting the stage.
error = redact_secret(f"{type(exc).__name__}: {exc}", config.api_key)
return RequestResult(
index,
False,
status,
time.perf_counter() - started,
ttft,
0,
error,
)
def summarize_stage(
concurrency: int, results: list[RequestResult], wall_time: float
) -> dict[str, Any]:
successes = [item for item in results if item.success]
latencies = [item.latency for item in successes]
ttfts = [item.ttft for item in successes if item.ttft is not None]
success_count = len(successes)
total = len(results)
usage = {
"input": sum(item.input_tokens for item in successes),
"output": sum(item.output_tokens for item in successes),
"cacheRead": sum(item.cache_read_tokens for item in successes),
"cacheWrite": sum(item.cache_write_tokens for item in successes),
}
return {
"concurrency": concurrency,
"requests": total,
"success": success_count,
"failure": total - success_count,
"successRate": success_count / total * 100 if total else 0,
"wallTime": wall_time,
"throughput": success_count / wall_time if wall_time > 0 else 0,
"avgLatency": sum(latencies) / len(latencies) if latencies else None,
"p95Latency": percentile(latencies, 0.95),
"avgTtft": sum(ttfts) / len(ttfts) if ttfts else None,
"statusDistribution": dict(Counter(str(item.status) for item in results)),
"failures": [
{"index": item.index, "status": item.status, "error": item.error}
for item in results
if not item.success
][:10],
"sampleOutput": next((item.preview for item in successes if item.preview), ""),
"usage": usage,
}
async def run_stage(
session: aiohttp.ClientSession,
config: TestConfig,
endpoint: str,
concurrency: int,
start_index: int,
on_progress: ProgressCallback,
) -> tuple[dict[str, Any], list[RequestResult]]:
started = time.perf_counter()
tasks = [
asyncio.create_task(run_request(session, config, endpoint, start_index + offset))
for offset in range(concurrency)
]
results: list[RequestResult] = []
try:
for completed in asyncio.as_completed(tasks):
result = await completed
results.append(result)
await on_progress(result)
except BaseException:
for task in tasks:
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
raise
wall_time = time.perf_counter() - started
return summarize_stage(concurrency, results, wall_time), results
def build_final_summary(
config: TestConfig,
endpoint: str,
stages: list[dict[str, Any]],
results: list[RequestResult],
wall_time: float,
) -> dict[str, Any]:
successful_stages = [
stage for stage in stages if stage["successRate"] >= config.success_threshold
]
recommended = max((stage["concurrency"] for stage in successful_stages), default=0)
success_count = sum(1 for item in results if item.success)
total = len(results)
all_latencies = [item.latency for item in results if item.success]
successful_results = [item for item in results if item.success]
return {
"provider": config.provider,
"model": config.model,
"endpoint": endpoint,
"maxTestedConcurrency": config.max_concurrency,
"recommendedConcurrency": recommended,
"recommendationBasis": message(
config,
"recommendation",
threshold=f"{config.success_threshold:g}",
),
"requests": total,
"success": success_count,
"failure": total - success_count,
"successRate": success_count / total * 100 if total else 0,
"wallTime": wall_time,
"peakThroughput": max((stage["throughput"] for stage in stages), default=0),
"p95Latency": percentile(all_latencies, 0.95),
"usage": {
"input": sum(item.input_tokens for item in successful_results),
"output": sum(item.output_tokens for item in successful_results),
"cacheRead": sum(item.cache_read_tokens for item in successful_results),
"cacheWrite": sum(item.cache_write_tokens for item in successful_results),
},
"stages": stages,
"failures": [asdict(item) for item in results if not item.success][:30],
}
def make_connector(config: TestConfig) -> aiohttp.TCPConnector:
ssl_context: ssl.SSLContext | bool = False if config.insecure else True
resolver = None if config.allow_private_targets else PublicTargetResolver()
return aiohttp.TCPConnector(
limit=max(config.max_concurrency, 10),
limit_per_host=max(config.max_concurrency, 10),
ttl_dns_cache=300,
ssl=ssl_context,
resolver=resolver,
)