Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
review nit
  • Loading branch information
kevmoo committed Dec 6, 2023
commit 3684f60070d6cc7fc811bbb687e58c3c95e67731
58 changes: 28 additions & 30 deletions packages/web_benchmarks/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:convert' show json;
import 'dart:js_interop';
import 'dart:math' as math;

import 'package:web/helpers.dart' as html;
import 'package:web/helpers.dart';

import 'src/common.dart';
import 'src/recorder.dart';
Expand Down Expand Up @@ -50,7 +50,7 @@ Future<void> runBenchmarks(

await _runBenchmark(nextBenchmark);

final Uri currentUri = Uri.parse(html.window.location.href);
final Uri currentUri = Uri.parse(window.location.href);
// Create a new URI with the current 'page' value set to [initialPage] to
// ensure the benchmark app is reloaded at the proper location.
final Uri newUri = Uri(
Expand All @@ -61,7 +61,7 @@ Future<void> runBenchmarks(
);

// Reloading the window will trigger the next benchmark to run.
html.window.location.replace(newUri.toString());
window.location.replace(newUri.toString());
}

Future<void> _runBenchmark(String? benchmarkName) async {
Expand Down Expand Up @@ -119,7 +119,7 @@ Future<void> _runBenchmark(String? benchmarkName) async {
}

void _fallbackToManual(String error) {
html.document.body!.appendHtml('''
document.body!.appendHtml('''
<div id="manual-panel">
<h3>$error</h3>

Expand All @@ -134,12 +134,11 @@ void _fallbackToManual(String error) {

for (final String benchmarkName in _benchmarks.keys) {
// Find the button elements added above.
final html.Element button = html.document.querySelector('#$benchmarkName')!;
final Element button = document.querySelector('#$benchmarkName')!;
button.addEventListener(
'click',
(JSAny? arg) {
final html.Element? manualPanel =
html.document.querySelector('#manual-panel');
final Element? manualPanel = document.querySelector('#manual-panel');
manualPanel?.remove();
_runBenchmark(benchmarkName);
}.toJS);
Expand All @@ -148,7 +147,7 @@ void _fallbackToManual(String error) {

/// Visualizes results on the Web page for manual inspection.
void _printResultsToScreen(Profile profile) {
final html.HTMLBodyElement body = html.document.body! as html.HTMLBodyElement;
final HTMLBodyElement body = document.body! as HTMLBodyElement;

body.innerHTML = '<h2>${profile.name}</h2>';

Expand All @@ -157,7 +156,7 @@ void _printResultsToScreen(Profile profile) {
body.appendHtml('<pre>${timeseries.computeStats()}</pre>');
// TODO(kevmoo): remove `NodeClue` cast when we no longer need to support
// pkg:web 0.3.0
html.NodeGlue(body).append(TimeseriesVisualization(timeseries).render());
NodeGlue(body).append(TimeseriesVisualization(timeseries).render());
});
}

Expand All @@ -166,10 +165,10 @@ class TimeseriesVisualization {
/// Creates a visualization for a [Timeseries].
TimeseriesVisualization(this._timeseries) {
_stats = _timeseries.computeStats();
_canvas = html.CanvasElement();
_screenWidth = html.window.screen.width;
_canvas = CanvasElement();
_screenWidth = window.screen.width;
_canvas.width = _screenWidth;
_canvas.height = (_kCanvasHeight * html.window.devicePixelRatio).round();
_canvas.height = (_kCanvasHeight * window.devicePixelRatio).round();
_canvas.style
..width = '100%'
..height = '${_kCanvasHeight}px'
Expand All @@ -190,8 +189,8 @@ class TimeseriesVisualization {

final Timeseries _timeseries;
late TimeseriesStats _stats;
late html.CanvasElement _canvas;
late html.CanvasRenderingContext2D _ctx;
late CanvasElement _canvas;
late CanvasRenderingContext2D _ctx;
late int _screenWidth;

// Used to normalize benchmark values to chart height.
Expand All @@ -213,9 +212,9 @@ class TimeseriesVisualization {
}

/// Renders the timeseries into a `<canvas>` and returns the canvas element.
html.CanvasElement render() {
_ctx.translate(0, _kCanvasHeight * html.window.devicePixelRatio);
_ctx.scale(1, -html.window.devicePixelRatio);
CanvasElement render() {
_ctx.translate(0, _kCanvasHeight * window.devicePixelRatio);
_ctx.scale(1, -window.devicePixelRatio);

final double barWidth = _screenWidth / _stats.samples.length;
double xOffset = 0;
Expand Down Expand Up @@ -287,7 +286,7 @@ class LocalBenchmarkServerClient {
/// Returns [kManualFallback] if local server is not available (uses 404 as a
/// signal).
Future<String> requestNextBenchmark() async {
final html.XMLHttpRequest request = await _requestXhr(
final XMLHttpRequest request = await _requestXhr(
'/next-benchmark',
method: 'POST',
mimeType: 'application/json',
Expand Down Expand Up @@ -318,7 +317,7 @@ class LocalBenchmarkServerClient {
/// DevTools Protocol.
Future<void> startPerformanceTracing(String? benchmarkName) async {
_checkNotManualMode();
await html.HttpRequest.request(
await HttpRequest.request(
'/start-performance-tracing?label=$benchmarkName',
method: 'POST',
mimeType: 'application/json',
Expand All @@ -328,7 +327,7 @@ class LocalBenchmarkServerClient {
/// Stops the performance tracing session started by [startPerformanceTracing].
Future<void> stopPerformanceTracing() async {
_checkNotManualMode();
await html.HttpRequest.request(
await HttpRequest.request(
'/stop-performance-tracing',
method: 'POST',
mimeType: 'application/json',
Expand All @@ -339,7 +338,7 @@ class LocalBenchmarkServerClient {
/// server.
Future<void> sendProfileData(Profile profile) async {
_checkNotManualMode();
final html.XMLHttpRequest request = await _requestXhr(
final XMLHttpRequest request = await _requestXhr(
'/profile-data',
method: 'POST',
mimeType: 'application/json',
Expand All @@ -356,7 +355,7 @@ class LocalBenchmarkServerClient {
/// The server will halt the devicelab task and log the error.
Future<void> reportError(dynamic error, StackTrace stackTrace) async {
_checkNotManualMode();
await html.HttpRequest.request(
await HttpRequest.request(
'/on-error',
method: 'POST',
mimeType: 'application/json',
Expand All @@ -370,28 +369,27 @@ class LocalBenchmarkServerClient {
/// Reports a message about the demo to the benchmark server.
Future<void> printToConsole(String report) async {
_checkNotManualMode();
await html.HttpRequest.request(
await HttpRequest.request(
'/print-to-console',
method: 'POST',
mimeType: 'text/plain',
sendData: report,
);
}

/// This is the same as calling [html.HttpRequest.request] but it doesn't
/// This is the same as calling [XMLHttpRequest.request] but it doesn't
/// crash on 404, which we use to detect `flutter run`.
Future<html.XMLHttpRequest> _requestXhr(
Future<XMLHttpRequest> _requestXhr(
String url, {
required String method,
required String mimeType,
required String sendData,
}) {
final Completer<html.XMLHttpRequest> completer =
Completer<html.XMLHttpRequest>();
final html.XMLHttpRequest xhr = html.XMLHttpRequest();
final Completer<XMLHttpRequest> completer = Completer<XMLHttpRequest>();
final XMLHttpRequest xhr = XMLHttpRequest();
xhr.open(method, url, true);
xhr.overrideMimeType(mimeType);
xhr.onLoad.listen((html.ProgressEvent e) {
xhr.onLoad.listen((ProgressEvent e) {
completer.complete(xhr);
});
xhr.onError.listen(completer.completeError);
Expand All @@ -400,6 +398,6 @@ class LocalBenchmarkServerClient {
}
}

extension on html.HTMLElement {
extension on HTMLElement {
void appendHtml(String input) => insertAdjacentHTML('beforeend', input);
}