From 50ea6f32d33b6b10ffcf527b9f2f72f04a7b4fab Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 5 Jun 2019 12:40:21 -0700 Subject: [PATCH] add benchmarks to track web size (#33892) --- .../bin/tasks/web_size__compile_test.dart | 10 +++ dev/devicelab/lib/tasks/perf_tests.dart | 69 +++++++++++++++++++ dev/devicelab/manifest.yaml | 8 +++ .../flutter_tools/lib/src/web/compile.dart | 1 + .../flutter_tools/test/web/compile_test.dart | 1 + 5 files changed, 89 insertions(+) create mode 100644 dev/devicelab/bin/tasks/web_size__compile_test.dart diff --git a/dev/devicelab/bin/tasks/web_size__compile_test.dart b/dev/devicelab/bin/tasks/web_size__compile_test.dart new file mode 100644 index 0000000000..f54bce5995 --- /dev/null +++ b/dev/devicelab/bin/tasks/web_size__compile_test.dart @@ -0,0 +1,10 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter_devicelab/framework/framework.dart'; +import 'package:flutter_devicelab/tasks/perf_tests.dart'; + +Future main() async { + await task(createWebCompileTest()); +} diff --git a/dev/devicelab/lib/tasks/perf_tests.dart b/dev/devicelab/lib/tasks/perf_tests.dart index c82e0163dc..13461de668 100644 --- a/dev/devicelab/lib/tasks/perf_tests.dart +++ b/dev/devicelab/lib/tasks/perf_tests.dart @@ -74,6 +74,10 @@ TaskFunction createHelloWorldCompileTest() { return CompileTest('${flutterDirectory.path}/examples/hello_world', reportPackageContentSizes: true).run; } +TaskFunction createWebCompileTest() { + return const WebCompileTest().run; +} + TaskFunction createComplexLayoutCompileTest() { return CompileTest('${flutterDirectory.path}/dev/benchmarks/complex_layout').run; } @@ -198,6 +202,71 @@ class PerfTest { } } +/// Measures how long it takes to compile a Flutter app to JavaScript and how +/// big the compiled code is. +class WebCompileTest { + const WebCompileTest(); + + Future run() async { + final Map metrics = {}; + await inDirectory('${flutterDirectory.path}/examples/hello_world', () async { + await flutter('packages', options: ['get']); + await evalFlutter('build', options: [ + 'web', + '-v', + '--release', + '--no-pub', + ]); + final String output = '${flutterDirectory.path}/examples/hello_world/build/web/main.dart.js'; + await _measureSize('hello_world', output, metrics); + return null; + }); + await inDirectory('${flutterDirectory.path}/examples/flutter_gallery', () async { + await flutter('packages', options: ['get']); + await evalFlutter('build', options: [ + 'web', + '-v', + '--release', + '--no-pub', + ]); + final String output = '${flutterDirectory.path}/examples/flutter_gallery/build/web/main.dart.js'; + await _measureSize('flutter_gallery', output, metrics); + return null; + }); + const String sampleAppName = 'sample_flutter_app'; + final Directory sampleDir = dir('${Directory.systemTemp.path}/$sampleAppName'); + + rmTree(sampleDir); + + await inDirectory(Directory.systemTemp, () async { + await flutter('create', options: ['--template=app', sampleAppName]); + await inDirectory(sampleDir, () async { + await flutter('packages', options: ['get']); + await evalFlutter('build', options: [ + 'web', + '-v', + '--release', + '--no-pub', + ]); + await _measureSize('basic_material_app', path.join(sampleDir.path, 'build/web/main.dart.js'), metrics); + }); + }); + return TaskResult.success(metrics, benchmarkScoreKeys: metrics.keys.toList()); + } + + static Future _measureSize(String metric, String output, Map metrics) async { + final ProcessResult result = await Process.run('du', ['-k', output]); + await Process.run('gzip',['-k', '9', output]); + final ProcessResult resultGzip = await Process.run('du', ['-k', output + '.gz']); + metrics['${metric}_dart2js_size'] = _parseDu(result.stdout); + metrics['${metric}_dart2js_size_gzip'] = _parseDu(resultGzip.stdout); + } + + static int _parseDu(String source) { + return int.parse(source.split(RegExp(r'\s+')).first.trim()); + } +} + /// Measures how long it takes to compile a Flutter app and how big the compiled /// code is. class CompileTest { diff --git a/dev/devicelab/manifest.yaml b/dev/devicelab/manifest.yaml index 02b57cefa1..4a7a6469b1 100644 --- a/dev/devicelab/manifest.yaml +++ b/dev/devicelab/manifest.yaml @@ -338,6 +338,14 @@ tasks: required_agent_capabilities: ["linux/android"] flaky: true + web_size__compile_test: + description: > + Measures the size of a dart2js bundle. + stage: devicelab + required_agent_capabilities: ["linux/android"] + flaky: true + + # iOS on-device tests flavors_test_ios: diff --git a/packages/flutter_tools/lib/src/web/compile.dart b/packages/flutter_tools/lib/src/web/compile.dart index b814d40779..e9874989d4 100644 --- a/packages/flutter_tools/lib/src/web/compile.dart +++ b/packages/flutter_tools/lib/src/web/compile.dart @@ -58,6 +58,7 @@ class WebCompiler { target, '-o', '$outputPath', + '-O4', '--libraries-spec=$librariesPath', ]; if (minify) { diff --git a/packages/flutter_tools/test/web/compile_test.dart b/packages/flutter_tools/test/web/compile_test.dart index edcb080b74..425c79d754 100644 --- a/packages/flutter_tools/test/web/compile_test.dart +++ b/packages/flutter_tools/test/web/compile_test.dart @@ -38,6 +38,7 @@ void main() { 'lib/main.dart', '-o', 'build/web/main.dart.js', + '-O4', '--libraries-spec=bin/cache/flutter_web_sdk/libraries.json', '-m', ])).called(1);