mirror of
https://github.com/flutter/packages.git
synced 2025-06-29 14:18:54 +08:00
Add parse
constructors for the BenchmarkResults
and BenchmarkScore
classes (#5614)
This commit is contained in:
@ -1,3 +1,7 @@
|
|||||||
|
## 1.0.1
|
||||||
|
|
||||||
|
* Adds `parse` constructors for the `BenchmarkResults` and `BenchmarkScore` classes.
|
||||||
|
|
||||||
## 1.0.0
|
## 1.0.0
|
||||||
|
|
||||||
* **Breaking change:** replace the `useCanvasKit` parameter in the `serveWebBenchmark`
|
* **Breaking change:** replace the `useCanvasKit` parameter in the `serveWebBenchmark`
|
||||||
|
@ -12,6 +12,16 @@ class BenchmarkScore {
|
|||||||
required this.value,
|
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.
|
/// The name of the metric that this score is categorized under.
|
||||||
///
|
///
|
||||||
/// Scores collected over time under the same name can be visualized as a
|
/// Scores collected over time under the same name can be visualized as a
|
||||||
@ -22,10 +32,10 @@ class BenchmarkScore {
|
|||||||
final num value;
|
final num value;
|
||||||
|
|
||||||
/// Serializes the benchmark metric to a JSON object.
|
/// Serializes the benchmark metric to a JSON object.
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, Object?> toJson() {
|
||||||
return <String, dynamic>{
|
return <String, Object?>{
|
||||||
'metric': metric,
|
_metricKey: metric,
|
||||||
'value': value,
|
_valueKey: value,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -35,22 +45,30 @@ class BenchmarkResults {
|
|||||||
/// Constructs a result containing scores from a single run benchmark run.
|
/// Constructs a result containing scores from a single run benchmark run.
|
||||||
BenchmarkResults(this.scores);
|
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.
|
/// Scores collected in a benchmark run.
|
||||||
final Map<String, List<BenchmarkScore>> scores;
|
final Map<String, List<BenchmarkScore>> scores;
|
||||||
|
|
||||||
/// Serializes benchmark metrics to JSON.
|
/// Serializes benchmark metrics to JSON.
|
||||||
Map<String, List<Map<String, dynamic>>> toJson() {
|
Map<String, List<Map<String, Object?>>> toJson() {
|
||||||
return scores.map<String, List<Map<String, dynamic>>>(
|
return scores.map<String, List<Map<String, Object?>>>(
|
||||||
(String benchmarkName, List<BenchmarkScore> scores) {
|
(String benchmarkName, List<BenchmarkScore> scores) {
|
||||||
return MapEntry<String, List<Map<String, dynamic>>>(
|
return MapEntry<String, List<Map<String, Object?>>>(
|
||||||
benchmarkName,
|
benchmarkName,
|
||||||
scores
|
scores.map((BenchmarkScore score) => score.toJson()).toList(),
|
||||||
.map<Map<String, dynamic>>(
|
|
||||||
(BenchmarkScore score) => <String, dynamic>{
|
|
||||||
'metric': score.metric,
|
|
||||||
'value': score.value,
|
|
||||||
})
|
|
||||||
.toList(),
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ name: web_benchmarks
|
|||||||
description: A benchmark harness for performance-testing Flutter apps in Chrome.
|
description: A benchmark harness for performance-testing Flutter apps in Chrome.
|
||||||
repository: https://github.com/flutter/packages/tree/main/packages/web_benchmarks
|
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
|
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:
|
environment:
|
||||||
sdk: ">=3.2.0 <4.0.0"
|
sdk: ">=3.2.0 <4.0.0"
|
||||||
|
52
packages/web_benchmarks/test/src/benchmark_result_test.dart
Normal file
52
packages/web_benchmarks/test/src/benchmark_result_test.dart
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Reference in New Issue
Block a user