MCPcopy Create free account
hub / github.com/nodejs/node / main

Function main

benchmark/fs/readfile-promises.js:27–82  ·  view source on GitHub ↗
({ len, duration, concurrent, encoding })

Source from the content-addressed store, hash-verified

25});
26
27function main({ len, duration, concurrent, encoding }) {
28 try {
29 fs.unlinkSync(filename);
30 } catch {
31 // Continue regardless of error.
32 }
33 let data = Buffer.alloc(len, 'x');
34 fs.writeFileSync(filename, data);
35 data = null;
36
37 let reads = 0;
38 let waitConcurrent = 0;
39
40 const startedAt = Date.now();
41 const endAt = startedAt + (duration * 1000);
42
43 bench.start();
44
45 function stop() {
46 bench.end(reads);
47
48 try {
49 fs.unlinkSync(filename);
50 } catch {
51 // Continue regardless of error.
52 }
53
54 process.exit(0);
55 }
56
57 function read() {
58 fs.promises.readFile(filename, encoding)
59 .then((res) => afterRead(undefined, res))
60 .catch((err) => afterRead(err));
61 }
62
63 function afterRead(er, data) {
64 if (er) {
65 throw er;
66 }
67
68 if (data.length !== len)
69 throw new Error('wrong number of bytes returned');
70
71 reads++;
72 const benchEnded = Date.now() >= endAt;
73
74 if (benchEnded && (++waitConcurrent) === concurrent) {
75 stop();
76 } else if (!benchEnded) {
77 read();
78 }
79 }
80
81 for (let i = 0; i < concurrent; i++) read();
82}

Callers

nothing calls this directly

Calls 6

allocMethod · 0.80
nowMethod · 0.80
readFunction · 0.70
unlinkSyncMethod · 0.45
writeFileSyncMethod · 0.45
startMethod · 0.45

Tested by

no test coverage detected