This repository has been archived by the owner on Jul 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
get-cpu-profile.js
43 lines (35 loc) · 1.51 KB
/
get-cpu-profile.js
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
const fs = require('fs');
const cdp = require('chrome-remote-interface');
const chromelauncher = require('chrome-launcher');
const sleep = n => new Promise(resolve => setTimeout(resolve, n));
const url = 'http://localhost:8080/demo/perf-test.html';
(async function() {
const chrome = await chromelauncher.launch({port: 9222});
const client = await cdp();
const {Profiler, Page, Runtime} = client;
// enable domains to get events.
await Page.enable();
await Profiler.enable();
// Set JS profiler sampling resolution to 100 microsecond (default is 1000)
await Profiler.setSamplingInterval({interval: 100});
await Page.navigate({url});
await client.on('Page.loadEventFired', async _ => {
// on load we'll start profiling, kick off the test, and finish
await Profiler.start();
await Runtime.evaluate({expression: 'startTest();'});
await sleep(600);
const data = await Profiler.stop();
saveProfile(data);
});
async function saveProfile(data) {
// data.profile described here: https://chromedevtools.github.io/devtools-protocol/tot/Profiler/#type-Profile
// Process the data however you wish… or,
// Use the JSON file, open Chrome DevTools, Menu, More Tools, JavaScript Profiler, `load`, view in the UI
const filename = `profile-${Date.now()}.cpuprofile`;
const string = JSON.stringify(data.profile, null, 2);
fs.writeFileSync(filename, string);
console.log('Done! Profile data saved to:', filename);
await client.close();
await chrome.kill();
}
})();