/** * Progressive single-bet ramp: 100 → 500 → 1000 → 2500 → 5000 → (optional higher) * Waits for a fresh betting round between steps so the same users can bet again. * * Usage: * node backend/scripts/loadtest-ramp-single.js --tokens path.json */ const { spawnSync } = require('child_process'); const path = require('path'); const fs = require('fs'); function arg(name, fallback) { const idx = process.argv.indexOf(`--${name}`); if (idx >= 0 && process.argv[idx + 1]) return process.argv[idx + 1]; return fallback; } const betsScript = path.join(__dirname, 'loadtest-bets.js'); const tokens = arg('tokens', ''); const base = arg('base', 'http://127.0.0.1:9080'); const sizes = String(arg('sizes', '100,500,1000,2500,5000')) .split(',') .map((s) => Number(s.trim())) .filter((n) => n > 0); const minSeconds = arg('minSeconds', '40'); const latencyMs = arg('latencyMs', '15000'); const outPath = arg('out', path.join(__dirname, 'loadtest-out', 'ramp-single.json')); if (!tokens) { console.error('Missing --tokens'); process.exit(1); } const rows = []; let lastRound = ''; for (const n of sizes) { console.log(`\n======== RAMP STEP n=${n} lastRound=${lastRound || '-'} ========`); const args = [ betsScript, '--tokens', tokens, '--base', base, '--mode', 'single', '--n', String(n), '--offset', '0', '--minSeconds', String(minSeconds), '--waitMs', '200000', '--latencyMs', String(latencyMs), ]; if (lastRound) { args.push('--requireNewRound', lastRound); } const res = spawnSync(process.execPath, args, { encoding: 'utf8' }); process.stdout.write(res.stdout || ''); process.stderr.write(res.stderr || ''); const roundMatch = (res.stdout || '').match(/ROUND_ID=([0-9a-f-]+)/i); if (roundMatch) lastRound = roundMatch[1]; const summaryMatch = (res.stdout || '').match(/=== SUMMARY ===\n(\{[\s\S]*?\n\})/); let summary = null; if (summaryMatch) { try { summary = JSON.parse(summaryMatch[1]); } catch { /* ignore */ } } const pass = /LOADTEST_PASS=1/.test(res.stdout || ''); rows.push({ n, pass, exitCode: res.status, summary }); fs.mkdirSync(path.dirname(outPath), { recursive: true }); fs.writeFileSync(outPath, JSON.stringify({ at: new Date().toISOString(), rows }, null, 2)); if (!pass) { console.log(`Retry once at n=${n} on next round...`); const args2 = args.filter((a, i) => !(args[i - 1] === '--requireNewRound')); // force new round from lastRound const retryArgs = [ betsScript, '--tokens', tokens, '--base', base, '--mode', 'single', '--n', String(n), '--minSeconds', String(minSeconds), '--waitMs', '200000', '--latencyMs', String(latencyMs), '--requireNewRound', lastRound || 'unknown', ]; const res2 = spawnSync(process.execPath, retryArgs, { encoding: 'utf8' }); process.stdout.write(res2.stdout || ''); process.stderr.write(res2.stderr || ''); const roundMatch2 = (res2.stdout || '').match(/ROUND_ID=([0-9a-f-]+)/i); if (roundMatch2) lastRound = roundMatch2[1]; const summaryMatch2 = (res2.stdout || '').match(/=== SUMMARY ===\n(\{[\s\S]*?\n\})/); let summary2 = null; if (summaryMatch2) { try { summary2 = JSON.parse(summaryMatch2[1]); } catch { /* ignore */ } } const pass2 = /LOADTEST_PASS=1/.test(res2.stdout || ''); rows.push({ n, pass: pass2, exitCode: res2.status, summary: summary2, retry: true }); fs.writeFileSync(outPath, JSON.stringify({ at: new Date().toISOString(), rows }, null, 2)); if (!pass2) { console.log(`Ceiling confirmed below/at n=${n}`); break; } } } console.log('\n=== RAMP TABLE ==='); console.log(JSON.stringify(rows.map((r) => ({ n: r.n, pass: r.pass, retry: !!r.retry, okRate: r.summary?.okRate, p95: r.summary?.p95, rps: r.summary?.rps, byErr: r.summary?.byErr, })), null, 2)); console.log(`Wrote ${outPath}`);