├── .gitignore ├── demo ├── layout-thrash.html ├── mdn-fling.json └── perf-test.html ├── get-cpu-profile.js ├── get-timeline-trace.js ├── log-trace-metrics.js ├── package.json ├── readme.md ├── test-for-layout-thrashing.js └── util └── browser-perf-summary.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /demo/layout-thrash.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
Start a server on port 8000 in from the project root
5 | 6 |Install the required node module:
7 | 8 |npm install chrome-remote-interface
9 |
10 |
11 | Run Chrome with an open debugging port:
12 | 13 |# linux
14 | google-chrome --remote-debugging-port=9222
15 |
16 | # mac
17 | /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
18 |
19 |
20 | Run the script from this folder:
21 | 22 |node test-layout-thrash
23 |
24 | easy peasy
25 | 26 | 27 | 31 | -------------------------------------------------------------------------------- /demo/perf-test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |Follow the readme directions
4 | 5 | 6 | -------------------------------------------------------------------------------- /get-cpu-profile.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const cdp = require('chrome-remote-interface'); 3 | const chromelauncher = require('chrome-launcher'); 4 | 5 | const sleep = n => new Promise(resolve => setTimeout(resolve, n)); 6 | 7 | const url = 'http://localhost:8080/demo/perf-test.html'; 8 | 9 | (async function() { 10 | const chrome = await chromelauncher.launch({port: 9222}); 11 | const client = await cdp(); 12 | 13 | const {Profiler, Page, Runtime} = client; 14 | // enable domains to get events. 15 | await Page.enable(); 16 | await Profiler.enable(); 17 | 18 | // Set JS profiler sampling resolution to 100 microsecond (default is 1000) 19 | await Profiler.setSamplingInterval({interval: 100}); 20 | 21 | await Page.navigate({url}); 22 | await client.on('Page.loadEventFired', async _ => { 23 | // on load we'll start profiling, kick off the test, and finish 24 | await Profiler.start(); 25 | await Runtime.evaluate({expression: 'startTest();'}); 26 | await sleep(600); 27 | const data = await Profiler.stop(); 28 | saveProfile(data); 29 | }); 30 | 31 | async function saveProfile(data) { 32 | // data.profile described here: https://chromedevtools.github.io/devtools-protocol/tot/Profiler/#type-Profile 33 | // Process the data however you wish… or, 34 | // Use the JSON file, open Chrome DevTools, Menu, More Tools, JavaScript Profiler, `load`, view in the UI 35 | const filename = `profile-${Date.now()}.cpuprofile`; 36 | const string = JSON.stringify(data.profile, null, 2); 37 | fs.writeFileSync(filename, string); 38 | console.log('Done! Profile data saved to:', filename); 39 | 40 | await client.close(); 41 | await chrome.kill(); 42 | } 43 | })(); 44 | -------------------------------------------------------------------------------- /get-timeline-trace.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var Chrome = require('chrome-remote-interface'); 3 | var summary = require('./util/browser-perf-summary') 4 | 5 | var TRACE_CATEGORIES = ["-*", "devtools.timeline", "disabled-by-default-devtools.timeline", "disabled-by-default-devtools.timeline.frame", "toplevel", "blink.console", "disabled-by-default-devtools.timeline.stack", "disabled-by-default-devtools.screenshot", "disabled-by-default-v8.cpu_profile", "disabled-by-default-v8.cpu_profiler", "disabled-by-default-v8.cpu_profiler.hires"]; 6 | 7 | var rawEvents = []; 8 | 9 | Chrome(function (chrome) { 10 | with (chrome) { 11 | Page.enable(); 12 | Tracing.start({ 13 | "categories": TRACE_CATEGORIES.join(','), 14 | "options": "sampling-frequency=10000" // 1000 is default and too slow. 15 | }); 16 | 17 | Page.navigate({'url': 'http://paulirish.com'}) 18 | Page.loadEventFired(function () { 19 | Tracing.end() 20 | }); 21 | 22 | Tracing.tracingComplete(function () { 23 | var file = 'profile-' + Date.now() + '.devtools.trace'; 24 | fs.writeFileSync(file, JSON.stringify(rawEvents, null, 2)); 25 | console.log('Trace file: ' + file); 26 | console.log('You can open the trace file in DevTools Timeline panel. (Turn on experiment: Timeline tracing based JS profiler)\n') 27 | 28 | summary.report(file); // superfluous 29 | 30 | chrome.close(); 31 | }); 32 | 33 | Tracing.dataCollected(function(data){ 34 | var events = data.value; 35 | rawEvents = rawEvents.concat(events); 36 | 37 | // this is just extra but not really important 38 | summary.onData(events) 39 | }); 40 | 41 | } 42 | }).on('error', function (e) { 43 | console.error('Cannot connect to Chrome', e); 44 | }); 45 | -------------------------------------------------------------------------------- /log-trace-metrics.js: -------------------------------------------------------------------------------- 1 | const filename = 'demo/mdn-fling.json' 2 | 3 | var fs = require('fs') 4 | var traceToTimelineModel = require('devtools-timeline-model') 5 | 6 | var events = fs.readFileSync(filename, 'utf8') 7 | var model = traceToTimelineModel(events) 8 | 9 | console.log('Timeline model events:', model.timelineModel.mainThreadEvents().length) 10 | console.log('IR model interactions', model.irModel.interactionRecords().length) 11 | console.log('Frame model frames', model.frameModel.frames().length) 12 | 13 | 14 | // console.log('Timeline model:', model.timelineModel) 15 | // console.log('IR model', model.irModel) 16 | // console.log('Frame model', model.frameModel) 17 | 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "automated-chrome-profiling", 3 | "version": "1.0.0", 4 | "description": "A few scripts to get started", 5 | "scripts": { 6 | "test": "echo \"Error: no test specified\" && exit 1", 7 | "start": "httpster -p 8080" 8 | }, 9 | "author": "", 10 | "license": "Apache-2.0", 11 | "repository": "https://github.com/paulirish/automated-chrome-profiling", 12 | "dependencies": { 13 | "chrome-launcher": "^0.3.2", 14 | "chrome-remote-interface": "^0.24.2", 15 | "devtools-timeline-model": "^1.0", 16 | "httpster": "^1.0.3" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | Let's say you want to evaluate the performance of some clientside JavaScript and want to automate it. Let's kick off our measurement in Node.js and collect the performance metrics from Chrome. Oh yeah. 2 | 3 | We can use the [Chrome debugging protocol](https://developer.chrome.com/devtools/docs/debugger-protocol) and go directly to [how Chrome's JS sampling profiler interacts with V8](https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/devtools/protocol.json&q=file:protocol.json%20%22Profiler%22,&sq=package:chromium&type=cs). So much power here, so we'll use [chrome-remote-interface](https://github.com/cyrus-and/chrome-remote-interface) as a nice client in front of the protocol: 4 | 5 | 6 | **Step 1: Clone this repo and serve it** 7 | 8 | ```sh 9 | git clone https://github.com/paulirish/automated-chrome-profiling 10 | cd automated-chrome-profiling 11 | npm install # get the dependencies 12 | npm start # serves the folder at http://localhost:8080/ (port hardcoded) 13 | ``` 14 | 15 | **Step 2: Run Chrome with an open debugging port:** 16 | 17 | ```sh 18 | # linux 19 | google-chrome --remote-debugging-port=9222 --user-data-dir=$TMPDIR/chrome-profiling --no-default-browser-check 20 | 21 | # mac 22 | /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 --user-data-dir=$TMPDIR/chrome-profiling --no-default-browser-check 23 | ``` 24 | Navigate off the start page to example.com or something. 25 | 26 | **Step 3: Run the CPU profiling demo app** 27 | 28 | ```sh 29 | node get-cpu-profile.js 30 | ``` 31 | 32 | #### CPU Profiling 33 | Read through [`get-cpu-profile.js`](https://github.com/paulirish/automated-chrome-profiling/blob/master/get-cpu-profile.js). Here's what it does: 34 | 35 | * It navigates your open tab to `http://localhost:8080/perf-test.html` 36 | * Starts profiling 37 | * run's the page's `startTest();` 38 | * Stop profiling and retrieve the profiling result 39 | * Save it to disk. We can then load the data into Chrome DevTools to view 40 | 41 |