|
1 | 1 | const { Suite, jsonReport } = require("bench-node");
|
2 | 2 | const addon = require("./index.node");
|
3 | 3 |
|
| 4 | +function median(values) { |
| 5 | + const sorted = [...values].sort((a, b) => a - b); |
| 6 | + const n = sorted.length; |
| 7 | + return n % 2 === 0 |
| 8 | + ? (sorted[n / 2 - 1] + sorted[n / 2]) / 2 |
| 9 | + : sorted[Math.floor(n / 2)]; |
| 10 | +} |
| 11 | + |
4 | 12 | // A custom reporter for the bencher.dev benchmarking platform.
|
5 | 13 | // Format: https://bencher.dev/docs/reference/bencher-metric-format/
|
| 14 | +// |
| 15 | +// The reporter provides two measures for each benchmark: |
| 16 | +// - "throughput": The number of operations per second. |
| 17 | +// - "latency": The time taken to perform an operation, in ns. |
| 18 | +// * "value": The median value of all samples. |
| 19 | +// * "lower_value": The minimum value of all samples. |
| 20 | +// * "upper_value": The maximum value of all samples. |
6 | 21 | function reportBencherDev(results) {
|
7 | 22 | const bmf = Object.create(null);
|
8 | 23 | for (const result of results) {
|
9 |
| - // If https://github.com/RafaelGSS/bench-node/issues/66 is fixed, then we can use |
10 |
| - // result.histogram to report a "latency" measure with the median as the "value", |
11 |
| - // min as "lower_value", and max as "upper_value". |
12 | 24 | bmf[result.name] = {
|
13 | 25 | throughput: {
|
14 | 26 | value: result.opsSec,
|
15 | 27 | },
|
| 28 | + latency: { |
| 29 | + value: median(result.histogram.sampleData), |
| 30 | + lower_value: result.histogram.min, |
| 31 | + upper_value: result.histogram.max, |
| 32 | + }, |
16 | 33 | };
|
17 | 34 | }
|
18 | 35 | console.log(JSON.stringify(bmf, null, 2));
|
|
0 commit comments