Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(driver): deliver trace as events rather than a stream #6056

Merged
merged 6 commits into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lighthouse-core/audits/accessibility/axe-audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AxeAudit extends Audit {
const impact = rule && rule.impact;
const tags = rule && rule.tags;

/** @type {Array<{node: LH.Audit.DetailsRendererNodeDetailsJSON}>}>} */
/** @type {Array<{node: LH.Audit.DetailsRendererNodeDetailsJSON}>} */
exterkamp marked this conversation as resolved.
Show resolved Hide resolved
let items = [];
if (rule && rule.nodes) {
items = rule.nodes.map(node => ({
Expand Down
62 changes: 15 additions & 47 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const LHError = require('../lib/lh-error');
const NetworkRequest = require('../lib/network-request');
const EventEmitter = require('events').EventEmitter;
const URL = require('../lib/url-shim');
const TraceParser = require('../lib/traces/trace-parser');
const constants = require('../config/constants');

const log = require('lighthouse-logger');
Expand Down Expand Up @@ -933,7 +932,6 @@ class Driver {
return this.sendCommand('Page.enable')
.then(_ => this.sendCommand('Tracing.start', {
categories: uniqueCategories.join(','),
transferMode: 'ReturnAsStream',
options: 'sampling-frequency=10000', // 1000 is default and too slow.
}));
}
Expand All @@ -942,55 +940,25 @@ class Driver {
* @return {Promise<LH.Trace>}
*/
endTrace() {
return new Promise((resolve, reject) => {
// When the tracing has ended this will fire with a stream handle.
this.once('Tracing.tracingComplete', completeEvent => {
this._readTraceFromStream(completeEvent)
.then(traceContents => resolve(traceContents), reject);
});
/** @type {Array<LH.TraceEvent>} */
const traceEvents = [];

// Issue the command to stop tracing.
return this.sendCommand('Tracing.end').catch(reject);
});
}
/**
* Listener for when dataCollected events fire for each trace chunk
* @param {LH.Crdp.Tracing.DataCollectedEvent} data
*/
const dataListener = function(data) {
traceEvents.push(...data.value);
};
this.on('Tracing.dataCollected', dataListener);

/**
* @param {LH.Crdp.Tracing.TracingCompleteEvent} traceCompleteEvent
* @return {Promise<LH.Trace>}
*/
_readTraceFromStream(traceCompleteEvent) {
return new Promise((resolve, reject) => {
let isEOF = false;
const parser = new TraceParser();

if (!traceCompleteEvent.stream) {
return reject('No streamHandle returned by traceCompleteEvent');
}

const readArguments = {
handle: traceCompleteEvent.stream,
};

/**
* @param {LH.Crdp.IO.ReadResponse} response
* @return {void|Promise<void>}
*/
const onChunkRead = response => {
if (isEOF) {
return;
}

parser.parseChunk(response.data);

if (response.eof) {
isEOF = true;
return resolve(parser.getTrace());
}

return this.sendCommand('IO.read', readArguments).then(onChunkRead);
};
this.once('Tracing.tracingComplete', _ => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so. much. easier. :D

this.off('Tracing.dataCollected', dataListener);
resolve({traceEvents});
});

this.sendCommand('IO.read', readArguments).then(onChunkRead).catch(reject);
return this.sendCommand('Tracing.end').catch(reject);
});
}

Expand Down