The best way to control Claude Code, Codex and more from any computer or phone

main
ts 162 lines 6.72 KB
Raw
1 // SVG line chart copied from the distilled benchmark dashboard
2 // (distilled-bench-dashboard/src/charts.js), without its zoom and tooltip
3 // wiring. One point per benchmark run, in run order.
4
5 export interface Point {
6 value: number | null;
7 label: string;
8 branch: boolean;
9 cls: string;
10 title: string;
11 }
12
13 export interface Ref {
14 value: number;
15 short: string;
16 label: string;
17 color: string;
18 kind: "exp" | "official";
19 }
20
21 export interface LineChartOptions {
22 format: (n: number) => string;
23 branchLabel: string;
24 second?: { label: string; values: ReadonlyArray<number | null> };
25 refs?: ReadonlyArray<Ref>;
26 }
27
28 const esc = (s: string | number) =>
29 String(s).replace(
30 /[&<>"]/g,
31 (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]!,
32 );
33
34 const fixed = (n: number) => n.toFixed(1);
35
36 /** Round tick step: 1, 2, 2.5 or 5 × 10^n. */
37 const niceStep = (span: number, count: number) => {
38 const raw = span / count;
39 const p = 10 ** Math.floor(Math.log10(raw));
40 for (const m of [1, 2, 2.5, 5, 10]) if (m * p >= raw) return m * p;
41 return 10 * p;
42 };
43
44 /** A short line sample in the same stroke style as the series it names. */
45 const swatch = (cls: string, color?: string) =>
46 `<svg class="sw" viewBox="0 0 26 8" aria-hidden="true"><line class="${cls}" x1="1" x2="25" y1="4" y2="4"${color ? ` style="stroke:${color}"` : ""}/></svg>`;
47
48 let clipId = 0;
49
50 export const lineChart = (points: ReadonlyArray<Point>, opts: LineChartOptions) => {
51 const data = points
52 .map((p, i) => ({ ...p, i }))
53 .filter((d): d is typeof d & { value: number } => d.value !== null && Number.isFinite(d.value));
54 const W = 560;
55 const H = 320;
56 const left = 86;
57 const right = 16;
58 const top = 22;
59 const bottom = 64;
60 const plotW = W - left - right;
61 const plotH = H - top - bottom;
62
63 const refs = (opts.refs ?? []).filter((r) => Number.isFinite(r.value));
64 const second = (opts.second?.values ?? [])
65 .map((value, i) => ({ value, i }))
66 .filter((d): d is { value: number; i: number } => d.value !== null && Number.isFinite(d.value));
67 const values = [
68 ...data.map((d) => d.value),
69 ...refs.map((r) => r.value),
70 ...second.map((d) => d.value),
71 ];
72 let lo = Math.min(0, ...values);
73 let hi = Math.max(...values);
74 const step = niceStep(hi - lo, 4);
75 lo = Math.floor(lo / step) * step;
76 hi = Math.ceil(hi / step) * step;
77 const n = points.length;
78 const colW = plotW / n;
79 const x = (i: number) => left + colW * (i + 0.5);
80 const y = (v: number) => top + plotH - ((v - lo) / (hi - lo)) * plotH;
81
82 const clip = `bench-clip-${++clipId}`;
83 let svg = `<svg class="bench-svg" viewBox="0 0 ${W} ${H}" role="img">`;
84 svg += `<defs><clipPath id="${clip}"><rect x="${left}" y="${top - 6}" width="${plotW}" height="${plotH + 12}"/></clipPath></defs>`;
85
86 // Shade the work branch.
87 const firstBranch = points.findIndex((p) => p.branch);
88 if (firstBranch !== -1) {
89 const bx = left + colW * firstBranch;
90 svg += `<rect class="branch-zone" x="${bx}" y="${top}" width="${left + plotW - bx}" height="${plotH}"/>`;
91 svg += `<text class="zone-label" x="${bx + 6}" y="${top - 7}">${esc(opts.branchLabel)} →</text>`;
92 if (firstBranch > 0)
93 svg += `<text class="zone-label" x="${bx - 6}" y="${top - 7}" text-anchor="end">← before</text>`;
94 }
95
96 for (let v = lo; v <= hi + step / 2; v += step) {
97 const gy = y(v);
98 svg += `<line class="grid-line" x1="${left}" x2="${left + plotW}" y1="${gy}" y2="${gy}"/>`;
99 svg += `<text x="${left - 8}" y="${gy + 4}" text-anchor="end">${esc(opts.format(v))}</text>`;
100 }
101
102 // Reference lines; short labels stack at the left edge so close values stay readable.
103 const refCls = (r: Ref) => `ref ${r.kind === "official" ? "official" : "exp"}`;
104 const placed: number[] = [];
105 for (const ref of [...refs].sort((a, b) => a.value - b.value)) {
106 const ry = y(ref.value);
107 let ly = ry - 5;
108 while (placed.some((p) => Math.abs(p - ly) < 13)) ly -= 13;
109 placed.push(ly);
110 svg += `<line class="${refCls(ref)}" x1="${left}" x2="${left + plotW}" y1="${ry}" y2="${ry}" style="stroke:${ref.color}"/>`;
111 svg += `<text class="ref-label" x="${left + 6}" y="${ly}" style="fill:${ref.color}">${esc(ref.short)} · ${esc(opts.format(ref.value))}<title>${esc(ref.label)}</title></text>`;
112 }
113
114 if (second.length && opts.second) {
115 svg += `<polyline class="series strict" clip-path="url(#${clip})" points="${second.map((d) => `${fixed(x(d.i))},${fixed(y(d.value))}`).join(" ")}"/>`;
116 const s = second.at(-1)!;
117 svg += `<text class="end-label strict" x="${x(s.i) - 7}" y="${y(s.value) + 16}" text-anchor="end">${esc(opts.second.label)} ${esc(opts.format(s.value))}</text>`;
118 }
119 svg += `<polyline class="series" clip-path="url(#${clip})" points="${data.map((d) => `${fixed(x(d.i))},${fixed(y(d.value))}`).join(" ")}"/>`;
120
121 // Only commits that moved the number get a dot; unchanged ones are just the line.
122 for (const d of data) {
123 if (d.cls !== "good" && d.cls !== "bad") continue;
124 svg += `<circle class="dot ${d.cls}" cx="${fixed(x(d.i))}" cy="${fixed(y(d.value))}" r="4.5"><title>${esc(`${d.title}: ${opts.format(d.value)}`)}</title></circle>`;
125 }
126
127 // Value labels on the first and last point.
128 const first = data[0]!;
129 const last = data.at(-1)!;
130 const firstRight = x(first.i) > left + plotW * 0.6;
131 if (data.length > 1)
132 svg += `<text class="end-label" x="${x(first.i) + (firstRight ? -7 : 7)}" y="${y(first.value) - 8}"${firstRight ? ' text-anchor="end"' : ""}>${esc(opts.format(first.value))}</text>`;
133 svg += `<text class="end-label" x="${x(last.i) - 7}" y="${y(last.value) - 10}" text-anchor="end">${esc(opts.format(last.value))}</text>`;
134
135 // Thin the commit labels so neighbours don't overlap; the newest one always shows.
136 const every = Math.max(1, Math.ceil(15 / colW));
137 points.forEach((p, i) => {
138 if ((n - 1 - i) % every !== 0) return;
139 svg += `<text class="xlabel" transform="translate(${x(i) + 3},${top + plotH + 12}) rotate(-55)" text-anchor="end">${esc(p.label)}</text>`;
140 });
141 svg += `</svg>`;
142
143 // Legend only when there is more than the one line.
144 const items: string[] = [];
145 if (second.length || refs.length) items.push(`<span>${swatch("series")}normal</span>`);
146 if (second.length && opts.second)
147 items.push(`<span>${swatch("series strict")}${esc(opts.second.label)}</span>`);
148 for (const r of refs)
149 items.push(`<span title="${esc(r.label)}">${swatch(refCls(r), r.color)}${esc(r.short)}</span>`);
150 const legend = items.length ? `<div class="bench-legend">${items.join("")}</div>` : "";
151
152 return { legend, svg };
153 };
154
155 export const fmtBytes = (n: number) => {
156 const a = Math.abs(n);
157 if (a < 1000) return `${Math.round(n)} B`;
158 if (a < 1e6) return `${(n / 1e3).toFixed(1)} KB`;
159 return `${(n / 1e6).toFixed(a < 1e7 ? 2 : 1)} MB`;
160 };
161
162 export const fmtUs = (n: number) => `${n.toFixed(1)} µs`;