|
| 1 | +import test from 'node:test' |
| 2 | +import assert from 'node:assert/strict' |
| 3 | +import { readFile } from 'node:fs/promises' |
| 4 | +import { getBarHeight, isBestCompressionRatio } from '../.vitepress/theme/components/benchmarkChartMetrics.js' |
| 5 | + |
| 6 | +const loadCompressionRatios = async dataset => { |
| 7 | + const source = await readFile(new URL('../.vitepress/data/benchmarks.json', import.meta.url), 'utf8') |
| 8 | + const { results } = JSON.parse(source) |
| 9 | + return results |
| 10 | + .filter(result => result.dataset === dataset) |
| 11 | + .map(result => result.compressionRatio) |
| 12 | +} |
| 13 | + |
| 14 | +test('compression ratio bars reward smaller output/input ratios', async () => { |
| 15 | + const values = await loadCompressionRatios('textlike_10MiB') |
| 16 | + const bestRatio = Math.min(...values) |
| 17 | + const worstRatio = Math.max(...values) |
| 18 | + |
| 19 | + assert.ok( |
| 20 | + getBarHeight('compressionRatio', bestRatio, worstRatio) > |
| 21 | + getBarHeight('compressionRatio', worstRatio, worstRatio), |
| 22 | + 'smaller compression ratios should render taller bars' |
| 23 | + ) |
| 24 | +}) |
| 25 | + |
| 26 | +test('compression ratio bars keep near-identical ratios visually close', () => { |
| 27 | + const better = getBarHeight('compressionRatio', 0.743, 0.746) |
| 28 | + const worse = getBarHeight('compressionRatio', 0.746, 0.746) |
| 29 | + |
| 30 | + assert.ok(better > worse, 'smaller compression ratios should still render taller bars') |
| 31 | + assert.ok( |
| 32 | + better - worse < 1, |
| 33 | + 'near-identical compression ratios should not be stretched across the full chart' |
| 34 | + ) |
| 35 | +}) |
| 36 | + |
| 37 | +test('compression ratio bars bottom out once compression stops helping', () => { |
| 38 | + assert.ok( |
| 39 | + getBarHeight('compressionRatio', 0.95, 1) > getBarHeight('compressionRatio', 1, 1), |
| 40 | + 'small-but-positive savings should still render above the no-compression baseline' |
| 41 | + ) |
| 42 | + assert.equal( |
| 43 | + getBarHeight('compressionRatio', 1.01, 2), |
| 44 | + 2, |
| 45 | + 'expanding results should stay at the floor because they save no space' |
| 46 | + ) |
| 47 | + assert.equal( |
| 48 | + getBarHeight('compressionRatio', 2, 2), |
| 49 | + 2, |
| 50 | + 'worse expansion should also stay at the floor' |
| 51 | + ) |
| 52 | +}) |
| 53 | + |
| 54 | +test('best compression ratio stays highlighted even when bars are close', async () => { |
| 55 | + const values = await loadCompressionRatios('textlike_10MiB') |
| 56 | + const bestRatio = Math.min(...values) |
| 57 | + const worstRatio = Math.max(...values) |
| 58 | + |
| 59 | + assert.equal( |
| 60 | + isBestCompressionRatio('compressionRatio', bestRatio, values), |
| 61 | + true, |
| 62 | + 'the lowest compression ratio should be highlighted' |
| 63 | + ) |
| 64 | + assert.equal( |
| 65 | + isBestCompressionRatio('compressionRatio', worstRatio, values), |
| 66 | + false, |
| 67 | + 'higher compression ratios should not be highlighted' |
| 68 | + ) |
| 69 | +}) |
| 70 | + |
| 71 | +test('best compression ratio still gets highlighted among expanding results', () => { |
| 72 | + assert.equal( |
| 73 | + isBestCompressionRatio('compressionRatio', 1.01, [1.01, 2]), |
| 74 | + true, |
| 75 | + 'the smaller expansion ratio should still be highlighted as the better result' |
| 76 | + ) |
| 77 | + assert.equal( |
| 78 | + isBestCompressionRatio('compressionRatio', 2, [1.01, 2]), |
| 79 | + false, |
| 80 | + 'the larger expansion ratio should not be highlighted' |
| 81 | + ) |
| 82 | +}) |
| 83 | + |
| 84 | +test('encode speed bars still reward faster results', () => { |
| 85 | + assert.ok( |
| 86 | + getBarHeight('encodeSpeed', 277, 277) > getBarHeight('encodeSpeed', 58.8, 277), |
| 87 | + 'higher encode speeds should still render taller bars' |
| 88 | + ) |
| 89 | +}) |
0 commit comments