"""流式探针:确认 SSE 是「逐块到达」而不是「攒完一次性返回」。 GW=http://127.0.0.1:18090 KEY=sk_xxx python3 sse_test.py 输出机器可读的四行,供 e2e.sh 断言: CT=text/event-stream CHUNKS=3 收到几个 data 块 FIRST=0.01 首块到达耗时(秒);被缓冲住时 ≈ TOTAL TOTAL=1.21 总耗时;上游 3 块 × 0.4s ≈ 1.2s """ import json import os import sys import time import urllib.request KEY = os.environ.get("KEY", "") GW = os.environ.get("GW", "http://127.0.0.1:18090") URL = GW.rstrip("/") + "/openai/v1/chat/completions" req = urllib.request.Request( URL, data=json.dumps({"model": "gpt-4o", "stream": True}).encode(), headers={"Authorization": "Bearer " + KEY, "Content-Type": "application/json"}, ) t0 = time.time() first = None chunks = 0 ctype = "" with urllib.request.urlopen(req) as r: ctype = r.headers.get("Content-Type", "") while True: line = r.readline() if not line: break s = line.decode().strip() if s.startswith("data:"): chunks += 1 if first is None: first = time.time() - t0 total = time.time() - t0 print("CT=%s" % ctype) print("CHUNKS=%d" % chunks) print("FIRST=%.2f" % (first if first is not None else -1)) print("TOTAL=%.2f" % total)