Skip to content

Instantly share code, notes, and snippets.

@patrickhulce
Created July 28, 2017 00:44
Show Gist options
  • Save patrickhulce/c260c460cf0b3344c04e72a3d313e79f to your computer and use it in GitHub Desktop.
Save patrickhulce/c260c460cf0b3344c04e72a3d313e79f to your computer and use it in GitHub Desktop.
Remote DevTools expensive layout
const fs = require('fs')
const CDP = require('chrome-remote-interface')
let traceStr = ''
let allEvts = [];
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
function printLayouts(data) {
const evts = data.value || data
evts.forEach(evt => {
allEvts.push(evt)
if (!/layout/.test(evt.name)) return
if (!evt.dur) console.log(evt)
console.log('layout evt!', evt.dur)
})
}
CDP.New().then(target => CDP({target})).then(client => {
const {Page, Tracing, IO, Network, Runtime, Emulation} = client
function handleTraceAsEvts() {
Tracing.dataCollected(printLayouts);
Tracing.tracingComplete(() => {
fs.writeFileSync('trace.json', JSON.stringify({traceEvents: allEvts}))
setTimeout(() =>client.close(), 50)
})
Tracing.end()
}
function handleTraceAsStream() {
Tracing.tracingComplete(data => {
function handleTraceRead(resp) {
traceStr += resp.data
if (resp.eof) {
printLayouts(JSON.parse(traceStr).traceEvents)
fs.writeFileSync('trace.json', traceStr)
setTimeout(() => client.close(), 50)
return
}
IO.read({handle: data.stream}).then(handleTraceRead)
}
IO.read({handle: data.stream}).then(handleTraceRead)
})
Tracing.end()
}
const categories = [
'-*', // exclude default
'toplevel',
'blink.console',
'blink.user_timing',
'benchmark',
'loading',
'latencyInfo',
'devtools.timeline',
'disabled-by-default-devtools.timeline',
'disabled-by-default-devtools.timeline.frame',
'disabled-by-default-devtools.timeline.stack',
// Flipped off until bugs.chromium.org/p/v8/issues/detail?id=5820 is fixed in Stable
// 'disabled-by-default-v8.cpu_profiler',
// 'disabled-by-default-v8.cpu_profiler.hires',
'disabled-by-default-devtools.screenshot'
].join(',')
return Promise.all([
Page.enable(),
// Network.enable(),
// Runtime.enable(),
// Tracing.enable(),
])
// .then(() => Emulation.setCPUThrottlingRate({rate: 6}))
// .then(() => Network.emulateNetworkConditions({latency: 562, downloadThroughput: 209715, offline: false, uploadThroughput: 20000}))
// .then(Page.navigate({url: 'about:blank'}))
// .then(() => delay(300))
.then(() => {
Page.loadEventFired(msg => {
console.log(msg)
setTimeout(() => {
handleTraceAsEvts()
// handleTraceAsStream()
}, 500)
})
})
.then(() => Tracing.start({categories,
// transferMode: 'ReturnAsStream',
// options: 'sampling-frequency=10000' // 1000 is default and too slow.
}))
.then(() => Page.navigate({url: 'http://localhost:8000/build/es6-bundled/'}))
}).catch(err => {
console.error(err);
process.exit(1);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment