// 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. // @dart = 2.6 import 'dart:convert'; import 'dart:io'; import 'package:git/git.dart'; import 'package:metrics_center/flutter.dart'; import 'package:metrics_center/google_benchmark.dart'; Future _getGitRevision() async { final GitDir gitDir = await GitDir.fromExisting('../../'); // Somehow gitDir.currentBranch() doesn't work in Cirrus with "fatal: 'HEAD' - // not a valid ref". Therefore, we use "git log" to get the revision manually. final ProcessResult logResult = await gitDir.runCommand(['log', '--pretty=format:%H', '-n', '1']); if (logResult.exitCode != 0) { throw 'Unexpected exit code ${logResult.exitCode}'; } return logResult.stdout.toString(); } Future> _parse(String jsonFileName) async { final String gitRevision = await _getGitRevision(); final List rawPoints = await GoogleBenchmarkParser.parse(jsonFileName); final List points = []; for (MetricPoint rawPoint in rawPoints) { points.add(FlutterEngineMetricPoint( rawPoint.tags[kNameKey], rawPoint.value, gitRevision, moreTags: rawPoint.tags, )); } return points; } Future connectFlutterDestination() async { const String kTokenPath = 'TOKEN_PATH'; const String kGcpProject = 'GCP_PROJECT'; final Map env = Platform.environment; if (env.containsKey(kTokenPath) && env.containsKey(kGcpProject)) { return FlutterDestination.makeFromAccessToken( File(env[kTokenPath]).readAsStringSync(), env[kGcpProject], ); } return await FlutterDestination.makeFromCredentialsJson( jsonDecode(Platform.environment['BENCHMARK_GCP_CREDENTIALS']) as Map, ); } Future main(List args) async { if (args.length != 1) { throw 'Must have one argument: '; } final List points = await _parse(args[0]); // The data will be sent to the Datastore of the GCP project specified through // environment variable BENCHMARK_GCP_CREDENTIALS, or TOKEN_PATH/GCP_PROJECT. // The engine Cirrus job has currently configured the GCP project to // flutter-cirrus for test. We'll eventually migrate to flutter-infra project // once the test is done. final FlutterDestination destination = await connectFlutterDestination(); await destination.update(points); }