1// Call fs.promises.readFile 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 6const path = require('path'); 7const common = require('../common.js'); 8const fs = require('fs'); 9const assert = require('assert'); 10 11const tmpdir = require('../../test/common/tmpdir'); 12tmpdir.refresh(); 13const filename = path.resolve(tmpdir.path, 14 `.removeme-benchmark-garbage-${process.pid}`); 15 16const bench = common.createBenchmark(main, { 17 duration: [5], 18 len: [1024, 16 * 1024 * 1024], 19 concurrent: [1, 10] 20}); 21 22function main({ len, duration, concurrent }) { 23 try { fs.unlinkSync(filename); } catch { } 24 let data = Buffer.alloc(len, 'x'); 25 fs.writeFileSync(filename, data); 26 data = null; 27 28 let writes = 0; 29 let benchEnded = false; 30 bench.start(); 31 setTimeout(() => { 32 benchEnded = true; 33 bench.end(writes); 34 try { fs.unlinkSync(filename); } catch { } 35 process.exit(0); 36 }, duration * 1000); 37 38 function read() { 39 fs.promises.readFile(filename) 40 .then((res) => afterRead(undefined, res)) 41 .catch((err) => afterRead(err)); 42 } 43 44 function afterRead(er, data) { 45 if (er) { 46 if (er.code === 'ENOENT') { 47 // Only OK if unlinked by the timer from main. 48 assert.ok(benchEnded); 49 return; 50 } 51 throw er; 52 } 53 54 if (data.length !== len) 55 throw new Error('wrong number of bytes returned'); 56 57 writes++; 58 if (!benchEnded) 59 read(); 60 } 61 62 while (concurrent--) read(); 63} 64