Skip to content

Commit aca03d9

Browse files
RafaelGSSaduh95
authored andcommitted
benchmark: add --runs support to run.js
PR-URL: #55158 Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
1 parent 7b3e38b commit aca03d9

File tree

2 files changed

+57
-36
lines changed

2 files changed

+57
-36
lines changed

‎benchmark/run.js‎

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const cli = new CLI(`usage: ./node run.js [options] [--] <category> ...
1212
(can be repeated)
1313
--exclude pattern excludes scripts matching <pattern> (can be
1414
repeated)
15+
--runs variable=value set the amount of benchmark suite execution.
16+
Default: 1
1517
--set variable=value set benchmark variable (can be repeated)
1618
--format [simple|csv] optional value that specifies the output format
1719
test only run a single configuration from the options
@@ -45,8 +47,7 @@ if (format === 'csv'){
4547
console.log('"filename", "configuration", "rate", "time"');
4648
}
4749

48-
(functionrecursive(i){
49-
constfilename=benchmarks[i];
50+
functionrunBenchmark(filename){
5051
constscriptPath=path.resolve(__dirname,filename);
5152

5253
constargs=cli.test ? ['--test'] : cli.optional.set;
@@ -63,42 +64,52 @@ if (format === 'csv'){
6364
);
6465
}
6566

66-
if(format!=='csv'){
67-
console.log();
68-
console.log(filename);
69-
}
70-
71-
child.on('message',(data)=>{
72-
if(data.type!=='report'){
73-
return;
74-
}
75-
// Construct configuration string, " A=a, B=b, ..."
76-
letconf='';
77-
for(constkeyofObject.keys(data.conf)){
78-
if(conf!=='')
79-
conf+=' ';
80-
conf+=`${key}=${JSON.stringify(data.conf[key])}`;
81-
}
82-
if(format==='csv'){
83-
// Escape quotes (") for correct csv formatting
84-
conf=conf.replace(/"/g,'""');
85-
console.log(`"${data.name}", "${conf}", ${data.rate}, ${data.time}`);
86-
}else{
87-
letrate=data.rate.toString().split('.');
88-
rate[0]=rate[0].replace(/(\d)(?=(?:\d\d\d)+(?!\d))/g,'$1,');
89-
rate=(rate[1] ? rate.join('.') : rate[0]);
90-
console.log(`${data.name}${conf}: ${rate}`);
91-
}
67+
returnnewPromise((resolve,reject)=>{
68+
child.on('message',(data)=>{
69+
if(data.type!=='report'){
70+
return;
71+
}
72+
// Construct configuration string, " A=a, B=b, ..."
73+
letconf='';
74+
for(constkeyofObject.keys(data.conf)){
75+
if(conf!=='')
76+
conf+=' ';
77+
conf+=`${key}=${JSON.stringify(data.conf[key])}`;
78+
}
79+
if(format==='csv'){
80+
// Escape quotes (") for correct csv formatting
81+
conf=conf.replace(/"/g,'""');
82+
console.log(`"${data.name}", "${conf}", ${data.rate}, ${data.time}`);
83+
}else{
84+
letrate=data.rate.toString().split('.');
85+
rate[0]=rate[0].replace(/(\d)(?=(?:\d\d\d)+(?!\d))/g,'$1,');
86+
rate=(rate[1] ? rate.join('.') : rate[0]);
87+
console.log(`${data.name}${conf}: ${rate}`);
88+
}
89+
});
90+
child.once('close',(code)=>{
91+
if(code){
92+
reject(code);
93+
}else{
94+
resolve(code);
95+
}
96+
});
9297
});
98+
}
9399

94-
child.once('close',(code)=>{
95-
if(code){
96-
process.exit(code);
100+
asyncfunctionrun(){
101+
for(leti=0;i<benchmarks.length;++i){
102+
letruns=cli.optional.runs??1;
103+
constfilename=benchmarks[i];
104+
if(format!=='csv'){
105+
console.log();
106+
console.log(filename);
97107
}
98108

99-
// If there are more benchmarks execute the next
100-
if(i+1<benchmarks.length){
101-
recursive(i+1);
109+
while(runs-->0){
110+
awaitrunBenchmark(filename);
102111
}
103-
});
104-
})(0);
112+
}
113+
}
114+
115+
run();

‎doc/contributing/writing-and-running-benchmarks.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ It is possible to execute more groups by adding extra process arguments.
174174
node benchmark/run.js assert async_hooks
175175
```
176176

177+
It's also possible to execute the benchmark more than once using the
178+
`--runs` flag.
179+
180+
```bash
181+
node benchmark/run.js --runs 10 assert async_hooks
182+
```
183+
184+
This command will run the benchmark files in `benchmark/assert` and `benchmark/async_hooks`
185+
10 times each.
186+
177187
#### Specifying CPU Cores for Benchmarks with run.js
178188

179189
When using `run.js` to execute a group of benchmarks,

0 commit comments

Comments
(0)