Unverified Commit 8df62188 authored by auto-submit[bot]'s avatar auto-submit[bot] Committed by GitHub

Reverts "Use `coverage.collect`'s `coverableLineCache` param to speed up coverage" (#137121)

Reverts flutter/flutter#136851
Initiated by: CaseyHillers
This change reverts the following previous change:
Original Description:
One of the reasons gathering coverage information is expensive is that we have to force compile every function in the libraries we're interested in. Without this, functions that haven't been invoked (so haven't been compiled) won't have any line number information, so the coverage tool doesn't know which lines to add to the list of misses. In flutter's case, the test infra spawns many VMs, and each of these needs to recompile all those libraries.

To fix this, we need a way of skipping force compilation for libraries we've already seen in previous tests, without losing the information about which lines in each library are coverable. So I [added](https://github.com/dart-lang/coverage/pull/466) the `coverableLineCache` to `coverage.collect` in package:coverage v1.7.0. This cache starts out empty, but fills up with lists of all the lines that are coverable for every library as coverage is gathered. package:coverage can then tell the VM not to force compile any libraries in this cache (using `getSourceReport`'s `librariesAlreadyCompiled` param). So the first test suite will still have to compile everything, but subsequent test suites will be much faster.

This speeds up coverage collection significantly, for large test suites:

| Running flutter/packages/flutter tests... | Time | Overhead |
| --- | --- | --- |
| without coverage | 8:53 | - |
| with coverage | 20:25 | 130% |
| with `coverableLineCache` | 12:21 | 40% |

Bug: https://github.com/flutter/flutter/issues/100751
parent d1884e7b
......@@ -37,7 +37,6 @@ class CoverageCollector extends TestWatcher {
final coverage.Resolver? resolver;
final Map<String, List<List<int>>?> _ignoredLinesInFilesCache = <String, List<List<int>>?>{};
final Map<String, Set<int>> _coverableLineCache = <String, Set<int>>{};
final TestTimeRecorder? testTimeRecorder;
......@@ -104,11 +103,7 @@ class CoverageCollector extends TestWatcher {
Future<void> collectCoverageIsolate(Uri vmServiceUri) async {
_logMessage('collecting coverage data from $vmServiceUri...');
final Map<String, dynamic> data = await collect(
vmServiceUri,
libraryNames,
branchCoverage: branchCoverage,
coverableLineCache: _coverableLineCache,
);
vmServiceUri, libraryNames, branchCoverage: branchCoverage);
_logMessage('($vmServiceUri): collected coverage data; merging...');
_addHitmap(await coverage.HitMap.parseJson(
......@@ -150,12 +145,9 @@ class CoverageCollector extends TestWatcher {
.then((Uri? vmServiceUri) {
_logMessage('collecting coverage data from $testDevice at $vmServiceUri...');
return collect(
vmServiceUri!,
libraryNames,
serviceOverride: serviceOverride,
branchCoverage: branchCoverage,
coverableLineCache: _coverableLineCache,
).then<void>((Map<String, dynamic> result) {
vmServiceUri!, libraryNames, serviceOverride: serviceOverride,
branchCoverage: branchCoverage)
.then<void>((Map<String, dynamic> result) {
_logMessage('Collected coverage data.');
data = result;
});
......@@ -275,12 +267,9 @@ Future<Map<String, dynamic>> collect(Uri serviceUri, Set<String>? libraryNames,
@visibleForTesting bool forceSequential = false,
@visibleForTesting FlutterVmService? serviceOverride,
bool branchCoverage = false,
required Map<String, Set<int>> coverableLineCache,
}) {
return coverage.collect(
serviceUri, false, false, false, libraryNames,
serviceOverrideForTesting: serviceOverride?.service,
branchCoverage: branchCoverage,
coverableLineCache: coverableLineCache,
);
branchCoverage: branchCoverage);
}
......@@ -53,7 +53,6 @@ void main() {
Uri(),
<String>{'foo'},
serviceOverride: fakeVmServiceHost.vmService,
coverableLineCache: <String, Set<int>>{},
);
expect(result, <String, Object>{'type': 'CodeCoverage', 'coverage': <Object>[]});
......@@ -124,7 +123,6 @@ void main() {
Uri(),
<String>{'foo'},
serviceOverride: fakeVmServiceHost.vmService,
coverableLineCache: <String, Set<int>>{},
);
expect(result, <String, Object>{
......@@ -153,7 +151,6 @@ void main() {
Uri(),
null,
serviceOverride: fakeVmServiceHost.vmService,
coverableLineCache: <String, Set<int>>{},
);
expect(result, <String, Object>{
......@@ -240,7 +237,6 @@ void main() {
Uri(),
<String>{'foo'},
serviceOverride: fakeVmServiceHost.vmService,
coverableLineCache: <String, Set<int>>{},
);
expect(result, <String, Object>{
......@@ -315,7 +311,6 @@ void main() {
Uri(),
null,
serviceOverride: fakeVmServiceHost.vmService,
coverableLineCache: <String, Set<int>>{},
);
expect(result, <String, Object>{
......@@ -406,7 +401,6 @@ void main() {
<String>{'foo'},
serviceOverride: fakeVmServiceHost.vmService,
branchCoverage: true,
coverableLineCache: <String, Set<int>>{},
);
expect(result, <String, Object>{
......@@ -607,179 +601,6 @@ void main() {
tempDir?.deleteSync(recursive: true);
}
});
testWithoutContext('Coverage collector fills coverableLineCache', () async {
final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(
requests: <VmServiceExpectation>[
FakeVmServiceRequest(
method: 'getVM',
jsonResponse: (VM.parse(<String, Object>{})!
..isolates = <IsolateRef>[
IsolateRef.parse(<String, Object>{
'id': '1',
})!,
]
).toJson(),
),
FakeVmServiceRequest(
method: 'getVersion',
jsonResponse: Version(major: 4, minor: 13).toJson(),
),
FakeVmServiceRequest(
method: 'getSourceReport',
args: <String, Object>{
'isolateId': '1',
'reports': <Object>['Coverage'],
'forceCompile': true,
'reportLines': true,
'libraryFilters': <String>['package:foo/'],
'librariesAlreadyCompiled': <String>[],
},
jsonResponse: SourceReport(
ranges: <SourceReportRange>[
SourceReportRange(
scriptIndex: 0,
startPos: 0,
endPos: 0,
compiled: true,
coverage: SourceReportCoverage(
hits: <int>[1, 3],
misses: <int>[2],
),
),
],
scripts: <ScriptRef>[
ScriptRef(
uri: 'package:foo/foo.dart',
id: '1',
),
],
).toJson(),
),
],
);
final Map<String, Set<int>> coverableLineCache = <String, Set<int>>{};
final Map<String, Object?> result = await collect(
Uri(),
<String>{'foo'},
serviceOverride: fakeVmServiceHost.vmService,
coverableLineCache: coverableLineCache,
);
expect(result, <String, Object>{
'type': 'CodeCoverage',
'coverage': <Object>[
<String, Object>{
'source': 'package:foo/foo.dart',
'script': <String, Object>{
'type': '@Script',
'fixedId': true,
'id': 'libraries/1/scripts/package%3Afoo%2Ffoo.dart',
'uri': 'package:foo/foo.dart',
'_kind': 'library',
},
'hits': <Object>[1, 1, 3, 1, 2, 0],
},
],
});
// coverableLineCache should contain every line mentioned in the report.
expect(coverableLineCache, <String, Set<int>>{
'package:foo/foo.dart': <int>{1, 2, 3},
});
expect(fakeVmServiceHost.hasRemainingExpectations, false);
});
testWithoutContext('Coverage collector avoids recompiling libraries in coverableLineCache', () async {
final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(
requests: <VmServiceExpectation>[
FakeVmServiceRequest(
method: 'getVM',
jsonResponse: (VM.parse(<String, Object>{})!
..isolates = <IsolateRef>[
IsolateRef.parse(<String, Object>{
'id': '1',
})!,
]
).toJson(),
),
FakeVmServiceRequest(
method: 'getVersion',
jsonResponse: Version(major: 4, minor: 13).toJson(),
),
// This collection sets librariesAlreadyCompiled. The response doesn't
// include any misses.
FakeVmServiceRequest(
method: 'getSourceReport',
args: <String, Object>{
'isolateId': '1',
'reports': <Object>['Coverage'],
'forceCompile': true,
'reportLines': true,
'libraryFilters': <String>['package:foo/'],
'librariesAlreadyCompiled': <String>['package:foo/foo.dart'],
},
jsonResponse: SourceReport(
ranges: <SourceReportRange>[
SourceReportRange(
scriptIndex: 0,
startPos: 0,
endPos: 0,
compiled: true,
coverage: SourceReportCoverage(
hits: <int>[1, 3],
misses: <int>[],
),
),
],
scripts: <ScriptRef>[
ScriptRef(
uri: 'package:foo/foo.dart',
id: '1',
),
],
).toJson(),
),
],
);
final Map<String, Set<int>> coverableLineCache = <String, Set<int>>{
'package:foo/foo.dart': <int>{1, 2, 3},
};
final Map<String, Object?> result2 = await collect(
Uri(),
<String>{'foo'},
serviceOverride: fakeVmServiceHost.vmService,
coverableLineCache: coverableLineCache,
);
// Expect that line 2 is marked as missed, even though it wasn't mentioned
// in the getSourceReport response.
expect(result2, <String, Object>{
'type': 'CodeCoverage',
'coverage': <Object>[
<String, Object>{
'source': 'package:foo/foo.dart',
'script': <String, Object>{
'type': '@Script',
'fixedId': true,
'id': 'libraries/1/scripts/package%3Afoo%2Ffoo.dart',
'uri': 'package:foo/foo.dart',
'_kind': 'library',
},
'hits': <Object>[1, 1, 2, 0, 3, 1],
},
],
});
expect(coverableLineCache, <String, Set<int>>{
'package:foo/foo.dart': <int>{1, 2, 3},
});
expect(fakeVmServiceHost.hasRemainingExpectations, false);
});
}
File writeFooBarPackagesJson(Directory tempDir) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment