Skip to content

Commit 935b92f

Browse files
Add parse constructors for the BenchmarkResults and BenchmarkScore classes (flutter#5614)
1 parent b7bf037 commit 935b92f

4 files changed

Lines changed: 89 additions & 15 deletions

File tree

packages/web_benchmarks/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.0.1
2+
3+
* Adds `parse` constructors for the `BenchmarkResults` and `BenchmarkScore` classes.
4+
15
## 1.0.0
26

37
* **Breaking change:** replace the `useCanvasKit` parameter in the `serveWebBenchmark`

packages/web_benchmarks/lib/src/benchmark_result.dart

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ class BenchmarkScore {
1212
required this.value,
1313
});
1414

15+
/// Deserializes a JSON object to create a [BenchmarkScore] object.
16+
factory BenchmarkScore.parse(Map<String, Object?> json) {
17+
final String metric = json[_metricKey]! as String;
18+
final double value = (json[_valueKey]! as num).toDouble();
19+
return BenchmarkScore(metric: metric, value: value);
20+
}
21+
22+
static const String _metricKey = 'metric';
23+
static const String _valueKey = 'value';
24+
1525
/// The name of the metric that this score is categorized under.
1626
///
1727
/// Scores collected over time under the same name can be visualized as a
@@ -22,10 +32,10 @@ class BenchmarkScore {
2232
final num value;
2333

2434
/// Serializes the benchmark metric to a JSON object.
25-
Map<String, dynamic> toJson() {
26-
return <String, dynamic>{
27-
'metric': metric,
28-
'value': value,
35+
Map<String, Object?> toJson() {
36+
return <String, Object?>{
37+
_metricKey: metric,
38+
_valueKey: value,
2939
};
3040
}
3141
}
@@ -35,22 +45,30 @@ class BenchmarkResults {
3545
/// Constructs a result containing scores from a single run benchmark run.
3646
BenchmarkResults(this.scores);
3747

48+
/// Deserializes a JSON object to create a [BenchmarkResults] object.
49+
factory BenchmarkResults.parse(Map<String, Object?> json) {
50+
final Map<String, List<BenchmarkScore>> results =
51+
<String, List<BenchmarkScore>>{};
52+
for (final String key in json.keys) {
53+
final List<BenchmarkScore> scores = (json[key]! as List<Object?>)
54+
.cast<Map<String, Object?>>()
55+
.map(BenchmarkScore.parse)
56+
.toList();
57+
results[key] = scores;
58+
}
59+
return BenchmarkResults(results);
60+
}
61+
3862
/// Scores collected in a benchmark run.
3963
final Map<String, List<BenchmarkScore>> scores;
4064

4165
/// Serializes benchmark metrics to JSON.
42-
Map<String, List<Map<String, dynamic>>> toJson() {
43-
return scores.map<String, List<Map<String, dynamic>>>(
66+
Map<String, List<Map<String, Object?>>> toJson() {
67+
return scores.map<String, List<Map<String, Object?>>>(
4468
(String benchmarkName, List<BenchmarkScore> scores) {
45-
return MapEntry<String, List<Map<String, dynamic>>>(
69+
return MapEntry<String, List<Map<String, Object?>>>(
4670
benchmarkName,
47-
scores
48-
.map<Map<String, dynamic>>(
49-
(BenchmarkScore score) => <String, dynamic>{
50-
'metric': score.metric,
51-
'value': score.value,
52-
})
53-
.toList(),
71+
scores.map((BenchmarkScore score) => score.toJson()).toList(),
5472
);
5573
});
5674
}

packages/web_benchmarks/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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: 1.0.0
5+
version: 1.0.1
66

77
environment:
88
sdk: ">=3.2.0 <4.0.0"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:web_benchmarks/server.dart';
7+
8+
void main() {
9+
group('can serialize and deserialize', () {
10+
test('$BenchmarkResults', () {
11+
final Map<String, Object?> data = <String, Object?>{
12+
'foo': <Map<String, Object?>>[
13+
<String, Object?>{'metric': 'foo.bar', 'value': 12.34},
14+
<String, Object?>{'metric': 'foo.baz', 'value': 10},
15+
],
16+
'bar': <Map<String, Object?>>[
17+
<String, Object?>{'metric': 'bar.foo', 'value': 1.23},
18+
]
19+
};
20+
21+
final BenchmarkResults benchmarkResults = BenchmarkResults.parse(data);
22+
expect(benchmarkResults.scores.length, 2);
23+
final List<BenchmarkScore> fooBenchmarks =
24+
benchmarkResults.scores['foo']!;
25+
final List<BenchmarkScore> barBenchmarks =
26+
benchmarkResults.scores['bar']!;
27+
expect(fooBenchmarks.length, 2);
28+
expect(fooBenchmarks[0].metric, 'foo.bar');
29+
expect(fooBenchmarks[0].value, 12.34);
30+
expect(fooBenchmarks[1].metric, 'foo.baz');
31+
expect(fooBenchmarks[1].value, 10);
32+
expect(barBenchmarks.length, 1);
33+
expect(barBenchmarks[0].metric, 'bar.foo');
34+
expect(barBenchmarks[0].value, 1.23);
35+
36+
expect(benchmarkResults.toJson(), data);
37+
});
38+
39+
test('$BenchmarkScore', () {
40+
final Map<String, Object?> data = <String, Object?>{
41+
'metric': 'foo',
42+
'value': 1.234
43+
};
44+
45+
final BenchmarkScore score = BenchmarkScore.parse(data);
46+
expect(score.metric, 'foo');
47+
expect(score.value, 1.234);
48+
49+
expect(score.toJson(), data);
50+
});
51+
});
52+
}

0 commit comments

Comments
 (0)