Skip to content

Commit a191bc7

Browse files
Linkgorontargos
authored andcommitted
benchmark: add benchmark for fsPromises.writeFile
added fs.promises.writeFile benchmark PR-URL: #37610 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 02cd404 commit a191bc7

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

‎benchmark/fs/writefile-promises.js‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Call fs.promises.writeFile over and over again really fast.
2+
// Then see how many times it got called.
3+
// Yes, this is a silly benchmark. Most benchmarks are silly.
4+
'use strict';
5+
6+
constpath=require('path');
7+
constcommon=require('../common.js');
8+
constfs=require('fs');
9+
constassert=require('assert');
10+
consttmpdir=require('../../test/common/tmpdir');
11+
12+
tmpdir.refresh();
13+
constfilename=path.resolve(tmpdir.path,
14+
`.removeme-benchmark-garbage-${process.pid}`);
15+
letfilesWritten=0;
16+
constbench=common.createBenchmark(main,{
17+
duration: [5],
18+
encodingType: ['buf','asc','utf'],
19+
size: [2,1024,65535,1024*1024],
20+
concurrent: [1,10]
21+
});
22+
23+
functionmain({ encodingType, duration, concurrent, size }){
24+
letencoding;
25+
letchunk;
26+
switch(encodingType){
27+
case'buf':
28+
chunk=Buffer.alloc(size,'b');
29+
break;
30+
case'asc':
31+
chunk='a'.repeat(size);
32+
encoding='ascii';
33+
break;
34+
case'utf':
35+
chunk='ü'.repeat(Math.ceil(size/2));
36+
encoding='utf8';
37+
break;
38+
default:
39+
thrownewError(`invalid encodingType: ${encodingType}`);
40+
}
41+
42+
letwrites=0;
43+
letbenchEnded=false;
44+
bench.start();
45+
setTimeout(()=>{
46+
benchEnded=true;
47+
bench.end(writes);
48+
for(leti=0;i<filesWritten;i++){
49+
try{fs.unlinkSync(`${filename}-${i}`);}catch{}
50+
}
51+
process.exit(0);
52+
},duration*1000);
53+
54+
functionwrite(){
55+
fs.promises.writeFile(`${filename}-${filesWritten++}`,chunk,encoding)
56+
.then(()=>afterWrite())
57+
.catch((err)=>afterWrite(err));
58+
}
59+
60+
functionafterWrite(er){
61+
if(er){
62+
if(er.code==='ENOENT'){
63+
// Only OK if unlinked by the timer from main.
64+
assert.ok(benchEnded);
65+
return;
66+
}
67+
thrower;
68+
}
69+
70+
writes++;
71+
if(!benchEnded)
72+
write();
73+
}
74+
75+
while(concurrent--)write();
76+
}

0 commit comments

Comments
 (0)