Created
April 18, 2019 13:32
-
-
Save smashwilson/8e32f6d40d3cb9153e63c84d14569c2a to your computer and use it in GitHub Desktop.
Collect tracing data from Electron that may be loaded into the Chrome devtools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TRACEOPTS = { | |
categoryFilter: [ | |
'-*', '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-v8.cpu_profile', 'disabled-by-default-v8.cpu_profiler', | |
'disabled-by-default-v8.cpu_profiler.hires' | |
].join(','), | |
traceOptions: 'record-until-full', | |
options: 'sampling-frequency=10000' | |
} | |
class Tracer { | |
async start () { | |
this.startMs = Date.now() | |
await new Promise(resolve => { | |
electron.contentTracing.startRecording(TRACEOPTS, resolve) | |
}) | |
} | |
async stop () { | |
this.endMs = Date.now() | |
await new Promise(resolve => { | |
electron.contentTracing.stopRecording('', tracePath => { | |
if (this.endMs - this.startMs < 10000) { | |
fs.removeSync(tracePath) | |
resolve() | |
return | |
} | |
const traceData = JSON.parse(fs.readFileSync(tracePath, {encoding: 'utf8'})) | |
const metadata = traceData.metadata | |
const traceEventsByPid = new Map() | |
for (const traceEvent of traceData.traceEvents) { | |
let events = traceEventsByPid.get(traceEvent.pid) | |
if (!events) { | |
events = [] | |
traceEventsByPid.set(traceEvent.pid, events) | |
} | |
events.push(traceEvent) | |
} | |
for (const [pid, traceEvents] of traceEventsByPid.entries()) { | |
const pidTracePath = path.resolve(`trace-${this.startMs}-${pid}.json`) | |
fs.writeFileSync(pidTracePath, JSON.stringify({metadata, traceEvents}), {encoding: 'utf8'}) | |
} | |
resolve() | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment