• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Submit a mix of short and long jobs to the threadpool.
2// Report total job throughput.
3// If we partition the long job, overall job throughput goes up significantly.
4// However, this comes at the cost of the long job throughput.
5//
6// Short jobs: small zip jobs.
7// Long jobs: fs.readFile on a large file.
8
9'use strict';
10
11const path = require('path');
12const common = require('../common.js');
13const filename = path.resolve(__dirname,
14                              `.removeme-benchmark-garbage-${process.pid}`);
15const fs = require('fs');
16const zlib = require('zlib');
17const assert = require('assert');
18
19const bench = common.createBenchmark(main, {
20  duration: [5],
21  encoding: ['', 'utf-8'],
22  len: [1024, 16 * 1024 * 1024],
23  concurrent: [1, 10],
24});
25
26function main({ len, duration, concurrent, encoding }) {
27  try {
28    fs.unlinkSync(filename);
29  } catch {
30    // Continue regardless of error.
31  }
32  let data = Buffer.alloc(len, 'x');
33  fs.writeFileSync(filename, data);
34  data = null;
35
36  const zipData = Buffer.alloc(1024, 'a');
37
38  let reads = 0;
39  let zips = 0;
40  let benchEnded = false;
41  bench.start();
42  setTimeout(() => {
43    const totalOps = reads + zips;
44    benchEnded = true;
45    bench.end(totalOps);
46    try {
47      fs.unlinkSync(filename);
48    } catch {
49      // Continue regardless of error.
50    }
51  }, duration * 1000);
52
53  function read() {
54    fs.readFile(filename, encoding, afterRead);
55  }
56
57  function afterRead(er, data) {
58    if (er) {
59      if (er.code === 'ENOENT') {
60        // Only OK if unlinked by the timer from main.
61        assert.ok(benchEnded);
62        return;
63      }
64      throw er;
65    }
66
67    if (data.length !== len)
68      throw new Error('wrong number of bytes returned');
69
70    reads++;
71    if (!benchEnded)
72      read();
73  }
74
75  function zip() {
76    zlib.deflate(zipData, afterZip);
77  }
78
79  function afterZip(er, data) {
80    if (er)
81      throw er;
82
83    zips++;
84    if (!benchEnded)
85      zip();
86  }
87
88  // Start reads
89  while (concurrent-- > 0) read();
90
91  // Start a competing zip
92  zip();
93}
94