Open History to see the last commit
1import { pathToFileURL } from 'node:url'
2import { createClientScenario, createSsrScenario } from './scenario'
3import type { LinkScenario } from './scenario'
4import type { WorkerRequest, WorkerResponse } from './worker-protocol'
5import type * as ClientApp from './src/client'
6import type * as SsrApp from './src/ssr'
7β
8const scenarios: Array<LinkScenario> = []
9const creationOrder: Array<LinkScenario> = []
10let closeWindow = () => {}
11β
12function reply(response: WorkerResponse) {
13 if (!process.send) {
14 throw new Error('The Link sampling worker requires an IPC parent')
15 }
16 process.send(response)
17}
18β
19async function handle(request: WorkerRequest) {
20 if (request.kind === 'init') {
21 if (typeof process.threadCpuUsage !== 'function') {
22 throw new Error(
23 'Stable sampling requires Node.js with process.threadCpuUsage',
24 )
25 }
26 if (scenarios[request.variant]) {
27 throw new Error('A worker must only initialize each variant once')
28 }
29 let scenario: LinkScenario
30 const bundleUrl = pathToFileURL(request.bundle)
31 bundleUrl.searchParams.set('linkPerfVariant', String(request.variant))
32 if (request.mode === 'client') {
33 const { window } = await import('../jsdom')
34 closeWindow = () => window.close()
35 const app: typeof ClientApp = await import(bundleUrl.href)
36 if (app.serverEnvironment !== false) {
37 throw new Error('Expected a production client bundle')
38 }
39 scenario = createClientScenario(app, request.caseId)
40 } else {
41 const app: typeof SsrApp = await import(bundleUrl.href)
42 if (app.serverEnvironment !== true) {
43 throw new Error('Expected a production SSR bundle')
44 }
45 scenario = createSsrScenario(app, request.caseId)
46 }
47 scenarios[request.variant] = scenario
48 creationOrder.push(scenario)
49 await scenario.setup()
50 const start = performance.now()
51 let iterations = 0
52 do {
53 await scenario.batch()
54 iterations++
55 } while (iterations < 100 || performance.now() - start < 2_000)
56 reply({
57 kind: 'ready',
58 batchMs: (performance.now() - start) / iterations,
59 })
60 return
61 }
62 if (request.kind === 'measure') {
63 const scenario = scenarios[request.variant]
64 if (!scenario) {
65 throw new Error('The sampling worker has not been initialized')
66 }
67 if (!Number.isInteger(request.iterations) || request.iterations < 1) {
68 throw new Error('Expected a positive batch count')
69 }
70 const cpuStart = process.threadCpuUsage()
71 const processStart = process.cpuUsage()
72 const start = performance.now()
73 for (let index = 0; index < request.iterations; index++) {
74 await scenario.batch()
75 }
76 const wallMs = performance.now() - start
77 const cpu = process.threadCpuUsage(cpuStart)
78 const processCpu = process.cpuUsage(processStart)
79 reply({
80 kind: 'sample',
81 sample: {
82 iterations: request.iterations,
83 wallMs: wallMs / request.iterations,
84 cpuMs: (cpu.user + cpu.system) / 1_000 / request.iterations,
85 processCpuMs:
86 (processCpu.user + processCpu.system) / 1_000 / request.iterations,
87 },
88 })
89 return
90 }
91 try {
92 for (const scenario of creationOrder) {
93 scenario.check()
94 }
95 } finally {
96 for (const scenario of creationOrder.reverse()) {
97 scenario.teardown()
98 }
99 closeWindow()
100 }
101 reply({ kind: 'stopped' })
102}
103β
104process.on('message', (request: WorkerRequest) => {
105 void handle(request).catch((error: unknown) => {
106 reply({
107 kind: 'error',
108 message:
109 error instanceof Error ? error.stack || error.message : String(error),
110 })
111 })
112})