|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { |
| 4 | + ArrayPrototypeJoin, |
| 5 | + ArrayPrototypePush, |
| 6 | + JSONStringify, |
| 7 | + MathAbs, |
| 8 | + NumberIsFinite, |
| 9 | + NumberPrototypeToFixed, |
| 10 | + NumberPrototypeToPrecision, |
| 11 | + ObjectEntries, |
| 12 | + String, |
| 13 | + StringPrototypeReplaceAll, |
| 14 | +} = primordials; |
| 15 | +const Transform = require('internal/streams/transform'); |
| 16 | + |
| 17 | +const kHeader = |
| 18 | + 'benchmark | samples | mean rate | 95% CI | median rate | warning\n'; |
| 19 | + |
| 20 | +function escapeCell(value) { |
| 21 | + let result = String(value); |
| 22 | + result = StringPrototypeReplaceAll(result, '\r', '\\r'); |
| 23 | + result = StringPrototypeReplaceAll(result, '\n', '\\n'); |
| 24 | + return StringPrototypeReplaceAll(result, '|', '\\|'); |
| 25 | +} |
| 26 | + |
| 27 | +function formatName(data) { |
| 28 | + const name = escapeCell(data.name); |
| 29 | + const entries = ObjectEntries(data.params); |
| 30 | + if (entries.length === 0) return name; |
| 31 | + |
| 32 | + const params = []; |
| 33 | + for (let i = 0; i < entries.length; i++) { |
| 34 | + const { 0: key, 1: value } = entries[i]; |
| 35 | + ArrayPrototypePush( |
| 36 | + params, `${escapeCell(key)}=${escapeCell(JSONStringify(value))}`); |
| 37 | + } |
| 38 | + return `${name} [${ArrayPrototypeJoin(params, ', ')}]`; |
| 39 | +} |
| 40 | + |
| 41 | +function formatRate(rate) { |
| 42 | + if (!NumberIsFinite(rate)) return '-'; |
| 43 | + |
| 44 | + const absolute = MathAbs(rate); |
| 45 | + let divisor = 1; |
| 46 | + let prefix = ''; |
| 47 | + if (absolute >= 1e9) { |
| 48 | + divisor = 1e9; |
| 49 | + prefix = 'G'; |
| 50 | + } else if (absolute >= 1e6) { |
| 51 | + divisor = 1e6; |
| 52 | + prefix = 'M'; |
| 53 | + } else if (absolute >= 1e3) { |
| 54 | + divisor = 1e3; |
| 55 | + prefix = 'k'; |
| 56 | + } |
| 57 | + |
| 58 | + const scaled = rate / divisor; |
| 59 | + const formatted = MathAbs(scaled) > 0 && MathAbs(scaled) < 1 ? |
| 60 | + NumberPrototypeToPrecision(scaled, 3) : |
| 61 | + NumberPrototypeToFixed(scaled, 2); |
| 62 | + return `${formatted}${prefix} ops/s`; |
| 63 | +} |
| 64 | + |
| 65 | +function formatWarning(summary) { |
| 66 | + const warnings = []; |
| 67 | + if (NumberIsFinite(summary.coefficientOfVariation) && |
| 68 | + summary.coefficientOfVariation > 0.05) { |
| 69 | + ArrayPrototypePush(warnings, 'noisy'); |
| 70 | + } |
| 71 | + if (NumberIsFinite(summary.skewness) && MathAbs(summary.skewness) > 1) { |
| 72 | + ArrayPrototypePush(warnings, 'skewed'); |
| 73 | + } |
| 74 | + return ArrayPrototypeJoin(warnings, ', '); |
| 75 | +} |
| 76 | + |
| 77 | +function formatResult(data) { |
| 78 | + const name = formatName(data); |
| 79 | + if (data.skip !== undefined) { |
| 80 | + const reason = typeof data.skip === 'string' && data.skip.length > 0 ? |
| 81 | + `: ${escapeCell(data.skip)}` : ''; |
| 82 | + return `${name} | 0 | - | - | - | skipped${reason}\n`; |
| 83 | + } |
| 84 | + if (data.error !== undefined) { |
| 85 | + const message = data.error?.message ?? data.error; |
| 86 | + return `${name} | ${data.samples.length} | - | - | - | ` + |
| 87 | + `error: ${escapeCell(message)}\n`; |
| 88 | + } |
| 89 | + |
| 90 | + const { confidenceInterval, median } = data.summary; |
| 91 | + return `${name} | ${data.samples.length} | ` + |
| 92 | + `${formatRate(data.summary.mean)} | ` + |
| 93 | + `[${formatRate(confidenceInterval.lower)}, ` + |
| 94 | + `${formatRate(confidenceInterval.upper)}] | ` + |
| 95 | + `${formatRate(median)} | ${formatWarning(data.summary)}\n`; |
| 96 | +} |
| 97 | + |
| 98 | +class SpecReporter extends Transform { |
| 99 | + #diagnostics = []; |
| 100 | + #reported = false; |
| 101 | + #results = []; |
| 102 | + |
| 103 | + constructor() { |
| 104 | + super({ __proto__: null, writableObjectMode: true }); |
| 105 | + } |
| 106 | + |
| 107 | + #format(summary = undefined) { |
| 108 | + let output = kHeader; |
| 109 | + for (let i = 0; i < this.#results.length; i++) { |
| 110 | + output += formatResult(this.#results[i]); |
| 111 | + } |
| 112 | + for (let i = 0; i < this.#diagnostics.length; i++) { |
| 113 | + output += `diagnostic: ${escapeCell(this.#diagnostics[i].message)}\n`; |
| 114 | + } |
| 115 | + if (summary !== undefined) { |
| 116 | + const { completed, failed, skipped } = summary.counts; |
| 117 | + output += `\n${completed} completed, ${failed} failed, ` + |
| 118 | + `${skipped} skipped\n`; |
| 119 | + } |
| 120 | + return output; |
| 121 | + } |
| 122 | + |
| 123 | + _transform({ type, data }, _encoding, callback) { |
| 124 | + switch (type) { |
| 125 | + case 'bench:complete': |
| 126 | + ArrayPrototypePush(this.#results, data); |
| 127 | + break; |
| 128 | + case 'bench:diagnostic': |
| 129 | + ArrayPrototypePush(this.#diagnostics, data); |
| 130 | + break; |
| 131 | + case 'bench:summary': |
| 132 | + this.#reported = true; |
| 133 | + callback(null, this.#format(data)); |
| 134 | + return; |
| 135 | + } |
| 136 | + callback(); |
| 137 | + } |
| 138 | + |
| 139 | + _flush(callback) { |
| 140 | + if (this.#reported || |
| 141 | + (this.#results.length === 0 && this.#diagnostics.length === 0)) { |
| 142 | + callback(); |
| 143 | + return; |
| 144 | + } |
| 145 | + callback(null, this.#format()); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +module.exports = SpecReporter; |
0 commit comments