After the improving of performance1
This commit is contained in:
parent
f334037861
commit
c356d70bc6
3
.gitignore
vendored
3
.gitignore
vendored
@ -30,3 +30,6 @@ redis_data/
|
||||
# Backend exports
|
||||
backend/data/exports/*
|
||||
!backend/data/exports/.gitkeep
|
||||
|
||||
# Loadtest tokens / local run artifacts (contain JWTs)
|
||||
backend/scripts/loadtest-out/
|
||||
|
||||
330
backend/scripts/loadtest-bets.js
Normal file
330
backend/scripts/loadtest-bets.js
Normal file
@ -0,0 +1,330 @@
|
||||
/**
|
||||
* Concurrent candle-predict load test (single | multi | auto).
|
||||
*
|
||||
* Usage (from host):
|
||||
* node backend/scripts/loadtest-bets.js --tokens path.json --mode single --n 500
|
||||
*
|
||||
* Env:
|
||||
* LOADTEST_BASE_URL=http://127.0.0.1:9080
|
||||
* LOADTEST_INTERVAL=1
|
||||
* LOADTEST_STAKE=0.01
|
||||
* LOADTEST_MIN_SECONDS=25 // wait until betting window has at least this many seconds left
|
||||
* LOADTEST_CONCURRENCY=0 // 0 = all at once (N)
|
||||
* LOADTEST_TIMEOUT_MS=30000
|
||||
*/
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const fs = require('fs');
|
||||
const { URL } = require('url');
|
||||
|
||||
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 baseUrl = (arg('base', process.env.LOADTEST_BASE_URL || 'http://127.0.0.1:9080')).replace(/\/$/, '');
|
||||
const mode = String(arg('mode', 'single'));
|
||||
const tokensPath = arg('tokens', process.env.LOADTEST_TOKENS || '');
|
||||
const n = Number(arg('n', process.env.LOADTEST_N || '0'));
|
||||
const offset = Number(arg('offset', process.env.LOADTEST_OFFSET || '0'));
|
||||
const intervalMinutes = Number(arg('interval', process.env.LOADTEST_INTERVAL || '1'));
|
||||
const stakeAmount = Number(arg('stake', process.env.LOADTEST_STAKE || '0.01'));
|
||||
const minSeconds = Number(arg('minSeconds', process.env.LOADTEST_MIN_SECONDS || '25'));
|
||||
const timeoutMs = Number(arg('timeoutMs', process.env.LOADTEST_TIMEOUT_MS || '30000'));
|
||||
const concurrencyArg = Number(arg('concurrency', process.env.LOADTEST_CONCURRENCY || '0'));
|
||||
const waitMs = Number(arg('waitMs', process.env.LOADTEST_WAIT_MS || '120000'));
|
||||
// Soft latency budget for PASS (queueing under burst often exceeds 2s on local Docker).
|
||||
const latencyBudgetMs = Number(arg('latencyMs', process.env.LOADTEST_LATENCY_MS || '10000'));
|
||||
const requireNewRound = String(arg('requireNewRound', process.env.LOADTEST_REQUIRE_NEW_ROUND || '')).trim();
|
||||
const ignoreAlreadyBet = ['1', 'true', 'yes'].includes(
|
||||
String(arg('ignoreAlreadyBet', process.env.LOADTEST_IGNORE_ALREADY_BET || '')).toLowerCase()
|
||||
);
|
||||
|
||||
if (!tokensPath) {
|
||||
console.error('Missing --tokens path.json');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!['single', 'multi', 'auto'].includes(mode)) {
|
||||
console.error('--mode must be single|multi|auto');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const file = JSON.parse(fs.readFileSync(tokensPath, 'utf8'));
|
||||
const allTokens = file.tokens || file;
|
||||
const sliced = n > 0 ? allTokens.slice(offset, offset + n) : allTokens.slice(offset);
|
||||
const selected = sliced.map((t) => (typeof t === 'string' ? { token: t } : t));
|
||||
if (!selected.length) {
|
||||
console.error(`No tokens loaded (offset=${offset} n=${n} total=${allTokens.length})`);
|
||||
process.exit(1);
|
||||
}
|
||||
const concurrency = concurrencyArg > 0 ? concurrencyArg : selected.length;
|
||||
|
||||
function request(method, path, { headers = {}, body, timeout = timeoutMs } = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const url = new URL(path, baseUrl);
|
||||
const lib = url.protocol === 'https:' ? https : http;
|
||||
const payload = body ? Buffer.from(body) : null;
|
||||
const req = lib.request(
|
||||
url,
|
||||
{
|
||||
method,
|
||||
headers: {
|
||||
...headers,
|
||||
...(payload
|
||||
? { 'Content-Type': 'application/json', 'Content-Length': payload.length }
|
||||
: {}),
|
||||
},
|
||||
timeout,
|
||||
},
|
||||
(res) => {
|
||||
const chunks = [];
|
||||
res.on('data', (c) => chunks.push(c));
|
||||
res.on('end', () => {
|
||||
const raw = Buffer.concat(chunks).toString('utf8');
|
||||
let json = null;
|
||||
try {
|
||||
json = raw ? JSON.parse(raw) : null;
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
resolve({ status: res.statusCode || 0, headers: res.headers, raw, json });
|
||||
});
|
||||
}
|
||||
);
|
||||
req.on('timeout', () => req.destroy(new Error(`timeout ${method} ${path}`)));
|
||||
req.on('error', reject);
|
||||
if (payload) req.write(payload);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
function percentile(sorted, p) {
|
||||
if (!sorted.length) return null;
|
||||
const idx = Math.min(sorted.length - 1, Math.max(0, Math.ceil((p / 100) * sorted.length) - 1));
|
||||
return sorted[idx];
|
||||
}
|
||||
|
||||
async function waitForBettingWindow() {
|
||||
const path = `/api/v1/games/candle-predict/state?intervalMinutes=${intervalMinutes}`;
|
||||
const started = Date.now();
|
||||
let last = null;
|
||||
while (Date.now() - started < waitMs) {
|
||||
const token = selected[0].token;
|
||||
const res = await request('GET', path, {
|
||||
headers: { Authorization: `Bearer ${token}`, Accept: 'application/json' },
|
||||
});
|
||||
last = res;
|
||||
const round = res.json?.round;
|
||||
const status = round?.status;
|
||||
const sec = Number(round?.secondsToClose ?? 0);
|
||||
const roundId = round?.id || '';
|
||||
const isNewEnough = !requireNewRound || roundId !== requireNewRound;
|
||||
if (res.status === 200 && status === 'betting' && sec >= minSeconds && isNewEnough) {
|
||||
console.log(`[wait] betting ready secondsToClose=${sec} round=${roundId}`);
|
||||
return round;
|
||||
}
|
||||
const msg = `status=${res.status} roundStatus=${status || '-'} sec=${sec || 0} round=${roundId || '-'} new=${isNewEnough}`;
|
||||
process.stdout.write(`[wait] ${msg}\n`);
|
||||
await new Promise((r) => setTimeout(r, 1500));
|
||||
}
|
||||
throw new Error(
|
||||
`Timed out waiting for betting window (>=${minSeconds}s). last=${JSON.stringify(last?.json?.round || last?.raw?.slice?.(0, 200))}`
|
||||
);
|
||||
}
|
||||
|
||||
function buildBody(i) {
|
||||
if (mode === 'single') {
|
||||
return JSON.stringify({
|
||||
direction: i % 2 === 0 ? 'long' : 'short',
|
||||
stakeAmount,
|
||||
intervalMinutes,
|
||||
stakeAccountKind: 'main',
|
||||
idempotencyKey: `lt-single-${Date.now()}-${i}`,
|
||||
});
|
||||
}
|
||||
if (mode === 'multi') {
|
||||
return JSON.stringify({
|
||||
level: 2,
|
||||
sequence: i % 2 === 0 ? ['long', 'short'] : ['short', 'long'],
|
||||
stakeAmount,
|
||||
intervalMinutes,
|
||||
stakeAccountKind: 'main',
|
||||
idempotencyKey: `lt-multi-${Date.now()}-${i}`,
|
||||
});
|
||||
}
|
||||
// auto
|
||||
return JSON.stringify({
|
||||
enabled: true,
|
||||
direction: i % 2 === 0 ? 'long' : 'short',
|
||||
stakeAmount,
|
||||
mode: 'each_round',
|
||||
intervalMinutes,
|
||||
stakeAccountKind: 'main',
|
||||
validateWalletBalance: true,
|
||||
attemptImmediateBet: false,
|
||||
});
|
||||
}
|
||||
|
||||
function endpoint() {
|
||||
if (mode === 'single') return '/api/v1/games/candle-predict/bets';
|
||||
if (mode === 'multi') return '/api/v1/games/candle-predict/bets/multi';
|
||||
return '/api/v1/games/candle-predict/auto';
|
||||
}
|
||||
|
||||
function method() {
|
||||
return mode === 'auto' ? 'PUT' : 'POST';
|
||||
}
|
||||
|
||||
function isSuccess(status) {
|
||||
if (mode === 'auto') return status === 200 || status === 201;
|
||||
return status === 201;
|
||||
}
|
||||
|
||||
async function mapPool(items, limit, fn) {
|
||||
const results = new Array(items.length);
|
||||
let next = 0;
|
||||
async function worker() {
|
||||
while (true) {
|
||||
const i = next++;
|
||||
if (i >= items.length) return;
|
||||
results[i] = await fn(items[i], i);
|
||||
}
|
||||
}
|
||||
const workers = Array.from({ length: Math.min(limit, items.length) }, () => worker());
|
||||
await Promise.all(workers);
|
||||
return results;
|
||||
}
|
||||
|
||||
function classifyError(status, raw, json) {
|
||||
const msg = String(json?.error || json?.message || raw || '').toLowerCase();
|
||||
if (status === 429) return 'rate_limit';
|
||||
if (status === 0) return 'network';
|
||||
if (status >= 500) return 'server_5xx';
|
||||
if (msg.includes('window') || msg.includes('closed')) return 'window_closed';
|
||||
if (msg.includes('already')) return 'already_bet';
|
||||
if (msg.includes('insufficient')) return 'insufficient_balance';
|
||||
if (msg.includes('disabled')) return 'disabled';
|
||||
if (msg.includes('in_progress') || msg.includes('multi')) return 'multi_conflict';
|
||||
return `http_${status}`;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log('=== LOADTEST candle-predict ===');
|
||||
console.log(
|
||||
JSON.stringify({
|
||||
baseUrl,
|
||||
mode,
|
||||
n: selected.length,
|
||||
offset,
|
||||
concurrency,
|
||||
intervalMinutes,
|
||||
stakeAmount,
|
||||
minSeconds,
|
||||
requireNewRound: requireNewRound || null,
|
||||
})
|
||||
);
|
||||
|
||||
let round = null;
|
||||
if (mode !== 'auto') {
|
||||
round = await waitForBettingWindow();
|
||||
} else {
|
||||
console.log('[wait] auto mode skips betting-window gate');
|
||||
}
|
||||
if (round?.id) console.log(`ROUND_ID=${round.id}`);
|
||||
|
||||
const path = endpoint();
|
||||
const httpMethod = method();
|
||||
const t0 = Date.now();
|
||||
const results = await mapPool(selected, concurrency, async (u, i) => {
|
||||
const started = Date.now();
|
||||
try {
|
||||
const res = await request(httpMethod, path, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${u.token}`,
|
||||
Accept: 'application/json',
|
||||
'Idempotency-Key': `lt-${mode}-${Date.now()}-${i}`,
|
||||
},
|
||||
body: buildBody(i),
|
||||
});
|
||||
return {
|
||||
i,
|
||||
status: res.status,
|
||||
ms: Date.now() - started,
|
||||
ok: isSuccess(res.status),
|
||||
err: isSuccess(res.status) ? null : classifyError(res.status, res.raw, res.json),
|
||||
snippet: isSuccess(res.status) ? null : String(res.raw || '').slice(0, 160),
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
i,
|
||||
status: 0,
|
||||
ms: Date.now() - started,
|
||||
ok: false,
|
||||
err: 'network',
|
||||
snippet: String(e.message || e).slice(0, 160),
|
||||
};
|
||||
}
|
||||
});
|
||||
const elapsed = Date.now() - t0;
|
||||
const latencies = results.map((r) => r.ms).sort((a, b) => a - b);
|
||||
const eligible = ignoreAlreadyBet
|
||||
? results.filter((r) => r.ok || r.err !== 'already_bet')
|
||||
: results;
|
||||
const okCount = results.filter((r) => r.ok).length;
|
||||
const okRate = ignoreAlreadyBet
|
||||
? eligible.length
|
||||
? eligible.filter((r) => r.ok).length / eligible.length
|
||||
: 0
|
||||
: okCount / selected.length;
|
||||
const byStatus = {};
|
||||
const byErr = {};
|
||||
for (const r of results) {
|
||||
byStatus[r.status] = (byStatus[r.status] || 0) + 1;
|
||||
if (!r.ok) byErr[r.err || 'unknown'] = (byErr[r.err || 'unknown'] || 0) + 1;
|
||||
}
|
||||
const summary = {
|
||||
mode,
|
||||
n: selected.length,
|
||||
offset,
|
||||
concurrency,
|
||||
roundId: round?.id || null,
|
||||
elapsedMs: elapsed,
|
||||
rps: selected.length / (elapsed / 1000),
|
||||
ok: okCount,
|
||||
okRate,
|
||||
p50: percentile(latencies, 50),
|
||||
p95: percentile(latencies, 95),
|
||||
p99: percentile(latencies, 99),
|
||||
max: latencies[latencies.length - 1] || null,
|
||||
byStatus,
|
||||
byErr,
|
||||
failSamples: results.filter((r) => !r.ok).slice(0, 8),
|
||||
};
|
||||
console.log('=== SUMMARY ===');
|
||||
console.log(JSON.stringify(summary, null, 2));
|
||||
|
||||
const server5xx = (byErr.server_5xx || 0) / selected.length;
|
||||
const rateLimited = (byErr.rate_limit || 0) / selected.length;
|
||||
const latencyOk = summary.p95 == null || summary.p95 <= latencyBudgetMs;
|
||||
const pass =
|
||||
summary.okRate >= 0.99 &&
|
||||
latencyOk &&
|
||||
server5xx <= 0.005 &&
|
||||
rateLimited <= 0.01;
|
||||
console.log(
|
||||
JSON.stringify({
|
||||
latencyBudgetMs,
|
||||
latencyOk,
|
||||
server5xxRate: server5xx,
|
||||
rateLimitedRate: rateLimited,
|
||||
})
|
||||
);
|
||||
console.log(`LOADTEST_PASS=${pass ? 1 : 0}`);
|
||||
process.exit(pass ? 0 : 2);
|
||||
}
|
||||
|
||||
main().catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
18
backend/scripts/loadtest-cancel-multi.js
Normal file
18
backend/scripts/loadtest-cancel-multi.js
Normal file
@ -0,0 +1,18 @@
|
||||
const db = require('/app/dist/db/connection');
|
||||
(async () => {
|
||||
const rows = await db.query(
|
||||
`UPDATE candle_predict_bets b
|
||||
SET status = 'refunded'
|
||||
FROM users u
|
||||
WHERE b.user_id = u.id
|
||||
AND u.first_name LIKE 'loadtest_%'
|
||||
AND b.prediction_mode = 'multi_candle'
|
||||
AND b.status = 'in_progress'
|
||||
RETURNING b.id`
|
||||
);
|
||||
console.log(JSON.stringify({ cleared: rows.length }));
|
||||
process.exit(0);
|
||||
})().catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
35
backend/scripts/loadtest-count-auto.js
Normal file
35
backend/scripts/loadtest-count-auto.js
Normal file
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Count auto bets placed for loadtest users after enabling auto.
|
||||
* Run inside API container:
|
||||
* node /tmp/loadtest-count-auto.js [minutesBack]
|
||||
*/
|
||||
const db = require('/app/dist/db/connection');
|
||||
const minutesBack = Number(process.argv[2] || 10);
|
||||
|
||||
(async () => {
|
||||
const rows = await db.query(
|
||||
`SELECT COUNT(*)::int AS cnt
|
||||
FROM candle_predict_bets b
|
||||
JOIN users u ON u.id = b.user_id
|
||||
WHERE u.first_name LIKE 'loadtest_%'
|
||||
AND b.is_auto = TRUE
|
||||
AND b.created_at > NOW() - ($1 || ' minutes')::interval`,
|
||||
[String(minutesBack)]
|
||||
);
|
||||
const byStatus = await db.query(
|
||||
`SELECT b.status, COUNT(*)::int AS cnt
|
||||
FROM candle_predict_bets b
|
||||
JOIN users u ON u.id = b.user_id
|
||||
WHERE u.first_name LIKE 'loadtest_%'
|
||||
AND b.is_auto = TRUE
|
||||
AND b.created_at > NOW() - ($1 || ' minutes')::interval
|
||||
GROUP BY b.status
|
||||
ORDER BY b.status`,
|
||||
[String(minutesBack)]
|
||||
);
|
||||
console.log(JSON.stringify({ minutesBack, count: rows[0]?.cnt || 0, byStatus }, null, 2));
|
||||
process.exit(0);
|
||||
})().catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
19
backend/scripts/loadtest-disable-auto.js
Normal file
19
backend/scripts/loadtest-disable-auto.js
Normal file
@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Disable auto for all loadtest_* users.
|
||||
* Run in API container: node /tmp/loadtest-disable-auto.js
|
||||
*/
|
||||
const db = require('/app/dist/db/connection');
|
||||
(async () => {
|
||||
const rows = await db.query(
|
||||
`UPDATE candle_predict_auto_settings a
|
||||
SET enabled = FALSE
|
||||
FROM users u
|
||||
WHERE a.user_id = u.id AND u.first_name LIKE 'loadtest_%' AND a.enabled = TRUE
|
||||
RETURNING a.user_id`
|
||||
);
|
||||
console.log(JSON.stringify({ disabled: rows.length }));
|
||||
process.exit(0);
|
||||
})().catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
23
backend/scripts/loadtest-raise-rate-limit.js
Normal file
23
backend/scripts/loadtest-raise-rate-limit.js
Normal file
@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Raise API rate_limit_max in system_settings (bypasses admin clamp).
|
||||
* Run inside crypto_wallet_api:
|
||||
* node /tmp/loadtest-raise-rate-limit.js [max]
|
||||
*/
|
||||
const db = require('/app/dist/db/connection');
|
||||
const max = String(process.argv[2] || '100000');
|
||||
|
||||
(async () => {
|
||||
await db.query(
|
||||
`INSERT INTO system_settings (key, value) VALUES ('rate_limit_max', $1)
|
||||
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value`,
|
||||
[max]
|
||||
);
|
||||
const rows = await db.query(
|
||||
`SELECT key, value FROM system_settings WHERE key = 'rate_limit_max'`
|
||||
);
|
||||
console.log(JSON.stringify(rows));
|
||||
process.exit(0);
|
||||
})().catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
137
backend/scripts/loadtest-ramp-single.js
Normal file
137
backend/scripts/loadtest-ramp-single.js
Normal file
@ -0,0 +1,137 @@
|
||||
/**
|
||||
* 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}`);
|
||||
125
backend/scripts/loadtest-seed-users.js
Normal file
125
backend/scripts/loadtest-seed-users.js
Normal file
@ -0,0 +1,125 @@
|
||||
/**
|
||||
* Seed N disposable loadtest users + fund main ledger + mint JWTs.
|
||||
*
|
||||
* Run INSIDE crypto_wallet_api (has DB + JWT_SECRET):
|
||||
* node /tmp/loadtest-seed-users.js <count> [outPath]
|
||||
*
|
||||
* Writes JSON: [{ i, userId, token }, ...]
|
||||
* email_hash prefix: loadtest_ (padded to 64 hex-like chars)
|
||||
*/
|
||||
const crypto = require('crypto');
|
||||
const fs = require('fs');
|
||||
const jwt = require('/app/node_modules/jsonwebtoken');
|
||||
const db = require('/app/dist/db/connection');
|
||||
|
||||
const count = Math.max(1, Number(process.argv[2] || 100));
|
||||
const outPath = process.argv[3] || '/tmp/loadtest-tokens.json';
|
||||
const creditRaw = BigInt(process.env.LOADTEST_CREDIT_RAW || '100000000'); // 100 USDT @ 6dp
|
||||
const HASH_PREFIX = 'loadtest_';
|
||||
|
||||
function emailHashFor(i) {
|
||||
// 64-char unique hash-looking string
|
||||
const dig = crypto.createHash('sha256').update(`${HASH_PREFIX}${i}`).digest('hex');
|
||||
return dig;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const wtRows = await db.query(
|
||||
`SELECT id, decimals FROM wallet_types WHERE code = 'USDT_TRC20' LIMIT 1`
|
||||
);
|
||||
if (!wtRows[0]) throw new Error('USDT_TRC20 wallet type missing');
|
||||
const walletTypeId = wtRows[0].id;
|
||||
|
||||
const existing = await db.query(
|
||||
`SELECT id, email_hash, COALESCE(token_version, 1) AS token_version
|
||||
FROM users
|
||||
WHERE email_hash LIKE $1
|
||||
ORDER BY created_at ASC`,
|
||||
// sha256 hex never starts with loadtest_ — we store marker in first_name instead
|
||||
['__never__']
|
||||
);
|
||||
void existing;
|
||||
|
||||
// Prefer lookup by first_name marker for re-runs
|
||||
const prior = await db.query(
|
||||
`SELECT id, email_hash, COALESCE(token_version, 1) AS token_version, first_name
|
||||
FROM users
|
||||
WHERE first_name LIKE 'loadtest_%'
|
||||
ORDER BY first_name ASC`
|
||||
);
|
||||
const byIndex = new Map();
|
||||
for (const u of prior) {
|
||||
const m = String(u.first_name || '').match(/^loadtest_(\d+)$/);
|
||||
if (m) byIndex.set(Number(m[1]), u);
|
||||
}
|
||||
|
||||
const tokens = [];
|
||||
const secret = process.env.JWT_SECRET;
|
||||
if (!secret) throw new Error('JWT_SECRET missing');
|
||||
|
||||
const batchSize = 100;
|
||||
for (let start = 0; start < count; start += batchSize) {
|
||||
const end = Math.min(count, start + batchSize);
|
||||
for (let i = start; i < end; i++) {
|
||||
let user = byIndex.get(i);
|
||||
if (!user) {
|
||||
const eh = emailHashFor(i);
|
||||
const rows = await db.query(
|
||||
`INSERT INTO users (email_hash, password_hash, role_id, first_name, email_verified, is_enabled)
|
||||
VALUES ($1, 'loadtest-hash', 1, $2, TRUE, TRUE)
|
||||
ON CONFLICT (email_hash) DO UPDATE SET first_name = EXCLUDED.first_name
|
||||
RETURNING id, email_hash, COALESCE(token_version, 1) AS token_version, first_name`,
|
||||
[eh, `loadtest_${i}`]
|
||||
);
|
||||
user = rows[0];
|
||||
// ensure user_roles
|
||||
await db.query(
|
||||
`INSERT INTO user_roles (user_id, role_id) VALUES ($1, 1) ON CONFLICT DO NOTHING`,
|
||||
[user.id]
|
||||
);
|
||||
}
|
||||
|
||||
// Ensure ledger account + top-up available balance to at least creditRaw
|
||||
const accRows = await db.query(
|
||||
`INSERT INTO ledger_accounts (user_id, wallet_type_id, account_kind, available_balance, locked_balance)
|
||||
VALUES ($1, $2, 'main', $3, 0)
|
||||
ON CONFLICT (user_id, wallet_type_id, account_kind) DO UPDATE
|
||||
SET available_balance = GREATEST(ledger_accounts.available_balance, EXCLUDED.available_balance),
|
||||
updated_at = NOW()
|
||||
RETURNING id, available_balance::text`,
|
||||
[user.id, walletTypeId, creditRaw.toString()]
|
||||
);
|
||||
|
||||
// If ON CONFLICT didn't raise balance (older row higher already), ensure floor via UPDATE
|
||||
if (accRows[0] && BigInt(accRows[0].available_balance) < creditRaw) {
|
||||
await db.query(
|
||||
`UPDATE ledger_accounts SET available_balance = $1, updated_at = NOW() WHERE id = $2`,
|
||||
[creditRaw.toString(), accRows[0].id]
|
||||
);
|
||||
}
|
||||
|
||||
const token = jwt.sign(
|
||||
{
|
||||
userId: user.id,
|
||||
emailHash: user.email_hash,
|
||||
role: 'user',
|
||||
roles: ['user'],
|
||||
permissions: [],
|
||||
primaryRole: 'user',
|
||||
tv: Number(user.token_version) || 1,
|
||||
},
|
||||
secret,
|
||||
{ expiresIn: '6h' }
|
||||
);
|
||||
tokens.push({ i, userId: user.id, token });
|
||||
}
|
||||
process.stdout.write(`seeded ${end}/${count}\n`);
|
||||
}
|
||||
|
||||
fs.writeFileSync(outPath, JSON.stringify({ count: tokens.length, walletTypeId, creditRaw: creditRaw.toString(), tokens }));
|
||||
console.log(JSON.stringify({ ok: true, count: tokens.length, outPath }));
|
||||
process.exit(0);
|
||||
})().catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user