|
| 1 | +import { ENGINE_TYPES } from './isolated-contracts.mjs'; |
| 2 | + |
| 3 | +const INTEGER = new Intl.NumberFormat('en', { maximumFractionDigits: 0 }); |
| 4 | +const ONE = new Intl.NumberFormat('en', { maximumFractionDigits: 1 }); |
| 5 | +const TWO = new Intl.NumberFormat('en', { maximumFractionDigits: 2 }); |
| 6 | +const SIGNED = new Intl.NumberFormat('en', { maximumFractionDigits: 0, signDisplay: 'exceptZero' }); |
| 7 | +const LATENCIES = Object.freeze(['p50', 'p95', 'p99']); |
| 8 | +const HEAT_STEPS = 5; |
| 9 | + |
| 10 | +export function el(document, tag, className, text) { |
| 11 | + const node = document.createElement(tag); |
| 12 | + if (className) node.className = className; |
| 13 | + if (text !== undefined && text !== null) node.textContent = text; |
| 14 | + return node; |
| 15 | +} |
| 16 | + |
| 17 | +// Chart labels round for reading; the table view keeps the recorded precision. |
| 18 | +export function friendly(value, unit) { |
| 19 | + if (!Number.isFinite(value)) return '—'; |
| 20 | + if (unit === 'ms') return value >= 1000 ? ONE.format(value / 1000) + ' s' |
| 21 | + : (value >= 10 ? INTEGER : value >= 1 ? ONE : TWO).format(value) + ' ms'; |
| 22 | + if (unit === 's') return (value >= 100 ? INTEGER : value >= 10 ? ONE : TWO).format(value) + ' s'; |
| 23 | + if (unit === '%') return TWO.format(value) + '%'; |
| 24 | + return (value >= 100 ? INTEGER : ONE).format(value) + (unit ? ' ' + unit : ''); |
| 25 | +} |
| 26 | + |
| 27 | +export function compact(value, unit) { |
| 28 | + if (!Number.isFinite(value) || unit !== 'ops/s') return friendly(value, unit); |
| 29 | + return value >= 1e6 ? ONE.format(value / 1e6) + 'M' : value >= 1e4 ? ONE.format(value / 1e3) + 'k' : INTEGER.format(value); |
| 30 | +} |
| 31 | + |
| 32 | +const position = (node, name, percent) => node.style.setProperty(name, Math.min(100, Math.max(0, percent)).toFixed(2) + '%'); |
| 33 | + |
| 34 | +export function engineName(document, name, tag = 'span') { |
| 35 | + const node = el(document, tag, 'engine-name'); |
| 36 | + node.append(el(document, 'strong', null, name), el(document, 'small', null, ENGINE_TYPES[name])); |
| 37 | + return node; |
| 38 | +} |
| 39 | + |
| 40 | +export function latencyScale(rows) { |
| 41 | + const values = rows.flatMap(row => [row.p50, row.p99]).filter(value => Number.isFinite(value) && value > 0); |
| 42 | + if (values.length === 0) return null; |
| 43 | + const low = Math.floor(Math.log10(Math.min(...values))); |
| 44 | + const span = Math.max(1, Math.ceil(Math.log10(Math.max(...values))) - low); |
| 45 | + return { ticks: Array.from({ length: span + 1 }, (_, index) => 10 ** (low + index)), |
| 46 | + at: value => (Math.log10(Math.max(value, 10 ** low)) - low) / span * 100 }; |
| 47 | +} |
| 48 | + |
| 49 | +function latency(document, row, scale) { |
| 50 | + const cell = el(document, 'span', 'rank-lat'); |
| 51 | + const track = el(document, 'span', 'lat-track'); |
| 52 | + const range = el(document, 'span', 'lat-range'); |
| 53 | + position(range, '--from', scale.at(row.p50)); |
| 54 | + position(range, '--to', scale.at(row.p99)); |
| 55 | + track.append(range); |
| 56 | + for (const key of LATENCIES) { |
| 57 | + const dot = el(document, 'span', 'lat-dot lat-' + key); |
| 58 | + position(dot, '--at', scale.at(row[key])); |
| 59 | + dot.title = `${row.name} · ${key} ${friendly(row[key], 'ms')}`; |
| 60 | + track.append(dot); |
| 61 | + } |
| 62 | + cell.append(track, el(document, 'small', null, `p50 ${friendly(row.p50, 'ms')} · p99 ${friendly(row.p99, 'ms')}`)); |
| 63 | + return cell; |
| 64 | +} |
| 65 | + |
| 66 | +export function rankChart(document, rows, metric) { |
| 67 | + const scale = latencyScale(rows); |
| 68 | + const chart = el(document, 'div', 'rank' + (scale ? ' has-latency' : '')); |
| 69 | + const head = el(document, 'div', 'rank-head'); |
| 70 | + head.setAttribute('aria-hidden', 'true'); |
| 71 | + head.append(el(document, 'span'), el(document, 'span', null, 'Engine'), el(document, 'span', null, `${metric.title} · ${metric.unit}`)); |
| 72 | + if (scale) { |
| 73 | + const ticks = el(document, 'span', 'lat-ticks', 'Latency per request · log scale'); |
| 74 | + for (const tick of scale.ticks) position(ticks.appendChild(el(document, 'i', null, friendly(tick, 'ms'))), '--at', scale.at(tick)); |
| 75 | + head.append(ticks); |
| 76 | + } |
| 77 | + const list = el(document, 'ol', 'rank-list'); |
| 78 | + list.setAttribute('aria-label', metric.title + ' ranking'); |
| 79 | + const maximum = Math.max(0, ...rows.map(row => row.value)); |
| 80 | + rows.forEach((row, index) => { |
| 81 | + const item = el(document, 'li', 'rank-row' + (row.name === 'KeyLoad' ? ' is-keyload' : '')); |
| 82 | + item.dataset.workerId = row.id; |
| 83 | + item.dataset.value = String(row.value); |
| 84 | + const bar = el(document, 'span', 'rank-bar'); |
| 85 | + bar.style.setProperty('--ratio', String(maximum > 0 ? row.value / maximum : 0)); |
| 86 | + const [number, unit] = metric.unit === 'ops/s' ? [friendly(row.value), metric.unit] : [friendly(row.value, metric.unit), '']; |
| 87 | + bar.append(el(document, 'span', 'rank-fill'), el(document, 'b', null, number)); |
| 88 | + if (unit) bar.lastChild.append(el(document, 'small', null, ' ' + unit)); |
| 89 | + item.append(el(document, 'span', 'rank-no', String(index + 1)), engineName(document, row.name), bar); |
| 90 | + if (scale) item.append(latency(document, row, scale)); |
| 91 | + list.append(item); |
| 92 | + }); |
| 93 | + chart.append(head, list); |
| 94 | + return chart; |
| 95 | +} |
| 96 | + |
| 97 | +function heat(cell, value, column) { |
| 98 | + if (!Number.isFinite(value)) { |
| 99 | + cell.className = 'heat-none'; |
| 100 | + return; |
| 101 | + } |
| 102 | + const score = column.higher ? value / column.best : column.best / value; |
| 103 | + cell.className = 'heat-' + Math.min(HEAT_STEPS - 1, Math.floor(score * HEAT_STEPS)) + (score === 1 ? ' is-best' : ''); |
| 104 | +} |
| 105 | + |
| 106 | +export function matrix(document, model) { |
| 107 | + const table = el(document, 'table', 'matrix'); |
| 108 | + table.append(el(document, 'caption', null, model.caption)); |
| 109 | + const header = el(document, 'tr'); |
| 110 | + header.append(el(document, 'th', null, 'Engine')); |
| 111 | + for (const column of model.columns) { |
| 112 | + const th = el(document, 'th', column.value === model.selected ? 'is-selected' : null); |
| 113 | + th.scope = 'col'; |
| 114 | + const button = el(document, 'button', null, column.label); |
| 115 | + button.type = 'button'; |
| 116 | + button.dataset.scenario = column.value; |
| 117 | + button.append(el(document, 'small', null, column.group ?? column.unit)); |
| 118 | + th.append(button); |
| 119 | + header.append(th); |
| 120 | + } |
| 121 | + table.appendChild(el(document, 'thead')).append(header); |
| 122 | + for (const group of model.groups) { |
| 123 | + const body = table.appendChild(el(document, 'tbody')); |
| 124 | + const label = body.appendChild(el(document, 'tr', 'matrix-group')).appendChild(el(document, 'th', null, group.type)); |
| 125 | + label.colSpan = model.columns.length + 1; |
| 126 | + label.scope = 'rowgroup'; |
| 127 | + for (const row of group.rows) { |
| 128 | + const tr = body.appendChild(el(document, 'tr', row.name === 'KeyLoad' ? 'is-keyload' : null)); |
| 129 | + tr.append(el(document, 'th', null, row.name)); |
| 130 | + tr.firstChild.scope = 'row'; |
| 131 | + model.columns.forEach((column, index) => { |
| 132 | + const value = row.values[index]; |
| 133 | + const td = tr.appendChild(el(document, 'td', null, compact(value, column.unit))); |
| 134 | + heat(td, value, column); |
| 135 | + if (Number.isFinite(value)) td.title = `${row.name} · ${column.title} · ${friendly(value, column.unit)}`; |
| 136 | + }); |
| 137 | + } |
| 138 | + } |
| 139 | + return table; |
| 140 | +} |
| 141 | + |
| 142 | +export function scaling(document, pairs, metric) { |
| 143 | + const list = el(document, 'ul', 'scale-list'); |
| 144 | + const maximum = Math.max(0, ...pairs.flatMap(pair => [pair.one, pair.three])); |
| 145 | + for (const pair of pairs) { |
| 146 | + const item = el(document, 'li', 'scale-row'); |
| 147 | + const track = el(document, 'span', 'scale-track'); |
| 148 | + const range = track.appendChild(el(document, 'span', 'scale-range')); |
| 149 | + const [one, three] = [pair.one, pair.three].map(value => maximum > 0 ? value / maximum * 100 : 0); |
| 150 | + position(range, '--from', Math.min(one, three)); |
| 151 | + position(range, '--to', Math.max(one, three)); |
| 152 | + for (const [className, at, value, nodes] of [['dot-one', one, pair.one, 1], ['dot-three', three, pair.three, 3]]) { |
| 153 | + const dot = track.appendChild(el(document, 'span', 'scale-dot ' + className)); |
| 154 | + position(dot, '--at', at); |
| 155 | + dot.title = `${pair.name} · ${nodes} node${nodes === 1 ? '' : 's'} · ${friendly(value, metric.unit)}`; |
| 156 | + } |
| 157 | + const change = (pair.three - pair.one) / pair.one * 100; |
| 158 | + item.append(engineName(document, pair.name), track, |
| 159 | + el(document, 'span', 'scale-values', `${friendly(pair.one, metric.unit)} → ${friendly(pair.three, metric.unit)}`), |
| 160 | + el(document, 'b', 'scale-change', SIGNED.format(change).replace('-', '−') + '%')); |
| 161 | + list.append(item); |
| 162 | + } |
| 163 | + return list; |
| 164 | +} |
0 commit comments