-
Notifications
You must be signed in to change notification settings - Fork 30.4k
/
Copy pathtest-http2-perf_hooks.js
117 lines (100 loc) Β· 4.03 KB
/
test-http2-perf_hooks.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Flags: --no-warnings
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const h2 = require('http2');
const { PerformanceObserver } = require('perf_hooks');
const obs = new PerformanceObserver(common.mustCallAtLeast((items) => {
const entry = items.getEntries()[0];
assert.strictEqual(entry.entryType, 'http2');
assert.strictEqual(typeof entry.startTime, 'number');
assert.strictEqual(typeof entry.duration, 'number');
switch (entry.name) {
case 'Http2Session':
assert.strictEqual(typeof entry.pingRTT, 'number');
assert.strictEqual(typeof entry.streamAverageDuration, 'number');
assert.strictEqual(typeof entry.streamCount, 'number');
assert.strictEqual(typeof entry.framesReceived, 'number');
assert.strictEqual(typeof entry.framesSent, 'number');
assert.strictEqual(typeof entry.bytesWritten, 'number');
assert.strictEqual(typeof entry.bytesRead, 'number');
assert.strictEqual(typeof entry.maxConcurrentStreams, 'number');
assert.strictEqual(typeof entry.detail.pingRTT, 'number');
assert.strictEqual(typeof entry.detail.streamAverageDuration, 'number');
assert.strictEqual(typeof entry.detail.streamCount, 'number');
assert.strictEqual(typeof entry.detail.framesReceived, 'number');
assert.strictEqual(typeof entry.detail.framesSent, 'number');
assert.strictEqual(typeof entry.detail.bytesWritten, 'number');
assert.strictEqual(typeof entry.detail.bytesRead, 'number');
assert.strictEqual(typeof entry.detail.maxConcurrentStreams, 'number');
switch (entry.type) {
case 'server':
assert.strictEqual(entry.detail.streamCount, 1);
assert(entry.detail.framesReceived >= 3);
break;
case 'client':
assert.strictEqual(entry.detail.streamCount, 1);
assert.strictEqual(entry.detail.framesReceived, 7);
break;
default:
assert.fail('invalid Http2Session type');
}
break;
case 'Http2Stream':
assert.strictEqual(typeof entry.timeToFirstByte, 'number');
assert.strictEqual(typeof entry.timeToFirstByteSent, 'number');
assert.strictEqual(typeof entry.timeToFirstHeader, 'number');
assert.strictEqual(typeof entry.bytesWritten, 'number');
assert.strictEqual(typeof entry.bytesRead, 'number');
assert.strictEqual(typeof entry.detail.timeToFirstByte, 'number');
assert.strictEqual(typeof entry.detail.timeToFirstByteSent, 'number');
assert.strictEqual(typeof entry.detail.timeToFirstHeader, 'number');
assert.strictEqual(typeof entry.detail.bytesWritten, 'number');
assert.strictEqual(typeof entry.detail.bytesRead, 'number');
break;
default:
assert.fail('invalid entry name');
}
}));
obs.observe({ type: 'http2' });
const body =
'<html><head></head><body><h1>this is some data</h2></body></html>';
const server = h2.createServer();
// We use the lower-level API here
server.on('stream', common.mustCall(onStream));
function onStream(stream, headers, flags) {
assert.strictEqual(headers[':scheme'], 'http');
assert.ok(headers[':authority']);
assert.strictEqual(headers[':method'], 'GET');
assert.strictEqual(flags, 5);
stream.respond({
'content-type': 'text/html',
':status': 200
});
stream.write(body.slice(0, 20));
stream.end(body.slice(20));
}
server.on('session', common.mustCall((session) => {
session.ping(common.mustCall());
}));
server.listen(0);
server.on('listening', common.mustCall(() => {
const client = h2.connect(`http://localhost:${server.address().port}`);
client.on('connect', common.mustCall(() => {
client.ping(common.mustCall());
}));
const req = client.request();
req.on('response', common.mustCall());
let data = '';
req.setEncoding('utf8');
req.on('data', (d) => data += d);
req.on('end', common.mustCall(() => {
assert.strictEqual(body, data);
}));
req.on('close', common.mustCall(() => {
client.close();
server.close();
}));
}));