Skip to content

Commit

Permalink
[webview_benchmarks] Migrate to null safety (flutter#879)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanv8060 authored Aug 24, 2022
1 parent f25a05a commit 2e17cff
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 207 deletions.
6 changes: 6 additions & 0 deletions packages/web_benchmarks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.1.0

* Migrates to null safety.
* **BREAKING CHANGES**:
* Required parameters are non-nullable.

## 0.0.7+1

* Updates text theme parameters to avoid deprecation issues.
Expand Down
52 changes: 26 additions & 26 deletions packages/web_benchmarks/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import 'dart:convert' show json;
import 'dart:html' as html;
import 'dart:math' as math;

import 'package:meta/meta.dart';

import 'src/common.dart';
import 'src/recorder.dart';
export 'src/recorder.dart';
Expand All @@ -20,7 +18,7 @@ typedef RecorderFactory = Recorder Function();
///
/// When adding a new benchmark, add it to this map. Make sure that the name
/// of your benchmark is unique.
Map<String, RecorderFactory> _benchmarks;
late Map<String, RecorderFactory> _benchmarks;

final LocalBenchmarkServerClient _client = LocalBenchmarkServerClient();

Expand Down Expand Up @@ -50,8 +48,8 @@ Future<void> runBenchmarks(Map<String, RecorderFactory> benchmarks) async {
html.window.location.reload();
}

Future<void> _runBenchmark(String benchmarkName) async {
final RecorderFactory recorderFactory = _benchmarks[benchmarkName];
Future<void> _runBenchmark(String? benchmarkName) async {
final RecorderFactory? recorderFactory = _benchmarks[benchmarkName];

if (recorderFactory == null) {
_fallbackToManual('Benchmark $benchmarkName not found.');
Expand Down Expand Up @@ -105,7 +103,7 @@ Future<void> _runBenchmark(String benchmarkName) async {
}

void _fallbackToManual(String error) {
html.document.body.appendHtml('''
html.document.body!.appendHtml('''
<div id="manual-panel">
<h3>$error</h3>
Expand All @@ -122,9 +120,10 @@ void _fallbackToManual(String error) {
..allowInlineStyles());

for (final String benchmarkName in _benchmarks.keys) {
final html.Element button = html.document.querySelector('#$benchmarkName');
// Find the button elements added above.
final html.Element button = html.document.querySelector('#$benchmarkName')!;
button.addEventListener('click', (_) {
final html.Element manualPanel =
final html.Element? manualPanel =
html.document.querySelector('#manual-panel');
manualPanel?.remove();
_runBenchmark(benchmarkName);
Expand All @@ -134,12 +133,14 @@ void _fallbackToManual(String error) {

/// Visualizes results on the Web page for manual inspection.
void _printResultsToScreen(Profile profile) {
html.document.body.innerHtml = '<h2>${profile.name}</h2>';
final html.BodyElement _body = html.document.body!;

_body.innerHtml = '<h2>${profile.name}</h2>';

profile.scoreData.forEach((String scoreKey, Timeseries timeseries) {
html.document.body.appendHtml('<h2>$scoreKey</h2>');
html.document.body.appendHtml('<pre>${timeseries.computeStats()}</pre>');
html.document.body.append(TimeseriesVisualization(timeseries).render());
_body.appendHtml('<h2>$scoreKey</h2>');
_body.appendHtml('<pre>${timeseries.computeStats()}</pre>');
_body.append(TimeseriesVisualization(timeseries).render());
});
}

Expand All @@ -149,7 +150,7 @@ class TimeseriesVisualization {
TimeseriesVisualization(this._timeseries) {
_stats = _timeseries.computeStats();
_canvas = html.CanvasElement();
_screenWidth = html.window.screen.width;
_screenWidth = html.window.screen!.width!;
_canvas.width = _screenWidth;
_canvas.height = (_kCanvasHeight * html.window.devicePixelRatio).round();
_canvas.style
Expand All @@ -171,13 +172,13 @@ class TimeseriesVisualization {
static const double _kCanvasHeight = 200;

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

// Used to normalize benchmark values to chart height.
double _maxValueChartRange;
late double _maxValueChartRange;

/// Converts a sample value to vertical canvas coordinates.
///
Expand Down Expand Up @@ -205,7 +206,7 @@ class TimeseriesVisualization {
final AnnotatedSample sample = _stats.samples[i];

if (sample.isWarmUpValue) {
// Put gray background behing warm-up samples.
// Put gray background behind warm-up samples.
_ctx.fillStyle = 'rgba(200,200,200,1)';
_ctx.fillRect(xOffset, 0, barWidth, _normalized(_maxValueChartRange));
}
Expand Down Expand Up @@ -262,7 +263,7 @@ class LocalBenchmarkServerClient {
/// This happens when you run benchmarks using plain `flutter run` rather than
/// devicelab test harness. The test harness spins up a special server that
/// provides API for automatically picking the next benchmark to run.
bool isInManualMode;
late bool isInManualMode;

/// Asks the local server for the name of the next benchmark to run.
///
Expand All @@ -284,7 +285,7 @@ class LocalBenchmarkServerClient {
}

isInManualMode = false;
return request.responseText;
return request.responseText ?? kManualFallback;
}

void _checkNotManualMode() {
Expand All @@ -298,7 +299,7 @@ class LocalBenchmarkServerClient {
/// This uses the chrome://tracing tracer, which is not available from within
/// the page itself, and therefore must be controlled from outside using the
/// DevTools Protocol.
Future<void> startPerformanceTracing(String benchmarkName) async {
Future<void> startPerformanceTracing(String? benchmarkName) async {
_checkNotManualMode();
await html.HttpRequest.request(
'/start-performance-tracing?label=$benchmarkName',
Expand Down Expand Up @@ -364,13 +365,12 @@ class LocalBenchmarkServerClient {
/// crash on 404, which we use to detect `flutter run`.
Future<html.HttpRequest> _requestXhr(
String url, {
@required String method,
@required String mimeType,
@required dynamic sendData,
required String method,
required String mimeType,
required dynamic sendData,
}) {
final Completer<html.HttpRequest> completer = Completer<html.HttpRequest>();
final html.HttpRequest xhr = html.HttpRequest();
method ??= 'GET';
xhr.open(method, url, async: true);
xhr.overrideMimeType(mimeType);
xhr.onLoad.listen((html.ProgressEvent e) {
Expand Down
7 changes: 3 additions & 4 deletions packages/web_benchmarks/lib/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'dart:async';
import 'dart:io' as io;

import 'package:logging/logging.dart';
import 'package:meta/meta.dart';

import 'src/benchmark_result.dart';
import 'src/runner.dart';
Expand Down Expand Up @@ -41,9 +40,9 @@ const int defaultChromeDebugPort = 10000;
/// If [headless] is true, runs Chrome without UI. In particular, this is
/// useful in environments (e.g. CI) that doesn't have a display.
Future<BenchmarkResults> serveWebBenchmark({
@required io.Directory benchmarkAppDirectory,
@required String entryPoint,
@required bool useCanvasKit,
required io.Directory benchmarkAppDirectory,
required String entryPoint,
required bool useCanvasKit,
int benchmarkServerPort = defaultBenchmarkServerPort,
int chromeDebugPort = defaultChromeDebugPort,
bool headless = true,
Expand Down
6 changes: 2 additions & 4 deletions packages/web_benchmarks/lib/src/benchmark_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:meta/meta.dart';

/// A single benchmark score value collected from the benchmark.
class BenchmarkScore {
/// Creates a benchmark score.
///
/// [metric] and [value] must not be null.
BenchmarkScore({
@required this.metric,
@required this.value,
required this.metric,
required this.value,
}) : assert(metric != null && value != null);

/// The name of the metric that this score is categorized under.
Expand Down
Loading

0 comments on commit 2e17cff

Please sign in to comment.