Skip to content

Commit

Permalink
refactor: devtools log (#1669)
Browse files Browse the repository at this point in the history
* contain in driver, s/MessageLog/DevtoolsLog
  • Loading branch information
patrickhulce authored and brendankenny committed Feb 8, 2017
1 parent ec5fbe3 commit e7c6c50
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
3 changes: 0 additions & 3 deletions lighthouse-core/gather/connections/connection.js
Expand Up @@ -18,7 +18,6 @@

const EventEmitter = require('events').EventEmitter;
const log = require('../../lib/log.js');
const MessageLog = require('./message-log');

class Connection {

Expand All @@ -27,7 +26,6 @@ class Connection {
/** @type {!Map<number, {resolve: function(*), reject: function(*), method: string}>}*/
this._callbacks = new Map();
this._eventEmitter = new EventEmitter();
this.log = new MessageLog();
}

/**
Expand Down Expand Up @@ -111,7 +109,6 @@ class Connection {
}));
}

this.log.record(object);
log.formatProtocol('<= event',
{method: object.method, params: object.params}, 'verbose');
this.emitNotification(object.method, object.params);
Expand Down
Expand Up @@ -16,14 +16,23 @@
*/
'use strict';

class MessageLog {
constructor() {
/**
* @fileoverview This class saves all protocol messages whose method match a particular
* regex filter. Used when saving assets for later analysis by another tool such as
* Webpagetest.
*/
class DevtoolsLog {
/**
* @param {RegExp=} regexFilter
*/
constructor(regexFilter) {
this._filter = regexFilter;
this._messages = [];
this._isRecording = false;
}

/**
* @return {!Array<Object>}
* @return {!Array<{method: string, params: !Object}>}
*/
get messages() {
return this._messages;
Expand All @@ -42,13 +51,14 @@ class MessageLog {
}

/**
* @param {{method: string, params: Object}} message
* Records a message if method matches filter and recording has been started.
* @param {{method: string, params: !Object}} message
*/
record(message) {
if (this._isRecording && /^(Page|Network)\./.test(message.method)) {
if (this._isRecording && (!this._filter || this._filter.test(message.method))) {
this._messages.push(message);
}
}
}

module.exports = MessageLog;
module.exports = DevtoolsLog;
19 changes: 14 additions & 5 deletions lighthouse-core/gather/driver.js
Expand Up @@ -23,6 +23,7 @@ const EventEmitter = require('events').EventEmitter;
const URL = require('../lib/url-shim');

const log = require('../lib/log.js');
const DevtoolsLog = require('./devtools-log');

const MAX_WAIT_FOR_FULLY_LOADED = 25 * 1000;
const PAUSE_AFTER_LOAD = 500;
Expand All @@ -37,7 +38,12 @@ class Driver {
this._traceCategories = Driver.traceCategories;
this._eventEmitter = new EventEmitter();
this._connection = connection;
connection.on('notification', event => this._eventEmitter.emit(event.method, event.params));
// currently only used by WPT where just Page and Network are needed
this._devtoolsLog = new DevtoolsLog(/^(Page|Network)\./);
connection.on('notification', event => {
this._devtoolsLog.record(event);
this._eventEmitter.emit(event.method, event.params);
});
this.online = true;
this._domainEnabledCounts = new Map();
}
Expand All @@ -62,8 +68,11 @@ class Driver {
];
}

/**
* @return {!Array<{method: string, params: !Object}>}
*/
get devtoolsLog() {
return this._connection.log.messages;
return this._devtoolsLog.messages;
}

/**
Expand Down Expand Up @@ -575,8 +584,8 @@ class Driver {
throw new Error('DOM domain enabled when starting trace');
}

this._connection.log.reset();
this._connection.log.beginRecording();
this._devtoolsLog.reset();
this._devtoolsLog.beginRecording();

// Enable Page domain to wait for Page.loadEventFired
return this.sendCommand('Page.enable')
Expand All @@ -587,7 +596,7 @@ class Driver {
return new Promise((resolve, reject) => {
// When the tracing has ended this will fire with a stream handle.
this.once('Tracing.tracingComplete', streamHandle => {
this._connection.log.endRecording();
this._devtoolsLog.endRecording();
this._readTraceFromStream(streamHandle)
.then(traceContents => resolve(traceContents), reject);
});
Expand Down
Expand Up @@ -18,15 +18,15 @@
/* eslint-env mocha */

const assert = require('assert');
const MessageLog = require('../../../gather/connections/message-log');
const DevtoolsLog = require('../../gather/devtools-log');

describe('MessageLog', () => {
describe('DevtoolsLog', () => {
let messageLog;
const pageMsg = {method: 'Page.frameStartedLoading'};
const networkMsg = {method: 'Network.requestWillBeSent'};
const otherMsg = {method: 'Storage.cleared'};

beforeEach(() => messageLog = new MessageLog());
beforeEach(() => messageLog = new DevtoolsLog(/^(Page|Network)/));

it('returns an array', () => {
assert.deepEqual(messageLog.messages, []);
Expand All @@ -42,7 +42,7 @@ describe('MessageLog', () => {
assert.equal(messageLog.messages[0].method, networkMsg.method);
});

it('does not record non-Network/Page events', () => {
it('does not record non-matching events', () => {
messageLog.beginRecording();
messageLog.record(pageMsg); // will record
messageLog.record(networkMsg); // will record
Expand All @@ -52,6 +52,15 @@ describe('MessageLog', () => {
assert.equal(messageLog.messages[0].method, pageMsg.method);
});

it('records everything when no filter provided', () => {
messageLog = new DevtoolsLog();
messageLog.beginRecording();
messageLog.record(pageMsg);
messageLog.record(networkMsg);
messageLog.record(otherMsg);
assert.equal(messageLog.messages.length, 3);
});

it('resets properly', () => {
messageLog.beginRecording();
messageLog.record(pageMsg);
Expand Down

0 comments on commit e7c6c50

Please sign in to comment.