Repository navigation
Expand file tree
/
Copy pathbenchmark.compare.mjs
More file actions
619 lines (556 loc) · 19.5 KB
/
Copy pathbenchmark.compare.mjs
File metadata and controls
619 lines (556 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
import { execFileSync } from 'node:child_process'
import { mkdir, mkdtemp, rm, stat, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { performance } from 'node:perf_hooks'
import { pathToFileURL } from 'node:url'
import { build } from 'esbuild'
const repoRoot = process.cwd()
const baseRef = process.env.BENCHMARK_BASE_REF ?? 'origin/master'
const sampleCount = Number(process.env.BENCHMARK_SAMPLES ?? 15)
const sampleTargetMs = Number(process.env.BENCHMARK_SAMPLE_MS ?? 75)
const materialThresholdPercent = Number(
process.env.BENCHMARK_MATERIAL_PERCENT ?? 3
)
const materialThresholdMs = Number(process.env.BENCHMARK_MATERIAL_MS ?? 0.001)
const bootstrapIterations = 5_000
const maxIterations = 200_000
const requireNumber = (name, value, minimum) => {
if (!Number.isFinite(value) || value < minimum) {
throw new Error(
`${name} must be a number greater than or equal to ${minimum}`
)
}
}
requireNumber('BENCHMARK_SAMPLES', sampleCount, 5)
requireNumber('BENCHMARK_SAMPLE_MS', sampleTargetMs, 10)
requireNumber('BENCHMARK_MATERIAL_PERCENT', materialThresholdPercent, 0)
requireNumber('BENCHMARK_MATERIAL_MS', materialThresholdMs, 0)
if (!global.gc) {
throw new Error(
'Run this benchmark with `node --expose-gc benchmark.compare.mjs`.'
)
}
const gitText = (...args) =>
execFileSync('git', args, {
cwd: repoRoot,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
}).trim()
const gitBuffer = (...args) =>
execFileSync('git', args, {
cwd: repoRoot,
stdio: ['ignore', 'pipe', 'pipe'],
})
const repeated = (count, create) =>
Array.from({ length: count }, (_, index) => create(index)).join('')
const benchmarkCases = {
small: '<div class="card"><h2>Title</h2><p>Short body</p></div>',
attributes: repeated(
30,
(index) =>
`<section id="item-${index}" data-label="value > ${index}" hidden>` +
`<input type="text" value="item-${index}"><span>Label ${index}</span></section>`
),
markupTemplate: repeated(
40,
(index) =>
`<button $val${index * 2} class="$val${index * 2 + 1}" ` +
`data-index="${index}">Item $val${index * 2 + 2}</button>`
),
medium: repeated(
100,
(index) =>
`<article class="card" data-index="${index}"><header><h2>Title ${index}</h2></header>` +
`<p>Body text for item ${index}</p><!-- marker --><img src="/${index}.jpg" alt="Image ${index}"></article>`
),
large: repeated(
1_000,
(index) =>
`<article class="card" data-index="${index}"><header><h2>Title ${index}</h2></header>` +
`<p>Body text for item ${index}</p><!-- marker --><img src="/${index}.jpg" alt="Image ${index}"></article>`
),
scripts: repeated(
150,
(index) =>
`<script type="module">const template${index} = '<span>${index}</span>'; console.log(template${index});</script>`
),
svg: `<svg viewBox="0 0 1000 1000">${repeated(
500,
(index) =>
`<g transform="translate(${index} ${index})"><path d="M0 0 L10 10" fill="none"/></g>`
)}</svg>`,
malformed: repeated(
250,
(index) => `<div data-index="${index}"><span>Unclosed ${index}`
),
}
const lightweightDocument = {
createTextNode: (value) => ({ nodeType: 3, nodeValue: value }),
createComment: (value) => ({ nodeType: 8, nodeValue: value }),
createDocumentFragment: () => {
const childNodes = []
return {
nodeType: 11,
childNodes,
appendChild(node) {
childNodes.push(node)
},
}
},
createElementNS: (namespaceURI, tagName) => {
const childNodes = []
return {
nodeType: 1,
namespaceURI,
tagName,
childNodes,
appendChild(node) {
childNodes.push(node)
},
setAttribute() {},
}
},
}
const benchmarkModes = {
tokenizer: lightweightDocument,
defaultDoc: undefined,
}
const selectEntries = (values, environmentName) => {
const requested = process.env[environmentName]
if (!requested) return Object.entries(values)
const names = requested
.split(',')
.map((name) => name.trim())
.filter(Boolean)
return names.map((name) => {
if (!(name in values)) {
throw new Error(
`${environmentName} contains unknown value "${name}". ` +
`Available values: ${Object.keys(values).join(', ')}`
)
}
return [name, values[name]]
})
}
const selectedCases = selectEntries(benchmarkCases, 'BENCHMARK_CASE')
const selectedModes = selectEntries(benchmarkModes, 'BENCHMARK_MODE')
const median = (values) => {
const sorted = [...values].sort((a, b) => a - b)
return sorted[Math.floor(sorted.length / 2)]
}
const geometricMean = (values) =>
Math.exp(
values.reduce((sum, value) => sum + Math.log(value), 0) / values.length
)
const createRandom = (seed) => {
let state = seed >>> 0
return () => {
state += 0x6d2b79f5
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) / 4_294_967_296
}
}
const hash = (value) => {
let result = 2_166_136_261
for (let index = 0; index < value.length; index++) {
result ^= value.charCodeAt(index)
result = Math.imul(result, 16_777_619)
}
return result >>> 0
}
const bootstrapMedianInterval = (values, seed) => {
const random = createRandom(seed)
const bootstrapped = new Array(bootstrapIterations)
for (let iteration = 0; iteration < bootstrapIterations; iteration++) {
const sample = new Array(values.length)
for (let index = 0; index < values.length; index++) {
sample[index] = values[Math.floor(random() * values.length)]
}
bootstrapped[iteration] = median(sample)
}
bootstrapped.sort((a, b) => a - b)
return {
low: bootstrapped[Math.floor(bootstrapIterations * 0.025)],
high: bootstrapped[Math.floor(bootstrapIterations * 0.975)],
}
}
const runIterations = (parse, markup, handler, iterations) => {
if (handler) {
for (let iteration = 0; iteration < iterations; iteration++) {
parse(markup, handler)
}
} else {
for (let iteration = 0; iteration < iterations; iteration++) {
parse(markup)
}
}
}
const measure = (parse, markup, handler, iterations) => {
const start = performance.now()
runIterations(parse, markup, handler, iterations)
return performance.now() - start
}
const calibrate = (parse, markup, handler) => {
let iterations = 1
let elapsed = 0
while (iterations < maxIterations) {
global.gc()
elapsed = measure(parse, markup, handler, iterations)
if (elapsed >= 10) break
iterations = Math.min(maxIterations, iterations * 2)
}
if (elapsed <= 0) return maxIterations
return Math.max(
1,
Math.min(
maxIterations,
Math.round((iterations * sampleTargetMs) / elapsed)
)
)
}
const classify = (changePercent, absoluteChangeMs, confidenceInterval) => {
const lowPercent = (confidenceInterval.low - 1) * 100
const highPercent = (confidenceInterval.high - 1) * 100
const isMaterial =
Math.abs(changePercent) >= materialThresholdPercent &&
Math.abs(absoluteChangeMs) >= materialThresholdMs
if (lowPercent > 0 && isMaterial) {
return 'meaningfully faster'
}
if (lowPercent > 0) return 'faster, below material threshold'
if (highPercent < 0 && isMaterial) {
return 'meaningfully slower'
}
if (highPercent < 0) return 'slower, below material threshold'
return 'inconclusive'
}
const measurePair = (beforeParse, afterParse, markup, handler, resultSeed) => {
const warmupIterations = Math.max(
20,
Math.min(2_000, Math.ceil(1_000_000 / markup.length))
)
runIterations(beforeParse, markup, handler, warmupIterations)
runIterations(afterParse, markup, handler, warmupIterations)
const iterations = Math.max(
calibrate(beforeParse, markup, handler),
calibrate(afterParse, markup, handler)
)
runIterations(beforeParse, markup, handler, iterations)
runIterations(afterParse, markup, handler, iterations)
const samples = {
before: new Array(sampleCount),
after: new Array(sampleCount),
}
for (let sample = 0; sample < sampleCount; sample++) {
const order = sample % 2 ? ['after', 'before'] : ['before', 'after']
for (const name of order) {
global.gc()
const parse = name === 'before' ? beforeParse : afterParse
samples[name][sample] =
measure(parse, markup, handler, iterations) / iterations
}
}
const pairedRatios = samples.before.map(
(before, index) => before / samples.after[index]
)
const speedup = median(pairedRatios)
const confidenceInterval = bootstrapMedianInterval(pairedRatios, resultSeed)
const changePercent = (speedup - 1) * 100
const beforeMs = median(samples.before)
const afterMs = median(samples.after)
return {
iterations,
beforeMs,
afterMs,
speedup,
changePercent,
confidenceInterval,
verdict: classify(
changePercent,
beforeMs - afterMs,
confidenceInterval
),
samples,
}
}
const materializeBaseSource = async (directory) => {
const sourceFiles = gitText(
'ls-tree',
'-r',
'--name-only',
baseRef,
'--',
'src'
)
.split('\n')
.filter((file) => /\.(?:ts|js)$/.test(file))
if (!sourceFiles.length) {
throw new Error(`No source files found at base ref "${baseRef}".`)
}
for (const file of sourceFiles) {
const destination = path.join(directory, file)
await mkdir(path.dirname(destination), { recursive: true })
await writeFile(destination, gitBuffer('show', `${baseRef}:${file}`))
}
}
const buildBundle = async (entryPoint, outfile) => {
await build({
entryPoints: [entryPoint],
outfile,
bundle: true,
minify: true,
keepNames: true,
legalComments: 'none',
format: 'esm',
platform: 'node',
target: 'esnext',
logLevel: 'silent',
})
}
const buildModuleTree = async (sourceDirectory, outputDirectory) => {
await mkdir(outputDirectory, { recursive: true })
await writeFile(
path.join(outputDirectory, 'package.json'),
'{"type":"module"}\n'
)
const builderUrl = pathToFileURL(
path.join(
repoRoot,
'node_modules',
'@beforesemicolon',
'builder',
'dist',
'esm',
'build-modules.js'
)
).href
execFileSync(
process.execPath,
[
'--input-type=module',
'--eval',
`import { buildModules } from ${JSON.stringify(builderUrl)}; ` +
'await buildModules({ directoryPath: ' +
'process.env.BENCHMARK_SOURCE_DIRECTORY })',
],
{
cwd: outputDirectory,
env: {
...process.env,
BENCHMARK_SOURCE_DIRECTORY: sourceDirectory,
},
stdio: ['ignore', 'ignore', 'inherit'],
}
)
return path.join(outputDirectory, 'dist', 'esm', 'index.js')
}
const formatMilliseconds = (value) =>
value < 0.01 ? value.toFixed(5) : value.toFixed(4)
const formatPercent = (value) => `${value >= 0 ? '+' : ''}${value.toFixed(1)}%`
if (process.env.BENCHMARK_INTERNAL_WORKER === '1') {
const caseName = process.env.BENCHMARK_INTERNAL_CASE
const mode = process.env.BENCHMARK_INTERNAL_MODE
const baseBundlePath = process.env.BENCHMARK_INTERNAL_BASE_BUNDLE
const currentBundlePath = process.env.BENCHMARK_INTERNAL_CURRENT_BUNDLE
if (
!caseName ||
!mode ||
!baseBundlePath ||
!currentBundlePath ||
!(caseName in benchmarkCases) ||
!(mode in benchmarkModes)
) {
throw new Error('Invalid internal benchmark worker configuration.')
}
const cacheKey = `${Date.now()}-${process.pid}`
const [baseModule, currentModule] = await Promise.all([
import(`${pathToFileURL(baseBundlePath).href}?${cacheKey}-base`),
import(`${pathToFileURL(currentBundlePath).href}?${cacheKey}-current`),
])
if (
typeof baseModule.parse !== 'function' ||
typeof currentModule.parse !== 'function'
) {
throw new Error('Both benchmark bundles must export a parse function.')
}
const measurement = measurePair(
baseModule.parse,
currentModule.parse,
benchmarkCases[caseName],
benchmarkModes[mode],
hash(`${mode}/${caseName}`)
)
await new Promise((resolve) =>
process.stdout.write(JSON.stringify(measurement), resolve)
)
process.exit(0)
}
const temporaryDirectory = await mkdtemp(
path.join(tmpdir(), 'html-parser-benchmark-')
)
try {
const baseSourceDirectory = path.join(temporaryDirectory, 'base')
const baseBundlePath = path.join(temporaryDirectory, 'base.mjs')
const currentBundlePath = path.join(temporaryDirectory, 'current.mjs')
const baseModuleRoot = path.join(temporaryDirectory, 'base-modules')
const currentModuleRoot = path.join(temporaryDirectory, 'current-modules')
await materializeBaseSource(baseSourceDirectory)
const baseEntryPath = await buildModuleTree(
path.join(baseSourceDirectory, 'src'),
baseModuleRoot
)
const currentEntryPath = await buildModuleTree(
path.join(repoRoot, 'src'),
currentModuleRoot
)
await Promise.all([
buildBundle(
path.join(baseSourceDirectory, 'src', 'index.ts'),
baseBundlePath
),
buildBundle(path.join(repoRoot, 'src', 'index.ts'), currentBundlePath),
])
const cacheKey = `${Date.now()}-${process.pid}`
const [baseModule, currentModule] = await Promise.all([
import(`${pathToFileURL(baseEntryPath).href}?${cacheKey}-base`),
import(`${pathToFileURL(currentEntryPath).href}?${cacheKey}-current`),
])
if (
typeof baseModule.parse !== 'function' ||
typeof currentModule.parse !== 'function'
) {
throw new Error('Both benchmark bundles must export a parse function.')
}
const baseSha = gitText('rev-parse', baseRef)
const currentSha = gitText('rev-parse', 'HEAD')
const dirty = Boolean(gitText('status', '--short'))
const bundleSizes = {
before: (await stat(baseBundlePath)).size,
after: (await stat(currentBundlePath)).size,
}
const results = []
const startedAt = performance.now()
console.error(
`Comparing ${baseRef} (${baseSha.slice(0, 8)}) with ` +
`HEAD (${currentSha.slice(0, 8)}${dirty ? ', dirty' : ''})`
)
console.error(
`${sampleCount} paired samples targeting ${sampleTargetMs}ms each`
)
for (const [mode] of selectedModes) {
for (const [caseName, markup] of selectedCases) {
console.error(`Measuring ${mode}/${caseName}...`)
const measurement = JSON.parse(
execFileSync(
process.execPath,
[
'--expose-gc',
path.join(repoRoot, 'benchmark.compare.mjs'),
],
{
cwd: repoRoot,
encoding: 'utf8',
maxBuffer: 10 * 1024 * 1024,
stdio: ['ignore', 'pipe', 'inherit'],
env: {
...process.env,
BENCHMARK_INTERNAL_WORKER: '1',
BENCHMARK_INTERNAL_CASE: caseName,
BENCHMARK_INTERNAL_MODE: mode,
BENCHMARK_INTERNAL_BASE_BUNDLE: baseEntryPath,
BENCHMARK_INTERNAL_CURRENT_BUNDLE: currentEntryPath,
},
}
)
)
results.push({
mode,
case: caseName,
bytes: markup.length,
...measurement,
})
}
}
const summaries = selectedModes.map(([mode]) => {
const modeResults = results.filter((result) => result.mode === mode)
const speedup = geometricMean(
modeResults.map((result) => result.speedup)
)
return {
mode,
speedup,
changePercent: (speedup - 1) * 100,
}
})
const report = {
environment: {
node: process.version,
platform: process.platform,
arch: process.arch,
baseRef,
baseSha,
currentSha,
dirty,
processIsolation: true,
sampleCount,
sampleTargetMs,
materialThresholdPercent,
materialThresholdMs,
elapsedSeconds: (performance.now() - startedAt) / 1_000,
},
bundleSizes: {
...bundleSizes,
changePercent: (bundleSizes.after / bundleSizes.before - 1) * 100,
},
results,
summaries,
}
console.log(
'| Mode | Case | Bytes | Iterations | Before ms | After ms | Before MB/s | After MB/s | Change | Paired 95% CI | Verdict |'
)
console.log('|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---|')
for (const result of results) {
const low = (result.confidenceInterval.low - 1) * 100
const high = (result.confidenceInterval.high - 1) * 100
const beforeThroughput = result.bytes / result.beforeMs / 1_000
const afterThroughput = result.bytes / result.afterMs / 1_000
console.log(
`| ${result.mode} | ${result.case} | ${result.bytes} | ` +
`${result.iterations} | ${formatMilliseconds(
result.beforeMs
)} | ` +
`${formatMilliseconds(result.afterMs)} | ` +
`${beforeThroughput.toFixed(1)} | ` +
`${afterThroughput.toFixed(1)} | ` +
`${formatPercent(result.changePercent)} | ` +
`${formatPercent(low)} to ${formatPercent(high)} | ` +
`${result.verdict} |`
)
}
console.log('')
for (const summary of summaries) {
console.log(
`${summary.mode} geometric-mean change: ` +
formatPercent(summary.changePercent)
)
}
console.log(
`Bundle size: ${bundleSizes.before.toLocaleString()} → ` +
`${bundleSizes.after.toLocaleString()} bytes ` +
`(${formatPercent(report.bundleSizes.changePercent)})`
)
console.log(
`Elapsed: ${report.environment.elapsedSeconds.toFixed(1)}s on ` +
`${process.version} ${process.platform}/${process.arch}`
)
if (process.env.BENCHMARK_JSON) {
const outputPath = path.resolve(repoRoot, process.env.BENCHMARK_JSON)
await writeFile(outputPath, `${JSON.stringify(report, null, 2)}\n`)
console.error(`Wrote ${outputPath}`)
}
} finally {
await rm(temporaryDirectory, { recursive: true, force: true })
}