summaryrefslogtreecommitdiff
path: root/benchmark/diagnostics_channel/http.js
blob: caf37a05a45c3ba35dc60eea92517e61e2ca81a8 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
const common = require('../common.js');
const dc = require('diagnostics_channel');
const { AsyncLocalStorage } = require('async_hooks');
const http = require('http');

const bench = common.createBenchmark(main, {
  apm: ['none', 'diagnostics_channel', 'patch'],
  type: 'buffer',
  len: 1024,
  chunks: 4,
  connections: [50, 500],
  chunkedEnc: 1,
  duration: 5,
});

function main({ apm, connections, duration, type, len, chunks, chunkedEnc }) {
  const done = { none, patch, diagnostics_channel }[apm]();

  const server = require('../fixtures/simple-http-server.js')
    .listen(common.PORT)
    .on('listening', () => {
      const path = `/${type}/${len}/${chunks}/normal/${chunkedEnc}`;
      bench.http({
        path,
        connections,
        duration,
      }, () => {
        server.close();
        if (done) done();
      });
    });
}

function none() {}

function patch() {
  const als = new AsyncLocalStorage();
  const times = [];

  const { emit } = http.Server.prototype;
  function wrappedEmit(...args) {
    const [name, req, res] = args;
    if (name === 'request') {
      als.enterWith({
        url: req.url,
        start: process.hrtime.bigint(),
      });

      res.on('finish', () => {
        times.push({
          ...als.getStore(),
          statusCode: res.statusCode,
          end: process.hrtime.bigint(),
        });
      });
    }
    return emit.apply(this, args);
  }
  http.Server.prototype.emit = wrappedEmit;

  return () => {
    http.Server.prototype.emit = emit;
  };
}

function diagnostics_channel() {
  const als = new AsyncLocalStorage();
  const times = [];

  const start = dc.channel('http.server.request.start');
  const finish = dc.channel('http.server.response.finish');

  function onStart(req) {
    als.enterWith({
      url: req.url,
      start: process.hrtime.bigint(),
    });
  }

  function onFinish(res) {
    times.push({
      ...als.getStore(),
      statusCode: res.statusCode,
      end: process.hrtime.bigint(),
    });
  }

  start.subscribe(onStart);
  finish.subscribe(onFinish);

  return () => {
    start.unsubscribe(onStart);
    finish.unsubscribe(onFinish);
  };
}