-
-
Notifications
You must be signed in to change notification settings - Fork 36.7k
Expand file tree
/
Copy path_node-bench.js
More file actions
160 lines (148 loc) Β· 4.7 KB
/
Copy path_node-bench.js
File metadata and controls
160 lines (148 loc) Β· 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
const { spawn } = require('node:child_process');
const path = require('node:path');
const { inspect } = require('node:util');
function parseInteger(value, defaultValue, name, minimum) {
if (value === undefined) return defaultValue;
if (!/^(?:0|[1-9]\d*)$/.test(value)) {
throw new TypeError(`${name} must be an integer`);
}
const number = Number(value);
if (!Number.isSafeInteger(number) || number < minimum) {
throw new RangeError(`${name} must be at least ${minimum}`);
}
return number;
}
function parseNumber(value, defaultValue, name, minimum) {
if (value === undefined) return defaultValue;
if (value.trim() === '') throw new TypeError(`${name} must be a number`);
const number = Number(value);
if (!Number.isFinite(number)) throw new TypeError(`${name} must be a number`);
if (number < minimum) {
throw new RangeError(`${name} must be at least ${minimum}`);
}
return number;
}
function csvEncode(value) {
if (typeof value === 'number' || typeof value === 'boolean') {
return String(value);
}
const string = String(value);
return `"${string.replace(/"/g, '""')}"`;
}
function formatConfiguration(params) {
return Object.keys(params)
.map((key) => `${key}=${inspect(params[key])}`)
.join(' ');
}
function durationToSeconds(duration) {
if (!/^\d+$/.test(duration)) {
throw new TypeError(`Invalid benchmark duration '${duration}'`);
}
const padded = duration.padStart(10, '0');
return `${padded.slice(0, -9)}.${padded.slice(-9)}`;
}
function runBenchmark(binary, file, options) {
const args = [
...options.nodeArgs,
'--no-warnings',
'--experimental-bench',
'--bench',
'--bench-reporter=json',
'--bench-samples=1',
`--bench-warmup=${options.warmup}`,
];
if (options.namePattern !== undefined) {
args.push(`--bench-name-pattern=${options.namePattern}`);
}
args.push('--', path.resolve(file));
return new Promise((resolve, reject) => {
const child = spawn(binary, args, {
env: process.env,
stdio: ['ignore', 'pipe', 'pipe'],
});
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
let stdout = '';
let stderr = '';
child.stdout.on('data', (data) => { stdout += data; });
child.stderr.on('data', (data) => { stderr += data; });
child.once('error', reject);
child.once('close', (code, signal) => {
let records;
try {
records = stdout.trim().split('\n')
.filter((line) => line.length > 0)
.map((line) => JSON.parse(line));
} catch (error) {
reject(new Error(
`Could not parse benchmark output from '${binary}': ${error.message}`,
{ cause: error },
));
return;
}
const summary = records.findLast(
({ type }) => type === 'bench:summary')?.data;
if (code !== 0 || signal !== null || summary?.success !== true) {
const diagnostics = records
.filter(({ type }) => type === 'bench:diagnostic')
.map(({ data }) => data.message)
.join('\n');
const status = signal === null ? `exit code ${code}` : `signal ${signal}`;
const details = stderr || diagnostics;
reject(new Error(
`Benchmark '${file}' failed with ${status}` +
(details ? `:\n${details}` : ''),
));
return;
}
const samples = [];
for (const record of records) {
if (record.type !== 'bench:complete' ||
record.data.skip !== undefined) {
continue;
}
if (record.data.error !== undefined) {
reject(new Error(
`Benchmark '${record.data.name}' failed: ` +
record.data.error.message,
));
return;
}
if (record.data.samples.length !== 1) {
reject(new Error(
`Benchmark '${record.data.name}' did not produce exactly one sample`,
));
return;
}
const sample = record.data.samples[0];
if (!Number.isFinite(sample.rate)) {
reject(new Error(
`Benchmark '${record.data.name}' produced a non-finite rate`,
));
return;
}
samples.push({
configuration: formatConfiguration(record.data.params),
duration: durationToSeconds(sample.duration_ns),
identity: record.data.benchId,
logicalIdentity: JSON.stringify([
record.data.file,
record.data.parentId,
record.data.name,
]),
name: record.data.name,
params: record.data.params,
rate: sample.rate,
});
}
resolve(samples);
});
});
}
module.exports = {
csvEncode,
parseInteger,
parseNumber,
runBenchmark,
};