-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathheap_resource_script.in.js
More file actions
65 lines (48 loc) · 1.51 KB
/
heap_resource_script.in.js
File metadata and controls
65 lines (48 loc) · 1.51 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
'use strict';
/* eslint-disable no-undef */
const driverPath = DRIVER_SOURCE_PATH;
const func = FUNCTION_STRING;
const name = SCRIPT_NAME_STRING;
const uri = URI_STRING;
const iterations = ITERATIONS_STRING;
const log = LOG_FN;
const { MongoClient } = require(driverPath);
const process = require('node:process');
const v8 = require('node:v8');
const util = require('node:util');
const timers = require('node:timers');
const now = performance.now.bind(performance);
const sleep = util.promisify(timers.setTimeout);
const run = func;
const MB = (2 ** 10) ** 2;
async function main() {
log('starting execution' + '\n');
const startingMemoryUsed = process.memoryUsage().heapUsed / MB;
process.send({ startingMemoryUsed });
log('sent first message' + '\n');
for (let iteration = 0; iteration < iterations; iteration++) {
await run({ MongoClient, uri, iteration });
iteration % 20 === 0 && log(`iteration ${iteration} complete\n`);
global.gc();
}
log('script executed' + '\n');
global.gc();
// Sleep b/c maybe gc will run
await sleep(100);
global.gc();
const endingMemoryUsed = process.memoryUsage().heapUsed / MB;
log('sending second message' + '\n');
process.send({ endingMemoryUsed });
log('second message sent.' + '\n');
const start = now();
v8.writeHeapSnapshot(`${name}.heapsnapshot.json`);
const end = now();
log(`heap snapshot written in ${end - start}ms. script exiting` + '\n');
}
main()
.then(() => {
process.exit(0);
})
.catch(() => {
process.exit(1);
});