• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const http = require('http');
6
7const { PerformanceObserver } = require('perf_hooks');
8
9const obs = new PerformanceObserver(common.mustCall((items) => {
10  const entry = items.getEntries()[0];
11  assert.strictEqual(entry.entryType, 'http');
12  assert.strictEqual(typeof entry.startTime, 'number');
13  assert.strictEqual(typeof entry.duration, 'number');
14}, 2));
15
16obs.observe({ entryTypes: ['http'] });
17
18const expected = 'Post Body For Test';
19const makeRequest = (options) => {
20  return new Promise((resolve, reject) => {
21    http.request(options, common.mustCall((res) => {
22      resolve();
23    })).on('error', reject).end(options.data);
24  });
25};
26
27const server = http.Server(common.mustCall((req, res) => {
28  let result = '';
29
30  req.setEncoding('utf8');
31  req.on('data', function(chunk) {
32    result += chunk;
33  });
34
35  req.on('end', common.mustCall(function() {
36    assert.strictEqual(result, expected);
37    res.writeHead(200);
38    res.end('hello world\n');
39  }));
40}, 2));
41
42server.listen(0, common.mustCall(async () => {
43  await Promise.all([
44    makeRequest({
45      port: server.address().port,
46      path: '/',
47      method: 'POST',
48      data: expected
49    }),
50    makeRequest({
51      port: server.address().port,
52      path: '/',
53      method: 'POST',
54      data: expected
55    }),
56  ]);
57  server.close();
58}));
59