Jede Antwort trägt jetzt, was sie gekostet hat: X-Junction-Tokens-In: 1200 X-Junction-Tokens-Out: 300 X-Junction-Cost-Usd: 0.000426 und im Log: usage model=deepseek-v4.1-flash in=1200 cached=1000 out=300 cost_usd=0.000426 Die Preise stehen in examples/prices.conf, abgeschrieben von der Preisseite des Anbieters (ollama.com/pricing, 5.11.2026) — pro Million Token, mit Off-Peak-Preisen, Monatsbudget und Anfragefenster für die Go-Modelle. Zwei Entscheidungen, die wichtiger sind als die Zahlen: Kein "*"-Eintrag. Ein Modell ohne Zeile kostet unbekannt, nicht null, und das steht dann auch so im Log. Eine erfundene Null in einem Kostenbericht ist schlimmer als gar keine Zahl. Ohne Preistabelle wird gar nichts gerechnet — dann fehlt die Kopfzeile, statt eine Null zu zeigen. Damit die Antwort vollständig vorliegt, wird bei gesetzter Tabelle auch ohne Cache vollständig gelesen; ein nicht gestreamter JSON-Rumpf ist ohnehin endlich. Dazu Single-Flight: gleiche Anfragen, die gleichzeitig eintreffen, warten auf die erste und teilen ihr Ergebnis. Drei identische Anfragen an ein Ziel mit 3 s Antwortzeit dauern 3 s statt 9, ein Aufruf geht raus, zwei werden bedient. 111 Prüfungen, keine Fehler.
104 lines
3.5 KiB
Bash
104 lines
3.5 KiB
Bash
#!/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+=($!)
|
|
sleep 0.6
|
|
|
|
cat > "$TMP/flight.conf" <<CFG
|
|
listen 127.0.0.1:$FPORT
|
|
cache_ttl 300
|
|
cache_entries 50
|
|
cache_bytes 2000000
|
|
endpoint slow { base http://127.0.0.1:$SPORT/v1; timeout 30 }
|
|
req for "*" pass slow
|
|
CFG
|
|
./junction --config "$TMP/flight.conf" > "$TMP/flight.log" 2>&1 &
|
|
PIDS+=($!)
|
|
sleep 0.8
|
|
|
|
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" <<CFG
|
|
listen 127.0.0.1:$((FPORT+2))
|
|
cache_ttl 300
|
|
cache_entries 50
|
|
cache_bytes 2000000
|
|
endpoint a { base http://127.0.0.1:$P_GATED/v1 }
|
|
req path "/v1/models" check none pass a
|
|
req for "*" pass a
|
|
CFG
|
|
./junction --config "$TMP/models.conf" > "$TMP/models.log" 2>&1 &
|
|
PIDS+=($!)
|
|
sleep 0.8
|
|
# 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
|