• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 reads = 0;
29  let benchEnded = false;
30  bench.start();
31  setTimeout(() => {
32    benchEnded = true;
33    bench.end(reads);
34    try { fs.unlinkSync(filename); } catch {}
35    process.exit(0);
36  }, duration * 1000);
37
38  function read() {
39    fs.readFile(filename, afterRead);
40  }
41
42  function afterRead(er, data) {
43    if (er) {
44      if (er.code === 'ENOENT') {
45        // Only OK if unlinked by the timer from main.
46        assert.ok(benchEnded);
47        return;
48      }
49      throw er;
50    }
51
52    if (data.length !== len)
53      throw new Error('wrong number of bytes returned');
54
55    reads++;
56    if (!benchEnded)
57      read();
58  }
59
60  while (concurrent--) read();
61}
62