Skip to content

Commit

Permalink
core(driver): deliver trace as events rather than a stream (#6056)
Browse files Browse the repository at this point in the history
  • Loading branch information
exterkamp authored and paulirish committed Sep 20, 2018
1 parent 1ac7bd1 commit 1af9f20
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 48 deletions.
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}>} */
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', _ => {
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

0 comments on commit 1af9f20

Please sign in to comment.