• 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  type: ['asc', 'utf', 'buf'],
15  len: [64 * 1024, 128 * 1024, 256 * 1024, 1024 * 1024],
16  c: [100],
17  method: ['write', 'end'],
18  duration: 5,
19});
20
21function main({ len, type, method, c, duration }) {
22  const http = require('http');
23  let chunk;
24  switch (type) {
25    case 'buf':
26      chunk = Buffer.alloc(len, 'x');
27      break;
28    case 'utf':
29      chunk = 'ü'.repeat(len / 2);
30      break;
31    case 'asc':
32      chunk = 'a'.repeat(len);
33      break;
34  }
35
36  function write(res) {
37    res.write(chunk);
38    res.end();
39  }
40
41  function end(res) {
42    res.end(chunk);
43  }
44
45  const fn = method === 'write' ? write : end;
46
47  const server = http.createServer((req, res) => {
48    fn(res);
49  });
50
51  server.listen(0, () => {
52    bench.http({
53      connections: c,
54      duration,
55      port: server.address().port,
56    }, () => {
57      server.close();
58    });
59  });
60}
61