Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parse constructors for the BenchmarkResults and BenchmarkScore classes #5614

Merged
merged 7 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions packages/web_benchmarks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.1

* Adds `parse` constructors for the `BenchmarkResults` and `BenchmarkScore` classes.

## 1.0.0

* **Breaking change:** replace the `useCanvasKit` parameter in the `serveWebBenchmark`
Expand Down
46 changes: 32 additions & 14 deletions packages/web_benchmarks/lib/src/benchmark_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ class BenchmarkScore {
required this.value,
});

/// Deserializes a JSON object to create a [BenchmarkScore] object.
factory BenchmarkScore.parse(Map<String, Object?> json) {
final String metric = json[_metricKey]! as String;
final double value = (json[_valueKey]! as num).toDouble();
return BenchmarkScore(metric: metric, value: value);
}

static const String _metricKey = 'metric';
static const String _valueKey = 'value';

/// The name of the metric that this score is categorized under.
///
/// Scores collected over time under the same name can be visualized as a
Expand All @@ -22,10 +32,10 @@ class BenchmarkScore {
final num value;

/// Serializes the benchmark metric to a JSON object.
Map<String, dynamic> toJson() {
return <String, dynamic>{
'metric': metric,
'value': value,
Map<String, Object?> toJson() {
return <String, Object?>{
_metricKey: metric,
_valueKey: value,
};
}
}
Expand All @@ -35,22 +45,30 @@ class BenchmarkResults {
/// Constructs a result containing scores from a single run benchmark run.
BenchmarkResults(this.scores);

/// Deserializes a JSON object to create a [BenchmarkResults] object.
factory BenchmarkResults.parse(Map<String, Object?> json) {
final Map<String, List<BenchmarkScore>> results =
<String, List<BenchmarkScore>>{};
for (final String key in json.keys) {
final List<BenchmarkScore> scores = (json[key]! as List<Object?>)
.cast<Map<String, Object?>>()
.map(BenchmarkScore.parse)
.toList();
results[key] = scores;
}
return BenchmarkResults(results);
}

/// Scores collected in a benchmark run.
final Map<String, List<BenchmarkScore>> scores;

/// Serializes benchmark metrics to JSON.
Map<String, List<Map<String, dynamic>>> toJson() {
return scores.map<String, List<Map<String, dynamic>>>(
Map<String, List<Map<String, Object?>>> toJson() {
return scores.map<String, List<Map<String, Object?>>>(
(String benchmarkName, List<BenchmarkScore> scores) {
return MapEntry<String, List<Map<String, dynamic>>>(
return MapEntry<String, List<Map<String, Object?>>>(
benchmarkName,
scores
.map<Map<String, dynamic>>(
(BenchmarkScore score) => <String, dynamic>{
'metric': score.metric,
'value': score.value,
})
.toList(),
scores.map((BenchmarkScore score) => score.toJson()).toList(),
);
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/web_benchmarks/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: web_benchmarks
description: A benchmark harness for performance-testing Flutter apps in Chrome.
repository: https://github.com/flutter/packages/tree/main/packages/web_benchmarks
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+web_benchmarks%22
version: 1.0.0
version: 1.0.1

environment:
sdk: ">=3.2.0 <4.0.0"
Expand Down
52 changes: 52 additions & 0 deletions packages/web_benchmarks/test/src/benchmark_result_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:web_benchmarks/server.dart';

void main() {
group('can serialize and deserialize', () {
test('$BenchmarkResults', () {
final Map<String, Object?> data = <String, Object?>{
'foo': <Map<String, Object?>>[
<String, Object?>{'metric': 'foo.bar', 'value': 12.34},
<String, Object?>{'metric': 'foo.baz', 'value': 10},
],
'bar': <Map<String, Object?>>[
<String, Object?>{'metric': 'bar.foo', 'value': 1.23},
]
};

final BenchmarkResults benchmarkResults = BenchmarkResults.parse(data);
expect(benchmarkResults.scores.length, 2);
final List<BenchmarkScore> fooBenchmarks =
benchmarkResults.scores['foo']!;
final List<BenchmarkScore> barBenchmarks =
benchmarkResults.scores['bar']!;
expect(fooBenchmarks.length, 2);
expect(fooBenchmarks[0].metric, 'foo.bar');
expect(fooBenchmarks[0].value, 12.34);
expect(fooBenchmarks[1].metric, 'foo.baz');
expect(fooBenchmarks[1].value, 10);
expect(barBenchmarks.length, 1);
expect(barBenchmarks[0].metric, 'bar.foo');
expect(barBenchmarks[0].value, 1.23);

expect(benchmarkResults.toJson(), data);
});

test('$BenchmarkScore', () {
final Map<String, Object?> data = <String, Object?>{
'metric': 'foo',
'value': 1.234
};

final BenchmarkScore score = BenchmarkScore.parse(data);
expect(score.metric, 'foo');
expect(score.value, 1.234);

expect(score.toJson(), data);
});
});
}