#!/usr/bin/env python3 """⑥ 답변속도 — 정식 부하 측정 (표준 라이브러리만, 기존 04 bench 방식 계승). 측정 구간: 답변 생성 엔진 요청 수신 → 응답 완료 (HTTP 왕복, localhost) 측정 경로: --profile heavy = 가격협상 진입 (Q-Table 카드 선택 경로, 기본) --profile light = 고정 스크립트 스텝(시작) 부하 형태: 동시 사용자(--vus) × 총 요청(--n), 웜업(--warmup) 제외 후 집계 사용: python bench.py [--n 1000] [--warmup 50] [--vus 10] [--target 600] 출력: result_bench_.json (p50/p95/p99, 판정) + detail_bench_.jsonl """ import argparse import json import time import urllib.request from concurrent.futures import ThreadPoolExecutor from pathlib import Path HERE = Path(__file__).parent HEAVY = {"rq_type": "재협상", "session_id": "BENCH", "step": "가격협상_확인", "index": 5, "user_input": "예", "item_price": 163600, "revenue": 163600, "dist": "R", "partners_yn": "없음", "partners": 0, "anchor": 8099, "target": 8180, "initial": 9000, "input": 9000, "iteration": 0, "is_new_quote": True, "profiling": False} LIGHT = {"rq_type": "재협상", "session_id": "BENCH", "step": "시작", "index": 0, "profiling": False} def once(url: str, body: bytes) -> float: t0 = time.perf_counter() req = urllib.request.Request(url, body, {"Content-Type": "application/json"}) with urllib.request.urlopen(req, timeout=60) as r: r.read() return (time.perf_counter() - t0) * 1000 def percentile(vals, p): if not vals: return None k = (len(vals) - 1) * p / 100 lo, hi = int(k), min(int(k) + 1, len(vals) - 1) return round(vals[lo] + (vals[hi] - vals[lo]) * (k - lo), 2) def main(): ap = argparse.ArgumentParser() ap.add_argument("--url", default="http://127.0.0.1:53117/v1/nego/chat") ap.add_argument("--n", type=int, default=1000) ap.add_argument("--warmup", type=int, default=50) ap.add_argument("--vus", type=int, default=10, help="동시 사용자 수") ap.add_argument("--profile", default="heavy", choices=["heavy", "light"]) ap.add_argument("--target", type=float, default=600.0) args = ap.parse_args() body = json.dumps(HEAVY if args.profile == "heavy" else LIGHT).encode() # 웜업 (집계 제외) for _ in range(args.warmup): once(args.url, body) t_start = time.perf_counter() with ThreadPoolExecutor(max_workers=args.vus) as ex: samples = list(ex.map(lambda _: once(args.url, body), range(args.n))) wall = time.perf_counter() - t_start with open(HERE / f"detail_bench_{args.profile}.jsonl", "w", encoding="utf-8") as f: for ms in samples: f.write(json.dumps({"ms": round(ms, 3)}) + "\n") vals = sorted(samples) p95 = percentile(vals, 95) out = { "측정구간": "요청 수신 → 응답 완료 (HTTP 왕복, localhost)", "부하": {"profile": args.profile, "n": args.n, "warmup_제외": args.warmup, "동시사용자": args.vus, "총소요_초": round(wall, 2), "처리율_req_s": round(args.n / wall, 1)}, "ms": {"p50": percentile(vals, 50), "p95": p95, "p99": percentile(vals, 99), "min": round(vals[0], 2), "max": round(vals[-1], 2), "mean": round(sum(vals) / len(vals), 2)}, "target_ms": args.target, "판정기준": "p95 ≤ 목표", "verdict": "PASS" if p95 is not None and p95 <= args.target else "FAIL", } (HERE / f"result_bench_{args.profile}.json").write_text( json.dumps(out, ensure_ascii=False, indent=2), encoding="utf-8") print(json.dumps(out, ensure_ascii=False, indent=1)) if __name__ == "__main__": main()