Skip to content

Commit 671bd3f

Browse files
Add the ability to run web_benchmarks with Wasm (flutter#5611)
This uses the same run flags that the flutter benchmark tests use: https://github.com/flutter/flutter/blob/master/dev/devicelab/lib/tasks/web_benchmarks.dart#L36-L40 CC @kevmoo
1 parent 17108a8 commit 671bd3f

File tree

7 files changed

+86
-11
lines changed

7 files changed

+86
-11
lines changed

packages/web_benchmarks/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 1.0.0
2+
3+
* **Breaking change:** replace the `useCanvasKit` parameter in the `serveWebBenchmark`
4+
method with a new parameter `compilationOptions`, which allows you to:
5+
* specify the web renderer to use for the benchmark app (html, canvaskit, or skwasm)
6+
* specify whether to use WebAssembly to build the benchmark app
7+
* **Breaking change:** `serveWebBenchmark` now uses `canvaskit` instead of `html` as the
8+
default web renderer.
9+
110
## 0.1.0+11
211

312
* Migrates benchmark recorder from `dart:html` to `package:web` to support WebAssembly.

packages/web_benchmarks/lib/client.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,18 @@ Future<void> runBenchmarks(
5353
final Uri currentUri = Uri.parse(window.location.href);
5454
// Create a new URI with the current 'page' value set to [initialPage] to
5555
// ensure the benchmark app is reloaded at the proper location.
56-
final Uri newUri = Uri(
56+
final String newUri = Uri(
5757
scheme: currentUri.scheme,
5858
host: currentUri.host,
5959
port: currentUri.port,
6060
path: initialPage,
61-
);
61+
).toString();
6262

6363
// Reloading the window will trigger the next benchmark to run.
64-
window.location.replace(newUri.toString());
64+
await _client.printToConsole(
65+
'Client preparing to reload the window to: "$newUri"',
66+
);
67+
window.location.replace(newUri);
6568
}
6669

6770
Future<void> _runBenchmark(String? benchmarkName) async {

packages/web_benchmarks/lib/server.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import 'package:logging/logging.dart';
99

1010
import 'src/benchmark_result.dart';
1111
import 'src/common.dart';
12+
import 'src/compilation_options.dart';
1213
import 'src/runner.dart';
1314

1415
export 'src/benchmark_result.dart';
16+
export 'src/compilation_options.dart';
1517

1618
/// The default port number used by the local benchmark server.
1719
const int defaultBenchmarkServerPort = 9999;
@@ -43,23 +45,23 @@ const int defaultChromeDebugPort = 10000;
4345
Future<BenchmarkResults> serveWebBenchmark({
4446
required io.Directory benchmarkAppDirectory,
4547
required String entryPoint,
46-
required bool useCanvasKit,
4748
int benchmarkServerPort = defaultBenchmarkServerPort,
4849
int chromeDebugPort = defaultChromeDebugPort,
4950
bool headless = true,
5051
bool treeShakeIcons = true,
5152
String initialPage = defaultInitialPage,
53+
CompilationOptions compilationOptions = const CompilationOptions(),
5254
}) async {
5355
// Reduce logging level. Otherwise, package:webkit_inspection_protocol is way too spammy.
5456
Logger.root.level = Level.INFO;
5557

5658
return BenchmarkServer(
5759
benchmarkAppDirectory: benchmarkAppDirectory,
5860
entryPoint: entryPoint,
59-
useCanvasKit: useCanvasKit,
6061
benchmarkServerPort: benchmarkServerPort,
6162
chromeDebugPort: chromeDebugPort,
6263
headless: headless,
64+
compilationOptions: compilationOptions,
6365
treeShakeIcons: treeShakeIcons,
6466
initialPage: initialPage,
6567
).run();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// Compilation options for bulding a Flutter web app.
6+
///
7+
/// This object holds metadata that is used to determine how the benchmark app
8+
/// should be built.
9+
class CompilationOptions {
10+
/// Creates a [CompilationOptions] object.
11+
const CompilationOptions({
12+
this.renderer = WebRenderer.canvaskit,
13+
this.useWasm = false,
14+
});
15+
16+
/// The renderer to use for the build.
17+
final WebRenderer renderer;
18+
19+
/// Whether to build the app with dart2wasm.
20+
final bool useWasm;
21+
22+
@override
23+
String toString() {
24+
return '(renderer: ${renderer.name}, compiler: ${useWasm ? 'dart2wasm' : 'dart2js'})';
25+
}
26+
}
27+
28+
/// The possible types of web renderers Flutter can build for.
29+
enum WebRenderer {
30+
/// The HTML web renderer.
31+
html,
32+
33+
/// The CanvasKit web renderer.
34+
canvaskit,
35+
36+
/// The SKIA Wasm web renderer.
37+
skwasm,
38+
}

packages/web_benchmarks/lib/src/runner.dart

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import 'package:shelf_static/shelf_static.dart';
1818
import 'benchmark_result.dart';
1919
import 'browser.dart';
2020
import 'common.dart';
21+
import 'compilation_options.dart';
2122

2223
/// The default port number used by the local benchmark server.
2324
const int defaultBenchmarkServerPort = 9999;
@@ -50,11 +51,11 @@ class BenchmarkServer {
5051
BenchmarkServer({
5152
required this.benchmarkAppDirectory,
5253
required this.entryPoint,
53-
required this.useCanvasKit,
5454
required this.benchmarkServerPort,
5555
required this.chromeDebugPort,
5656
required this.headless,
5757
required this.treeShakeIcons,
58+
this.compilationOptions = const CompilationOptions(),
5859
this.initialPage = defaultInitialPage,
5960
});
6061

@@ -72,8 +73,8 @@ class BenchmarkServer {
7273
/// the app.
7374
final String entryPoint;
7475

75-
/// Whether to build the app in CanvasKit mode.
76-
final bool useCanvasKit;
76+
/// The compilation options to use for building the benchmark web app.
77+
final CompilationOptions compilationOptions;
7778

7879
/// The port this benchmark server serves the app on.
7980
final int benchmarkServerPort;
@@ -109,13 +110,20 @@ class BenchmarkServer {
109110
"flutter executable is not runnable. Make sure it's in the PATH.");
110111
}
111112

113+
final DateTime startTime = DateTime.now();
114+
print('Building Flutter web app $compilationOptions...');
112115
final io.ProcessResult buildResult = await _processManager.run(
113116
<String>[
114117
'flutter',
115118
'build',
116119
'web',
120+
if (compilationOptions.useWasm) ...<String>[
121+
'--wasm',
122+
'--wasm-opt=debug',
123+
'--omit-type-checks',
124+
],
125+
'--web-renderer=${compilationOptions.renderer.name}',
117126
'--dart-define=FLUTTER_WEB_ENABLE_PROFILING=true',
118-
if (useCanvasKit) '--dart-define=FLUTTER_WEB_USE_SKIA=true',
119127
if (!treeShakeIcons) '--no-tree-shake-icons',
120128
'--profile',
121129
'-t',
@@ -124,6 +132,12 @@ class BenchmarkServer {
124132
workingDirectory: benchmarkAppDirectory.path,
125133
);
126134

135+
final int buildTime = Duration(
136+
milliseconds: DateTime.now().millisecondsSinceEpoch -
137+
startTime.millisecondsSinceEpoch,
138+
).inSeconds;
139+
print('Build took ${buildTime}s to complete.');
140+
127141
if (buildResult.exitCode != 0) {
128142
io.stderr.writeln(buildResult.stdout);
129143
io.stderr.writeln(buildResult.stderr);

packages/web_benchmarks/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: web_benchmarks
22
description: A benchmark harness for performance-testing Flutter apps in Chrome.
33
repository: https://github.com/flutter/packages/tree/main/packages/web_benchmarks
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+web_benchmarks%22
5-
version: 0.1.0+11
5+
version: 1.0.0
66

77
environment:
88
sdk: ">=3.2.0 <4.0.0"

packages/web_benchmarks/testing/web_benchmarks_test.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,28 @@ Future<void> main() async {
2525
initialPage: 'index.html#about',
2626
);
2727
}, timeout: Timeout.none);
28+
29+
test('Can run a web benchmark with wasm', () async {
30+
await _runBenchmarks(
31+
benchmarkNames: <String>['simple'],
32+
entryPoint: 'lib/benchmarks/runner_simple.dart',
33+
compilationOptions: const CompilationOptions(useWasm: true),
34+
);
35+
}, timeout: Timeout.none);
2836
}
2937

3038
Future<void> _runBenchmarks({
3139
required List<String> benchmarkNames,
3240
required String entryPoint,
3341
String initialPage = defaultInitialPage,
42+
CompilationOptions compilationOptions = const CompilationOptions(),
3443
}) async {
3544
final BenchmarkResults taskResult = await serveWebBenchmark(
3645
benchmarkAppDirectory: Directory('testing/test_app'),
3746
entryPoint: entryPoint,
38-
useCanvasKit: false,
3947
treeShakeIcons: false,
4048
initialPage: initialPage,
49+
compilationOptions: compilationOptions,
4150
);
4251

4352
for (final String benchmarkName in benchmarkNames) {

0 commit comments

Comments
 (0)