|
| 1 | +#!/usr/bin/env bun |
| 2 | +// @ts-ignore |
| 3 | +import { $ } from 'bun'; |
| 4 | + |
| 5 | +const count = parseInt(process.argv[2] ?? '10'); |
| 6 | + |
| 7 | +async function run(algo: string) { |
| 8 | + // Run the command and capture both stdout and stderr |
| 9 | + const proc = |
| 10 | + await $`/usr/bin/time -v pnpm tsx test/payload.ts ${algo}`.quiet(); |
| 11 | + |
| 12 | + // time -v outputs to stderr |
| 13 | + const output = proc.stderr.toString(); |
| 14 | + |
| 15 | + // Extract user time (in seconds) |
| 16 | + const userTimeMatch = output.match(/User time \(seconds\): ([\d.]+)/); |
| 17 | + const userTime = userTimeMatch ? parseFloat(userTimeMatch[1]) : null; |
| 18 | + |
| 19 | + // Extract maximum resident set size (in KB) |
| 20 | + const maxRssMatch = output.match( |
| 21 | + /Maximum resident set size \(kbytes\): (\d+)/ |
| 22 | + ); |
| 23 | + const maxRss = maxRssMatch ? parseInt(maxRssMatch[1]) : null; |
| 24 | + |
| 25 | + // Create JSON output |
| 26 | + return { |
| 27 | + time: userTime, |
| 28 | + mem: maxRss, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +async function bench(algo: string) { |
| 33 | + console.log(`\n==RUNNING BENCHMARK FOR ${algo.toUpperCase()}==\n`); |
| 34 | + |
| 35 | + // one once to warm any caching - the very first run is often slowe |
| 36 | + await run(algo); |
| 37 | + |
| 38 | + // Run 10 times and collect results |
| 39 | + const results = []; |
| 40 | + for (let i = 0; i < count; i++) { |
| 41 | + const result = await run(algo); |
| 42 | + results.push(result); |
| 43 | + console.log(`Run ${i + 1}: used ${result.mem}kb in ${result.time}ms`); |
| 44 | + } |
| 45 | + |
| 46 | + // Calculate statistics |
| 47 | + const times = results.map((r) => r.time || 0); |
| 48 | + const mems = results.map((r) => r.mem || 0); |
| 49 | + |
| 50 | + const avgTime = times.reduce((sum, t) => sum + t, 0) / times.length; |
| 51 | + const minTime = Math.min(...times); |
| 52 | + const maxTime = Math.max(...times); |
| 53 | + |
| 54 | + const avgMem = mems.reduce((sum, m) => sum + m, 0) / mems.length; |
| 55 | + const minMem = Math.min(...mems); |
| 56 | + const maxMem = Math.max(...mems); |
| 57 | + |
| 58 | + const stats = { |
| 59 | + time: { min: minTime, max: maxTime, avg: avgTime }, |
| 60 | + mem: { min: minMem, max: maxMem, avg: avgMem }, |
| 61 | + }; |
| 62 | + |
| 63 | + console.log(`\n${algo} results:`); |
| 64 | + console.log( |
| 65 | + ` Time (ms): min=${minTime.toFixed(2)}, max=${maxTime.toFixed( |
| 66 | + 2 |
| 67 | + )}, avg=${avgTime.toFixed(2)}` |
| 68 | + ); |
| 69 | + console.log( |
| 70 | + ` Memory (MB): min=${(minMem / 1024).toFixed(2)}, max=${( |
| 71 | + maxMem / 1024 |
| 72 | + ).toFixed(2)}, avg=${(avgMem / 1024).toFixed(2)}` |
| 73 | + ); |
| 74 | + console.log(); |
| 75 | + |
| 76 | + return stats; |
| 77 | +} |
| 78 | + |
| 79 | +// @ts-ignore |
| 80 | +const stringifyStats = await bench('stringify'); |
| 81 | +// @ts-ignore |
| 82 | +const streamStats = await bench('stream'); |
| 83 | + |
| 84 | +// Calculate percentage differences (traverse vs stringify baseline) |
| 85 | +function calcDiff(baseline: number, comparison: number) { |
| 86 | + return (((comparison - baseline) / baseline) * 100).toFixed(2); |
| 87 | +} |
| 88 | + |
| 89 | +console.log('\n=== COMPARISON (stream vs stringify baseline) ===\n'); |
| 90 | + |
| 91 | +console.log('Time Performance:'); |
| 92 | +console.log( |
| 93 | + ` Min: ${calcDiff(stringifyStats.time.min, streamStats.time.min)}%` |
| 94 | +); |
| 95 | +console.log( |
| 96 | + ` Max: ${calcDiff(stringifyStats.time.max, streamStats.time.max)}%` |
| 97 | +); |
| 98 | +console.log( |
| 99 | + ` Avg: ${calcDiff(stringifyStats.time.avg, streamStats.time.avg)}%` |
| 100 | +); |
| 101 | + |
| 102 | +console.log('\nMemory Usage:'); |
| 103 | +console.log(` Min: ${calcDiff(stringifyStats.mem.min, streamStats.mem.min)}%`); |
| 104 | +console.log(` Max: ${calcDiff(stringifyStats.mem.max, streamStats.mem.max)}%`); |
| 105 | +console.log(` Avg: ${calcDiff(stringifyStats.mem.avg, streamStats.mem.avg)}%`); |
| 106 | + |
| 107 | +console.log('\n(Negative values mean stream is better)'); |
0 commit comments