summaryrefslogtreecommitdiff
path: root/benchmark/_test-double-benchmarker.js
blob: b9379b907ffa07957c8c0a57f6d597f5c19cec0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';

const myModule = process.argv[2];
if (!['http', 'http2'].includes(myModule)) {
  throw new Error(`Invalid module for benchmark test double: ${myModule}`);
}

const http = require(myModule);

const duration = process.env.duration || 0;
const url = process.env.test_url;

const start = process.hrtime();
let throughput = 0;

function request(res, client) {
  res.resume();
  res.on('error', () => {});
  res.on('end', () => {
    throughput++;
    const diff = process.hrtime(start);
    if (duration > 0 && diff[0] < duration) {
      run();
    } else {
      console.log(JSON.stringify({ throughput }));
      if (client) {
        client.destroy();
      }
    }
  });
}

function run() {
  if (http.get) { // HTTP
    http.get(url, request);
  } else {        // HTTP/2
    const client = http.connect(url);
    client.on('error', (e) => { throw e; });
    request(client.request(), client);
  }
}

run();