Skip to content

Commit c861917

Browse files
committed
fix(website): keep bundle-size ranges from inverting on gzip noise
A separately built whole-runtime ceiling can sit a few hundred bytes below a fixture or round into the next kilobyte. Draw a range only when that ceiling clears the shipped size by at least 1 kB, and scale the track to the solid bar so a lower ceiling cannot overflow it.
1 parent 3f20bcc commit c861917

2 files changed

Lines changed: 59 additions & 34 deletions

File tree

‎website/src/components/BenchBars.tsrx‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,20 @@ interface BarItem {
7676
ink: FillInk;
7777
}
7878

79-
// A ceiling that prints the same as its value would read "10 kB–10 kB", so the
80-
// bar stays a single value.
79+
// A ceiling draws a range only when it is a higher displayed value. The
80+
// whole-runtime build is separate from the fixture, so gzip can disagree by a
81+
// few hundred bytes in either direction and still round across a kilobyte.
82+
// That noise is not unused client API: a byte ceiling has to clear the bar by
83+
// at least 1 kB, and a ceiling below the bar must not invert the label.
8184
function rangeEnd(
8285
value: number,
8386
ceiling: number | undefined,
8487
format: (value: unknown) => string,
8588
): number | undefined {
86-
return ceiling !== undefined && format(ceiling) !== format(value) ? ceiling : undefined;
89+
if (ceiling === undefined || !(ceiling > value)) return undefined;
90+
if (format(ceiling) === format(value)) return undefined;
91+
if (format === fmtBytes && ceiling - value < 1024) return undefined;
92+
return ceiling;
8793
}
8894

8995
function geomeanRatio(pairs: Array<[number, number]>): number {
@@ -101,7 +107,9 @@ interface BarView {
101107
function barView(present: BarItem[], missing: BarView['missing']): BarView {
102108
present.sort((a, b) => a.value - b.value);
103109
let max = 0;
104-
for (const item of present) max = Math.max(max, item.ceiling ?? item.value);
110+
// The track has to cover the solid bar. A ceiling below its value is noise,
111+
// not a shorter scale.
112+
for (const item of present) max = Math.max(max, item.value, item.ceiling ?? 0);
105113
// 1.05 headroom keeps the longest bar off the card edge; an all-zero row
106114
// (sub-timer-resolution scores) falls back to 1 so fractions stay finite.
107115
return { present, missing, scaleMax: max > 0 ? max * 1.05 : 1 };

‎website/tests/bench-bars.test.ts‎

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -157,44 +157,61 @@ describe('benchmark card bars', () => {
157157

158158
it('extends a bundle-size bar to its whole-API ceiling only where tree-shaking leaves a gap', async () => {
159159
const bundleSize = FRAMEWORK_CARDS.find((c) => c.id === 'bundle-size')!;
160-
const row = bundleSize.rows[0];
161-
const ceilings = bundleSize.ceilings![row.op as string];
162160
const { container, opButton } = await mountCard(bundleSize);
163161

164-
fireEvent.click(opButton(row.op as string));
165-
166-
await waitFor(() =>
167-
expect(opButton(row.op as string).getAttribute('aria-pressed')).toBe('true'),
168-
);
169-
const ranged: string[] = [];
170-
const single: string[] = [];
171-
for (const series of numericSeries(bundleSize, 0)) {
172-
const value = row[series.key] as number;
173-
const ceiling = ceilings[series.key];
174-
// Ranges whose ends round to the same kB stay single bars.
175-
(Math.round(ceiling / 1024) > Math.round(value / 1024) ? ranged : single).push(series.key);
176-
}
177162
// The fixtures leave both kinds of framework on the card.
178-
expect(ranged.length).toBeGreaterThan(0);
179-
expect(single.length).toBeGreaterThan(0);
180-
181-
const rows = Array.from(container.querySelectorAll('.bench-row:not(.bench-row-empty)'));
182-
const labelOf = (key: string) => rows.find((r) => r.getAttribute('data-series') === key)!;
183-
for (const key of ranged) {
184-
const bar = labelOf(key);
185-
expect(bar.querySelector('.bench-fill-range'), key).not.toBeNull();
186-
expect(bar.querySelector('.bench-val')!.textContent, key).toMatch(/kB–\d+ kB$/);
187-
}
188-
for (const key of single) {
189-
const bar = labelOf(key);
190-
expect(bar.querySelector('.bench-fill-range'), key).toBeNull();
191-
expect(bar.querySelector('.bench-val')!.textContent, key).not.toContain('–');
163+
let sawRange = false;
164+
let sawSingle = false;
165+
for (const [index, row] of bundleSize.rows.entries()) {
166+
const ceilings = bundleSize.ceilings![row.op as string];
167+
fireEvent.click(opButton(row.op as string));
168+
await waitFor(() =>
169+
expect(opButton(row.op as string).getAttribute('aria-pressed')).toBe('true'),
170+
);
171+
const ranged: string[] = [];
172+
const single: string[] = [];
173+
for (const series of numericSeries(bundleSize, index)) {
174+
const value = row[series.key] as number;
175+
const ceiling = ceilings[series.key];
176+
// A separately built whole runtime can sit a few hundred gzip bytes
177+
// either side of the fixture and still round to another kilobyte.
178+
// Only a gap of at least 1 kB is unused client API.
179+
(ceiling - value >= 1024 && Math.round(ceiling / 1024) > Math.round(value / 1024)
180+
? ranged
181+
: single
182+
).push(series.key);
183+
}
184+
if (ranged.length > 0) sawRange = true;
185+
if (single.length > 0) sawSingle = true;
186+
187+
const rows = Array.from(container.querySelectorAll('.bench-row:not(.bench-row-empty)'));
188+
const labelOf = (key: string) => rows.find((r) => r.getAttribute('data-series') === key)!;
189+
for (const key of ranged) {
190+
const bar = labelOf(key);
191+
const hatch = bar.querySelector<HTMLElement>('.bench-fill-range');
192+
const fill = bar.querySelector<HTMLElement>('.bench-fill')!;
193+
expect(hatch, `${row.op}/${key}`).not.toBeNull();
194+
expect(parseFloat(hatch!.style.width), `${row.op}/${key}`).toBeGreaterThan(0);
195+
expect(parseFloat(fill.style.width), `${row.op}/${key}`).toBeLessThanOrEqual(100);
196+
expect(bar.querySelector('.bench-val')!.textContent, `${row.op}/${key}`).toMatch(
197+
/kB–\d+ kB$/,
198+
);
199+
}
200+
for (const key of single) {
201+
const bar = labelOf(key);
202+
const fill = bar.querySelector<HTMLElement>('.bench-fill')!;
203+
expect(bar.querySelector('.bench-fill-range'), `${row.op}/${key}`).toBeNull();
204+
expect(bar.querySelector('.bench-val')!.textContent, `${row.op}/${key}`).not.toContain('–');
205+
expect(parseFloat(fill.style.width), `${row.op}/${key}`).toBeLessThanOrEqual(100);
206+
}
192207
}
208+
expect(sawRange).toBe(true);
209+
expect(sawSingle).toBe(true);
193210
// The exact ceilings stay readable without the hatching.
194211
const tableRows = Array.from(container.querySelectorAll('tbody th'), (th) =>
195212
th.textContent!.trim(),
196213
);
197-
expect(tableRows).toContain(row.op + ', whole client API');
214+
expect(tableRows).toContain(bundleSize.rows[0].op + ', whole client API');
198215
});
199216

200217
it('charts a single-series card as one bar per operation, with no picker', async () => {

0 commit comments

Comments
 (0)