Skip to content

Generate timeline data

Andrea Cardaci edited this page May 4, 2017 · 1 revision

Generate timeline data from the loading of an URL, the resulting JSON file can be opened from the Performace tab of DevTools or using DevTools Timeline Viewer.

const CDP = require('chrome-remote-interface');
const fs = require('fs');

CDP(async (client) => {
    try {
        const {Page, Tracing} = client;
        // enable Page domain events
        await Page.enable();
        // trace a page load
        const events = [];
        Tracing.dataCollected(({value}) => {
            events.push(...value);
        });
        await Tracing.start();
        await Page.navigate({url: 'https://github.com'});
        await Page.loadEventFired();
        await Tracing.end();
        await Tracing.tracingComplete();
        // save the tracing data
        fs.writeFileSync('/tmp/timeline.json', JSON.stringify(events));
    } catch (err) {
        console.error(err);
    } finally {
        await client.close();
    }
}).on('error', (err) => {
    console.error(err);
});