#!/bin/bash # Wird von tests/run.sh eingebunden: Single-Flight und Modell-Listen im Cache. echo "== 23. Single-Flight: gleiche Anfrage, ein Aufruf ==" FPORT=$((PORT+61)) SPORT=$((PORT+62)) # Ein Ziel, das absichtlich langsam antwortet — nur so überlappen sich die Anfragen. cat > "$TMP/slow.py" <<'PY' import http.server, json, sys, time class H(http.server.BaseHTTPRequestHandler): def log_message(self, *a): pass def do_POST(self): n = int(self.headers.get("Content-Length", "0")) self.rfile.read(n) time.sleep(3) out = json.dumps({"id": "slow", "choices": [{"index": 0, "message": {"role": "assistant", "content": "antwort"}}]}).encode() self.send_response(200) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(out))) self.end_headers() self.wfile.write(out) http.server.HTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever() PY python3 "$TMP/slow.py" "$SPORT" & PIDS+=($!) wait_port "$SPORT" 30 || bad "the slow stub did not come up" cat > "$TMP/flight.conf" < "$TMP/flight.log" 2>&1 & PIDS+=($!) wait_port "$FPORT" 30 || bad "the flight instance did not come up" BODY='{"model":"m","messages":[{"role":"user","content":"teure frage"}],"max_tokens":5,"temperature":0}' ask() { curl -s -m 30 -D - -o /dev/null -X POST -H 'Content-Type: application/json' -d "$BODY" \ "http://127.0.0.1:$FPORT/v1/chat/completions" | tr -d '\r' \ | sed -n 's/^[Xx]-[Jj]unction-[Cc]ache: //p' } # Drei Anfragen gleichzeitig. Ohne Single-Flight dauern sie 9 s, mit 3 s. START=$(date +%s) ask > "$TMP/f1.txt" & A=$! ask > "$TMP/f2.txt" & B=$! ask > "$TMP/f3.txt" & C=$! wait $A $B $C ELAPSED=$(( $(date +%s) - START )) HITS=$(cat "$TMP/f1.txt" "$TMP/f2.txt" "$TMP/f3.txt" | grep -c '^hit$') MISS=$(cat "$TMP/f1.txt" "$TMP/f2.txt" "$TMP/f3.txt" | grep -c '^miss$') if [ "$ELAPSED" -le 5 ]; then ok "three concurrent identical requests took ${ELAPSED}s, not 3x3s" else bad "three concurrent identical requests took ${ELAPSED}s — no sharing happened" fi if [ "$MISS" -eq 1 ] && [ "$HITS" -eq 2 ]; then ok "exactly one request went upstream, two were served from its result" else bad "expected 1 miss and 2 hits, got ${MISS} misses and ${HITS} hits" fi # Der Zähler muss das auch sagen. STATUS=$(curl -s -m 5 "http://127.0.0.1:$FPORT/status") case "$STATUS" in *'"joined":2'*) ok "/status counts the two joined requests" ;; *) bad "/status does not count joined requests" ;; esac # Modell-Listen sind reproduzierbar und werden ohne Temperatur gecacht. cat > "$TMP/models.conf" < "$TMP/models.log" 2>&1 & PIDS+=($!) wait_port $((FPORT+2)) 30 || bad "the model-list instance did not come up" # POST, weil der Test-Stub nur POST beantwortet; geprüft wird der Pfad, nicht die Methode. mfetch() { curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' \ -d '{"model":"m","messages":[]}' "http://127.0.0.1:$((FPORT+2))/v1/models" \ | tr -d '\r' | sed -n 's/^[Xx]-[Jj]unction-[Cc]ache: //p' } M1=$(mfetch) M2=$(mfetch) if [ "$M1" = "miss" ] && [ "$M2" = "hit" ]; then ok "a model listing is cached without a temperature" else bad "model listing cache: first=${M1:-none} second=${M2:-none}" fi