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