oxedyne/fe2o3/fe2o3_austenite/tools/bench/percentile_check.mjs
2.1 KiB, 1 run, executable
created by r1870400018:66245, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #!/usr/bin/env node |
| 2 | // Check: the bench's percentile is nearest-rank, and its typing threshold is max(1.25 t, t + 16 ms). Over the values 1..50, p50 is the 25th smallest |
| 3 | // and p95 the 48th (a `floor(p * n / 100)` index gives 26 and 48, so the p50 case is the one a |
| 4 | // floor plant turns red). The other cases fix the rank at the ends and at a count where |
| 5 | // `p / 100 * n` is not exact in floating point. |
| 6 | import { nearestRank, median, typingThreshold, floorBinds } from './lib/stats.mjs'; |
| 7 | |
| 8 | const seq = (n) => Array.from({ length: n }, (_, i) => i + 1); |
| 9 | let ran = 0; |
| 10 | let failed = 0; |
| 11 | function expect(what, got, want) { |
| 12 | ran += 1; |
| 13 | if (got !== want) { |
| 14 | failed += 1; |
| 15 | process.stdout.write(`FAIL ${what}: got ${got}, want ${want}\n`); |
| 16 | } else { |
| 17 | process.stdout.write(`ok ${what} = ${got}\n`); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | const s50 = seq(50); |
| 22 | expect('p50 of 1..50', nearestRank(s50, 50), 25); |
| 23 | expect('p95 of 1..50', nearestRank(s50, 95), 48); |
| 24 | expect('p100 of 1..50', nearestRank(s50, 100), 50); |
| 25 | expect('p0 of 1..50 (clamps to the minimum)', nearestRank(s50, 0), 1); |
| 26 | expect('p50 of 1..10', nearestRank(seq(10), 50), 5); |
| 27 | expect('p95 of 1..10', nearestRank(seq(10), 95), 10); |
| 28 | expect('p95 of 1..100', nearestRank(seq(100), 95), 95); |
| 29 | expect('p7 of 1..100 (0.07 * 100 is not exact in floating point)', nearestRank(seq(100), 7), 7); |
| 30 | expect('p50 of one value', nearestRank([42], 50), 42); |
| 31 | expect('p95 of one value', nearestRank([42], 95), 42); |
| 32 | expect('p50 of nothing', nearestRank([], 50), null); |
| 33 | expect('median of an odd count', median([3, 1, 2]), 2); |
| 34 | expect('median of an even count', median([4, 1, 3, 2]), 2.5); |
| 35 | |
| 36 | // The typing threshold: the 16 ms floor governs under 64 ms, the 1.25x ratio above it. |
| 37 | const near = (a, b) => Math.abs(a - b) < 1e-12; |
| 38 | expect('threshold at 10 ms is 26 ms (floor governs)', near(typingThreshold(0.010), 0.026), true); |
| 39 | expect('threshold at 100 ms is 125 ms (ratio governs)', near(typingThreshold(0.100), 0.125), true); |
| 40 | expect('the floor binds at 40 ms', floorBinds(0.040), true); |
| 41 | expect('the floor does not bind at 100 ms', floorBinds(0.100), false); |
| 42 | |
| 43 | process.stdout.write(`percentile_check: ${ran} checks, ${failed} failed\n`); |
| 44 | process.exit(failed ? 1 : 0); |