Skip to content

Commit 9a55d4c

Browse files
Fix benchmark reload bug and migrate away from deprecated js_util APIs (#5577)
This PR: - Ensures the benchmark client reloads with the proper `initialPage`. Without this change, the benchmark app may not reload properly, which causes a hang on the server. - Migrates away from `js_util` APIs to `dart:ui_web`. This was causing spurious deprecation warnings. The changes in this PR are test covered by existing tests.
1 parent afd4517 commit 9a55d4c

File tree

8 files changed

+44
-23
lines changed

8 files changed

+44
-23
lines changed

packages/web_benchmarks/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.1.0+10
2+
3+
* Ensure the benchmark client reloads with the proper `initialPage`.
4+
* Migrates benchmark recorder away from deprecated `js_util` APIs.
5+
16
## 0.1.0+9
27

38
* Updates minimum supported SDK version to Flutter 3.10/Dart 3.0.

packages/web_benchmarks/lib/client.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ final LocalBenchmarkServerClient _client = LocalBenchmarkServerClient();
2929
///
3030
/// When used without a server, prompts the user to select a benchmark to
3131
/// run next.
32-
Future<void> runBenchmarks(Map<String, RecorderFactory> benchmarks) async {
32+
Future<void> runBenchmarks(
33+
Map<String, RecorderFactory> benchmarks, {
34+
String initialPage = defaultInitialPage,
35+
}) async {
3336
// Set local benchmarks.
3437
_benchmarks = benchmarks;
3538

@@ -43,7 +46,19 @@ Future<void> runBenchmarks(Map<String, RecorderFactory> benchmarks) async {
4346
}
4447

4548
await _runBenchmark(nextBenchmark);
46-
html.window.location.reload();
49+
50+
final Uri currentUri = Uri.parse(html.window.location.href);
51+
// Create a new URI with the current 'page' value set to [initialPage] to
52+
// ensure the benchmark app is reloaded at the proper location.
53+
final Uri newUri = Uri(
54+
scheme: currentUri.scheme,
55+
host: currentUri.host,
56+
port: currentUri.port,
57+
path: initialPage,
58+
);
59+
60+
// Reloading the window will trigger the next benchmark to run.
61+
html.window.location.replace(newUri.toString());
4762
}
4863

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

packages/web_benchmarks/lib/server.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:io' as io;
88
import 'package:logging/logging.dart';
99

1010
import 'src/benchmark_result.dart';
11+
import 'src/common.dart';
1112
import 'src/runner.dart';
1213

1314
export 'src/benchmark_result.dart';
@@ -47,7 +48,7 @@ Future<BenchmarkResults> serveWebBenchmark({
4748
int chromeDebugPort = defaultChromeDebugPort,
4849
bool headless = true,
4950
bool treeShakeIcons = true,
50-
String initialPage = BenchmarkServer.defaultInitialPage,
51+
String initialPage = defaultInitialPage,
5152
}) async {
5253
// Reduce logging level. Otherwise, package:webkit_inspection_protocol is way too spammy.
5354
Logger.root.level = Level.INFO;

packages/web_benchmarks/lib/src/common.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ const int kMeasuredSampleCount = 100;
1414
/// A special value returned by the `/next-benchmark` HTTP POST request when
1515
/// all benchmarks have run and there are no more benchmarks to run.
1616
const String kEndOfBenchmarks = '__end_of_benchmarks__';
17+
18+
/// The default initial page to load upon opening the benchmark app or reloading
19+
/// it in Chrome.
20+
const String defaultInitialPage = 'index.html';

packages/web_benchmarks/lib/src/recorder.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
import 'dart:async';
66
import 'dart:html' as html;
7-
import 'dart:js';
8-
import 'dart:js_util' as js_util;
97
import 'dart:math' as math;
108
import 'dart:ui';
9+
import 'dart:ui_web' as ui_web;
1110

1211
import 'package:flutter/foundation.dart';
1312
import 'package:flutter/gestures.dart';
@@ -1211,19 +1210,21 @@ final Map<String, EngineBenchmarkValueListener> _engineBenchmarkListeners =
12111210
///
12121211
/// If another listener is already registered, overrides it.
12131212
void registerEngineBenchmarkValueListener(
1214-
String name, EngineBenchmarkValueListener listener) {
1213+
String name,
1214+
EngineBenchmarkValueListener listener,
1215+
) {
12151216
if (_engineBenchmarkListeners.containsKey(name)) {
1216-
throw StateError('A listener for "$name" is already registered.\n'
1217-
'Call `stopListeningToEngineBenchmarkValues` to unregister the previous '
1218-
'listener before registering a new one.');
1217+
throw StateError(
1218+
'A listener for "$name" is already registered.\n'
1219+
'Call `stopListeningToEngineBenchmarkValues` to unregister the previous '
1220+
'listener before registering a new one.',
1221+
);
12191222
}
12201223

12211224
if (_engineBenchmarkListeners.isEmpty) {
12221225
// The first listener is being registered. Register the global listener.
1223-
js_util.setProperty(html.window, '_flutter_internal_on_benchmark',
1224-
allowInterop(_dispatchEngineBenchmarkValue));
1226+
ui_web.benchmarkValueCallback = _dispatchEngineBenchmarkValue;
12251227
}
1226-
12271228
_engineBenchmarkListeners[name] = listener;
12281229
}
12291230

@@ -1232,7 +1233,7 @@ void stopListeningToEngineBenchmarkValues(String name) {
12321233
_engineBenchmarkListeners.remove(name);
12331234
if (_engineBenchmarkListeners.isEmpty) {
12341235
// The last listener unregistered. Remove the global listener.
1235-
js_util.setProperty(html.window, '_flutter_internal_on_benchmark', null);
1236+
ui_web.benchmarkValueCallback = null;
12361237
}
12371238
}
12381239

packages/web_benchmarks/lib/src/runner.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ class BenchmarkServer {
5858
this.initialPage = defaultInitialPage,
5959
});
6060

61-
/// The default initial page to load upon opening the benchmark app in Chrome.
62-
///
63-
/// This value will be used by default if no value is passed to [initialPage].
64-
static const String defaultInitialPage = 'index.html';
65-
6661
final ProcessManager _processManager = const LocalProcessManager();
6762

6863
/// The directory containing the app that's being benchmarked.

packages/web_benchmarks/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ 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+9
5+
version: 0.1.0+10
66

77
environment:
8-
sdk: ">=3.0.0 <4.0.0"
9-
flutter: ">=3.10.0"
8+
sdk: ">=3.2.0 <4.0.0"
9+
flutter: ">=3.16.0"
1010

1111
dependencies:
1212
flutter:

packages/web_benchmarks/testing/web_benchmarks_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'dart:io';
88
import 'package:test/test.dart';
99

1010
import 'package:web_benchmarks/server.dart';
11-
import 'package:web_benchmarks/src/runner.dart';
11+
import 'package:web_benchmarks/src/common.dart';
1212

1313
Future<void> main() async {
1414
test('Can run a web benchmark', () async {
@@ -30,7 +30,7 @@ Future<void> main() async {
3030
Future<void> _runBenchmarks({
3131
required List<String> benchmarkNames,
3232
required String entryPoint,
33-
String initialPage = BenchmarkServer.defaultInitialPage,
33+
String initialPage = defaultInitialPage,
3434
}) async {
3535
final BenchmarkResults taskResult = await serveWebBenchmark(
3636
benchmarkAppDirectory: Directory('testing/test_app'),

0 commit comments

Comments
 (0)