• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// When calling .end(buffer) right away, this triggers a "hot path"
2// optimization in http.js, to avoid an extra write call.
3//
4// However, the overhead of copying a large buffer is higher than
5// the overhead of an extra write() call, so the hot path was not
6// always as hot as it could be.
7//
8// Verify that our assumptions are valid.
9'use strict';
10
11const common = require('../common.js');
12
13const bench = common.createBenchmark(main, {
14  n: [1, 4, 8, 16],
15  len: [1, 64, 256],
16  c: [100],
17  duration: 5,
18});
19
20function main({ len, n, c, duration }) {
21  const http = require('http');
22  const chunk = Buffer.alloc(len, '8');
23
24  const server = http.createServer((req, res) => {
25    function send(left) {
26      if (left === 0) return res.end();
27      res.write(chunk);
28      setTimeout(() => {
29        send(left - 1);
30      }, 0);
31    }
32    send(n);
33  });
34
35  server.listen(0, () => {
36    bench.http({
37      connections: c,
38      duration,
39      port: server.address().port,
40    }, () => {
41      server.close();
42    });
43  });
44}
45