Off-Peak wurde nur im Usage-Poller gesetzt. Endpoints ohne usage_cmd hatten deshalb nie einen offpeak_active, und bei den anderen war der Wert bis zu drei Minuten alt. forward_step und die Review-Kosten fragen das Fenster jetzt beim Call selbst ab (ep_in_offpeak); der Statuswert wird dabei aktualisiert. Die -go-Zeile wurde über kind opencode-go gewählt. Dieses Kind tragen aber auch die ollama.com-Endpoints, sodass deren Pay-as-you-go-Modelle fälschlich 0 gekostet hätten. Die Preiswahl hängt jetzt nur am Modellnamen: #variant -> Basiszeile, "-go" nur, wenn der Name selbst so endet. Der Endpoint-Kind ändert den Preis nicht. Chunked non-streaming responses wurden nicht bepreist, weil want_usage !r.chunked verlangte. read_full_body dechunked bereits; die Einschränkung ist entfernt. Tests in usage.sh: #variant -> 0.000426; opencode-go-Kind ändert den Preis nicht; Off-Peak-Fenster 00:00-23:59 mit offpeak=0.15,0.60 -> 0.000216 (200 frisch, 1000 cached, 300 out); Cache-Hit 0/0. Der no-price-Test hat jetzt einen eigenen Port und wait_port. Tests: 526 OK, 0 FAIL.
178 lines
8.2 KiB
Bash
178 lines
8.2 KiB
Bash
#!/bin/bash
|
|
# Wird von tests/run.sh eingebunden: Verbrauch und Kosten pro Anfrage.
|
|
|
|
echo "== 24. Verbrauch und Kosten pro Anfrage =="
|
|
|
|
UPORT=$((PORT+71))
|
|
USPORT=$((PORT+72))
|
|
|
|
cat > "$TMP/ustub.py" <<'PY'
|
|
import http.server, json, sys
|
|
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)
|
|
out = json.dumps({"id": "u",
|
|
"choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"}}],
|
|
"usage": {"prompt_tokens": 1200, "completion_tokens": 300,
|
|
"prompt_tokens_details": {"cached_tokens": 1000}}}).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/ustub.py" "$USPORT" & PIDS+=($!)
|
|
sleep 0.6
|
|
|
|
cat > "$TMP/prices.conf" <<'CFG'
|
|
# test/preis in cached out
|
|
test/preis 0.30 0.006 1.20
|
|
# A -go row exists but the model name asks for test/preis (no -go suffix), so it
|
|
# must not be selected just because the endpoint kind is opencode-go.
|
|
test/preis-go 0 0 0 budget=10 requests=100/5h
|
|
test/offpeak 0.30 0.006 1.20 offpeak=0.15,0.60
|
|
CFG
|
|
|
|
cat > "$TMP/usage.conf" <<CFG
|
|
listen 127.0.0.1:$UPORT
|
|
prices $TMP/prices.conf
|
|
cache_ttl 0
|
|
endpoint a { base http://127.0.0.1:$USPORT/v1; timeout 20 }
|
|
req for "*" pass a
|
|
CFG
|
|
./junction --config "$TMP/usage.conf" > "$TMP/usage.log" 2>&1 &
|
|
PIDS+=($!)
|
|
sleep 0.8
|
|
|
|
hdr() { # hdr <body> <header-suffix> [url]
|
|
local url=${3:-"http://127.0.0.1:$UPORT/v1/chat/completions"}
|
|
curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' -d "$1" \
|
|
"$url" | tr -d '\r' \
|
|
| sed -n "s/^[Xx]-[Jj]unction-${2}: //p"
|
|
}
|
|
|
|
PRICED='{"model":"test/preis","messages":[{"role":"user","content":"hi"}],"max_tokens":50}'
|
|
# Eine Anfrage, alle Kopfzeilen — sonst zählt der Test drei Aufrufe statt einem.
|
|
ALL=$(curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' -d "$PRICED" \
|
|
"http://127.0.0.1:$UPORT/v1/chat/completions" | tr -d '\r')
|
|
IN=$(printf '%s' "$ALL" | sed -n 's/^[Xx]-[Jj]unction-[Tt]okens-[Ii]n: //p')
|
|
OUT=$(printf '%s' "$ALL" | sed -n 's/^[Xx]-[Jj]unction-[Tt]okens-[Oo]ut: //p')
|
|
COST=$(printf '%s' "$ALL" | sed -n 's/^[Xx]-[Jj]unction-[Cc]ost-[Uu]sd: //p')
|
|
|
|
[ "$IN" = "1200" ] && ok "input tokens are reported in the response headers" \
|
|
|| bad "input tokens missing (got '${IN:-none}')"
|
|
[ "$OUT" = "300" ] && ok "output tokens are reported in the response headers" \
|
|
|| bad "output tokens missing (got '${OUT:-none}')"
|
|
|
|
# 1000 von 1200 Eingaben waren zwischengespeichert: 200*0.30 + 1000*0.006 + 300*1.20,
|
|
# alles durch eine Million.
|
|
WANT="0.000426"
|
|
[ "$COST" = "$WANT" ] && ok "cost uses the cached-input price where the provider reports it" \
|
|
|| bad "cost was '${COST:-none}', expected $WANT"
|
|
|
|
UNKNOWN='{"model":"gibt-es-nicht","messages":[{"role":"user","content":"hi"}],"max_tokens":50}'
|
|
UCOST=$(hdr "$UNKNOWN" "Cost-Usd")
|
|
[ "$UCOST" = "unknown" ] && ok "a model without a price reports unknown, not zero" \
|
|
|| bad "unpriced model reported '${UCOST:-none}'"
|
|
|
|
STATUS=$(curl -s -m 5 "http://127.0.0.1:$UPORT/status")
|
|
case "$STATUS" in
|
|
*'"cost_unknown":1'*) ok "/status counts the unpriced request separately" ;;
|
|
*) bad "/status does not count unpriced requests" ;;
|
|
esac
|
|
case "$STATUS" in
|
|
*'"tokens_in":2400'*) ok "/status accumulates tokens across requests" ;;
|
|
*) bad "/status token totals are wrong" ;;
|
|
esac
|
|
|
|
# Ohne Preistabelle darf gar nichts gerechnet werden — nur gezählt.
|
|
NOPRICE=$((PORT+69))
|
|
cat > "$TMP/noprice.conf" <<CFG
|
|
listen 127.0.0.1:$NOPRICE
|
|
cache_ttl 0
|
|
endpoint a { base http://127.0.0.1:$USPORT/v1; timeout 20 }
|
|
req for "*" pass a
|
|
CFG
|
|
./junction --config "$TMP/noprice.conf" > "$TMP/noprice.log" 2>&1 &
|
|
PIDS+=($!)
|
|
wait_port "$NOPRICE" 30 || bad "the no-price instance did not come up"
|
|
NP=$(curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' -d "$PRICED" \
|
|
"http://127.0.0.1:$NOPRICE/v1/chat/completions" | tr -d '\r' | sed -n 's/^[Xx]-[Jj]unction-[Cc]ost-Usd: //p')
|
|
[ -z "$NP" ] && ok "without a price table no cost header is invented" \
|
|
|| bad "a cost was reported without any price table ('$NP')"
|
|
|
|
# Ein "#variant" ist ein Selektor, kein eigener Modellname: der Basispreis gilt.
|
|
VARIANT='{"model":"test/preis#max","messages":[{"role":"user","content":"hi"}],"max_tokens":50}'
|
|
VCOST=$(hdr "$VARIANT" "Cost-Usd")
|
|
[ "$VCOST" = "0.000426" ] && ok "a #variant uses the base model's price" \
|
|
|| bad "variant cost was '${VCOST:-none}', expected 0.000426"
|
|
|
|
# Ein opencode-go-Endpoint nutzt die -go-Zeile (Abo: 0 Tokenpreis), nicht den
|
|
# Listenpreis der Basiszeile.
|
|
GOPORT=$((PORT+70))
|
|
cat > "$TMP/go.conf" <<CFG
|
|
listen 127.0.0.1:$GOPORT
|
|
prices $TMP/prices.conf
|
|
cache_ttl 0
|
|
endpoint go { base http://127.0.0.1:$USPORT/v1; kind opencode-go; timeout 20 }
|
|
req for "*" pass go
|
|
CFG
|
|
./junction --config "$TMP/go.conf" > "$TMP/go.log" 2>&1 &
|
|
PIDS+=($!)
|
|
wait_port "$GOPORT" 30 || bad "the opencode-go cost instance did not come up"
|
|
GOCOST=$(hdr "$VARIANT" "Cost-Usd" "http://127.0.0.1:$GOPORT/v1/chat/completions")
|
|
[ "$GOCOST" = "0.000426" ] && ok "the endpoint kind does not change the model's price" \
|
|
|| bad "opencode-go cost was '${GOCOST:-none}', expected 0.000426"
|
|
|
|
# Off-Peak: das Zeitfenster des Endpoints wird beim Request ausgewertet, nicht
|
|
# nur vom Usage-Poller. 200 frische * 0.15 + 1000 cached * 0.006 + 300 * 0.60.
|
|
OFFPORT=$((PORT+75))
|
|
cat > "$TMP/offpeak.conf" <<CFG
|
|
listen 127.0.0.1:$OFFPORT
|
|
prices $TMP/prices.conf
|
|
cache_ttl 0
|
|
endpoint a { base http://127.0.0.1:$USPORT/v1; offpeak 00:00-23:59; timeout 20 }
|
|
req for "*" pass a
|
|
CFG
|
|
./junction --config "$TMP/offpeak.conf" > "$TMP/offpeak.log" 2>&1 &
|
|
PIDS+=($!)
|
|
wait_port "$OFFPORT" 30 || bad "the off-peak instance did not come up"
|
|
OFFBODY='{"model":"test/offpeak","messages":[{"role":"user","content":"hi"}],"max_tokens":50}'
|
|
OFFCOST=$(hdr "$OFFBODY" "Cost-Usd" "http://127.0.0.1:$OFFPORT/v1/chat/completions")
|
|
[ "$OFFCOST" = "0.000216" ] && ok "off-peak prices are applied from the endpoint window" \
|
|
|| bad "off-peak cost was '${OFFCOST:-none}', expected 0.000216"
|
|
|
|
# Ein Cache-Hit kostet nichts; der gespeicherte Rumpf trägt aber noch die Usage
|
|
# des ursprünglichen Calls. Kopfzeilen und Ledger müssen den Hit mit 0 zeigen.
|
|
CACHEPORT=$((PORT+74))
|
|
cat > "$TMP/cost-cache.conf" <<CFG
|
|
listen 127.0.0.1:$CACHEPORT
|
|
prices $TMP/prices.conf
|
|
cache_ttl 300
|
|
cache_entries 50
|
|
cache_bytes 2000000
|
|
endpoint a { base http://127.0.0.1:$USPORT/v1; timeout 20 }
|
|
req for "*" pass a
|
|
CFG
|
|
./junction --config "$TMP/cost-cache.conf" > "$TMP/cost-cache.log" 2>&1 &
|
|
PIDS+=($!)
|
|
wait_port "$CACHEPORT" 30 || bad "the cost-cache instance did not come up"
|
|
CACHED='{"model":"test/preis","messages":[{"role":"user","content":"cache cost"}],"max_tokens":50,"temperature":0}'
|
|
C1=$(curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' -d "$CACHED" \
|
|
"http://127.0.0.1:$CACHEPORT/v1/chat/completions" | tr -d '\r')
|
|
C2=$(curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' -d "$CACHED" \
|
|
"http://127.0.0.1:$CACHEPORT/v1/chat/completions" | tr -d '\r')
|
|
C1COST=$(printf '%s' "$C1" | sed -n 's/^[Xx]-[Jj]unction-[Cc]ost-[Uu]sd: //p')
|
|
C2COST=$(printf '%s' "$C2" | sed -n 's/^[Xx]-[Jj]unction-[Cc]ost-[Uu]sd: //p')
|
|
C2IN=$(printf '%s' "$C2" | sed -n 's/^[Xx]-[Jj]unction-[Tt]okens-[Ii]n: //p')
|
|
C2OUT=$(printf '%s' "$C2" | sed -n 's/^[Xx]-[Jj]unction-[Tt]okens-[Oo]ut: //p')
|
|
[ "$C1COST" = "0.000426" ] && ok "the first priced call reports its exact cost" \
|
|
|| bad "first cached call cost was '${C1COST:-none}'"
|
|
[ "$C2COST" = "0.000000" ] && ok "a cache hit reports cost 0, not the original cost" \
|
|
|| bad "cache hit cost was '${C2COST:-none}'"
|
|
[ "$C2IN" = "0" ] && [ "$C2OUT" = "0" ] && ok "a cache hit reports zero tokens" \
|
|
|| bad "cache hit tokens were '${C2IN:-none}'/'${C2OUT:-none}'"
|