-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathheap-profiler-labels.js
More file actions
59 lines (51 loc) Β· 1.69 KB
/
heap-profiler-labels.js
File metadata and controls
59 lines (51 loc) Β· 1.69 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
'use strict';
// Benchmark: overhead of V8 sampling heap profiler with and without labels.
//
// Measures per-allocation cost across three modes:
// - none: no profiler running (baseline)
// - sampling: profiler active, no labels callback
// - sampling-with-labels: profiler active with labels via withHeapProfileLabels
//
// Run standalone:
// node benchmark/v8/heap-profiler-labels.js
//
// Run with compare.js for statistical analysis:
// node benchmark/compare.js --old ./node-baseline --new ./node-with-labels \
// --filter heap-profiler-labels
const common = require('../common.js');
const v8 = require('v8');
const bench = common.createBenchmark(main, {
mode: ['none', 'sampling', 'sampling-with-labels'],
n: [1e6],
});
function main({ mode, n }) {
const interval = 512 * 1024; // 512KB β V8 default, production-realistic.
if (mode === 'sampling') {
v8.startSamplingHeapProfiler(interval);
} else if (mode === 'sampling-with-labels') {
v8.startSamplingHeapProfiler(interval);
}
if (mode === 'sampling-with-labels') {
v8.withHeapProfileLabels({ route: '/bench' }, () => {
runWorkload(n);
});
} else {
runWorkload(n);
}
if (mode !== 'none') {
v8.stopSamplingHeapProfiler();
}
}
function runWorkload(n) {
const arr = [];
bench.start();
for (let i = 0; i < n; i++) {
// Allocate objects with string properties β representative of JSON API
// workloads. Each object is ~100-200 bytes on the V8 heap.
arr.push({ id: i, name: `item-${i}`, value: Math.random() });
// Prevent unbounded growth β keep last 1000 to maintain GC pressure
// without running out of memory.
if (arr.length > 1000) arr.shift();
}
bench.end(n);
}