193 lines
7.4 KiB
Python
193 lines
7.4 KiB
Python
#!/usr/bin/env python3
|
|
import argparse
|
|
import datetime
|
|
import ipaddress
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import secrets
|
|
import socket
|
|
import time
|
|
import urllib.error
|
|
import urllib.parse
|
|
import urllib.request
|
|
|
|
|
|
MAX_RESPONSE_BYTES = 2 * 1024 * 1024
|
|
|
|
|
|
class NoRedirectHandler(urllib.request.HTTPRedirectHandler):
|
|
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
return None
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser(
|
|
description="Create one local benchmark user and a temporary bearer header file."
|
|
)
|
|
parser.add_argument("--base-url", default="http://127.0.0.1:3001")
|
|
parser.add_argument("--headers-output", required=True)
|
|
parser.add_argument("--metadata-output", required=True)
|
|
parser.add_argument("--timeout", type=float, default=10.0)
|
|
parser.add_argument("--deployment-commit", default="unknown")
|
|
parser.add_argument("--deployment-dirty", default="unknown")
|
|
parser.add_argument("--source-commit", default="unknown")
|
|
parser.add_argument("--backend-image", default="unknown")
|
|
parser.add_argument("--backend-image-id", default="unknown")
|
|
parser.add_argument("--backend-container-started-at", default="unknown")
|
|
parser.add_argument("--requests-per-level", type=int, default=0)
|
|
parser.add_argument("--repeat-count", type=int, default=0)
|
|
parser.add_argument("--concurrencies", default="")
|
|
return parser.parse_args()
|
|
|
|
|
|
def validate_base_url(raw_url):
|
|
parsed = urllib.parse.urlsplit(raw_url)
|
|
if parsed.scheme not in {"http", "https"} or not parsed.hostname:
|
|
raise ValueError("Base URL must use http or https and include a host.")
|
|
if parsed.username or parsed.password or parsed.query or parsed.fragment:
|
|
raise ValueError("Credentials, query strings, and fragments are forbidden.")
|
|
if parsed.path not in {"", "/"}:
|
|
raise ValueError("Base URL must not include an API path.")
|
|
if parsed.hostname == "localhost":
|
|
addresses = [ipaddress.ip_address("127.0.0.1")]
|
|
else:
|
|
addresses = [
|
|
ipaddress.ip_address(item[4][0])
|
|
for item in socket.getaddrinfo(
|
|
parsed.hostname, parsed.port, type=socket.SOCK_STREAM
|
|
)
|
|
]
|
|
if not addresses or any(
|
|
not (address.is_loopback or address.is_private or address.is_link_local)
|
|
for address in addresses
|
|
):
|
|
raise ValueError("Refusing to create a benchmark session on a non-local host.")
|
|
return raw_url.rstrip("/")
|
|
|
|
|
|
def request_json(opener, timeout, method, url, body=None):
|
|
headers = {
|
|
"Accept": "application/json",
|
|
"User-Agent": "ai-soulmate-local-business-benchmark/1",
|
|
}
|
|
data = None
|
|
if body is not None:
|
|
data = json.dumps(body, separators=(",", ":")).encode("utf-8")
|
|
headers["Content-Type"] = "application/json"
|
|
request = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
started = time.perf_counter()
|
|
try:
|
|
with opener.open(request, timeout=timeout) as response:
|
|
raw_body = response.read(MAX_RESPONSE_BYTES + 1)
|
|
if len(raw_body) > MAX_RESPONSE_BYTES:
|
|
raise RuntimeError("Benchmark setup response exceeded 2 MiB.")
|
|
payload = json.loads(raw_body.decode("utf-8"))
|
|
if not isinstance(payload, dict):
|
|
raise RuntimeError("Benchmark setup returned a non-object JSON response.")
|
|
return response.status, payload, round(
|
|
(time.perf_counter() - started) * 1000, 3
|
|
)
|
|
except urllib.error.HTTPError as exc:
|
|
exc.close()
|
|
raise RuntimeError(f"{method} {url} returned HTTP {exc.code}.") from exc
|
|
|
|
|
|
def write_private_json(path, payload):
|
|
output_path = pathlib.Path(path)
|
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
output_path.write_text(
|
|
json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
|
|
)
|
|
os.chmod(output_path, 0o600)
|
|
|
|
|
|
def main():
|
|
args = parse_args()
|
|
if args.timeout <= 0 or args.timeout > 60:
|
|
raise SystemExit("--timeout must be between 0 and 60 seconds.")
|
|
try:
|
|
base_url = validate_base_url(args.base_url)
|
|
opener = urllib.request.build_opener(
|
|
urllib.request.ProxyHandler({}), NoRedirectHandler()
|
|
)
|
|
health_status, health, health_latency = request_json(
|
|
opener, args.timeout, "GET", base_url + "/api/health"
|
|
)
|
|
mode = str(health.get("tripo_mode") or "").strip().lower()
|
|
profile = str(health.get("tripo_mock_profile") or "").strip()
|
|
if (
|
|
health_status != 200
|
|
or mode != "mock"
|
|
or not profile
|
|
or profile == "not-selected"
|
|
):
|
|
raise RuntimeError("Backend did not confirm an explicit Tripo mock profile.")
|
|
|
|
username = f"bench_business_{int(time.time())}_{secrets.token_hex(4)}"
|
|
password = "Bm!9" + secrets.token_urlsafe(18)
|
|
register_status, registration, register_latency = request_json(
|
|
opener,
|
|
args.timeout,
|
|
"POST",
|
|
base_url + "/api/auth/register",
|
|
{
|
|
"username": username,
|
|
"password": password,
|
|
"password_confirm": password,
|
|
"register_via": "account",
|
|
},
|
|
)
|
|
token = registration.get("access_token")
|
|
user = registration.get("user")
|
|
if register_status != 200 or not isinstance(token, str) or not token:
|
|
raise RuntimeError("Registration did not return a bearer token.")
|
|
if not isinstance(user, dict) or not user.get("id"):
|
|
raise RuntimeError("Registration did not return a user ID.")
|
|
|
|
write_private_json(
|
|
args.headers_output,
|
|
{
|
|
"Accept": "application/json",
|
|
"Authorization": f"Bearer {token}",
|
|
"Content-Type": "application/json",
|
|
"User-Agent": "ai-soulmate-local-business-benchmark/1",
|
|
},
|
|
)
|
|
write_private_json(
|
|
args.metadata_output,
|
|
{
|
|
"schema_version": 1,
|
|
"created_at_utc": datetime.datetime.now(
|
|
datetime.timezone.utc
|
|
).isoformat(),
|
|
"base_url": base_url,
|
|
"tripo_mode": mode,
|
|
"tripo_mock_profile": profile,
|
|
"health_latency_ms": health_latency,
|
|
"registration_latency_ms": register_latency,
|
|
"user_id": str(user["id"]),
|
|
"deployment_commit": args.deployment_commit,
|
|
"deployment_dirty": args.deployment_dirty.lower() == "true",
|
|
"source_commit": args.source_commit,
|
|
"backend_image": args.backend_image,
|
|
"backend_image_id": args.backend_image_id,
|
|
"backend_container_started_at": args.backend_container_started_at,
|
|
"requests_per_level": args.requests_per_level,
|
|
"repeat_count": args.repeat_count,
|
|
"concurrencies": [
|
|
int(value)
|
|
for value in args.concurrencies.split(",")
|
|
if value.strip()
|
|
],
|
|
"test_account_persisted": True,
|
|
"plaintext_credentials_persisted": False,
|
|
},
|
|
)
|
|
except (OSError, ValueError, RuntimeError, json.JSONDecodeError) as exc:
|
|
raise SystemExit(str(exc)) from exc
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|