Skip to content

Commit bbc2113

Browse files
asdf
1 parent 8c86e30 commit bbc2113

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

test/integration/node-specific/resource_tracking_script_builder.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { on, once } from 'node:events';
33
import { openSync } from 'node:fs';
44
import { readFile, unlink, writeFile } from 'node:fs/promises';
55
import * as path from 'node:path';
6+
import { inspect } from 'node:util';
67

78
import { AssertionError, expect } from 'chai';
89
import type * as timers from 'timers';
@@ -50,6 +51,7 @@ export async function testScriptFactory(
5051
uri: string,
5152
resourceScriptPath: string,
5253
func: ResourceTestFunction,
54+
logFn: string,
5355
iterations?: number
5456
) {
5557
let resourceScript = await readFile(resourceScriptPath, { encoding: 'utf8' });
@@ -59,6 +61,7 @@ export async function testScriptFactory(
5961
resourceScript = resourceScript.replace('SCRIPT_NAME_STRING', JSON.stringify(name));
6062
resourceScript = resourceScript.replace('URI_STRING', JSON.stringify(uri));
6163
resourceScript = resourceScript.replace('ITERATIONS_STRING', `${iterations}`);
64+
resourceScript = resourceScript.replace('LOG_FN', `${logFn.toString()}`);
6265

6366
return resourceScript;
6467
}
@@ -92,6 +95,9 @@ export async function runScriptAndReturnHeapInfo(
9295
func: HeapResourceTestFunction,
9396
{ iterations = 100 } = {}
9497
) {
98+
const log = (...args) => process.stdout.write(`${args}\n`);
99+
100+
log('starting');
95101
const scriptName = `${name}.cjs`;
96102
const heapsnapshotFile = `${name}.heapsnapshot.json`;
97103

@@ -100,36 +106,55 @@ export async function runScriptAndReturnHeapInfo(
100106
config.url(),
101107
HEAP_RESOURCE_SCRIPT_PATH,
102108
func,
109+
`(...args) => process.stdout.write('(subprocess): ' + args)`,
103110
iterations
104111
);
105112
await writeFile(scriptName, scriptContent, { encoding: 'utf8' });
106113

107114
const processDiedController = new AbortController();
108-
const script = fork(scriptName, { execArgv: ['--expose-gc'] });
115+
const script = fork(scriptName, { execArgv: ['--expose-gc'], stdio: 'inherit' });
109116
// Interrupt our awaiting of messages if the process crashed
110117
script.once('close', exitCode => {
111118
if (exitCode !== 0) {
119+
log('process exited with non-zero: ', exitCode);
112120
processDiedController.abort(new Error(`process exited with: ${exitCode}`));
113121
}
114122
});
115123

124+
script.once('error', error => {
125+
log(`processed errored: ${error}`);
126+
processDiedController.abort(new Error(`process errored: `, { cause: error }));
127+
});
128+
129+
script.once('spawn', () => log('script spawned successfully.'));
130+
116131
const messages = on(script, 'message', { signal: processDiedController.signal });
117132
const willClose = once(script, 'close');
118133

134+
log('fetching messages 1...');
119135
const starting = await messages.next();
136+
log('fetching messages 2: ', inspect(starting, { depth: Infinity }));
137+
120138
const ending = await messages.next();
121139

140+
log('fetching messages 3: ', inspect(ending, { depth: Infinity }));
141+
122142
const startingMemoryUsed = starting.value[0].startingMemoryUsed;
123143
const endingMemoryUsed = ending.value[0].endingMemoryUsed;
124144

125145
// make sure the process ended
126146
const [exitCode] = await willClose;
147+
148+
log('child process closed.');
149+
127150
expect(exitCode, 'process should have exited with zero').to.equal(0);
128151

129152
const heap = await readFile(heapsnapshotFile, { encoding: 'utf8' }).then(c =>
130153
parseSnapshot(JSON.parse(c))
131154
);
132155

156+
log('done.');
157+
133158
// If any of the above throws we won't reach these unlinks that clean up the created files.
134159
// This is intentional so that when debugging the file will still be present to check it for errors
135160
await unlink(scriptName);

test/tools/fixtures/heap_resource_script.in.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,53 @@ const func = FUNCTION_STRING;
77
const name = SCRIPT_NAME_STRING;
88
const uri = URI_STRING;
99
const iterations = ITERATIONS_STRING;
10+
const log = LOG_FN;
1011

1112
const { MongoClient } = require(driverPath);
1213
const process = require('node:process');
1314
const v8 = require('node:v8');
1415
const util = require('node:util');
1516
const timers = require('node:timers');
1617

18+
const now = performance.now.bind(performance);
1719
const sleep = util.promisify(timers.setTimeout);
1820

1921
const run = func;
2022

2123
const MB = (2 ** 10) ** 2;
2224

2325
async function main() {
26+
log('starting execution' + '\n');
2427
const startingMemoryUsed = process.memoryUsage().heapUsed / MB;
2528
process.send({ startingMemoryUsed });
2629

30+
log('sent first message' + '\n');
31+
2732
for (let iteration = 0; iteration < iterations; iteration++) {
2833
await run({ MongoClient, uri, iteration });
34+
iteration % 20 === 0 && log(`iteration ${iteration} complete\n`);
2935
global.gc();
3036
}
3137

38+
log('script executed' + '\n');
39+
3240
global.gc();
3341
// Sleep b/c maybe gc will run
3442
await sleep(100);
3543
global.gc();
3644

3745
const endingMemoryUsed = process.memoryUsage().heapUsed / MB;
46+
47+
log('sending second message' + '\n');
48+
3849
process.send({ endingMemoryUsed });
50+
log('second message sent.' + '\n');
51+
52+
const start = now();
3953
v8.writeHeapSnapshot(`${name}.heapsnapshot.json`);
54+
const end = now();
55+
56+
log(`heap snapshot written in ${end - start}ms. script exiting` + '\n');
4057
}
4158

4259
main()

0 commit comments

Comments
 (0)