decypharr-phase1: add-latency harness

Runs on the media host so neither the Decypharr token nor the TorBox key leaves
it, and prints only HTTP codes and timings. Uses fresh shuffled hashes per run:
re-adding a hash the sandbox already knows answers in about a millisecond and
halves the measured median.
This commit is contained in:
ziadarafat committed 2026-11-28 21:31:39 -07:00
1 parent deb83c1a73
commit 8d719140ba
2 files changed
+148

No files matched your search

+116
View File
@@ -0,0 +1,116 @@
#!/bin/bash
# measure.sh — Decypharr phase 1 retest: how long does an add take?
#
# Runs ON the media host (yveltal). Both secrets stay on that machine: the
# Decypharr API token is read from its own file and the TorBox key from rdtc's
# SQLite Settings table. Neither is ever printed, echoed or written to a file —
# only HTTP codes and timings come out.
#
# sudo -n ./measure.sh [runs-per-case]
#
# Cases:
# cached a real, cached TorBox hash (the path the *arr actually uses)
# bogus a well-formed magnet nothing has ever seen (uncached/blocked)
# noauth whether an add without any Authorization header is accepted
#
# Config under test (per-debrid, config.json): download_uncached=false,
# rate_limit=600/minute, workers=20, max_active_downloads=3.
set -uo pipefail
API=http://127.0.0.1:8282
CFG=/home/ziadarafat/.local/share/decypharr-sandbox/config/config.json
TOKEN_FILE=/home/ziadarafat/.local/share/decypharr-sandbox/config/.sandbox_token
RUNS=${1:-3}
RDTC_DB=/home/ziadarafat/.local/share/rdtc-fresh/db/rdtclient.db
TOKEN=$(cat "$TOKEN_FILE" 2>/dev/null)
[ -n "$TOKEN" ] || { echo "no API token at $TOKEN_FILE" >&2; exit 1; }
torbox_key() {
python3 - "$RDTC_DB" <<'PY' 2>/dev/null
import sqlite3, sys
db = sqlite3.connect(sys.argv[1])
# Settings is a (SettingId, Value) key/value table, not a JSON blob.
row = db.execute("SELECT Value FROM Settings WHERE SettingId='Provider:ApiKey'").fetchone()
print(row[0] if row and row[0] else "")
PY
}
add_timed() { # $1=label $2=magnet $3=auth|noauth
local label=$1 magnet=$2 mode=$3 out code secs
if [ "$mode" = "auth" ]; then
out=$(curl -sS -m 180 -o /tmp/.dcy_body -w '%{http_code} %{time_total}' \
-H "Authorization: Bearer $TOKEN" \
--data-urlencode "urls=$magnet" --data-urlencode "category=radarr" "$API/api/v2/torrents/add" 2>&1)
else
out=$(curl -sS -m 180 -o /tmp/.dcy_body -w '%{http_code} %{time_total}' \
--data-urlencode "urls=$magnet" --data-urlencode "category=radarr" "$API/api/v2/torrents/add" 2>&1)
fi
code=${out%% *}; secs=${out##* }
printf '%-8s %-6s run=%-2s http=%-4s seconds=%-8s body=%s\n' \
"$label" "$mode" "${CURRENT_RUN}" "${code:-ERR}" "${secs:-timeout}" \
"$(head -c 80 /tmp/.dcy_body 2>/dev/null | tr -d '\n')"
printf '%s,%s,%s,%s,%s\n' "$label" "$mode" "${CURRENT_RUN}" "${code:-ERR}" "${secs:-timeout}" >> "$RESULTS"
rm -f /tmp/.dcy_body
}
U=$(id -u ziadarafat)
UC="sudo -u ziadarafat env XDG_RUNTIME_DIR=/run/user/$U"
echo "=== sandbox: $($UC systemctl --user is-active decypharr-sandbox 2>/dev/null || echo n/a) api: $API ==="
python3 - "$CFG" <<'PY' 2>/dev/null
import json, sys
d = json.load(open(sys.argv[1]))
for deb in d.get("debrids", []):
print("=== config: provider=%s download_uncached=%s rate_limit=%s workers=%s max_active=%s" % (
deb.get("provider"), deb.get("download_uncached"), deb.get("rate_limit"),
deb.get("workers"), d.get("max_active_downloads")))
PY
KEY=$(torbox_key)
if [ -n "${KEY:-}" ]; then
# Distinct hashes per run: re-adding the same hash is idempotent and would
# measure nothing after the first call.
CACHED_LIST=$(curl -sS -m 60 -H "Authorization: Bearer $KEY" \
"https://api.torbox.app/v1/api/torrents/mylist?bypass_cache=true" 2>/dev/null \
| python3 -c "import json,sys
import random
n=$RUNS+1
try: d=json.load(sys.stdin)
except Exception: raise SystemExit
hs=[t['hash'] for t in (d.get('data') or []) if t.get('download_state')=='cached' and t.get('hash')]
random.shuffle(hs)
# Shuffle: re-running the same hashes measures nothing after the first call,
# because a hash the sandbox already knows answers in about a millisecond.
print(chr(10).join(hs[:n]))" 2>/dev/null)
else
CACHED_LIST=""
fi
unset KEY
N_CACHED=$(printf '%s\n' "$CACHED_LIST" | grep -c . || true)
if [ "${N_CACHED:-0}" -gt 0 ]; then
echo "=== cached hashes available: $N_CACHED (not printed)"
else
echo "=== cached hashes available: none -- skipping the cached case"
fi
BOGUS=0000000000000000000000000000000000000001
RESULTS=${RESULTS:-/tmp/decypharr-phase1-results.csv}
echo "label,mode,run,http,seconds" > "$RESULTS"
for CURRENT_RUN in $(seq 1 "$RUNS"); do
H=$(printf '%s\n' "$CACHED_LIST" | sed -n "${CURRENT_RUN}p")
if [ -n "$H" ]; then
add_timed cached "magnet:?xt=urn:btih:$H&dn=phase1-test-$CURRENT_RUN" auth
fi
add_timed bogus "magnet:?xt=urn:btih:$BOGUS&dn=phase1-nonexistent-$CURRENT_RUN" auth
done
H=$(printf '%s\n' "$CACHED_LIST" | sed -n "$((RUNS + 1))p")
if [ -n "$H" ]; then
CURRENT_RUN=1
add_timed cached "magnet:?xt=urn:btih:$H&dn=phase1-noauth" noauth
fi
echo "=== results csv: $RESULTS ($(($(wc -l < "$RESULTS") - 1)) rows) ==="
echo "=== last sandbox log lines ==="
$UC journalctl --user -u decypharr-sandbox -n 8 --no-pager 2>/dev/null | tail -8 | cut -c1-150
+32
View File
@@ -0,0 +1,32 @@
#!/bin/bash
# probe-torbox.sh — check the TorBox mylist shape without printing the key.
set -uo pipefail
DB=/home/ziadarafat/.local/share/rdtc-fresh/db/rdtclient.db
K=$(python3 - "$DB" <<'PY'
import sqlite3, sys
db = sqlite3.connect(sys.argv[1])
r = db.execute("SELECT Value FROM Settings WHERE SettingId='Provider:ApiKey'").fetchone()
print(r[0] if r and r[0] else "")
PY
)
echo " key extracted: $([ -n "$K" ] && echo yes || echo no) (len ${#K})"
[ -n "$K" ] || exit 1
curl -sS -m 60 -H "Authorization: Bearer $K" \
"https://api.torbox.app/v1/api/torrents/mylist?bypass_cache=true" \
-o /tmp/tb.json -w ' http=%{http_code} bytes=%{size_download}\n'
unset K
python3 - <<'PY'
import json
from collections import Counter
try:
d = json.load(open("/tmp/tb.json"))
except Exception as e:
print(" not json:", e); raise SystemExit
data = d.get("data")
print(" top keys:", list(d)[:6], "| err:", d.get("error"), d.get("detail"))
print(" data:", type(data).__name__, "n =", len(data or []))
if isinstance(data, list) and data:
print(" item keys:", list(data[0])[:14])
print(" states:", Counter(x.get("download_state") for x in data).most_common(6))
PY
rm -f /tmp/tb.json