From 090fc5cc53d5d23d0a3462c21d01d3098fe9679b Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Wed, 8 Apr 2020 09:07:02 -0700 Subject: [PATCH] [flutter_tools] Use gzip level 1 for devfs transfer compression (#54123) --- packages/flutter_tools/lib/src/base/io.dart | 1 + packages/flutter_tools/lib/src/base/os.dart | 8 ++++++ packages/flutter_tools/lib/src/devfs.dart | 28 +++++++++++++++---- .../lib/src/resident_runner.dart | 1 + .../test/general.shard/base/os_test.dart | 4 +++ .../test/general.shard/devfs_test.dart | 19 +++++++++++-- packages/flutter_tools/test/src/context.dart | 3 ++ 7 files changed, 56 insertions(+), 8 deletions(-) diff --git a/packages/flutter_tools/lib/src/base/io.dart b/packages/flutter_tools/lib/src/base/io.dart index f70e486ee6..734a2fc1b4 100644 --- a/packages/flutter_tools/lib/src/base/io.dart +++ b/packages/flutter_tools/lib/src/base/io.dart @@ -61,6 +61,7 @@ export 'dart:io' // File, NO! Use `file_system.dart` // FileSystemEntity, NO! Use `file_system.dart` gzip, + GZipCodec, HandshakeException, HttpClient, HttpClientRequest, diff --git a/packages/flutter_tools/lib/src/base/os.dart b/packages/flutter_tools/lib/src/base/os.dart index 2c21b8a542..12b34fe3ea 100644 --- a/packages/flutter_tools/lib/src/base/os.dart +++ b/packages/flutter_tools/lib/src/base/os.dart @@ -52,6 +52,9 @@ abstract class OperatingSystemUtils { processManager: processManager, ); + @visibleForTesting + static final GZipCodec gzipLevel1 = GZipCodec(level: 1); + final FileSystem _fileSystem; final Logger _logger; final Platform _platform; @@ -98,6 +101,11 @@ abstract class OperatingSystemUtils { /// Returns true if the gzip is not corrupt (does not check tar). bool verifyGzip(File gzippedFile); + /// Compresses a stream using gzip level 1 (faster but larger). + Stream> gzipLevel1Stream(Stream> stream) { + return stream.cast>().transform>(gzipLevel1.encoder); + } + /// Returns a pretty name string for the current operating system. /// /// If available, the detailed version of the OS is included. diff --git a/packages/flutter_tools/lib/src/devfs.dart b/packages/flutter_tools/lib/src/devfs.dart index 11171ab2e0..7af81d80f2 100644 --- a/packages/flutter_tools/lib/src/devfs.dart +++ b/packages/flutter_tools/lib/src/devfs.dart @@ -12,6 +12,7 @@ import 'base/context.dart'; import 'base/file_system.dart'; import 'base/io.dart'; import 'base/net.dart'; +import 'base/os.dart'; import 'build_info.dart'; import 'bundle.dart'; import 'compile.dart'; @@ -46,8 +47,10 @@ abstract class DevFSContent { Stream> contentsAsStream(); - Stream> contentsAsCompressedStream() { - return contentsAsStream().cast>().transform>(gzip.encoder); + Stream> contentsAsCompressedStream( + OperatingSystemUtils osUtils, + ) { + return osUtils.gzipLevel1Stream(contentsAsStream()); } /// Return the list of files this content depends on. @@ -265,15 +268,21 @@ class DevFSException implements Exception { } class _DevFSHttpWriter { - _DevFSHttpWriter(this.fsName, VMService serviceProtocol) + _DevFSHttpWriter( + this.fsName, + VMService serviceProtocol, { + @required OperatingSystemUtils osUtils, + }) : httpAddress = serviceProtocol.httpAddress, _client = (context.get() == null) ? HttpClient() - : context.get()(); + : context.get()(), + _osUtils = osUtils; final String fsName; final Uri httpAddress; final HttpClient _client; + final OperatingSystemUtils _osUtils; static const int kMaxInFlight = 6; @@ -312,7 +321,9 @@ class _DevFSHttpWriter { request.headers.removeAll(HttpHeaders.acceptEncodingHeader); request.headers.add('dev_fs_name', fsName); request.headers.add('dev_fs_uri_b64', base64.encode(utf8.encode('$deviceUri'))); - final Stream> contents = content.contentsAsCompressedStream(); + final Stream> contents = content.contentsAsCompressedStream( + _osUtils, + ); await request.addStream(contents); final HttpClientResponse response = await request.close(); response.listen((_) => null, @@ -385,8 +396,13 @@ class DevFS { this.fsName, this.rootDirectory, { String packagesFilePath, + @required OperatingSystemUtils osUtils, }) : _operations = ServiceProtocolDevFSOperations(serviceProtocol), - _httpWriter = _DevFSHttpWriter(fsName, serviceProtocol), + _httpWriter = _DevFSHttpWriter( + fsName, + serviceProtocol, + osUtils: osUtils, + ), _packagesFilePath = packagesFilePath ?? globals.fs.path.join(rootDirectory.path, kPackagesFileName); DevFS.operations( diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index f719d8366f..92409ddfb9 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -281,6 +281,7 @@ class FlutterDevice { fsName, rootDirectory, packagesFilePath: packagesFilePath, + osUtils: globals.os, ); return devFS.create(); } diff --git a/packages/flutter_tools/test/general.shard/base/os_test.dart b/packages/flutter_tools/test/general.shard/base/os_test.dart index 751e8b705d..b7ca1f2678 100644 --- a/packages/flutter_tools/test/general.shard/base/os_test.dart +++ b/packages/flutter_tools/test/general.shard/base/os_test.dart @@ -86,6 +86,10 @@ void main() { expect(result[1].path, kPath2); }); }); + + testWithoutContext('stream compression level', () { + expect(OperatingSystemUtils.gzipLevel1.level, equals(1)); + }); } class MockProcessManager extends Mock implements ProcessManager {} diff --git a/packages/flutter_tools/test/general.shard/devfs_test.dart b/packages/flutter_tools/test/general.shard/devfs_test.dart index 401075885b..1b47fe5cca 100644 --- a/packages/flutter_tools/test/general.shard/devfs_test.dart +++ b/packages/flutter_tools/test/general.shard/devfs_test.dart @@ -11,6 +11,7 @@ import 'package:file/memory.dart'; import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/net.dart'; +import 'package:flutter_tools/src/base/os.dart'; import 'package:flutter_tools/src/compile.dart'; import 'package:flutter_tools/src/devfs.dart'; import 'package:flutter_tools/src/vmservice.dart'; @@ -96,6 +97,7 @@ void main() { group('mocked http client', () { HttpOverrides savedHttpOverrides; HttpClient httpClient; + OperatingSystemUtils osUtils; setUpAll(() { tempDir = _newTempDir(fs); @@ -103,6 +105,7 @@ void main() { savedHttpOverrides = HttpOverrides.current; httpClient = MockOddlyFailingHttpClient(); HttpOverrides.global = MyHttpOverrides(httpClient); + osUtils = MockOperatingSystemUtils(); }); tearDownAll(() async { @@ -147,7 +150,12 @@ void main() { return Future.value(httpClientResponse); }); - final DevFS devFS = DevFS(vmService, 'test', tempDir); + final DevFS devFS = DevFS( + vmService, + 'test', + tempDir, + osUtils: osUtils, + ); await devFS.create(); final MockResidentCompiler residentCompiler = MockResidentCompiler(); @@ -163,6 +171,7 @@ void main() { expect(report.success, isTrue); verify(httpClient.putUrl(any)).called(kFailedAttempts + 1); verify(httpRequest.close()).called(kFailedAttempts + 1); + verify(osUtils.gzipLevel1Stream(any)).called(kFailedAttempts + 1); }, overrides: { FileSystem: () => fs, HttpClientFactory: () => () => httpClient, @@ -185,7 +194,12 @@ void main() { setUp(() { vmService.resetState(); - devFS = DevFS(vmService, 'test', tempDir); + devFS = DevFS( + vmService, + 'test', + tempDir, + osUtils: FakeOperatingSystemUtils(), + ); }); tearDownAll(() async { @@ -477,3 +491,4 @@ class MockOddlyFailingHttpClient extends Mock implements HttpClient {} class MockHttpClientRequest extends Mock implements HttpClientRequest {} class MockHttpHeaders extends Mock implements HttpHeaders {} class MockHttpClientResponse extends Mock implements HttpClientResponse {} +class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {} diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart index 1f8181b04e..d2753197fb 100644 --- a/packages/flutter_tools/test/src/context.dart +++ b/packages/flutter_tools/test/src/context.dart @@ -321,6 +321,9 @@ class FakeOperatingSystemUtils implements OperatingSystemUtils { @override bool verifyGzip(File gzippedFile) => true; + @override + Stream> gzipLevel1Stream(Stream> stream) => stream; + @override String get name => 'fake OS name and version';