-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathheap-profiler-labels.js
More file actions
88 lines (76 loc) Β· 2.5 KB
/
heap-profiler-labels.js
File metadata and controls
88 lines (76 loc) Β· 2.5 KB
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
'use strict';
// Benchmark: HTTP server throughput impact of heap profiler with labels.
//
// Measures requests/sec across three modes:
// - none: no profiler (baseline)
// - sampling: profiler active, no labels
// - sampling-with-labels: profiler active with labels via withHeapProfileLabels
//
// Workload per request: ~100KB V8 heap (JSON parse/stringify) + ~50KB Buffer
// to exercise both HeapProfileLabelsCallback and ProfilingArrayBufferAllocator.
//
// Run with compare.js:
// node benchmark/compare.js --old ./out/Release/node --new ./out/Release/node \
// --runs 10 --filter heap-profiler-labels --set c=50 -- http
const common = require('../common.js');
const { PORT } = require('../_http-benchmarkers.js');
const v8 = require('v8');
const bench = common.createBenchmark(main, {
mode: ['none', 'sampling', 'sampling-with-labels'],
c: [50],
duration: 10,
});
// Build a ~100KB realistic JSON payload template (API response shape).
const items = [];
for (let i = 0; i < 200; i++) {
items.push({
id: i,
name: `user-${i}`,
email: `user${i}@example.com`,
role: 'admin',
metadata: { created: '2024-01-01', tags: ['a', 'b', 'c'] },
});
}
const payloadTemplate = JSON.stringify({ data: items, total: 200 });
function main({ mode, c, duration }) {
const http = require('http');
const interval = 512 * 1024; // 512KB β V8 default, production-realistic.
if (mode !== 'none') {
v8.startSamplingHeapProfiler(interval);
}
const server = http.createServer((req, res) => {
const handler = () => {
// Realistic mixed workload:
// 1. ~100KB V8 heap: JSON parse + stringify (simulates API response building)
const parsed = JSON.parse(payloadTemplate);
parsed.requestId = Math.random();
const body = JSON.stringify(parsed);
// 2. ~50KB Buffer (simulates response buffering / crypto / compression)
const buf = Buffer.alloc(50 * 1024, 0x42);
// Keep buf reference alive until response is sent.
res.writeHead(200, {
'Content-Type': 'application/json',
'Content-Length': body.length,
'X-Buf-Check': buf[0],
});
res.end(body);
};
if (mode === 'sampling-with-labels') {
v8.withHeapProfileLabels({ route: req.url }, handler);
} else {
handler();
}
});
server.listen(PORT, () => {
bench.http({
path: '/api/bench',
connections: c,
duration,
}, () => {
if (mode !== 'none') {
v8.stopSamplingHeapProfiler();
}
server.close();
});
});
}