import { createHash } from 'node:crypto'; import { arch, cpus, platform, release } from 'node:os'; const BENCHMARK_DATE = '2026-07-13'; const TRIALS = 100; const REQUESTS_PER_TRIAL = 48; const SERVER_WORKERS = 8; const REQUEST_TIMEOUT_MS = 1500; const scenario = { requests_per_trial: REQUESTS_PER_TRIAL, server_workers: SERVER_WORKERS, request_timeout_ms: REQUEST_TIMEOUT_MS, workload_seeds: 'Integers 1 through 100, using Mulberry32', duration_distribution: { common: '75% from 80 through 240 milliseconds', slower: '20% from 241 through 500 milliseconds', long_tail: '5% from 800 through 1200 milliseconds', }, server_queue: 'FIFO, with timed-out work continuing to completion', }; const strategies = [ { id: 'sequential', label: 'Sequential', description: 'Issue one request and wait for its outcome before issuing the next.', client_limit: 1, }, { id: 'bounded-4', label: 'Bounded concurrency: 4', description: 'Keep at most four requests awaiting a client outcome.', client_limit: 4, }, { id: 'bounded-8', label: 'Bounded concurrency: 8', description: 'Match the client limit to the eight available server workers.', client_limit: 8, }, { id: 'unbounded', label: 'Unbounded concurrency', description: 'Issue all 48 requests at simulated time zero.', client_limit: REQUESTS_PER_TRIAL, }, ]; class MinHeap { constructor(compare) { this.compare = compare; this.items = []; } get size() { return this.items.length; } push(value) { this.items.push(value); let index = this.items.length - 1; while (index > 0) { const parent = Math.floor((index - 1) / 2); if (this.compare(this.items[parent], this.items[index]) <= 0) break; [this.items[parent], this.items[index]] = [this.items[index], this.items[parent]]; index = parent; } } pop() { if (this.items.length === 0) return null; const first = this.items[0]; const last = this.items.pop(); if (this.items.length > 0) { this.items[0] = last; let index = 0; while (true) { const left = index * 2 + 1; const right = left + 1; let smallest = index; if (left < this.items.length && this.compare(this.items[left], this.items[smallest]) < 0) smallest = left; if (right < this.items.length && this.compare(this.items[right], this.items[smallest]) < 0) smallest = right; if (smallest === index) break; [this.items[index], this.items[smallest]] = [this.items[smallest], this.items[index]]; index = smallest; } } return first; } } function seededRandom(seed) { let state = seed >>> 0; return () => { state = (state + 0x6D2B79F5) >>> 0; let value = state; value = Math.imul(value ^ (value >>> 15), value | 1); value ^= value + Math.imul(value ^ (value >>> 7), value | 61); return ((value ^ (value >>> 14)) >>> 0) / 4294967296; }; } function workloadForSeed(seed) { const random = seededRandom(seed); return Array.from({ length: REQUESTS_PER_TRIAL }, (_, id) => { const band = random(); const sample = random(); let durationMs; if (band < 0.75) { durationMs = 80 + Math.floor(sample * 161); } else if (band < 0.95) { durationMs = 241 + Math.floor(sample * 260); } else { durationMs = 800 + Math.floor(sample * 401); } return { id, durationMs }; }); } function percentile(values, fraction) { if (values.length === 0) return 0; const sorted = [...values].sort((left, right) => left - right); return sorted[Math.ceil(sorted.length * fraction) - 1]; } function median(values) { if (values.length === 0) return 0; const sorted = [...values].sort((left, right) => left - right); const middle = Math.floor(sorted.length / 2); return sorted.length % 2 === 0 ? (sorted[middle - 1] + sorted[middle]) / 2 : sorted[middle]; } function simulate(workload, clientLimit) { const events = new MinHeap((left, right) => left.time - right.time || left.priority - right.priority || left.sequence - right.sequence ); const requests = workload.map((request) => ({ ...request, status: 'unissued', issuedAt: null, timeoutAt: null, startedAt: null, completedAt: null, })); const serverQueue = []; const queueWaits = []; let sequence = 0; let nextRequest = 0; let pendingClientOutcomes = 0; let activeServerWork = 0; let peakClientInFlight = 0; let peakServerQueue = 0; let successfulRequests = 0; let timedOutRequests = 0; let clientOutcomeTimeMs = 0; let serviceDrainTimeMs = 0; let workAfterTimeoutMs = 0; const pushEvent = (event) => events.push({ ...event, sequence: sequence++ }); const startServerWork = (time) => { while (activeServerWork < SERVER_WORKERS && serverQueue.length > 0) { const request = serverQueue.shift(); request.startedAt = time; queueWaits.push(time - request.issuedAt); activeServerWork++; pushEvent({ type: 'complete', time: time + request.durationMs, priority: 0, requestId: request.id, }); } }; const issueRequest = (time) => { const request = requests[nextRequest++]; request.status = 'pending'; request.issuedAt = time; request.timeoutAt = time + REQUEST_TIMEOUT_MS; pendingClientOutcomes++; peakClientInFlight = Math.max(peakClientInFlight, pendingClientOutcomes); serverQueue.push(request); pushEvent({ type: 'timeout', time: request.timeoutAt, priority: 1, requestId: request.id, }); startServerWork(time); peakServerQueue = Math.max(peakServerQueue, serverQueue.length); }; const fillClientWindow = (time) => { while (nextRequest < requests.length && pendingClientOutcomes < clientLimit) { issueRequest(time); } }; fillClientWindow(0); while (events.size > 0) { const event = events.pop(); const request = requests[event.requestId]; if (event.type === 'complete') { activeServerWork--; request.completedAt = event.time; serviceDrainTimeMs = Math.max(serviceDrainTimeMs, event.time); if (request.status === 'pending') { request.status = 'successful'; pendingClientOutcomes--; successfulRequests++; clientOutcomeTimeMs = Math.max(clientOutcomeTimeMs, event.time); } else if (request.status === 'timed-out') { workAfterTimeoutMs += event.time - Math.max(request.startedAt, request.timeoutAt); } startServerWork(event.time); fillClientWindow(event.time); continue; } if (request.status === 'pending') { request.status = 'timed-out'; pendingClientOutcomes--; timedOutRequests++; clientOutcomeTimeMs = Math.max(clientOutcomeTimeMs, event.time); fillClientWindow(event.time); } } return { successful_requests: successfulRequests, timed_out_requests: timedOutRequests, client_outcome_time_ms: clientOutcomeTimeMs, service_drain_time_ms: serviceDrainTimeMs, peak_client_in_flight: peakClientInFlight, peak_server_queue: peakServerQueue, median_queue_wait_ms: median(queueWaits), p95_queue_wait_ms: percentile(queueWaits, 0.95), maximum_queue_wait_ms: Math.max(...queueWaits), work_after_timeout_ms: workAfterTimeoutMs, }; } const metricNames = [ 'successful_requests', 'timed_out_requests', 'client_outcome_time_ms', 'service_drain_time_ms', 'peak_client_in_flight', 'peak_server_queue', 'median_queue_wait_ms', 'p95_queue_wait_ms', 'maximum_queue_wait_ms', 'work_after_timeout_ms', ]; const trialWorkloads = Array.from({ length: TRIALS }, (_, index) => workloadForSeed(index + 1)); const reportStrategies = strategies.map((strategy) => { const trials = trialWorkloads.map((workload) => simulate(workload, strategy.client_limit)); const medians = {}; const ranges = {}; for (const metric of metricNames) { const values = trials.map((trial) => trial[metric]); medians[metric] = median(values); ranges[metric] = { minimum: Math.min(...values), maximum: Math.max(...values), }; } return { id: strategy.id, label: strategy.label, description: strategy.description, client_limit: strategy.client_limit, medians, ranges, }; }); const scenarioSha256 = createHash('sha256').update(JSON.stringify(scenario)).digest('hex'); console.log(JSON.stringify({ benchmark: 'sequential-concurrent-requests', benchmark_version: '1.0.0', benchmark_date: BENCHMARK_DATE, scenario, scenario_sha256: scenarioSha256, method: { trials_per_strategy: TRIALS, event_order: 'Ascending simulated millisecond, service completion before timeout, then insertion sequence', server_model: 'Eight workers with a FIFO queue; timed-out work is not canceled', result_statistic: 'Median trial metric, with minimum and maximum retained', timeout_definition: 'A request without a completed response 1500 milliseconds after client issue time', }, environment: { node: process.version, operating_system: `${platform()} ${release()} ${arch()}`, cpu: cpus()[0]?.model ?? 'unknown', logical_cpus: cpus().length, }, strategies: reportStrategies, }, null, 2));