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: replace WebInspector traceparser with native JSON.parse #6099

Merged
merged 1 commit into from
Sep 24, 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
23 changes: 6 additions & 17 deletions lighthouse-core/lib/asset-saver.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const stream = require('stream');
const Simulator = require('./dependency-graph/simulator/simulator');
const lanternTraceSaver = require('./lantern-trace-saver');
const Metrics = require('./traces/pwmetrics-events');
const TraceParser = require('./traces/trace-parser');
const rimraf = require('rimraf');
const mkdirp = require('mkdirp');

Expand Down Expand Up @@ -94,30 +93,20 @@ async function loadArtifacts(basePath) {

// load devtoolsLogs
artifacts.devtoolsLogs = {};
filenames.filter(f => f.endsWith(devtoolsLogSuffix)).map(filename => {
filenames.filter(f => f.endsWith(devtoolsLogSuffix)).forEach(filename => {
const passName = filename.replace(devtoolsLogSuffix, '');
const devtoolsLog = JSON.parse(fs.readFileSync(path.join(basePath, filename), 'utf8'));
artifacts.devtoolsLogs[passName] = devtoolsLog;
});

// load traces
artifacts.traces = {};
const promises = filenames.filter(f => f.endsWith(traceSuffix)).map(filename => {
return new Promise(resolve => {
const passName = filename.replace(traceSuffix, '');
const readStream = fs.createReadStream(path.join(basePath, filename), {
encoding: 'utf-8',
highWaterMark: 4 * 1024 * 1024, // TODO benchmark to find the best buffer size here
});
const parser = new TraceParser();
readStream.on('data', chunk => parser.parseChunk(chunk));
readStream.on('end', _ => {
artifacts.traces[passName] = parser.getTrace();
resolve();
});
});
filenames.filter(f => f.endsWith(traceSuffix)).forEach(filename => {
const file = fs.readFileSync(path.join(basePath, filename), {encoding: 'utf-8'});
const trace = JSON.parse(file);
const passName = filename.replace(traceSuffix, '');
artifacts.traces[passName] = Array.isArray(trace) ? {traceEvents: trace} : trace;
});
await Promise.all(promises);

return artifacts;
}
Expand Down
74 changes: 0 additions & 74 deletions lighthouse-core/lib/traces/trace-parser.js

This file was deleted.

21 changes: 0 additions & 21 deletions lighthouse-core/lib/web-inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ module.exports = (function() {
global.self = global;
}

if (typeof global.window === 'undefined') {
global.window = global;
}

global.Node = {
ELEMENT_NODE: 1,
TEXT_NODE: 3,
Expand All @@ -45,28 +41,11 @@ module.exports = (function() {
// See https://github.com/GoogleChrome/lighthouse/issues/73
const _setImmediate = global.setImmediate;

global.Protocol = {
Agents() {},
};

global.WebInspector = {};
const WebInspector = global.WebInspector;

// Shared Dependencies
require('chrome-devtools-frontend/front_end/common/Object.js');
require('chrome-devtools-frontend/front_end/common/UIString.js');
require('chrome-devtools-frontend/front_end/platform/utilities.js');
require('chrome-devtools-frontend/front_end/sdk/Target.js');
require('chrome-devtools-frontend/front_end/sdk/TargetManager.js');

// Dependencies for timeline-model
WebInspector.console = {
error() {},
};

// used for streaming json parsing
require('chrome-devtools-frontend/front_end/common/TextUtils.js');
require('chrome-devtools-frontend/front_end/timeline/TimelineLoader.js');

// Dependencies for effective CSS rule calculation.
require('chrome-devtools-frontend/front_end/common/TextRange.js');
Expand Down
11 changes: 11 additions & 0 deletions lighthouse-core/test/lib/asset-saver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,15 @@ describe('asset-saver helper', () => {
});
}, 40 * 1000);
});

describe('loadArtifacts', () => {
it('loads artifacts from disk', async () => {
const artifactsPath = __dirname + '/../fixtures/artifacts/perflog/';
const artifacts = await assetSaver.loadArtifacts(artifactsPath);
assert.strictEqual(artifacts.LighthouseRunWarnings.length, 2);
assert.strictEqual(artifacts.URL.requestedUrl, 'https://www.reddit.com/r/nba');
assert.strictEqual(artifacts.devtoolsLogs.defaultPass.length, 555);
assert.strictEqual(artifacts.traces.defaultPass.traceEvents.length, 12);
});
});
});
93 changes: 0 additions & 93 deletions lighthouse-core/test/lib/traces/trace-parser-test.js

This file was deleted.

4 changes: 2 additions & 2 deletions lighthouse-extension/app/src/lighthouse-ext-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ function isRunning() {
return lighthouseIsRunning;
}

// Run when in extension context, but not in devtools.
if ('chrome' in window && chrome.runtime) {
// Run when in extension context, but not in devtools or unit tests.
if (typeof window !== 'undefined' && 'chrome' in window && chrome.runtime) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Before this PR, lighthouse-ext-background.js was pulling in assetSaver, which was pulling in WebInspector, which was setting global.window = global, so this file was fine to pull into node unit tests.

With that dependency path removed, this protects us from erroring on the window access (in the actual extension the behavior is unchanged since window is already defined :)

chrome.runtime.onInstalled.addListener(details => {
if (details.previousVersion) {
// eslint-disable-next-line no-console
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"chrome-debug": "./lighthouse-core/scripts/manual-chrome-launcher.js"
},
"engines": {
"node": ">=8.9"
"node": ">=8.10"
},
"scripts": {
"install-all": "npm-run-posix-or-windows install-all:task",
Expand Down