-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle_benchmark.dart
67 lines (59 loc) · 1.86 KB
/
google_benchmark.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Transform the json result of https://github.com/google/benchmark
import 'dart:convert';
import 'dart:io';
import 'package:metrics_center/src/common.dart';
const String _kTimeUnitKey = 'time_unit';
const List<String> _kNonNumericalValueSubResults = <String>[
kNameKey,
_kTimeUnitKey,
'iterations',
'big_o',
];
class GoogleBenchmarkParser {
static Future<List<MetricPoint>> parse(String jsonFileName) async {
final Map<String, dynamic> jsonResult =
jsonDecode(File(jsonFileName).readAsStringSync());
final Map<String, dynamic> rawContext = jsonResult['context'];
final Map<String, String> context = rawContext.map<String, String>(
(String k, dynamic v) => MapEntry<String, String>(k, v.toString()),
);
final List<MetricPoint> points = [];
for (Map<String, dynamic> item in jsonResult['benchmarks']) {
_parseAnItem(item, points, context);
}
return points;
}
}
void _parseAnItem(
Map<String, dynamic> item,
List<MetricPoint> points,
Map<String, String> context,
) {
final String name = item[kNameKey];
final Map<String, String> timeUnitMap = <String, String>{
kUnitKey: item[_kTimeUnitKey]
};
for (String subResult in item.keys) {
if (!_kNonNumericalValueSubResults.contains(subResult)) {
num rawValue;
try {
rawValue = item[subResult];
} catch (e) {
print('$subResult: ${item[subResult]} (${item[subResult].runtimeType}) '
'is not a number');
rethrow;
}
final double value = rawValue is int ? rawValue.toDouble() : rawValue;
points.add(
MetricPoint(
value,
<String, String>{kNameKey: name, kSubResultKey: subResult}
..addAll(context)
..addAll(
subResult.endsWith('time') ? timeUnitMap : <String, String>{}),
kFlutterCenterId,
),
);
}
}
}