MCPcopy Index your code
hub / github.com/nodejs/node / main

Function main

benchmark/fs/readfile.js:20–73  ·  view source on GitHub ↗
({ len, duration, concurrent, encoding })

Source from the content-addressed store, hash-verified

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

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…