metrics_center_test.dart 6.37 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2014 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_devicelab/framework/metrics_center.dart';
import 'package:metrics_center/metrics_center.dart';

import 'common.dart';

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
class FakeFlutterDestination implements FlutterDestination {
  /// Overrides the skia perf `update` function, which uploads new data to gcs if there
  /// doesn't exist the commit, otherwise updates existing data by appending new ones.
  @override
  Future<void> update(List<MetricPoint> points, DateTime commitTime, String taskName) async {
    lastUpdatedPoints = points;
    time = commitTime;
    name = taskName;
  }

  List<MetricPoint>? lastUpdatedPoints;
  DateTime? time;
  String? name;
}

25 26
void main() {
  group('Parse', () {
27 28 29 30 31 32 33 34 35 36 37 38 39 40
    test('duplicate entries for both builder name and test name', () {
      final Map<String, dynamic> results = <String, dynamic>{
        'CommitBranch': 'master',
        'CommitSha': 'abc',
        'BuilderName': 'Linux test',
        'ResultData': <String, dynamic>{
          'average_frame_build_time_millis': 0.4550425531914895,
        },
        'BenchmarkScoreKeys': <String>[
          'average_frame_build_time_millis',
        ],
      };
      final List<MetricPoint> metricPoints = parse(results, <String, String>{}, 'test');

41
      expect(metricPoints.length, 1);
42
      expect(metricPoints[0].value, equals(0.4550425531914895));
43
      expect(metricPoints[0].tags[kNameKey], 'test');
44 45
    });

46
    test('without additional benchmark tags', () {
47 48 49 50 51 52
      final Map<String, dynamic> results = <String, dynamic>{
        'CommitBranch': 'master',
        'CommitSha': 'abc',
        'BuilderName': 'test',
        'ResultData': <String, dynamic>{
          'average_frame_build_time_millis': 0.4550425531914895,
53
          '90th_percentile_frame_build_time_millis': 0.473,
54 55 56
        },
        'BenchmarkScoreKeys': <String>[
          'average_frame_build_time_millis',
57 58
          '90th_percentile_frame_build_time_millis',
        ],
59
      };
60
      final List<MetricPoint> metricPoints = parse(results, <String, String>{}, 'task abc');
61 62

      expect(metricPoints[0].value, equals(0.4550425531914895));
63
      expect(metricPoints[1].value, equals(0.473));
64
    });
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    test('with additional benchmark tags', () {
      final Map<String, dynamic> results = <String, dynamic>{
        'CommitBranch': 'master',
        'CommitSha': 'abc',
        'BuilderName': 'test',
        'ResultData': <String, dynamic>{
          'average_frame_build_time_millis': 0.4550425531914895,
          '90th_percentile_frame_build_time_millis': 0.473,
        },
        'BenchmarkScoreKeys': <String>[
          'average_frame_build_time_millis',
          '90th_percentile_frame_build_time_millis',
        ],
      };
      final Map<String, dynamic> tags = <String, dynamic>{
        'arch': 'intel',
        'device_type': 'Moto G Play',
        'device_version': 'android-25',
        'host_type': 'linux',
85
        'host_version': 'debian-10.11',
86
      };
87
      final List<MetricPoint> metricPoints = parse(results, tags, 'task abc');
88 89 90

      expect(metricPoints[0].value, equals(0.4550425531914895));
      expect(metricPoints[0].tags.keys.contains('arch'), isTrue);
91 92
      expect(metricPoints[1].value, equals(0.473));
      expect(metricPoints[1].tags.keys.contains('device_type'), isTrue);
93 94
    });

95 96 97 98 99 100 101 102
    test('succeeds - null ResultData', () {
      final Map<String, dynamic> results = <String, dynamic>{
        'CommitBranch': 'master',
        'CommitSha': 'abc',
        'BuilderName': 'test',
        'ResultData': null,
        'BenchmarkScoreKeys': null,
      };
103
      final List<MetricPoint> metricPoints = parse(results, <String, String>{}, 'tetask abcst');
104 105 106

      expect(metricPoints.length, 0);
    });
107
  });
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

  group('Update', () {
    test('without taskName', () async {
      final Map<String, dynamic> results = <String, dynamic>{
        'CommitBranch': 'master',
        'CommitSha': 'abc',
        'BuilderName': 'test',
        'ResultData': <String, dynamic>{
          'average_frame_build_time_millis': 0.4550425531914895,
          '90th_percentile_frame_build_time_millis': 0.473,
        },
        'BenchmarkScoreKeys': <String>[
          'average_frame_build_time_millis',
          '90th_percentile_frame_build_time_millis',
        ],
      };
124
      final List<MetricPoint> metricPoints = parse(results, <String, String>{}, 'task abc');
125
      final FakeFlutterDestination flutterDestination = FakeFlutterDestination();
126
      const String taskName = 'default';
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
      const int commitTimeSinceEpoch = 1629220312;

      await upload(flutterDestination, metricPoints, commitTimeSinceEpoch, taskName);

      expect(flutterDestination.name, 'default');
    });

    test('with taskName', () async {
      final Map<String, dynamic> results = <String, dynamic>{
        'CommitBranch': 'master',
        'CommitSha': 'abc',
        'BuilderName': 'test',
        'ResultData': <String, dynamic>{
          'average_frame_build_time_millis': 0.4550425531914895,
          '90th_percentile_frame_build_time_millis': 0.473,
        },
        'BenchmarkScoreKeys': <String>[
          'average_frame_build_time_millis',
          '90th_percentile_frame_build_time_millis',
        ],
      };
148
      final List<MetricPoint> metricPoints = parse(results, <String, String>{}, 'task abc');
149 150 151 152 153 154 155 156 157
      final FakeFlutterDestination flutterDestination = FakeFlutterDestination();
      const String taskName = 'test';
      const int commitTimeSinceEpoch = 1629220312;

      await upload(flutterDestination, metricPoints, commitTimeSinceEpoch, taskName);

      expect(flutterDestination.name, taskName);
    });
  });
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

  group('metric file name', () {
    test('without tags', () async {
      final Map<String, dynamic> tags = <String, dynamic>{};
      final String fileName = metricFileName('test', tags);
      expect(fileName, 'test');
    });

    test('with device tags', () async {
      final Map<String, dynamic> tags = <String, dynamic>{'device_type': 'ab-c'};
      final String fileName = metricFileName('test', tags);
      expect(fileName, 'test_abc');
    });

    test('with device host and arch tags', () async {
      final Map<String, dynamic> tags = <String, dynamic>{'device_type': 'ab-c', 'host_type': 'de-f', 'arch': 'm1'};
      final String fileName = metricFileName('test', tags);
      expect(fileName, 'test_m1_def_abc');
    });
  });
178
}