|
| 1 | +import { readFileSync, writeFileSync } from 'node:fs'; |
| 2 | +import type { BenchmarkResult } from './procedural.ts'; |
| 3 | + |
| 4 | +interface Dataset { |
| 5 | + label: string; |
| 6 | + data: BenchmarkResult[]; |
| 7 | +} |
| 8 | + |
| 9 | +const args = process.argv.slice(2); |
| 10 | + |
| 11 | +const inputs: Dataset[] = []; |
| 12 | +let title = ''; |
| 13 | +let output = ''; |
| 14 | +let xAxisTitle = ''; |
| 15 | +let yAxisTitle = ''; |
| 16 | + |
| 17 | +// parsing arguments |
| 18 | +for (let i = 0; i < args.length; i++) { |
| 19 | + if (args[i] === '--input') { |
| 20 | + // oxlint-disable-next-line typescript/no-non-null-assertion |
| 21 | + const [label, filepath] = args[++i]!.split(':', 2); |
| 22 | + inputs.push({ |
| 23 | + label: label as string, |
| 24 | + data: JSON.parse(readFileSync(filepath as string, 'utf8')), |
| 25 | + }); |
| 26 | + } else if (args[i] === '--title') { |
| 27 | + if (title !== '') { |
| 28 | + throw new Error('Ambiguous title'); |
| 29 | + } |
| 30 | + // oxlint-disable-next-line typescript/no-non-null-assertion |
| 31 | + title = args[++i]!; |
| 32 | + } else if (args[i] === '--output') { |
| 33 | + if (output !== '') { |
| 34 | + throw new Error('Ambiguous output'); |
| 35 | + } |
| 36 | + // oxlint-disable-next-line typescript/no-non-null-assertion |
| 37 | + output = args[++i]!; |
| 38 | + } else if (args[i] === '--xAxisTitle') { |
| 39 | + if (xAxisTitle !== '') { |
| 40 | + throw new Error('Ambiguous xAxisTitle'); |
| 41 | + } |
| 42 | + // oxlint-disable-next-line typescript/no-non-null-assertion |
| 43 | + xAxisTitle = args[++i]!; |
| 44 | + } else if (args[i] === '--yAxisTitle') { |
| 45 | + if (yAxisTitle !== '') { |
| 46 | + throw new Error('Ambiguous yAxisTitle'); |
| 47 | + } |
| 48 | + // oxlint-disable-next-line typescript/no-non-null-assertion |
| 49 | + yAxisTitle = args[++i]!; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +if (inputs.length === 0) { |
| 54 | + throw new Error('No input path provided'); |
| 55 | +} |
| 56 | + |
| 57 | +if (inputs.length > 3) { |
| 58 | + throw new Error('Inputs are limited to 3'); |
| 59 | +} |
| 60 | + |
| 61 | +if (title === '') { |
| 62 | + throw new Error('No title provided'); |
| 63 | +} |
| 64 | + |
| 65 | +function median(values: number[]): number { |
| 66 | + // oxlint-disable-next-line eslint-plugin-unicorn(no-array-sort) |
| 67 | + const sorted = values.sort((a, b) => a - b); |
| 68 | + const mid = Math.floor(sorted.length / 2); |
| 69 | + // oxlint-disable-next-line typescript/no-non-null-assertion |
| 70 | + return sorted.length % 2 ? sorted[mid]! : (sorted[mid - 1]! + sorted[mid]!) / 2; |
| 71 | +} |
| 72 | + |
| 73 | +// oxlint-disable-next-line eslint-plugin-unicorn(no-array-sort) |
| 74 | +const allDepths = [...new Set(inputs.flatMap((ds) => ds.data.map((d) => d.maxDepth)))].sort( |
| 75 | + (a, b) => a - b, |
| 76 | +); |
| 77 | + |
| 78 | +// mermaid chart |
| 79 | +const lines: string[] = []; |
| 80 | + |
| 81 | +const symbols = ['\u{1f534}', '\u{1f535}', '\u{1f7e2}']; |
| 82 | +const legend = inputs.map((ds, i) => `${symbols[i]} ${ds.label}`).join(' | '); |
| 83 | + |
| 84 | +lines.push('```mermaid'); |
| 85 | +lines.push(`--- |
| 86 | +config: |
| 87 | + themeVariables: |
| 88 | + xyChart: |
| 89 | + plotColorPalette: "#E63946, #3B82F6, #059669" |
| 90 | +---`); |
| 91 | +lines.push('xychart'); |
| 92 | +lines.push(` title "${title} (${legend})"`); |
| 93 | +lines.push(` x-axis "${xAxisTitle ? xAxisTitle : ' '}" [${allDepths.join(', ')}]`); |
| 94 | +lines.push(` y-axis "${yAxisTitle ? yAxisTitle : ' '}"`); |
| 95 | + |
| 96 | +for (const ds of inputs) { |
| 97 | + const medians = allDepths.map((depth) => { |
| 98 | + const times = ds.data.filter((d) => d.maxDepth === depth).map((d) => d.timeMs); |
| 99 | + return times.length ? median(times).toFixed(2) : '0'; |
| 100 | + }); |
| 101 | + lines.push(` line [${medians.join(', ')}]`); |
| 102 | +} |
| 103 | + |
| 104 | +lines.push('```'); |
| 105 | + |
| 106 | +const md = lines.join('\n'); |
| 107 | + |
| 108 | +if (output) { |
| 109 | + writeFileSync(output, md); |
| 110 | +} else { |
| 111 | + console.log(md); |
| 112 | +} |
0 commit comments