Skip to content

Commit 4fea3ef

Browse files
authored
Migrate benchmarks to package:web (#126848)
1 parent 2d6c67f commit 4fea3ef

File tree

6 files changed

+125
-89
lines changed

6 files changed

+125
-89
lines changed

dev/benchmarks/macrobenchmarks/lib/src/web/bench_image_decoding.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:html' as html;
5+
import 'dart:js_interop';
66
import 'dart:typed_data';
77
import 'dart:ui' as ui;
88

9+
import 'package:web/web.dart' as web;
10+
911
import 'recorder.dart';
1012

1113
// Measures the performance of image decoding.
@@ -43,8 +45,11 @@ class BenchImageDecoding extends RawRecorder {
4345
return;
4446
}
4547
for (final String imageUrl in _imageUrls) {
46-
final html.Body image = await html.window.fetch(imageUrl) as html.Body;
47-
_imageData.add((await image.arrayBuffer() as ByteBuffer).asUint8List());
48+
final Future<JSAny?> fetchFuture = web.window.fetch(imageUrl.toJS).toDart;
49+
final web.Body image = (await fetchFuture)! as web.Body;
50+
final Future<JSAny?> imageFuture = image.arrayBuffer().toDart;
51+
final JSArrayBuffer imageBuffer = (await imageFuture)! as JSArrayBuffer;
52+
_imageData.add(imageBuffer.toDart.asUint8List());
4853
}
4954
}
5055

dev/benchmarks/macrobenchmarks/lib/src/web/bench_platform_view_infinite_scroll.dart

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,32 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:html' as html;
6+
import 'dart:js_interop';
77

88
import 'package:flutter/material.dart';
9+
import 'package:web/web.dart' as web;
910

1011
// TODO(mdebbar): flutter/flutter#55000 Remove this conditional import once
1112
// web-only dart:ui_web APIs are exposed from a dedicated place.
1213
import 'platform_views/non_web.dart'
13-
if (dart.library.html) 'platform_views/web.dart';
14+
if (dart.library.js_interop) 'platform_views/web.dart';
1415
import 'recorder.dart';
1516

1617
const String benchmarkViewType = 'benchmark_element';
1718

1819
void _registerFactory() {
1920
platformViewRegistry.registerViewFactory(benchmarkViewType, (int viewId) {
20-
final html.Element htmlElement = html.DivElement();
21-
htmlElement.id = '${benchmarkViewType}_$viewId';
22-
htmlElement.innerText = 'Google';
21+
final web.HTMLElement htmlElement = web.document.createElement('div'.toJS)
22+
as web.HTMLDivElement;
23+
htmlElement.id = '${benchmarkViewType}_$viewId'.toJS;
24+
htmlElement.innerText = 'Google'.toJS;
2325
htmlElement.style
24-
..width = '100%'
25-
..height = '100%'
26-
..color = 'black'
27-
..backgroundColor = 'rgba(0, 255, 0, .5)'
28-
..textAlign = 'center'
29-
..border = '1px solid black';
26+
..setProperty('width'.toJS, '100%'.toJS)
27+
..setProperty('height'.toJS, '100%'.toJS)
28+
..setProperty('color'.toJS, 'black'.toJS)
29+
..setProperty('backgroundColor'.toJS, 'rgba(0, 255, 0, .5)'.toJS)
30+
..setProperty('textAlign'.toJS, 'center'.toJS)
31+
..setProperty('border'.toJS, '1px solid black'.toJS);
3032
return htmlElement;
3133
});
3234
}

dev/benchmarks/macrobenchmarks/lib/src/web/recorder.dart

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:html' as html;
7-
import 'dart:js_util' as js_util;
6+
import 'dart:js_interop';
7+
// The analyzer currently thinks `js_interop_unsafe` is unused, but it is used
8+
// for `JSObject.[]=`.
9+
// ignore: unused_import
10+
import 'dart:js_interop_unsafe';
811
import 'dart:math' as math;
912
import 'dart:ui';
1013

@@ -15,6 +18,7 @@ import 'package:flutter/scheduler.dart';
1518
import 'package:flutter/services.dart';
1619
import 'package:flutter/widgets.dart';
1720
import 'package:meta/meta.dart';
21+
import 'package:web/web.dart' as web;
1822

1923
/// The default number of samples from warm-up iterations.
2024
///
@@ -1253,7 +1257,7 @@ void startMeasureFrame(Profile profile) {
12531257

12541258
if (!profile.isWarmingUp) {
12551259
// Tell the browser to mark the beginning of the frame.
1256-
html.window.performance.mark('measured_frame_start#$_currentFrameNumber');
1260+
web.window.performance.mark('measured_frame_start#$_currentFrameNumber'.toJS);
12571261

12581262
_isMeasuringFrame = true;
12591263
}
@@ -1276,11 +1280,11 @@ void endMeasureFrame() {
12761280

12771281
if (_isMeasuringFrame) {
12781282
// Tell the browser to mark the end of the frame, and measure the duration.
1279-
html.window.performance.mark('measured_frame_end#$_currentFrameNumber');
1280-
html.window.performance.measure(
1281-
'measured_frame',
1282-
'measured_frame_start#$_currentFrameNumber',
1283-
'measured_frame_end#$_currentFrameNumber',
1283+
web.window.performance.mark('measured_frame_end#$_currentFrameNumber'.toJS);
1284+
web.window.performance.measure(
1285+
'measured_frame'.toJS,
1286+
'measured_frame_start#$_currentFrameNumber'.toJS,
1287+
'measured_frame_end#$_currentFrameNumber'.toJS,
12841288
);
12851289

12861290
// Increment the current frame number.
@@ -1310,7 +1314,10 @@ void registerEngineBenchmarkValueListener(String name, EngineBenchmarkValueListe
13101314

13111315
if (_engineBenchmarkListeners.isEmpty) {
13121316
// The first listener is being registered. Register the global listener.
1313-
js_util.setProperty(html.window, '_flutter_internal_on_benchmark', _dispatchEngineBenchmarkValue);
1317+
web.window['_flutter_internal_on_benchmark'.toJS] =
1318+
// Upcast to [Object] to export.
1319+
// ignore: unnecessary_cast
1320+
(_dispatchEngineBenchmarkValue as Object).toJS;
13141321
}
13151322

13161323
_engineBenchmarkListeners[name] = listener;
@@ -1321,7 +1328,7 @@ void stopListeningToEngineBenchmarkValues(String name) {
13211328
_engineBenchmarkListeners.remove(name);
13221329
if (_engineBenchmarkListeners.isEmpty) {
13231330
// The last listener unregistered. Remove the global listener.
1324-
js_util.setProperty(html.window, '_flutter_internal_on_benchmark', null);
1331+
web.window['_flutter_internal_on_benchmark'.toJS] = null;
13251332
}
13261333
}
13271334

0 commit comments

Comments
 (0)