-
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
75 lines (57 loc) · 1.74 KB
/
heap_resource_script.in.js
File metadata and controls
75 lines (57 loc) · 1.74 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
'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 { inspect } = require('util');
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;
const log = (...args) => {
const payload =
args
.map(item =>
typeof item === 'string' ? item : inspect(item, { depth: Infinity, breakLength: Infinity })
)
.join(', ') + '\n';
process.stdout.write('(subprocess): ' + payload);
};
async function main() {
log('starting execution');
const startingMemoryUsed = process.memoryUsage().heapUsed / MB;
process.send({ startingMemoryUsed });
log('sent first message');
for (let iteration = 0; iteration < iterations; iteration++) {
await run({ MongoClient, uri, iteration });
iteration % 20 === 0 && log(`iteration ${iteration} complete`);
global.gc();
}
log('script executed');
global.gc();
// Sleep b/c maybe gc will run
await sleep(100);
global.gc();
const endingMemoryUsed = process.memoryUsage().heapUsed / MB;
log('sending second message');
process.send({ endingMemoryUsed });
log('second message sent.');
const start = now();
v8.writeHeapSnapshot(`${name}.heapsnapshot.json`);
const end = now();
log(`heap snapshot written in ${end - start}ms. script exiting`);
}
main()
.then(() => {
process.exit(0);
})
.catch(() => {
process.exit(1);
});