|
| 1 | +import { readFile } from "node:fs/promises" |
| 2 | +import { cpus, platform, release } from "node:os" |
| 3 | +import { createRuntime } from "solid-objects" |
| 4 | +import { sqlite } from "solid-objects/database/sqlite" |
| 5 | +import type { WakeUpAdapter, WakeUpRole, WakeUpWatch } from "../src/wake-up.ts" |
| 6 | + |
| 7 | +const roles = ["actors", "effects", "reminders", "broadcasts"] as const |
| 8 | +const intervals = option("intervals", "20,100,500") |
| 9 | + .split(",") |
| 10 | + .map((value) => positiveNumber(value, "intervals")) |
| 11 | +const warmupMilliseconds = positiveNumber(option("warmup", "3000"), "warmup") |
| 12 | +const durationMilliseconds = positiveNumber(option("duration", "10000"), "duration") |
| 13 | + |
| 14 | +async function main(): Promise<void> { |
| 15 | + const packageMetadata = JSON.parse( |
| 16 | + await readFile(new URL("../package.json", import.meta.url), "utf8"), |
| 17 | + ) as { version: string } |
| 18 | + const results = [] |
| 19 | + const databaseVersion = await readDatabaseVersion() |
| 20 | + |
| 21 | + for (const pollingIntervalMilliseconds of intervals) { |
| 22 | + results.push(await measure(pollingIntervalMilliseconds)) |
| 23 | + } |
| 24 | + |
| 25 | + process.stdout.write( |
| 26 | + `${JSON.stringify( |
| 27 | + { |
| 28 | + measuredAt: new Date().toISOString(), |
| 29 | + packageVersion: packageMetadata.version, |
| 30 | + runtime: { |
| 31 | + node: process.version, |
| 32 | + platform: `${platform()} ${release()}`, |
| 33 | + cpu: cpus()[0]?.model ?? "unknown", |
| 34 | + logicalCpus: cpus().length, |
| 35 | + }, |
| 36 | + database: { adapter: "sqlite", version: databaseVersion, path: ":memory:" }, |
| 37 | + methodology: { |
| 38 | + roles, |
| 39 | + warmupMilliseconds, |
| 40 | + durationMilliseconds, |
| 41 | + cpuPercent: "process user plus system CPU time divided by wall time", |
| 42 | + }, |
| 43 | + results, |
| 44 | + }, |
| 45 | + null, |
| 46 | + 2, |
| 47 | + )}\n`, |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +async function readDatabaseVersion(): Promise<string> { |
| 52 | + const database = sqlite({ path: ":memory:" }) |
| 53 | + try { |
| 54 | + return await database.connection(async (connection) => { |
| 55 | + const row = await connection.get<{ version: string }>("SELECT sqlite_version() AS version") |
| 56 | + return row?.version ?? "unknown" |
| 57 | + }) |
| 58 | + } finally { |
| 59 | + await database.close() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +async function measure(pollingIntervalMilliseconds: number) { |
| 64 | + const wakeUp = new CountingWakeUpAdapter() |
| 65 | + const runtime = createRuntime({ |
| 66 | + database: sqlite({ path: ":memory:" }), |
| 67 | + pollingIntervalMilliseconds, |
| 68 | + workerCount: 1, |
| 69 | + effectWorkerCount: 1, |
| 70 | + reminderSchedulerCount: 1, |
| 71 | + broadcastWorkerCount: 1, |
| 72 | + retentionIntervalMilliseconds: 0, |
| 73 | + deadProcessCleanupIntervalMilliseconds: 0, |
| 74 | + authorizeSubscription: () => true, |
| 75 | + broadcast: async () => {}, |
| 76 | + wakeUp, |
| 77 | + }) |
| 78 | + await runtime.install() |
| 79 | + const controller = new AbortController() |
| 80 | + const running = [ |
| 81 | + runtime.worker().run(controller.signal), |
| 82 | + runtime.effectWorker().run(controller.signal), |
| 83 | + runtime.reminderScheduler().run(controller.signal), |
| 84 | + runtime.broadcastWorker().run(controller.signal), |
| 85 | + ] |
| 86 | + |
| 87 | + try { |
| 88 | + await wait(warmupMilliseconds) |
| 89 | + wakeUp.resetCounts() |
| 90 | + const cpuStartedAt = process.cpuUsage() |
| 91 | + const wallStartedAt = performance.now() |
| 92 | + await wait(durationMilliseconds) |
| 93 | + const elapsedMilliseconds = performance.now() - wallStartedAt |
| 94 | + const cpuUsage = process.cpuUsage(cpuStartedAt) |
| 95 | + const polls = wakeUp.pollCounts() |
| 96 | + const totalPolls = Object.values(polls).reduce((total, count) => total + count, 0) |
| 97 | + |
| 98 | + return { |
| 99 | + pollingIntervalMilliseconds, |
| 100 | + idlePollingIntervalMilliseconds: runtime.settings.idlePollingIntervalMilliseconds, |
| 101 | + polls, |
| 102 | + pollsPerSecond: round((totalPolls * 1_000) / elapsedMilliseconds), |
| 103 | + idleCpuPercent: round( |
| 104 | + ((cpuUsage.user + cpuUsage.system) / 1_000 / elapsedMilliseconds) * 100, |
| 105 | + ), |
| 106 | + } |
| 107 | + } finally { |
| 108 | + controller.abort() |
| 109 | + await Promise.all(running) |
| 110 | + await runtime.close() |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +class CountingWakeUpAdapter implements WakeUpAdapter { |
| 115 | + private readonly counts = new Map<WakeUpRole, number>() |
| 116 | + |
| 117 | + watch(role: WakeUpRole): WakeUpWatch { |
| 118 | + return { |
| 119 | + wait: async ({ timeoutMilliseconds, signal }) => { |
| 120 | + this.counts.set(role, (this.counts.get(role) ?? 0) + 1) |
| 121 | + return new Promise<boolean>((resolve) => { |
| 122 | + let settled = false |
| 123 | + const finish = () => { |
| 124 | + if (settled) return |
| 125 | + settled = true |
| 126 | + clearTimeout(timeout) |
| 127 | + signal?.removeEventListener("abort", finish) |
| 128 | + resolve(false) |
| 129 | + } |
| 130 | + const timeout = setTimeout(finish, timeoutMilliseconds) |
| 131 | + signal?.addEventListener("abort", finish, { once: true }) |
| 132 | + if (signal?.aborted) finish() |
| 133 | + }) |
| 134 | + }, |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + notify(_role: WakeUpRole): void {} |
| 139 | + |
| 140 | + close(): void {} |
| 141 | + |
| 142 | + resetCounts(): void { |
| 143 | + this.counts.clear() |
| 144 | + } |
| 145 | + |
| 146 | + pollCounts(): Record<WakeUpRole, number> { |
| 147 | + return Object.fromEntries(roles.map((role) => [role, this.counts.get(role) ?? 0])) as Record< |
| 148 | + WakeUpRole, |
| 149 | + number |
| 150 | + > |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +function option(name: string, fallback: string): string { |
| 155 | + const prefix = `--${name}=` |
| 156 | + return ( |
| 157 | + process.argv.find((argument) => argument.startsWith(prefix))?.slice(prefix.length) ?? fallback |
| 158 | + ) |
| 159 | +} |
| 160 | + |
| 161 | +function positiveNumber(value: string, name: string): number { |
| 162 | + const number = Number(value) |
| 163 | + if (!Number.isFinite(number) || number <= 0) throw new TypeError(`${name} must be positive`) |
| 164 | + return number |
| 165 | +} |
| 166 | + |
| 167 | +function wait(milliseconds: number): Promise<void> { |
| 168 | + return new Promise((resolve) => setTimeout(resolve, milliseconds)) |
| 169 | +} |
| 170 | + |
| 171 | +function round(value: number): number { |
| 172 | + return Math.round(value * 1_000) / 1_000 |
| 173 | +} |
| 174 | + |
| 175 | +await main() |
0 commit comments