100 lines
4.1 KiB
JavaScript
100 lines
4.1 KiB
JavaScript
#!/usr/bin/env node
|
|
// Scrape benchmark: time-to-first-stream and total time over SSE for movie 550 and tv 1399 S1E1.
|
|
//
|
|
// node scripts/bench.js [--base http://127.0.0.1:8789] [--old http://127.0.0.1:8788]
|
|
// [--providers a,b,c] [--runs 1] [--cached]
|
|
//
|
|
// --old benchmarks a v4 server (GET /streams, JSON only: first stream == total).
|
|
// Runs bypass the v5 result cache (fresh=1) unless --cached is given. TMDB counters come from
|
|
// the `done` event's debug field: `calls` = TMDB requests the providers made (what v4 sent to
|
|
// the network), `network` = requests the v5 main thread actually sent to TMDB.
|
|
const args = process.argv.slice(2);
|
|
const opt = (name, d) => {
|
|
const i = args.indexOf(`--${name}`);
|
|
return i >= 0 ? args[i + 1] : d;
|
|
};
|
|
const flag = (name) => args.includes(`--${name}`);
|
|
|
|
const BASE = opt('base', 'http://127.0.0.1:8789').replace(/\/+$/, '');
|
|
const OLD = opt('old', '')?.replace(/\/+$/, '');
|
|
const RUNS = parseInt(opt('runs', '1'), 10);
|
|
const CASES = [
|
|
{ label: 'movie 550', q: { tmdb: '550', type: 'movie' } },
|
|
{ label: 'tv 1399 S1E1', q: { tmdb: '1399', type: 'tv', season: '1', episode: '1' } },
|
|
];
|
|
|
|
async function providers() {
|
|
const p = opt('providers', '');
|
|
if (p) return p;
|
|
const r = await fetch(`${BASE}/v1/providers`).then((r) => r.json());
|
|
return r.defaults.join(',');
|
|
}
|
|
|
|
// minimal SSE reader over fetch
|
|
async function sseRun(url) {
|
|
const t0 = performance.now();
|
|
const res = await fetch(url, { headers: { Accept: 'text/event-stream' } });
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
|
const dec = new TextDecoder();
|
|
let buf = '';
|
|
const out = { first: null, total: null, streams: 0, providers: {}, done: null };
|
|
for await (const chunk of res.body) {
|
|
buf += dec.decode(chunk, { stream: true });
|
|
let i;
|
|
while ((i = buf.indexOf('\n\n')) >= 0) {
|
|
const block = buf.slice(0, i);
|
|
buf = buf.slice(i + 2);
|
|
const ev = /^event: (.*)$/m.exec(block)?.[1];
|
|
const data = /^data: (.*)$/m.exec(block)?.[1];
|
|
if (!ev || !data) continue;
|
|
const d = JSON.parse(data);
|
|
const t = performance.now() - t0;
|
|
if (ev === 'streams' && d.streams.length) {
|
|
if (out.first == null) out.first = { ms: t, provider: d.provider };
|
|
out.streams += d.streams.length;
|
|
} else if (ev === 'provider') out.providers[d.name] = `${d.status}:${d.ms}`;
|
|
else if (ev === 'done') {
|
|
out.total = t;
|
|
out.done = d;
|
|
}
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
async function oldRun(url) {
|
|
const t0 = performance.now();
|
|
const r = await fetch(url).then((r) => r.json());
|
|
const t = performance.now() - t0;
|
|
return { first: r.streams?.length ? { ms: t, provider: '(all)' } : null, total: t, streams: r.streams?.length || 0, errors: r.providerErrors };
|
|
}
|
|
|
|
const fmt = (ms) => (ms == null ? ' - ' : `${(ms / 1000).toFixed(2).padStart(6)}s`);
|
|
|
|
(async () => {
|
|
const list = await providers();
|
|
console.log(`providers (${list.split(',').length}): ${list}\n`);
|
|
const qs = (q, extra = {}) => new URLSearchParams({ ...q, providers: list, ...extra }).toString();
|
|
for (const c of CASES) {
|
|
for (let run = 1; run <= RUNS; run++) {
|
|
const tag = `${c.label}${RUNS > 1 ? ` #${run}` : ''}`;
|
|
const r = await sseRun(`${BASE}/v1/streams?${qs(c.q, flag('cached') ? {} : { fresh: '1' })}`);
|
|
const tm = r.done?.debug?.tmdb;
|
|
console.log(
|
|
`v5 ${tag.padEnd(16)} first ${fmt(r.first?.ms)} (${r.first?.provider || 'none'}) total ${fmt(r.total)} streams ${r.streams}` +
|
|
(tm ? ` tmdb: providers asked ${tm.calls} (worker-local ${tm.local}, via main ${tm.remote}), network ${tm.network}` : '')
|
|
);
|
|
if (flag('verbose')) console.log(' ', r.providers);
|
|
if (OLD) {
|
|
const o = await oldRun(`${OLD}/streams?${qs(c.q)}`);
|
|
console.log(`v4 ${tag.padEnd(16)} first ${fmt(o.first?.ms)} total ${fmt(o.total)} streams ${o.streams}`);
|
|
}
|
|
}
|
|
}
|
|
const s = await fetch(`${BASE}/v1/providers`).then((r) => r.json()).catch(() => null);
|
|
if (s?.stats?.tmdb) console.log(`\nv5 TMDB totals since boot: ${JSON.stringify(s.stats.tmdb)}`);
|
|
})().catch((e) => {
|
|
console.error(e.message);
|
|
process.exit(1);
|
|
});
|