"use strict";
// Scale Tester — Synthetic data generator + load test harness
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateUsers = generateUsers;
exports.benchmark = benchmark;
exports.loadTest = loadTest;
const LANGUAGES = ['typescript', 'python', 'rust', 'javascript', 'go'];
const LANG_WEIGHTS = [0.40, 0.25, 0.15, 0.10, 0.10];
const PATTERN_POOL = {
    typescript: [{ from: 'var', to: 'const' }, { from: 'function', to: 'arrow' }, { from: 'default export', to: 'named export' }, { from: 'let', to: 'const' }, { from: '.then()', to: 'async/await' }],
    python: [{ from: 'def', to: 'async def' }, { from: 'print', to: 'logging' }, { from: 'format()', to: 'f-string' }, { from: 'for loop', to: 'comprehension' }],
    rust: [{ from: 'let mut', to: 'let' }, { from: 'unwrap()', to: '?' }, { from: 'panic!', to: 'Result' }, { from: 'clone()', to: 'borrow' }],
    javascript: [{ from: 'var', to: 'const' }, { from: 'function', to: 'arrow' }, { from: 'callback', to: 'promise' }, { from: '==', to: '===' }],
    go: [{ from: 'interface{}', to: 'generics' }, { from: 'panic', to: 'error return' }, { from: 'go func', to: 'errgroup' }],
};
function weightedRandom(items, weights) {
    const r = Math.random();
    let cumulative = 0;
    for (let i = 0; i < items.length; i++) {
        cumulative += weights[i];
        if (r <= cumulative)
            return items[i];
    }
    return items[items.length - 1];
}
function generateUsers(count) {
    const users = [];
    for (let i = 0; i < count; i++) {
        const lang = weightedRandom(LANGUAGES, LANG_WEIGHTS);
        const pool = PATTERN_POOL[lang] || PATTERN_POOL.typescript;
        const patternCount = 3 + Math.floor(Math.random() * 8);
        const patterns = pool.slice(0, Math.min(patternCount, pool.length)).map(p => ({
            ...p,
            confidence: 0.5 + Math.random() * 0.45,
            signals: 10 + Math.floor(Math.random() * 90),
        }));
        if (Math.random() < 0.05)
            patterns.push({ from: 'const', to: 'let', confidence: 0.3 + Math.random() * 0.3, signals: 1 + Math.floor(Math.random() * 5) });
        if (Math.random() < 0.02)
            patterns.push({ from: 'const', to: 'var', confidence: 0.2 + Math.random() * 0.2, signals: 1 + Math.floor(Math.random() * 3) });
        users.push({ id: `user_${i}`, language: lang, patterns });
    }
    return users;
}
function benchmark(label, fn) {
    const start = performance.now();
    fn();
    const elapsed = performance.now() - start;
    console.log(`${label}: ${elapsed.toFixed(2)}ms`);
    return elapsed;
}
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
async function loadTest(users, targetRps, durationSeconds) {
    const latencies = [];
    let errors = 0;
    const totalRequests = targetRps * durationSeconds;
    const intervalMs = 1000 / targetRps;
    for (let i = 0; i < totalRequests; i++) {
        const start = performance.now();
        try {
            const user = users[i % users.length];
            if (!user) {
                errors++;
                continue;
            }
            const pattern = user.patterns[i % user.patterns.length];
            if (!pattern) {
                errors++;
                continue;
            }
        }
        catch {
            errors++;
        }
        latencies.push(performance.now() - start);
        const elapsed = performance.now() - start;
        const waitMs = Math.max(0, intervalMs - elapsed);
        await sleep(waitMs);
    }
    latencies.sort((a, b) => a - b);
    return {
        totalRequests,
        avgLatency: latencies.reduce((a, b) => a + b, 0) / latencies.length,
        p95Latency: latencies[Math.floor(latencies.length * 0.95)] || 0,
        errors,
    };
}
//# sourceMappingURL=scale-tester.js.map