|
| 1 | +/** |
| 2 | + * Cost of one concurrency-sweeper pass (`scanConcurrencySets`) at different SCAN COUNTs. |
| 3 | + * |
| 4 | + * Seeds a testcontainers Redis with BENCH_SETS queue currentConcurrency sets plus |
| 5 | + * BENCH_UNRELATED_PER_SET non-matching keys per set (default 4,000 sets x (1 set + 99 |
| 6 | + * unrelated) = 400k keys, 1% matching), then runs one full sweep per entry in |
| 7 | + * BENCH_SCAN_COUNTS and reports SCAN calls, SSCAN calls, total commands, server usec for |
| 8 | + * each, Redis process CPU, wall time, peak RSS during the sweep, and whether the callback |
| 9 | + * saw exactly the seeded run ids. |
| 10 | + * |
| 11 | + * Run on demand: `pnpm run test:bench src/run-queue/bench/concurrencySweeperScan --silent=false`. |
| 12 | + * Knobs: BENCH_SETS, BENCH_UNRELATED_PER_SET, BENCH_SCAN_COUNTS (default "10,1000"). |
| 13 | + */ |
| 14 | +import { redisTest } from "@internal/testcontainers"; |
| 15 | +import { trace } from "@internal/tracing"; |
| 16 | +import { Logger } from "@trigger.dev/core/logger"; |
| 17 | +import { describe } from "vitest"; |
| 18 | +import { FairQueueSelectionStrategy } from "../fairQueueSelectionStrategy.js"; |
| 19 | +import { RunQueue } from "../index.js"; |
| 20 | +import { RunQueueFullKeyProducer } from "../keyProducer.js"; |
| 21 | +import { seedSweeperKeyspace } from "../tests/sweeperKeyspace.js"; |
| 22 | + |
| 23 | +vi.setConfig({ testTimeout: 900_000 }); |
| 24 | + |
| 25 | +const SETS = Number(process.env.BENCH_SETS ?? 4_000); |
| 26 | +const UNRELATED_PER_SET = Number(process.env.BENCH_UNRELATED_PER_SET ?? 99); |
| 27 | +const SCAN_COUNTS = (process.env.BENCH_SCAN_COUNTS ?? "10,1000").split(",").map(Number); |
| 28 | +const KEY_PREFIX = "engine:runqueue:"; |
| 29 | + |
| 30 | +function createQueue(redisContainer: any, scanCount: number, seen: Set<string>) { |
| 31 | + const redis = { |
| 32 | + keyPrefix: KEY_PREFIX, |
| 33 | + host: redisContainer.getHost(), |
| 34 | + port: redisContainer.getPort(), |
| 35 | + }; |
| 36 | + return new RunQueue({ |
| 37 | + name: "rq", |
| 38 | + tracer: trace.getTracer("rq"), |
| 39 | + logger: new Logger("RunQueue", "warn"), |
| 40 | + defaultEnvConcurrency: 10, |
| 41 | + keys: new RunQueueFullKeyProducer(), |
| 42 | + masterQueueConsumersDisabled: true, |
| 43 | + workerOptions: { disabled: true }, |
| 44 | + queueSelectionStrategy: new FairQueueSelectionStrategy({ |
| 45 | + redis, |
| 46 | + keys: new RunQueueFullKeyProducer(), |
| 47 | + }), |
| 48 | + redis, |
| 49 | + concurrencySweeper: { |
| 50 | + scanCount, |
| 51 | + callback: async (runIds) => { |
| 52 | + runIds.forEach((id) => seen.add(id)); |
| 53 | + return []; |
| 54 | + }, |
| 55 | + }, |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +async function stats(queue: RunQueue) { |
| 60 | + const [commandstats, info, cpu] = await Promise.all([ |
| 61 | + queue.redis.info("commandstats"), |
| 62 | + queue.redis.info("stats"), |
| 63 | + queue.redis.info("cpu"), |
| 64 | + ]); |
| 65 | + const cpuSeconds = (name: string) => Number(cpu.match(new RegExp(`${name}:([\\d.]+)`))![1]); |
| 66 | + const cmd = (name: string) => { |
| 67 | + const m = commandstats.match(new RegExp(`cmdstat_${name}:calls=(\\d+),usec=(\\d+)`)); |
| 68 | + return { calls: m ? Number(m[1]) : 0, usec: m ? Number(m[2]) : 0 }; |
| 69 | + }; |
| 70 | + return { |
| 71 | + scan: cmd("scan"), |
| 72 | + sscan: cmd("sscan"), |
| 73 | + total: Number(info.match(/total_commands_processed:(\d+)/)![1]), |
| 74 | + // Whole-process Redis CPU, including the per-call network/parse cost commandstats leaves out. |
| 75 | + cpuMs: (cpuSeconds("used_cpu_user") + cpuSeconds("used_cpu_sys")) * 1000, |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +describe("concurrency sweeper SCAN COUNT benchmark", () => { |
| 80 | + redisTest("one full sweep per SCAN COUNT", async ({ redisContainer }) => { |
| 81 | + const seedQueue = createQueue(redisContainer, 1000, new Set()); |
| 82 | + let expected: Set<string>; |
| 83 | + let dbsize: number; |
| 84 | + try { |
| 85 | + const startedAt = performance.now(); |
| 86 | + expected = await seedSweeperKeyspace(seedQueue.redis, { |
| 87 | + concurrencySets: SETS, |
| 88 | + unrelatedPerSet: UNRELATED_PER_SET, |
| 89 | + }); |
| 90 | + dbsize = await seedQueue.redis.dbsize(); |
| 91 | + console.log( |
| 92 | + `seeded ${dbsize} keys (${SETS} matching sets, ${expected.size} run ids) in ${Math.round( |
| 93 | + performance.now() - startedAt |
| 94 | + )}ms` |
| 95 | + ); |
| 96 | + } finally { |
| 97 | + await seedQueue.quit(); |
| 98 | + } |
| 99 | + |
| 100 | + const results: Array<Record<string, number | string | boolean>> = []; |
| 101 | + for (const scanCount of SCAN_COUNTS) { |
| 102 | + const seen = new Set<string>(); |
| 103 | + const queue = createQueue(redisContainer, scanCount, seen); |
| 104 | + try { |
| 105 | + const rssBefore = process.memoryUsage.rss(); |
| 106 | + let rssPeak = rssBefore; |
| 107 | + const sampler = setInterval(() => { |
| 108 | + rssPeak = Math.max(rssPeak, process.memoryUsage.rss()); |
| 109 | + }, 5); |
| 110 | + |
| 111 | + const before = await stats(queue); |
| 112 | + const startedAt = performance.now(); |
| 113 | + const sweep = await queue["scanConcurrencySets"](); |
| 114 | + const wallMs = performance.now() - startedAt; |
| 115 | + const after = await stats(queue); |
| 116 | + clearInterval(sampler); |
| 117 | + rssPeak = Math.max(rssPeak, process.memoryUsage.rss()); |
| 118 | + |
| 119 | + const identical = seen.size === expected.size && [...seen].every((id) => expected.has(id)); |
| 120 | + results.push({ |
| 121 | + scanCount, |
| 122 | + scanCalls: after.scan.calls - before.scan.calls, |
| 123 | + scanServerMs: Math.round((after.scan.usec - before.scan.usec) / 1000), |
| 124 | + sscanCalls: after.sscan.calls - before.sscan.calls, |
| 125 | + sscanServerMs: Math.round((after.sscan.usec - before.sscan.usec) / 1000), |
| 126 | + totalCommands: after.total - before.total, |
| 127 | + redisCpuMs: Math.round(after.cpuMs - before.cpuMs), |
| 128 | + wallMs: Math.round(wallMs), |
| 129 | + rssBeforeMB: Math.round(rssBefore / 2 ** 20), |
| 130 | + rssPeakMB: Math.round(rssPeak / 2 ** 20), |
| 131 | + keysProcessed: sweep?.processedKeys ?? 0, |
| 132 | + runIdsSeen: seen.size, |
| 133 | + identical, |
| 134 | + }); |
| 135 | + expect(identical).toBe(true); |
| 136 | + } finally { |
| 137 | + await queue.quit(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + console.log(`dbsize=${dbsize}`); |
| 142 | + console.table(results); |
| 143 | + }); |
| 144 | +}); |
0 commit comments