|
| 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