benchforge 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/README.md +432 -0
  2. package/bin/benchforge +3 -0
  3. package/dist/bin/benchforge.mjs +9 -0
  4. package/dist/bin/benchforge.mjs.map +1 -0
  5. package/dist/browser/index.js +914 -0
  6. package/dist/index.mjs +3 -0
  7. package/dist/src-CGuaC3Wo.mjs +3676 -0
  8. package/dist/src-CGuaC3Wo.mjs.map +1 -0
  9. package/package.json +49 -0
  10. package/src/BenchMatrix.ts +380 -0
  11. package/src/Benchmark.ts +33 -0
  12. package/src/BenchmarkReport.ts +156 -0
  13. package/src/GitUtils.ts +79 -0
  14. package/src/HtmlDataPrep.ts +148 -0
  15. package/src/MeasuredResults.ts +127 -0
  16. package/src/NodeGC.ts +48 -0
  17. package/src/PermutationTest.ts +115 -0
  18. package/src/StandardSections.ts +268 -0
  19. package/src/StatisticalUtils.ts +176 -0
  20. package/src/TypeUtil.ts +8 -0
  21. package/src/bin/benchforge.ts +4 -0
  22. package/src/browser/BrowserGcStats.ts +44 -0
  23. package/src/browser/BrowserHeapSampler.ts +248 -0
  24. package/src/cli/CliArgs.ts +64 -0
  25. package/src/cli/FilterBenchmarks.ts +68 -0
  26. package/src/cli/RunBenchCLI.ts +856 -0
  27. package/src/export/JsonExport.ts +103 -0
  28. package/src/export/JsonFormat.ts +91 -0
  29. package/src/export/PerfettoExport.ts +203 -0
  30. package/src/heap-sample/HeapSampleReport.ts +196 -0
  31. package/src/heap-sample/HeapSampler.ts +78 -0
  32. package/src/html/HtmlReport.ts +131 -0
  33. package/src/html/HtmlTemplate.ts +284 -0
  34. package/src/html/Types.ts +88 -0
  35. package/src/html/browser/CIPlot.ts +287 -0
  36. package/src/html/browser/HistogramKde.ts +118 -0
  37. package/src/html/browser/LegendUtils.ts +163 -0
  38. package/src/html/browser/RenderPlots.ts +263 -0
  39. package/src/html/browser/SampleTimeSeries.ts +389 -0
  40. package/src/html/browser/Types.ts +96 -0
  41. package/src/html/browser/index.ts +1 -0
  42. package/src/html/index.ts +17 -0
  43. package/src/index.ts +92 -0
  44. package/src/matrix/CaseLoader.ts +36 -0
  45. package/src/matrix/MatrixFilter.ts +103 -0
  46. package/src/matrix/MatrixReport.ts +290 -0
  47. package/src/matrix/VariantLoader.ts +46 -0
  48. package/src/runners/AdaptiveWrapper.ts +391 -0
  49. package/src/runners/BasicRunner.ts +368 -0
  50. package/src/runners/BenchRunner.ts +60 -0
  51. package/src/runners/CreateRunner.ts +11 -0
  52. package/src/runners/GcStats.ts +107 -0
  53. package/src/runners/RunnerOrchestrator.ts +374 -0
  54. package/src/runners/RunnerUtils.ts +2 -0
  55. package/src/runners/TimingUtils.ts +13 -0
  56. package/src/runners/WorkerScript.ts +256 -0
  57. package/src/table-util/ConvergenceFormatters.ts +19 -0
  58. package/src/table-util/Formatters.ts +152 -0
  59. package/src/table-util/README.md +70 -0
  60. package/src/table-util/TableReport.ts +293 -0
  61. package/src/table-util/test/TableReport.test.ts +105 -0
  62. package/src/table-util/test/TableValueExtractor.test.ts +41 -0
  63. package/src/table-util/test/TableValueExtractor.ts +100 -0
  64. package/src/test/AdaptiveRunner.test.ts +185 -0
  65. package/src/test/AdaptiveStatistics.integration.ts +119 -0
  66. package/src/test/BenchmarkReport.test.ts +82 -0
  67. package/src/test/BrowserBench.e2e.test.ts +44 -0
  68. package/src/test/BrowserBench.test.ts +79 -0
  69. package/src/test/GcStats.test.ts +94 -0
  70. package/src/test/PermutationTest.test.ts +121 -0
  71. package/src/test/RunBenchCLI.test.ts +166 -0
  72. package/src/test/RunnerOrchestrator.test.ts +102 -0
  73. package/src/test/StatisticalUtils.test.ts +112 -0
  74. package/src/test/TestUtils.ts +93 -0
  75. package/src/test/fixtures/test-bench-script.ts +30 -0
  76. package/src/tests/AdaptiveConvergence.test.ts +177 -0
  77. package/src/tests/AdaptiveSampling.test.ts +240 -0
  78. package/src/tests/BenchMatrix.test.ts +366 -0
  79. package/src/tests/MatrixFilter.test.ts +117 -0
  80. package/src/tests/MatrixReport.test.ts +139 -0
  81. package/src/tests/RealDataValidation.test.ts +177 -0
  82. package/src/tests/fixtures/baseline/impl.ts +4 -0
  83. package/src/tests/fixtures/bevy30-samples.ts +158 -0
  84. package/src/tests/fixtures/cases/asyncCases.ts +7 -0
  85. package/src/tests/fixtures/cases/cases.ts +8 -0
  86. package/src/tests/fixtures/cases/variants/product.ts +2 -0
  87. package/src/tests/fixtures/cases/variants/sum.ts +2 -0
  88. package/src/tests/fixtures/discover/fast.ts +1 -0
  89. package/src/tests/fixtures/discover/slow.ts +4 -0
  90. package/src/tests/fixtures/invalid/bad.ts +1 -0
  91. package/src/tests/fixtures/loader/fast.ts +1 -0
  92. package/src/tests/fixtures/loader/slow.ts +4 -0
  93. package/src/tests/fixtures/loader/stateful.ts +2 -0
  94. package/src/tests/fixtures/stateful/stateful.ts +2 -0
  95. package/src/tests/fixtures/variants/extra.ts +1 -0
  96. package/src/tests/fixtures/variants/impl.ts +1 -0
  97. package/src/tests/fixtures/worker/fast.ts +1 -0
  98. package/src/tests/fixtures/worker/slow.ts +4 -0
