blob: b3133a8a7d6ebd9ab15e927f8acd8dab6cdf426d [file] [log] [blame]
// 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 'dart:convert';
import 'dart:io';
import 'common.dart';
import 'constants.dart';
const String _kTimeUnitKey = 'time_unit';
const List<String> _kNonNumericalValueSubResults = <String>[
// Context has some keys such as 'host_name' which need to be ignored
// so that we can group series together
const List<String> _kContextIgnoreKeys = <String>[
// ignore: avoid_classes_with_only_static_members
/// Parse the json result of
class GoogleBenchmarkParser {
/// Given a Google benchmark json output, parse its content into a list of [MetricPoint].
static Future<List<MetricPoint>> parse(String jsonFileName) async {
final Map<String, dynamic> jsonResult =
as Map<String, dynamic>;
final Map<String, dynamic> rawContext =
jsonResult['context'] as Map<String, dynamic>;
final Map<String, String> context =<String, String>(
(String k, dynamic v) => MapEntry<String, String>(k, v.toString()),
)..removeWhere((String k, String v) => _kContextIgnoreKeys.contains(k));
final List<MetricPoint> points = <MetricPoint>[];
for (final dynamic item in jsonResult['benchmarks'] as List<dynamic>) {
_parseAnItem(item as Map<String, dynamic>, points, context);
return points;
void _parseAnItem(
Map<String, dynamic> item,
List<MetricPoint> points,
Map<String, String> context,
) {
final String name = item[kNameKey] as String;
final Map<String, String> timeUnitMap = <String, String>{
if (item.containsKey(_kTimeUnitKey)) kUnitKey: item[_kTimeUnitKey] as String
for (final String subResult in item.keys) {
if (!_kNonNumericalValueSubResults.contains(subResult)) {
num? rawValue;
try {
rawValue = item[subResult] as num?;
} catch (e) {
// ignore: avoid_print
'$subResult: ${item[subResult]} (${(item[subResult] as Object?).runtimeType}) is not a number');
final double? value =
rawValue is int ? rawValue.toDouble() : rawValue as double?;
<String, String?>{kNameKey: name, kSubResultKey: subResult}
subResult.endsWith('time') ? timeUnitMap : <String, String>{}),