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

Function main

benchmark/fs/readfile-permission-enabled.js:27–80  ·  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.readFile(filename, encoding, afterRead);
59 }
60
61 function afterRead(er, data) {
62 if (er) {
63 throw er;
64 }
65
66 if (data.length !== len)
67 throw new Error('wrong number of bytes returned');
68
69 reads++;
70 const benchEnded = Date.now() >= endAt;
71
72 if (benchEnded && (++waitConcurrent) === concurrent) {
73 stop();
74 } else if (!benchEnded) {
75 read();
76 }
77 }
78
79 for (let i = 0; i < concurrent; i++) read();
80}

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