@@ -0,0 +1,914 @@
1
+ import * as Plot from "@observablehq/plot";
2
+ import * as d3 from "d3";
3
+
4
+ //#region src/html/browser/CIPlot.ts
5
+ const defaultOpts = {
6
+ width: 260,
7
+ height: 85,
8
+ title: "p50 Δ%",
9
+ smooth: false,
10
+ direction: "uncertain"
11
+ };
12
+ const colors = {
13
+ faster: {
14
+ fill: "#dcfce7",
15
+ stroke: "#22c55e"
16
+ },
17
+ slower: {
18
+ fill: "#fee2e2",
19
+ stroke: "#ef4444"
20
+ },
21
+ uncertain: {
22
+ fill: "#dbeafe",
23
+ stroke: "#3b82f6"
24
+ }
25
+ };
26
+ const defaultMargin = {
27
+ top: 22,
28
+ right: 12,
29
+ bottom: 22,
30
+ left: 12
31
+ };
32
+ /** Create a small distribution plot showing histogram with CI shading */
33
+ function createDistributionPlot(histogram, ci, pointEstimate, options = {}) {
34
+ const opts = {
35
+ ...defaultOpts,
36
+ ...options
37
+ };
38
+ const layout = buildLayout(opts.width, opts.height);
39
+ const svg = createSvg(layout.width, layout.height);
40
+ if (!histogram?.length) return svg;
41
+ const { fill, stroke } = colors[opts.direction];
42
+ const scales = buildScales(histogram, ci, layout);
43
+ drawTitle(svg, opts.title, layout.margin.left);
44
+ drawCIRegion(svg, ci, scales, layout, fill);
45
+ opts.smooth ? drawSmoothedDist(svg, histogram, scales, stroke) : drawHistogramBars(svg, histogram, scales, layout, stroke);
46
+ drawZeroLine(svg, scales, layout);
47
+ drawPointEstimate(svg, pointEstimate, scales, layout, stroke);
48
+ drawCILabels(svg, ci, scales, layout.height);
49
+ return svg;
50
+ }
51
+ /** Convenience wrapper for ComparisonCI data */
52
+ function createCIPlot(ci, options = {}) {
53
+ if (!ci.histogram) return createSvg(0, 0);
54
+ return createDistributionPlot(ci.histogram, ci.ci, ci.percent, {
55
+ direction: ci.direction,
56
+ ...options
57
+ });
58
+ }
59
+ function buildLayout(width, height) {
60
+ const margin = defaultMargin;
61
+ return {
62
+ width,
63
+ height,
64
+ margin,
65
+ plot: {
66
+ w: width - margin.left - margin.right,
67
+ h: height - margin.top - margin.bottom
68
+ }
69
+ };
70
+ }
71
+ /** Compute x/y scale functions mapping data values to SVG coordinates */
72
+ function buildScales(histogram, ci, layout) {
73
+ const { margin, plot } = layout;
74
+ const xMin = Math.min(...histogram.map((b) => b.x), ci[0], 0);
75
+ const xMax = Math.max(...histogram.map((b) => b.x), ci[1], 0);
76
+ const yMax = Math.max(...histogram.map((b) => b.count));
77
+ return {
78
+ x: (v) => margin.left + (v - xMin) / (xMax - xMin) * plot.w,
79
+ y: (v) => margin.top + plot.h - v / yMax * plot.h
80
+ };
81
+ }
82
+ function drawTitle(svg, title, x) {
83
+ svg.appendChild(text(x, 14, title, "start", "13", "#333", "600"));
84
+ }
85
+ function drawCIRegion(svg, ci, scales, layout, fill) {
86
+ const x = scales.x(ci[0]);
87
+ const w = scales.x(ci[1]) - x;
88
+ svg.appendChild(rect(x, layout.margin.top, w, layout.plot.h, {
89
+ fill,
90
+ opacity: "0.5"
91
+ }));
92
+ }
93
+ function drawSmoothedDist(svg, histogram, scales, stroke) {
94
+ const smoothed = gaussianSmooth([...histogram].sort((a, b) => a.x - b.x), 2);
95
+ const pts = smoothed.map((b) => `${scales.x(b.x)},${scales.y(b.count)}`);
96
+ const baseline = scales.y(0);
97
+ svg.appendChild(path(`M${scales.x(smoothed[0].x)},${baseline}L${pts.join("L")}L${scales.x(smoothed.at(-1).x)},${baseline}Z`, {
98
+ fill: stroke,
99
+ opacity: "0.3"
100
+ }));
101
+ svg.appendChild(path(`M${pts.join("L")}`, {
102
+ stroke,
103
+ fill: "none",
104
+ strokeWidth: "1.5"
105
+ }));
106
+ }
107
+ function drawHistogramBars(svg, histogram, scales, layout, stroke) {
108
+ const sorted = [...histogram].sort((a, b) => a.x - b.x);
109
+ const binW = sorted.length > 1 ? sorted[1].x - sorted[0].x : 1;
110
+ const yMax = Math.max(...histogram.map((b) => b.count));
111
+ const xRange = scales.x(sorted.at(-1).x) - scales.x(sorted[0].x) + binW;
112
+ for (const bin of sorted) {
113
+ const barW = binW / xRange * layout.plot.w * .9;
114
+ const barH = bin.count / yMax * layout.plot.h;
115
+ svg.appendChild(rect(scales.x(bin.x) - barW / 2, layout.margin.top + layout.plot.h - barH, barW, barH, {
116
+ fill: stroke,
117
+ opacity: "0.6"
118
+ }));
119
+ }
120
+ }
121
+ function drawZeroLine(svg, scales, layout) {
122
+ const zeroX = scales.x(0);
123
+ if (zeroX < layout.margin.left || zeroX > layout.width - layout.margin.right) return;
124
+ const top = layout.margin.top;
125
+ svg.appendChild(line(zeroX, top, zeroX, top + layout.plot.h, {
126
+ stroke: "#666",
127
+ strokeWidth: "1",
128
+ strokeDasharray: "3,2"
129
+ }));
130
+ }
131
+ function drawPointEstimate(svg, pt, scales, layout, stroke) {
132
+ const x = scales.x(pt);
133
+ const top = layout.margin.top;
134
+ svg.appendChild(line(x, top, x, top + layout.plot.h, {
135
+ stroke,
136
+ strokeWidth: "2"
137
+ }));
138
+ }
139
+ function drawCILabels(svg, ci, scales, height) {
140
+ svg.appendChild(text(scales.x(ci[0]), height - 4, formatPct$1(ci[0]), "middle", "12"));
141
+ svg.appendChild(text(scales.x(ci[1]), height - 4, formatPct$1(ci[1]), "middle", "12"));
142
+ }
143
+ /** Apply gaussian kernel smoothing to histogram bins */
144
+ function gaussianSmooth(bins, sigma) {
145
+ return bins.map((bin, i) => {
146
+ let sum = 0;
147
+ let wt = 0;
148
+ for (let j = 0; j < bins.length; j++) {
149
+ const w = Math.exp(-((i - j) ** 2) / (2 * sigma ** 2));
150
+ sum += bins[j].count * w;
151
+ wt += w;
152
+ }
153
+ return {
154
+ x: bin.x,
155
+ count: sum / wt
156
+ };
157
+ });
158
+ }
159
+ const formatPct$1 = (v) => (v >= 0 ? "+" : "") + v.toFixed(0) + "%";
160
+ function createSvg(w, h) {
161
+ const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
162
+ svg.setAttribute("width", String(w));
163
+ svg.setAttribute("height", String(h));
164
+ if (w && h) svg.setAttribute("viewBox", `0 0 ${w} ${h}`);
165
+ return svg;
166
+ }
167
+ function rect(x, y, w, h, attrs) {
168
+ const el = document.createElementNS("http://www.w3.org/2000/svg", "rect");
169
+ el.setAttribute("x", String(x));
170
+ el.setAttribute("y", String(y));
171
+ el.setAttribute("width", String(w));
172
+ el.setAttribute("height", String(h));
173
+ setAttrs(el, attrs);
174
+ return el;
175
+ }
176
+ function path(d, attrs) {
177
+ const el = document.createElementNS("http://www.w3.org/2000/svg", "path");
178
+ el.setAttribute("d", d);
179
+ setAttrs(el, attrs);
180
+ return el;
181
+ }
182
+ function line(x1, y1, x2, y2, attrs) {
183
+ const el = document.createElementNS("http://www.w3.org/2000/svg", "line");
184
+ el.setAttribute("x1", String(x1));
185
+ el.setAttribute("y1", String(y1));
186
+ el.setAttribute("x2", String(x2));
187
+ el.setAttribute("y2", String(y2));
188
+ setAttrs(el, attrs);
189
+ return el;
190
+ }
191
+ function text(x, y, content, anchor = "start", size = "9", fill = "#666", weight = "400") {
192
+ const el = document.createElementNS("http://www.w3.org/2000/svg", "text");
193
+ el.setAttribute("x", String(x));
194
+ el.setAttribute("y", String(y));
195
+ el.setAttribute("text-anchor", anchor);
196
+ el.setAttribute("font-size", size);
197
+ el.setAttribute("font-weight", weight);
198
+ el.setAttribute("fill", fill);
199
+ el.textContent = content;
200
+ return el;
201
+ }
202
+ /** Set SVG attributes, converting camelCase keys to kebab-case */
203
+ function setAttrs(el, attrs) {
204
+ for (const [k, v] of Object.entries(attrs)) el.setAttribute(k.replace(/[A-Z]/g, (c) => "-" + c.toLowerCase()), v);
205
+ }
206
+
207
+ //#endregion
208
+ //#region src/html/browser/LegendUtils.ts
209
+ /** Draw a semi-transparent white background behind the legend area */
210
+ function legendBackground(bounds) {
211
+ const xRange = bounds.xMax - bounds.xMin;
212
+ const data = [{
213
+ x1: bounds.xMin + xRange * .65,
214
+ x2: bounds.xMin + xRange * 1.05,
215
+ y1: bounds.yMax * .65,
216
+ y2: bounds.yMax * 1.05
217
+ }];
218
+ return Plot.rect(data, {
219
+ x1: "x1",
220
+ x2: "x2",
221
+ y1: "y1",
222
+ y2: "y2",
223
+ fill: "white",
224
+ fillOpacity: .9,
225
+ stroke: "#ddd",
226
+ strokeWidth: 1
227
+ });
228
+ }
229
+ function dotMark(x, y, color, filled) {
230
+ return Plot.dot([{
231
+ x,
232
+ y
233
+ }], filled ? {
234
+ x: "x",
235
+ y: "y",
236
+ fill: color,
237
+ r: 4
238
+ } : {
239
+ x: "x",
240
+ y: "y",
241
+ stroke: color,
242
+ fill: "none",
243
+ strokeWidth: 1.5,
244
+ r: 4
245
+ });
246
+ }
247
+ function verticalBarMark(pos, color) {
248
+ const { legendX, y, xRange, yMax } = pos;
249
+ const w = xRange * .012;
250
+ const h = yMax * .05;
251
+ const data = [{
252
+ x1: legendX - w / 2,
253
+ x2: legendX + w / 2,
254
+ y1: y - h / 2,
255
+ y2: y + h / 2
256
+ }];
257
+ return Plot.rect(data, {
258
+ x1: "x1",
259
+ x2: "x2",
260
+ y1: "y1",
261
+ y2: "y2",
262
+ fill: color,
263
+ fillOpacity: .6
264
+ });
265
+ }
266
+ function verticalLineMark(pos, color, strokeDash) {
267
+ const { legendX, y, yMax } = pos;
268
+ return Plot.ruleX([legendX], {
269
+ y1: y - yMax * .025,
270
+ y2: y + yMax * .025,
271
+ stroke: color,
272
+ strokeWidth: 2,
273
+ strokeDasharray: strokeDash
274
+ });
275
+ }
276
+ function rectMark(pos, color) {
277
+ const { legendX, y, xRange, yMax } = pos;
278
+ const data = [{
279
+ x1: legendX - xRange * .01,
280
+ x2: legendX + xRange * .03,
281
+ y1: y - yMax * .02,
282
+ y2: y + yMax * .02
283
+ }];
284
+ const opts = {
285
+ x1: "x1",
286
+ x2: "x2",
287
+ y1: "y1",
288
+ y2: "y2",
289
+ fill: color,
290
+ fillOpacity: .3,
291
+ stroke: color,
292
+ strokeWidth: 1
293
+ };
294
+ return Plot.rect(data, opts);
295
+ }
296
+ function symbolMark(pos, item) {
297
+ switch (item.style) {
298
+ case "filled-dot": return dotMark(pos.legendX, pos.y, item.color, true);
299
+ case "hollow-dot": return dotMark(pos.legendX, pos.y, item.color, false);
300
+ case "vertical-bar": return verticalBarMark(pos, item.color);
301
+ case "vertical-line": return verticalLineMark(pos, item.color, item.strokeDash);
302
+ case "rect": return rectMark(pos, item.color);
303
+ }
304
+ }
305
+ function textMark(pos, label) {
306
+ const data = [{
307
+ x: pos.textX,
308
+ y: pos.y,
309
+ text: label
310
+ }];
311
+ return Plot.text(data, {
312
+ x: "x",
313
+ y: "y",
314
+ text: "text",
315
+ fontSize: 11,
316
+ textAnchor: "start",
317
+ fill: "#333"
318
+ });
319
+ }
320
+ /** Build complete legend marks array */
321
+ function buildLegend(bounds, items) {
322
+ const xRange = bounds.xMax - bounds.xMin;
323
+ const legendX = bounds.xMin + xRange * .68;
324
+ const textX = legendX + xRange * .04;
325
+ const getY = (i) => bounds.yMax * .98 - i * (bounds.yMax * .08);
326
+ const marks = [legendBackground(bounds)];
327
+ for (let i = 0; i < items.length; i++) {
328
+ const pos = {
329
+ legendX,
330
+ y: getY(i),
331
+ textX,
332
+ xRange,
333
+ yMax: bounds.yMax
334
+ };
335
+ marks.push(symbolMark(pos, items[i]), textMark(pos, items[i].label));
336
+ }
337
+ return marks;
338
+ }
339
+
340
+ //#endregion
341
+ //#region src/html/browser/HistogramKde.ts
342
+ /** Create histogram + KDE plot for sample distribution */
343
+ function createHistogramKde(allSamples, benchmarkNames) {
344
+ const { barData, binMin, binMax, yMax } = buildBarData(allSamples, benchmarkNames);
345
+ const { colorMap, legendItems } = buildColorData(benchmarkNames);
346
+ const xMax = binMax + (binMax - binMin) * .45;
347
+ return Plot.plot({
348
+ marginTop: 24,
349
+ marginLeft: 70,
350
+ marginRight: 10,
351
+ marginBottom: 60,
352
+ width: 550,
353
+ height: 300,
354
+ style: { fontSize: "14px" },
355
+ x: {
356
+ label: "Time (ms)",
357
+ labelAnchor: "center",
358
+ domain: [binMin, xMax],
359
+ labelOffset: 45,
360
+ tickFormat: (d) => d.toFixed(1),
361
+ ticks: 5
362
+ },
363
+ y: {
364
+ label: "Count",
365
+ labelAnchor: "top",
366
+ labelArrow: false,
367
+ grid: true,
368
+ domain: [0, yMax]
369
+ },
370
+ marks: [
371
+ Plot.rectY(barData, {
372
+ x1: "x1",
373
+ x2: "x2",
374
+ y: "count",
375
+ fill: (d) => colorMap.get(d.benchmark),
376
+ fillOpacity: .6,
377
+ tip: true,
378
+ title: (d) => `${d.benchmark}: ${d.count}`
379
+ }),
380
+ Plot.ruleY([0]),
381
+ ...buildLegend({
382
+ xMin: binMin,
383
+ xMax,
384
+ yMax
385
+ }, legendItems)
386
+ ]
387
+ });
388
+ }
389
+ function buildColorData(benchmarkNames) {
390
+ const scheme = d3.schemeObservable10;
391
+ return {
392
+ colorMap: new Map(benchmarkNames.map((name, i) => [name, scheme[i % 10]])),
393
+ legendItems: benchmarkNames.map((name, i) => ({
394
+ color: scheme[i % 10],
395
+ label: name,
396
+ style: "vertical-bar"
397
+ }))
398
+ };
399
+ }
400
+ /** Bin samples into grouped histogram bars for each benchmark */
401
+ function buildBarData(allSamples, benchmarkNames) {
402
+ const sorted = allSamples.map((d) => d.value).sort((a, b) => a - b);
403
+ const binMin = d3.quantile(sorted, .01);
404
+ const binMax = d3.quantile(sorted, .99);
405
+ const binCount = 25;
406
+ const step = (binMax - binMin) / binCount;
407
+ const thresholds = d3.range(1, binCount).map((i) => binMin + i * step);
408
+ const plotWidth = 550;
409
+ const bins = d3.bin().domain([binMin, binMax]).thresholds(thresholds).value((d) => d.value)(allSamples);
410
+ const barData = [];
411
+ const n = benchmarkNames.length;
412
+ const unitsPerPx = (binMax - binMin) / plotWidth;
413
+ const groupGapPx = 8;
414
+ for (const bin of bins) {
415
+ const counts = /* @__PURE__ */ new Map();
416
+ for (const d of bin) counts.set(d.benchmark, (counts.get(d.benchmark) || 0) + 1);
417
+ const full = bin.x1 - bin.x0;
418
+ const groupGap = Math.min(full * .5, unitsPerPx * groupGapPx);
419
+ const start = bin.x0 + groupGap / 2;
420
+ const w = (full - groupGap) / n;
421
+ benchmarkNames.forEach((benchmark, i) => {
422
+ const x1 = start + i * w;
423
+ const x2 = start + (i + 1) * w;
424
+ barData.push({
425
+ benchmark,
426
+ count: counts.get(benchmark) || 0,
427
+ x1,
428
+ x2
429
+ });
430
+ });
431
+ }
432
+ return {
433
+ barData,
434
+ binMin,
435
+ binMax,
436
+ yMax: (d3.max(barData, (d) => d.count) || 1) * 1.15
437
+ };
438
+ }
439
+
440
+ //#endregion
441
+ //#region src/html/browser/SampleTimeSeries.ts
442
+ const OPT_STATUS_NAMES = {
443
+ 1: "interpreted",
444
+ 129: "sparkplug",
445
+ 17: "turbofan",
446
+ 33: "maglev",
447
+ 49: "turbofan+maglev",
448
+ 32769: "optimized"
449
+ };
450
+ const OPT_TIER_COLORS = {
451
+ turbofan: "#22c55e",
452
+ optimized: "#22c55e",
453
+ "turbofan+maglev": "#22c55e",
454
+ maglev: "#eab308",
455
+ sparkplug: "#f97316",
456
+ interpreted: "#dc3545"
457
+ };
458
+ /** Create sample time series showing each sample in order */
459
+ function createSampleTimeSeries(timeSeries, gcEvents = [], pausePoints = [], heapSeries = []) {
460
+ const ctx = buildPlotContext(timeSeries);
461
+ const heapData = prepareHeapData(heapSeries, ctx.yMin, ctx.yMax);
462
+ return Plot.plot({
463
+ marginTop: 24,
464
+ marginLeft: 70,
465
+ marginBottom: 60,
466
+ marginRight: 110,
467
+ width: 550,
468
+ height: 300,
469
+ style: { fontSize: "14px" },
470
+ x: {
471
+ label: "Sample",
472
+ labelAnchor: "center",
473
+ labelOffset: 45,
474
+ grid: true,
475
+ domain: [ctx.xMin, ctx.xMax]
476
+ },
477
+ y: {
478
+ label: `Time (${ctx.unitSuffix})`,
479
+ labelAnchor: "top",
480
+ labelArrow: false,
481
+ grid: true,
482
+ domain: [ctx.yMin, ctx.yMax],
483
+ tickFormat: ctx.formatValue
484
+ },
485
+ color: {
486
+ legend: false,
487
+ scheme: "observable10"
488
+ },
489
+ marks: [
490
+ ...heapMarks(heapData, ctx.yMin),
491
+ ...ctx.hasWarmup ? [Plot.ruleX([0], {
492
+ stroke: "#999",
493
+ strokeWidth: 1,
494
+ strokeDasharray: "4,4"
495
+ })] : [],
496
+ gcMark(gcEvents, ctx.yMin, ctx.convertValue),
497
+ ...pauseMarks(pausePoints, ctx.yMin, ctx.yMax),
498
+ ...sampleDotMarks(ctx),
499
+ Plot.ruleY([ctx.yMin], {
500
+ stroke: "black",
501
+ strokeWidth: 1
502
+ }),
503
+ ...buildLegend({
504
+ xMin: ctx.xMin,
505
+ xMax: ctx.xMax,
506
+ yMax: ctx.yMax
507
+ }, buildLegendItems(ctx.hasWarmup, gcEvents.length, pausePoints.length, heapData.length > 0, ctx.optTiers, ctx.benchmarks))
508
+ ]
509
+ });
510
+ }
511
+ function buildPlotContext(timeSeries) {
512
+ const benchmarks = [...new Set(timeSeries.map((d) => d.benchmark))];
513
+ const sampleData = buildSampleData(timeSeries, benchmarks);
514
+ const { unitSuffix, convertValue, formatValue } = getTimeUnit(sampleData.map((d) => d.value));
515
+ const convertedData = sampleData.map((d) => ({
516
+ ...d,
517
+ displayValue: convertValue(d.value)
518
+ }));
519
+ const { yMin, yMax } = computeYRange(convertedData.map((d) => d.displayValue));
520
+ return {
521
+ convertedData,
522
+ xMin: d3.min(convertedData, (d) => d.sample),
523
+ xMax: d3.max(convertedData, (d) => d.sample),
524
+ yMin,
525
+ yMax,
526
+ unitSuffix,
527
+ formatValue,
528
+ convertValue,
529
+ hasWarmup: convertedData.some((d) => d.isWarmup),
530
+ optTiers: [...new Set(convertedData.filter((d) => d.optTier && !d.isWarmup).map((d) => d.optTier))].filter((t) => t !== null),
531
+ benchmarks
532
+ };
533
+ }
534
+ function buildSampleData(timeSeries, benchmarks) {
535
+ const result = [];
536
+ for (const benchmark of benchmarks) {
537
+ const isBaseline = benchmark.includes("(baseline)");
538
+ for (const d of timeSeries.filter((t) => t.benchmark === benchmark)) {
539
+ const optTier = d.optStatus !== void 0 ? OPT_STATUS_NAMES[d.optStatus] || "unknown" : null;
540
+ result.push({
541
+ benchmark,
542
+ sample: d.iteration,
543
+ value: d.value,
544
+ isBaseline,
545
+ isWarmup: d.isWarmup || false,
546
+ optTier
547
+ });
548
+ }
549
+ }
550
+ return result;
551
+ }
552
+ /** Pick display unit (ns/us/ms) based on average value magnitude */
553
+ function getTimeUnit(values) {
554
+ const avg = d3.mean(values);
555
+ const fmt0 = (d) => d3.format(",.0f")(d);
556
+ const fmt1 = (d) => d3.format(",.1f")(d);
557
+ if (avg < .001) return {
558
+ unitSuffix: "ns",
559
+ convertValue: (ms) => ms * 1e6,
560
+ formatValue: fmt0
561
+ };
562
+ if (avg < 1) return {
563
+ unitSuffix: "μs",
564
+ convertValue: (ms) => ms * 1e3,
565
+ formatValue: fmt1
566
+ };
567
+ return {
568
+ unitSuffix: "ms",
569
+ convertValue: (ms) => ms,
570
+ formatValue: fmt1
571
+ };
572
+ }
573
+ /** Compute Y axis range with padding, snapping yMin to a round number */
574
+ function computeYRange(values) {
575
+ const dataMin = d3.min(values);
576
+ const dataMax = d3.max(values);
577
+ const dataRange = dataMax - dataMin;
578
+ let yMin = dataMin - dataRange * .15;
579
+ const magnitude = 10 ** Math.floor(Math.log10(Math.abs(yMin)));
580
+ yMin = Math.floor(yMin / magnitude) * magnitude;
581
+ if (dataMin > 0 && yMin < 0) yMin = 0;
582
+ return {
583
+ yMin,
584
+ yMax: dataMax + dataRange * .05
585
+ };
586
+ }
587
+ /** Scale heap byte values into the plot's Y coordinate range */
588
+ function prepareHeapData(heapSeries, yMin, yMax) {
589
+ if (heapSeries.length === 0) return [];
590
+ const heapMin = d3.min(heapSeries, (d) => d.value);
591
+ const heapRange = d3.max(heapSeries, (d) => d.value) - heapMin || 1;
592
+ const scale = (yMax - yMin) * .25 / heapRange;
593
+ return heapSeries.map((d) => ({
594
+ sample: d.iteration,
595
+ y: yMin + (d.value - heapMin) * scale,
596
+ heapMB: d.value / 1024 / 1024
597
+ }));
598
+ }
599
+ function heapMarks(heapData, yMin) {
600
+ if (heapData.length === 0) return [];
601
+ return [Plot.areaY(heapData, {
602
+ x: "sample",
603
+ y: "y",
604
+ y1: yMin,
605
+ fill: "#9333ea",
606
+ fillOpacity: .15,
607
+ stroke: "#9333ea",
608
+ strokeWidth: 1,
609
+ strokeOpacity: .4
610
+ }), Plot.tip(heapData, Plot.pointerX({
611
+ x: "sample",
612
+ y: "y",
613
+ title: (d) => `Heap: ${d.heapMB.toFixed(1)} MB`
614
+ }))];
615
+ }
616
+ function gcMark(gcEvents, yMin, convertValue) {
617
+ const data = gcEvents.map((gc) => ({
618
+ x1: gc.sampleIndex,
619
+ y1: yMin,
620
+ x2: gc.sampleIndex,
621
+ y2: yMin + convertValue(gc.duration),
622
+ duration: gc.duration
623
+ }));
624
+ return Plot.link(data, {
625
+ x1: "x1",
626
+ y1: "y1",
627
+ x2: "x2",
628
+ y2: "y2",
629
+ stroke: "#22c55e",
630
+ strokeWidth: 2,
631
+ strokeOpacity: .8,
632
+ title: (d) => `GC: ${d.duration.toFixed(2)}ms`
633
+ });
634
+ }
635
+ function pauseMarks(pausePoints, yMin, yMax) {
636
+ return pausePoints.map((p) => Plot.ruleX([p.sampleIndex], {
637
+ y1: yMin,
638
+ y2: yMax,
639
+ stroke: "#888",
640
+ strokeWidth: 1,
641
+ strokeDasharray: "4,4",
642
+ strokeOpacity: .7,
643
+ title: `Pause: ${p.durationMs}ms`
644
+ }));
645
+ }
646
+ function sampleDotMarks(ctx) {
647
+ const { convertedData, unitSuffix, formatValue } = ctx;
648
+ const tipTitle = (d) => d.optTier ? `Sample ${d.sample}: ${formatValue(d.displayValue)}${unitSuffix} [${d.optTier}]` : `Sample ${d.sample}: ${formatValue(d.displayValue)}${unitSuffix}`;
649
+ return [
650
+ Plot.dot(convertedData.filter((d) => d.isWarmup), {
651
+ x: "sample",
652
+ y: "displayValue",
653
+ stroke: "#dc3545",
654
+ fill: "none",
655
+ strokeWidth: 1.5,
656
+ r: 3,
657
+ opacity: .7,
658
+ title: (d) => `Warmup ${d.sample}: ${formatValue(d.displayValue)}${unitSuffix}`
659
+ }),
660
+ Plot.dot(convertedData.filter((d) => d.isBaseline && !d.isWarmup), {
661
+ x: "sample",
662
+ y: "displayValue",
663
+ stroke: "#ffa500",
664
+ fill: "none",
665
+ strokeWidth: 2,
666
+ r: 3,
667
+ opacity: .8,
668
+ title: tipTitle
669
+ }),
670
+ Plot.dot(convertedData.filter((d) => !d.isBaseline && !d.isWarmup), {
671
+ x: "sample",
672
+ y: "displayValue",
673
+ fill: (d) => d.optTier ? OPT_TIER_COLORS[d.optTier] || "#4682b4" : "#4682b4",
674
+ r: 3,
675
+ opacity: .8,
676
+ title: tipTitle
677
+ })
678
+ ];
679
+ }
680
+ function buildLegendItems(hasWarmup, gcCount, pauseCount, hasHeap, optTiers, benchmarks) {
681
+ const items = [];
682
+ if (hasWarmup) items.push({
683
+ color: "#dc3545",
684
+ label: "warmup",
685
+ style: "hollow-dot"
686
+ });
687
+ if (gcCount > 0) items.push({
688
+ color: "#22c55e",
689
+ label: `gc (${gcCount})`,
690
+ style: "vertical-line"
691
+ });
692
+ if (pauseCount > 0) items.push({
693
+ color: "#888",
694
+ label: `pause (${pauseCount})`,
695
+ style: "vertical-line",
696
+ strokeDash: "4,4"
697
+ });
698
+ if (hasHeap) items.push({
699
+ color: "#9333ea",
700
+ label: "heap",
701
+ style: "rect"
702
+ });
703
+ for (const tier of optTiers) items.push({
704
+ color: OPT_TIER_COLORS[tier] || "#4682b4",
705
+ label: tier,
706
+ style: "filled-dot"
707
+ });
708
+ if (optTiers.length === 0) {
709
+ const sorted = [...benchmarks].sort((a, b) => {
710
+ const aBase = a.includes("(baseline)");
711
+ return aBase === b.includes("(baseline)") ? 0 : aBase ? 1 : -1;
712
+ });
713
+ for (const bm of sorted) {
714
+ const isBase = bm.includes("(baseline)");
715
+ items.push({
716
+ color: isBase ? "#ffa500" : "#4682b4",
717
+ label: bm,
718
+ style: isBase ? "hollow-dot" : "filled-dot"
719
+ });
720
+ }
721
+ }
722
+ return items;
723
+ }
724
+
725
+ //#endregion
726
+ //#region src/html/browser/RenderPlots.ts
727
+ /** Render all plots for the benchmark report */
728
+ function renderPlots(data) {
729
+ const gcEnabled = data.metadata.gcTrackingEnabled ?? false;
730
+ data.groups.forEach((group, groupIndex) => {
731
+ try {
732
+ renderGroup(group, groupIndex, gcEnabled);
733
+ } catch (error) {
734
+ console.error("Error rendering plots for group", groupIndex, error);
735
+ showError(groupIndex, `Error rendering visualizations: ${error.message}`);
736
+ }
737
+ });
738
+ }
739
+ function renderGroup(group, groupIndex, gcEnabled) {
740
+ const benchmarks = prepareBenchmarks(group);
741
+ if (benchmarks.length === 0 || !benchmarks[0].samples?.length) {
742
+ showError(groupIndex, "No sample data available for visualization");
743
+ return;
744
+ }
745
+ const flattened = flattenSamples(benchmarks);
746
+ const benchmarkNames = benchmarks.map((b) => b.name);
747
+ const currentBenchmark = benchmarks.find((b) => !b.isBaseline);
748
+ if (currentBenchmark?.comparisonCI?.histogram) renderToContainer(`#ci-plot-${groupIndex}`, true, () => createCIPlot(currentBenchmark.comparisonCI));
749
+ renderToContainer(`#histogram-${groupIndex}`, flattened.allSamples.length > 0, () => createHistogramKde(flattened.allSamples, benchmarkNames));
750
+ const { timeSeries, allGcEvents, allPausePoints, heapSeries } = flattened;
751
+ renderToContainer(`#sample-timeseries-${groupIndex}`, timeSeries.length > 0, () => createSampleTimeSeries(timeSeries, allGcEvents, allPausePoints, heapSeries));
752
+ const statsContainer = document.querySelector(`#stats-${groupIndex}`);
753
+ if (statsContainer) statsContainer.innerHTML = benchmarks.map((b) => generateStatsHtml(b, gcEnabled)).join("");
754
+ }
755
+ /** Clear a container element and append a freshly created plot */
756
+ function renderToContainer(selector, condition, create) {
757
+ const container = document.querySelector(selector);
758
+ if (!container || !condition) return;
759
+ container.innerHTML = "";
760
+ container.appendChild(create());
761
+ }
762
+ /** Combine baseline and benchmarks into a single list with display names */
763
+ function prepareBenchmarks(group) {
764
+ const benchmarks = [];
765
+ if (group.baseline) {
766
+ const name = group.baseline.name + " (baseline)";
767
+ benchmarks.push({
768
+ ...group.baseline,
769
+ name,
770
+ isBaseline: true
771
+ });
772
+ }
773
+ for (const b of group.benchmarks) benchmarks.push({
774
+ ...b,
775
+ isBaseline: false
776
+ });
777
+ return benchmarks;
778
+ }
779
+ function flattenSamples(benchmarks) {
780
+ const result = {
781
+ allSamples: [],
782
+ timeSeries: [],
783
+ heapSeries: [],
784
+ allGcEvents: [],
785
+ allPausePoints: []
786
+ };
787
+ for (const b of benchmarks) if (b.samples?.length) flattenBenchmark(b, result);
788
+ return result;
789
+ }
790
+ /** Extract time series, heap, GC, and pause data from one benchmark */
791
+ function flattenBenchmark(b, out) {
792
+ const warmupCount = b.warmupSamples?.length || 0;
793
+ b.warmupSamples?.forEach((value, i) => {
794
+ out.timeSeries.push({
795
+ benchmark: b.name,
796
+ iteration: i - warmupCount,
797
+ value,
798
+ isWarmup: true
799
+ });
800
+ });
801
+ const sampleEndTimes = cumulativeSum(b.samples);
802
+ b.samples.forEach((value, i) => {
803
+ out.allSamples.push({
804
+ benchmark: b.name,
805
+ value,
806
+ iteration: i
807
+ });
808
+ out.timeSeries.push({
809
+ benchmark: b.name,
810
+ iteration: i,
811
+ value,
812
+ isWarmup: false,
813
+ optStatus: b.optSamples?.[i]
814
+ });
815
+ if (b.heapSamples?.[i] !== void 0) out.heapSeries.push({
816
+ benchmark: b.name,
817
+ iteration: i,
818
+ value: b.heapSamples[i]
819
+ });
820
+ });
821
+ b.gcEvents?.forEach((gc) => {
822
+ const idx = sampleEndTimes.findIndex((t) => t >= gc.offset);
823
+ out.allGcEvents.push({
824
+ benchmark: b.name,
825
+ sampleIndex: idx >= 0 ? idx : b.samples.length - 1,
826
+ duration: gc.duration
827
+ });
828
+ });
829
+ b.pausePoints?.forEach((p) => {
830
+ out.allPausePoints.push({
831
+ benchmark: b.name,
832
+ sampleIndex: p.sampleIndex,
833
+ durationMs: p.durationMs
834
+ });
835
+ });
836
+ }
837
+ function cumulativeSum(arr) {
838
+ const result = [];
839
+ let sum = 0;
840
+ for (const v of arr) {
841
+ sum += v;
842
+ result.push(sum);
843
+ }
844
+ return result;
845
+ }
846
+ function showError(groupIndex, message) {
847
+ const container = document.querySelector(`#group-${groupIndex}`);
848
+ if (container) container.innerHTML = `<div class="error">${message}</div>`;
849
+ }
850
+ function formatPct(v) {
851
+ return (v >= 0 ? "+" : "") + v.toFixed(1) + "%";
852
+ }
853
+ function generateCIHtml(ci) {
854
+ if (!ci) return "";
855
+ const text = `${formatPct(ci.percent)} [${formatPct(ci.ci[0])}, ${formatPct(ci.ci[1])}]`;
856
+ return `
857
+ <div class="stat-item">
858
+ <div class="stat-label">vs Baseline</div>
859
+ <div class="stat-value ci-${ci.direction}">${text}</div>
860
+ </div>
861
+ `;
862
+ }
863
+ function generateStatsHtml(b, gcEnabled) {
864
+ const ciHtml = generateCIHtml(b.comparisonCI);
865
+ if (b.sectionStats?.length) {
866
+ const statsHtml = (gcEnabled ? b.sectionStats : b.sectionStats.filter((s) => s.groupTitle !== "gc")).map((stat) => `
867
+ <div class="stat-item">
868
+ <div class="stat-label">${stat.groupTitle ? stat.groupTitle + " " : ""}${stat.label}</div>
869
+ <div class="stat-value">${stat.value}</div>
870
+ </div>
871
+ `).join("");
872
+ return `
873
+ <div class="summary-stats">
874
+ <h3 style="margin-bottom: 10px; color: #333;">${b.name}</h3>
875
+ <div class="stats-grid">${ciHtml}${statsHtml}</div>
876
+ </div>
877
+ `;
878
+ }
879
+ return `
880
+ <div class="summary-stats">
881
+ <h3 style="margin-bottom: 10px; color: #333;">${b.name}</h3>
882
+ <div class="stats-grid">
883
+ ${ciHtml}
884
+ <div class="stat-item">
885
+ <div class="stat-label">Min</div>
886
+ <div class="stat-value">${b.stats.min.toFixed(3)}ms</div>
887
+ </div>
888
+ <div class="stat-item">
889
+ <div class="stat-label">Median</div>
890
+ <div class="stat-value">${b.stats.p50.toFixed(3)}ms</div>
891
+ </div>
892
+ <div class="stat-item">
893
+ <div class="stat-label">Mean</div>
894
+ <div class="stat-value">${b.stats.avg.toFixed(3)}ms</div>
895
+ </div>
896
+ <div class="stat-item">
897
+ <div class="stat-label">Max</div>
898
+ <div class="stat-value">${b.stats.max.toFixed(3)}ms</div>
899
+ </div>
900
+ <div class="stat-item">
901
+ <div class="stat-label">P75</div>
902
+ <div class="stat-value">${b.stats.p75.toFixed(3)}ms</div>
903
+ </div>
904
+ <div class="stat-item">
905
+ <div class="stat-label">P99</div>
906
+ <div class="stat-value">${b.stats.p99.toFixed(3)}ms</div>
907
+ </div>
908
+ </div>
909
+ </div>
910
+ `;
911
+ }
912
+
913
+ //#endregion
914
+ export { renderPlots };