Add parse constructors for the BenchmarkResults and BenchmarkScore classes (#5614)

This commit is contained in:
Kenzie Davisson
2023-12-08 11:01:49 -08:00
committed by GitHub
parent b7bf0373a7
commit 935b92f537
4 changed files with 89 additions and 15 deletions

View File

@ -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`

View File

@ -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
@ -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,
};
}
}
@ -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(),
);
});
}

View File

@ -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"

View File

@ -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);
});
});
}