summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-perf_hooks.js
blob: de6ed0295a1152040a1351f415c5156a02d605f9 (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
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');

const { PerformanceObserver } = require('perf_hooks');
const entries = [];
const obs = new PerformanceObserver(common.mustCallAtLeast((items) => {
  entries.push(...items.getEntries());
}));

obs.observe({ type: 'http' });

const expected = 'Post Body For Test';
const makeRequest = (options) => {
  return new Promise((resolve, reject) => {
    http.request(options, common.mustCall((res) => {
      resolve();
    })).on('error', reject).end(options.data);
  });
};

const server = http.Server(common.mustCall((req, res) => {
  let result = '';

  req.setEncoding('utf8');
  req.on('data', function(chunk) {
    result += chunk;
  });

  req.on('end', common.mustCall(function() {
    assert.strictEqual(result, expected);
    res.writeHead(200);
    res.end('hello world\n');
  }));
}, 2));

server.listen(0, common.mustCall(async () => {
  await Promise.all([
    makeRequest({
      port: server.address().port,
      path: '/',
      method: 'POST',
      data: expected
    }),
    makeRequest({
      port: server.address().port,
      path: '/',
      method: 'POST',
      data: expected
    }),
  ]);
  server.close();
}));

process.on('exit', () => {
  let numberOfHttpClients = 0;
  let numberOfHttpRequests = 0;
  entries.forEach((entry) => {
    assert.strictEqual(entry.entryType, 'http');
    assert.strictEqual(typeof entry.startTime, 'number');
    assert.strictEqual(typeof entry.duration, 'number');
    if (entry.name === 'HttpClient') {
      numberOfHttpClients++;
    } else if (entry.name === 'HttpRequest') {
      numberOfHttpRequests++;
    }
    assert.strictEqual(typeof entry.detail.req.method, 'string');
    assert.strictEqual(typeof entry.detail.req.url, 'string');
    assert.strictEqual(typeof entry.detail.req.headers, 'object');
    assert.strictEqual(typeof entry.detail.res.statusCode, 'number');
    assert.strictEqual(typeof entry.detail.res.statusMessage, 'string');
    assert.strictEqual(typeof entry.detail.res.headers, 'object');
  });
  assert.strictEqual(numberOfHttpClients, 2);
  assert.strictEqual(numberOfHttpRequests, 2);
});