feat: add authenticated business benchmark suite

This commit is contained in:
wlx committed 2026-08-13 14:22:34 +08:00
1 parent 5293d93a30
commit c25b180021
9 files changed
+652 -19

No files matched your search

+33 -16
View File
@@ -31,6 +31,8 @@ def parse_args():
parser.add_argument("--body-file")
parser.add_argument("--headers-file")
parser.add_argument("--output")
parser.add_argument("--allow-failures", action="store_true")
parser.add_argument("--max-response-bytes", type=int, default=64 * 1024)
return parser.parse_args()
@@ -68,6 +70,8 @@ def main():
raise SystemExit("--concurrency must be between 1 and 256")
if args.warmup < 0 or args.timeout <= 0:
raise SystemExit("--warmup must be non-negative and --timeout must be positive")
if not 1 <= args.max_response_bytes <= 64 * 1024 * 1024:
raise SystemExit("--max-response-bytes must be between 1 byte and 64 MiB")
validate_target(args.url)
body = pathlib.Path(args.body_file).read_bytes() if args.body_file else None
@@ -83,20 +87,38 @@ def main():
NoRedirectHandler(),
)
def application_code(response_body):
if not response_body:
return None
try:
parsed_body = json.loads(response_body)
except (UnicodeDecodeError, json.JSONDecodeError):
return None
if isinstance(parsed_body, dict) and parsed_body.get("code") is not None:
return str(parsed_body["code"])
return None
def request_once():
request = urllib.request.Request(args.url, data=body, headers=headers, method=args.method)
started = time.perf_counter()
try:
with opener.open(request, timeout=args.timeout) as response:
response_body = response.read()
response_body = response.read(args.max_response_bytes + 1)
if len(response_body) > args.max_response_bytes:
return response.status, (time.perf_counter() - started) * 1000, "ResponseTooLarge", None, response.headers.get("Retry-After")
retry_after = response.headers.get("Retry-After")
return response.status, (time.perf_counter() - started) * 1000, None, response_body, retry_after
return response.status, (time.perf_counter() - started) * 1000, None, application_code(response_body), retry_after
except urllib.error.HTTPError as exc:
response_body = exc.read()
response_body = exc.read(args.max_response_bytes + 1)
if len(response_body) > args.max_response_bytes:
response_body = b""
error_name = "ResponseTooLarge"
else:
error_name = None
retry_after = exc.headers.get("Retry-After")
return exc.code, (time.perf_counter() - started) * 1000, None, response_body, retry_after
return exc.code, (time.perf_counter() - started) * 1000, error_name, application_code(response_body), retry_after
except Exception as exc:
return None, (time.perf_counter() - started) * 1000, type(exc).__name__, b"", None
return None, (time.perf_counter() - started) * 1000, type(exc).__name__, None, None
for _ in range(args.warmup):
request_once()
@@ -113,23 +135,17 @@ def main():
response_codes = {}
retry_after_values = {}
successful = 0
for status, _, error, response_body, retry_after in results:
for status, _, error, response_code, retry_after in results:
if status is not None:
statuses[str(status)] = statuses.get(str(status), 0) + 1
if status in expected_statuses:
if status in expected_statuses and error is None:
successful += 1
if error:
errors[error] = errors.get(error, 0) + 1
if retry_after:
retry_after_values[retry_after] = retry_after_values.get(retry_after, 0) + 1
if response_body:
try:
parsed_body = json.loads(response_body)
except (UnicodeDecodeError, json.JSONDecodeError):
parsed_body = None
if isinstance(parsed_body, dict) and parsed_body.get("code") is not None:
code = str(parsed_body["code"])
response_codes[code] = response_codes.get(code, 0) + 1
if response_code is not None:
response_codes[response_code] = response_codes.get(response_code, 0) + 1
report = {
"schema_version": 1,
@@ -142,6 +158,7 @@ def main():
"warmup_requests": args.warmup,
"body_bytes": len(body or b""),
"custom_headers_supplied": bool(headers),
"max_response_bytes": args.max_response_bytes,
"duration_seconds": round(duration_seconds, 6),
"requests_per_second": round(args.requests / duration_seconds, 3),
"successful_requests": successful,
@@ -167,7 +184,7 @@ def main():
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(rendered + "\n", encoding="utf-8")
if successful != args.requests:
if not args.allow_failures and successful != args.requests:
raise SystemExit(2)