Skip to content

Commit 5ad17de

Browse files
author
Robert Jackson
authored
Add basic memory profiling script to dev/ folder. (#238)
Add basic memory profiling script to `dev/` folder.
2 parents 749a4c7 + 0c1b2c1 commit 5ad17de

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
npm-debug.log
44
tmp*
55
dist/
6+
/snapshots

dev/memory-usage.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
3+
/**
4+
* This script is used for gathering basic memory statistics. It takes a heapsnapshot at the following locations:
5+
*
6+
* - `new FastBoot({ distPath: '...' })` - Saved in `snapshots/0-setup.heapsnapshot`
7+
* - `await fastboot.visit('/')` - Saved in `snapshots/1-first-visit.heapsnapshot`
8+
* - `await fastboot.visit('/')` - Saved in `snapshots/2-second-visit.heapsnapshot`
9+
* - `await fastboot.visit('/')` - Saved in `snapshots/3-third-visit.heapsnapshot`
10+
*
11+
* General usage and evaluation steps:
12+
*
13+
* 1. run the script (e.g. `node dev/memory-usage.js`)
14+
* 2. Navigate Chrome to the `about:blank`
15+
* 3. Open the Chrome DevTools
16+
* 4. Click into the "Memory" tab.
17+
* 5. Click "Load" and load each of the files above (in order)
18+
* 6. Review / evaluate
19+
*/
20+
21+
const inspector = require('inspector');
22+
const path = require('path');
23+
const fs = require('fs');
24+
const session = new inspector.Session();
25+
26+
session.connect();
27+
28+
let file;
29+
// uses whatever the "current" file is
30+
session.on('HeapProfiler.addHeapSnapshotChunk', m => {
31+
fs.writeSync(file, m.params.chunk);
32+
});
33+
34+
function takeHeapSnapshot(path) {
35+
file = fs.openSync(path, 'w');
36+
37+
return new Promise((resolve, reject) => {
38+
session.post('HeapProfiler.takeHeapSnapshot', null, (err, r) => {
39+
fs.closeSync(file);
40+
41+
if (err) {
42+
reject(err);
43+
}
44+
45+
resolve(r);
46+
});
47+
});
48+
}
49+
50+
async function main() {
51+
fs.mkdirSync('snapshots');
52+
53+
const FastBoot = require('../src/index');
54+
55+
const distPath = path.join(__dirname, '../test/fixtures/basic-app');
56+
const fastboot = new FastBoot({
57+
distPath,
58+
});
59+
60+
await takeHeapSnapshot('snapshots/0-setup.heapsnapshot');
61+
62+
let result = await fastboot.visit('/');
63+
await result.html();
64+
65+
await takeHeapSnapshot('snapshots/1-first-visit.heapsnapshot');
66+
67+
result = await fastboot.visit('/');
68+
await result.html();
69+
70+
await takeHeapSnapshot('snapshots/2-second-visit.heapsnapshot');
71+
72+
result = await fastboot.visit('/');
73+
await result.html();
74+
75+
await takeHeapSnapshot('snapshots/3-third-visit.heapsnapshot');
76+
}
77+
78+
main().finally(() => {
79+
session.disconnect();
80+
});

0 commit comments

Comments
 (0)