|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { readFileSync } from 'fs'; |
| 4 | + |
| 5 | +function addCommas(num) { |
| 6 | + const str = num.toString(); |
| 7 | + if (str.length <= 3) return str; |
| 8 | + return addCommas(str.slice(0, -3)) + ',' + str.slice(-3); |
| 9 | +} |
| 10 | + |
| 11 | +function formatNumber(num, decimals = 4) { |
| 12 | + const multiplier = Math.pow(10, decimals); |
| 13 | + return (Math.round(num * multiplier) / multiplier).toString(); |
| 14 | +} |
| 15 | + |
| 16 | +function formatBenchmarkResults(jsonFile) { |
| 17 | + const data = JSON.parse(readFileSync(jsonFile, 'utf8')); |
| 18 | + |
| 19 | + const lines = []; |
| 20 | + lines.push('## 📊 Performance Benchmark Results'); |
| 21 | + lines.push(''); |
| 22 | + lines.push('| Benchmark | Hz | Min | Max | Mean | P75 | P99 | P995 | P999 |'); |
| 23 | + lines.push('|-----------|-------|------|------|------|------|------|------|------|'); |
| 24 | + |
| 25 | + let totalBenchmarks = 0; |
| 26 | + |
| 27 | + for (const file of data.files) { |
| 28 | + for (const group of file.groups) { |
| 29 | + for (const benchmark of group.benchmarks) { |
| 30 | + totalBenchmarks++; |
| 31 | + |
| 32 | + const row = [ |
| 33 | + benchmark.name, |
| 34 | + addCommas(Math.floor(benchmark.hz)), |
| 35 | + formatNumber(benchmark.min), |
| 36 | + formatNumber(benchmark.max), |
| 37 | + formatNumber(benchmark.mean), |
| 38 | + formatNumber(benchmark.p75), |
| 39 | + formatNumber(benchmark.p99), |
| 40 | + formatNumber(benchmark.p995), |
| 41 | + formatNumber(benchmark.p999) |
| 42 | + ]; |
| 43 | + |
| 44 | + lines.push('| ' + row.join(' | ') + ' |'); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + lines.push(''); |
| 50 | + lines.push(`_Total benchmarks: ${totalBenchmarks}_`); |
| 51 | + lines.push(''); |
| 52 | + |
| 53 | + return lines.join('\n'); |
| 54 | +} |
| 55 | + |
| 56 | +// Main execution |
| 57 | +const jsonFile = process.argv[2] || 'bench-results.json'; |
| 58 | + |
| 59 | +try { |
| 60 | + const markdown = formatBenchmarkResults(jsonFile); |
| 61 | + console.log(markdown); |
| 62 | +} catch (error) { |
| 63 | + console.error('Error formatting benchmark results:', error.message); |
| 64 | + process.exit(1); |
| 65 | +} |
0 commit comments