-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathheap-profiler-realistic.js
More file actions
150 lines (135 loc) Β· 4.18 KB
/
heap-profiler-realistic.js
File metadata and controls
150 lines (135 loc) Β· 4.18 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
// Benchmark: realistic app-server + DB-server heap profiler overhead.
//
// Architecture: wrk β [App Server :PORT] β [DB Server :PORT+1]
//
// The app server fetches JSON rows from the DB server, parses,
// sums two columns over all rows, and returns the result. This exercises:
// - http.get (async I/O + Buffer allocation for response body)
// - JSON.parse of realistic DB response (V8 heap allocation)
// - Two iteration passes over rows (intermediate values)
// - ALS label propagation across async I/O boundary
//
// Run with compare.js for statistical significance:
// node benchmark/compare.js --old ./out/Release/node --new ./out/Release/node \
// --runs 30 --filter heap-profiler-realistic --set rows=1000 -- http
const common = require('../common.js');
const { PORT } = require('../_http-benchmarkers.js');
const v8 = require('v8');
const http = require('http');
const DB_PORT = PORT + 1;
const bench = common.createBenchmark(main, {
mode: ['none', 'sampling', 'sampling-with-labels'],
rows: [100, 1000],
c: [50],
duration: 10,
});
// --- DB Server: pre-built JSON responses keyed by row count ---
function buildDBResponse(n) {
const categories = ['electronics', 'clothing', 'food', 'books', 'tools'];
const rows = [];
for (let i = 0; i < n; i++) {
rows.push({
id: i,
amount: Math.round(Math.random() * 10000) / 100,
quantity: Math.floor(Math.random() * 500),
name: `user-${String(i).padStart(6, '0')}`,
email: `user${i}@example.com`,
category: categories[i % categories.length],
});
}
const body = JSON.stringify({ rows, total: n });
return { body, len: Buffer.byteLength(body) };
}
// --- App Server helpers ---
function fetchFromDB(rows) {
return new Promise((resolve, reject) => {
const req = http.get(
`http://127.0.0.1:${DB_PORT}/?rows=${rows}`,
(res) => {
const chunks = [];
res.on('data', (chunk) => chunks.push(chunk));
res.on('end', () => {
try {
resolve(JSON.parse(Buffer.concat(chunks).toString()));
} catch (e) {
reject(e);
}
});
},
);
req.on('error', reject);
});
}
function processRows(data) {
const { rows } = data;
// Two passes β simulates light business logic (column aggregation).
let totalAmount = 0;
for (let i = 0; i < rows.length; i++) {
totalAmount += rows[i].amount;
}
let totalQuantity = 0;
for (let i = 0; i < rows.length; i++) {
totalQuantity += rows[i].quantity;
}
return {
totalAmount: Math.round(totalAmount * 100) / 100,
totalQuantity,
count: rows.length,
};
}
function main({ mode, rows, c, duration }) {
// Pre-build DB responses.
const dbResponses = {};
for (const n of [100, 1000]) {
dbResponses[n] = buildDBResponse(n);
}
// Start DB server.
const dbServer = http.createServer((req, res) => {
const url = new URL(req.url, `http://127.0.0.1:${DB_PORT}`);
const n = parseInt(url.searchParams.get('rows') || '1000', 10);
const resp = dbResponses[n] || dbResponses[1000];
res.writeHead(200, {
'Content-Type': 'application/json',
'Content-Length': resp.len,
});
res.end(resp.body);
});
dbServer.listen(DB_PORT, () => {
const interval = 512 * 1024;
if (mode !== 'none') {
v8.startSamplingHeapProfiler(interval);
}
// Start app server.
const appServer = http.createServer((req, res) => {
const handler = async () => {
const data = await fetchFromDB(rows);
const result = processRows(data);
const body = JSON.stringify(result);
res.writeHead(200, {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
});
res.end(body);
};
if (mode === 'sampling-with-labels') {
v8.withHeapProfileLabels({ route: req.url }, handler);
} else {
handler();
}
});
appServer.listen(PORT, () => {
bench.http({
path: '/api/data',
connections: c,
duration,
}, () => {
if (mode !== 'none') {
v8.stopSamplingHeapProfiler();
}
appServer.close();
dbServer.close();
});
});
});
}