[go: nahoru, domu]

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(fr): add snapshot and timespan support to no-unload-listeners audit #12830

Merged
merged 7 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fill
  • Loading branch information
adamraine committed Jul 21, 2021
commit 4676d0d2b7c7e3e7cfd1c712e00d3c20762aaa8b
34 changes: 20 additions & 14 deletions lighthouse-core/gather/gatherers/js-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,6 @@ class JsUsage extends FRGatherer {
if (context.gatherMode === 'snapshot') {
await this.startSensitiveInstrumentation(context);
await this.stopSensitiveInstrumentation(context);

for (const scriptParsedEvent of this._scriptParsedEvents) {
const url = scriptParsedEvent.embedderName;
if (!url) continue;
const scripts = usageByUrl[url] || [];
scripts.push({
url,
scriptId: scriptParsedEvent.scriptId,
functions: [],
});
usageByUrl[url] = scripts;
}

return usageByUrl;
}

for (const scriptUsage of this._scriptUsages) {
Expand Down Expand Up @@ -125,6 +111,26 @@ class JsUsage extends FRGatherer {
usageByUrl[url] = scripts;
}

// Usages alone do not always generate an exhaustive list of scripts in timespan and snapshot.
// For audits which use this for url/scriptId mappings, we can include an empty usage object.
// TODO(FR-COMPAT): Enable this logic for legacy and navigation or make a separate artifact for url/scriptId mappings.
if (context.gatherMode !== 'navigation') {
for (const scriptParsedEvent of this._scriptParsedEvents) {
adamraine marked this conversation as resolved.
Show resolved Hide resolved
const url = scriptParsedEvent.embedderName;
if (!url) continue;

const scripts = usageByUrl[url] || [];
if (!scripts.find(s => s.scriptId === scriptParsedEvent.scriptId)) {
scripts.push({
url,
scriptId: scriptParsedEvent.scriptId,
functions: [],
});
}
usageByUrl[url] = scripts;
}
}

return usageByUrl;
}
}
Expand Down
57 changes: 57 additions & 0 deletions lighthouse-core/test/gather/gatherers/js-usage-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,61 @@ describe('JsUsage gatherer', () => {
],
});
});

it('adds script coverages without coverage in timespan', async () => {
const context = createMockContext();
context.gatherMode = 'timespan';
context.driver._session.on
.mockEvent('Debugger.scriptParsed', {
scriptId: '1',
embedderName: 'https://www.example.com',
})
.mockEvent('Debugger.scriptParsed', {
scriptId: '2',
embedderName: 'https://www.example.com/script.js',
});
context.driver._session.sendCommand
.mockResponse('Profiler.enable', {})
.mockResponse('Profiler.disable', {})
.mockResponse('Debugger.enable', {})
.mockResponse('Debugger.disable', {})
.mockResponse('Profiler.startPreciseCoverage', {})
.mockResponse('Profiler.takePreciseCoverage', {
result: [{
scriptId: '1',
url: 'https://www.example.com',
functions: [],
}],
})
.mockResponse('Profiler.stopPreciseCoverage', {});

const gatherer = new JsUsage();
await gatherer.startInstrumentation(context);
await gatherer.startSensitiveInstrumentation(context);

// Needed for protocol events to emit.
await flushAllTimersAndMicrotasks(1);

await gatherer.stopSensitiveInstrumentation(context);
await gatherer.stopInstrumentation(context);

const artifact = await gatherer.getArtifact(context);

expect(artifact).toEqual({
'https://www.example.com': [
{
scriptId: '1',
url: 'https://www.example.com',
functions: [],
},
],
'https://www.example.com/script.js': [
{
scriptId: '2',
url: 'https://www.example.com/script.js',
functions: [],
},
],
});
});
});