timeline_summary_test.dart 8.24 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:convert' show json;
6

7
import 'package:file/file.dart';
8 9
import 'package:flutter_driver/flutter_driver.dart';
import 'package:flutter_driver/src/driver/common.dart';
10
import 'package:path/path.dart' as path;
11 12 13 14 15 16
import 'package:test/test.dart';

void main() {
  group('TimelineSummary', () {

    TimelineSummary summarize(List<Map<String, dynamic>> testEvents) {
17
      return new TimelineSummary.summarize(new Timeline.fromJson(<String, dynamic>{
18
        'traceEvents': testEvents,
19
      }));
20 21
    }

22
    Map<String, dynamic> build(int timeStamp, int duration) => <String, dynamic>{
23
      'name': 'Frame', 'ph': 'X', 'ts': timeStamp, 'dur': duration
24 25
    };

26 27 28 29 30 31 32 33
    Map<String, dynamic> begin(int timeStamp) => <String, dynamic>{
      'name': 'GPURasterizer::Draw', 'ph': 'B', 'ts': timeStamp
    };

    Map<String, dynamic> end(int timeStamp) => <String, dynamic>{
      'name': 'GPURasterizer::Draw', 'ph': 'E', 'ts': timeStamp
    };

34 35 36
    group('frame_count', () {
      test('counts frames', () {
        expect(
pq's avatar
pq committed
37
          summarize(<Map<String, dynamic>>[
38 39
            build(1000, 1000),
            build(3000, 2000),
40 41 42 43 44 45 46 47
          ]).countFrames(),
          2
        );
      });
    });

    group('average_frame_build_time_millis', () {
      test('returns null when there is no data', () {
pq's avatar
pq committed
48
        expect(summarize(<Map<String, dynamic>>[]).computeAverageFrameBuildTimeMillis(), isNull);
49 50 51 52
      });

      test('computes average frame build time in milliseconds', () {
        expect(
pq's avatar
pq committed
53
          summarize(<Map<String, dynamic>>[
54 55
            build(1000, 1000),
            build(3000, 2000),
56 57 58 59
          ]).computeAverageFrameBuildTimeMillis(),
          1.5
        );
      });
60 61 62 63
    });

    group('worst_frame_build_time_millis', () {
      test('returns null when there is no data', () {
pq's avatar
pq committed
64
        expect(summarize(<Map<String, dynamic>>[]).computeWorstFrameBuildTimeMillis(), isNull);
65 66 67 68
      });

      test('computes worst frame build time in milliseconds', () {
        expect(
pq's avatar
pq committed
69
          summarize(<Map<String, dynamic>>[
70 71
            build(1000, 1000),
            build(3000, 2000),
72 73 74 75
          ]).computeWorstFrameBuildTimeMillis(),
          2.0
        );
        expect(
pq's avatar
pq committed
76
          summarize(<Map<String, dynamic>>[
77 78
            build(3000, 2000),
            build(1000, 1000),
79 80
          ]).computeWorstFrameBuildTimeMillis(),
          2.0
81 82 83 84 85 86
        );
      });
    });

    group('computeMissedFrameBuildBudgetCount', () {
      test('computes the number of missed build budgets', () {
87
        final TimelineSummary summary = summarize(<Map<String, dynamic>>[
88 89 90
          build(1000, 9000),
          build(11000, 1000),
          build(13000, 10000),
91 92 93 94 95 96 97
        ]);

        expect(summary.countFrames(), 3);
        expect(summary.computeMissedFrameBuildBudgetCount(), 2);
      });
    });

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    group('average_frame_rasterizer_time_millis', () {
      test('returns null when there is no data', () {
        expect(summarize(<Map<String, dynamic>>[]).computeAverageFrameRasterizerTimeMillis(), isNull);
      });

      test('computes average frame rasterizer time in milliseconds', () {
        expect(
            summarize(<Map<String, dynamic>>[
              begin(1000), end(2000),
              begin(3000), end(5000),
            ]).computeAverageFrameRasterizerTimeMillis(),
            1.5
        );
      });

      test('skips leading "end" events', () {
        expect(
            summarize(<Map<String, dynamic>>[
              end(1000),
              begin(2000), end(4000),
            ]).computeAverageFrameRasterizerTimeMillis(),
            2.0
        );
      });

      test('skips trailing "begin" events', () {
        expect(
            summarize(<Map<String, dynamic>>[
              begin(2000), end(4000),
              begin(5000),
            ]).computeAverageFrameRasterizerTimeMillis(),
            2.0
        );
      });
    });

    group('worst_frame_rasterizer_time_millis', () {
      test('returns null when there is no data', () {
        expect(summarize(<Map<String, dynamic>>[]).computeWorstFrameRasterizerTimeMillis(), isNull);
      });

      test('computes worst frame rasterizer time in milliseconds', () {
        expect(
            summarize(<Map<String, dynamic>>[
              begin(1000), end(2000),
              begin(3000), end(5000),
            ]).computeWorstFrameRasterizerTimeMillis(),
            2.0
        );
        expect(
            summarize(<Map<String, dynamic>>[
              begin(3000), end(5000),
              begin(1000), end(2000),
            ]).computeWorstFrameRasterizerTimeMillis(),
            2.0
        );
      });

      test('skips leading "end" events', () {
        expect(
            summarize(<Map<String, dynamic>>[
              end(1000),
              begin(2000), end(4000),
            ]).computeWorstFrameRasterizerTimeMillis(),
            2.0
        );
      });

      test('skips trailing "begin" events', () {
        expect(
            summarize(<Map<String, dynamic>>[
              begin(2000), end(4000),
              begin(5000),
            ]).computeWorstFrameRasterizerTimeMillis(),
            2.0
        );
      });
    });

    group('computeMissedFrameRasterizerBudgetCount', () {
      test('computes the number of missed rasterizer budgets', () {
179
        final TimelineSummary summary = summarize(<Map<String, dynamic>>[
180 181 182 183 184 185 186 187 188
          begin(1000), end(10000),
          begin(11000), end(12000),
          begin(13000), end(23000),
        ]);

        expect(summary.computeMissedFrameRasterizerBudgetCount(), 2);
      });
    });

189 190 191
    group('summaryJson', () {
      test('computes and returns summary as JSON', () {
        expect(
pq's avatar
pq committed
192
          summarize(<Map<String, dynamic>>[
193 194 195 196 197 198
            begin(1000), end(11000),
            begin(11000), end(13000),
            begin(13000), end(25000),
            build(1000, 9000),
            build(11000, 1000),
            build(13000, 11000),
199
          ]).summaryJson,
pq's avatar
pq committed
200
          <String, dynamic>{
201
            'average_frame_build_time_millis': 7.0,
202
            'worst_frame_build_time_millis': 11.0,
203
            'missed_frame_build_budget_count': 2,
204 205 206
            'average_frame_rasterizer_time_millis': 8.0,
            'worst_frame_rasterizer_time_millis': 12.0,
            'missed_frame_rasterizer_budget_count': 2,
207
            'frame_count': 3,
208
            'frame_build_times': <int>[9000, 1000, 11000],
209
            'frame_rasterizer_times': <int>[10000, 2000, 12000],
210 211 212 213 214 215
          }
        );
      });
    });

    group('writeTimelineToFile', () {
216 217 218

      Directory tempDir;

219 220
      setUp(() {
        useMemoryFileSystemForTesting();
221
        tempDir = fs.systemTempDirectory.createTempSync('flutter_driver_test');
222 223 224
      });

      tearDown(() {
225
        tempDir.deleteSync(recursive: true);
226 227 228 229
        restoreFileSystem();
      });

      test('writes timeline to JSON file', () async {
pq's avatar
pq committed
230
        await summarize(<Map<String, String>>[<String, String>{'foo': 'bar'}])
231
          .writeTimelineToFile('test', destinationDirectory: tempDir.path);
232
        final String written =
233
            await fs.file(path.join(tempDir.path, 'test.timeline.json')).readAsString();
234 235 236 237
        expect(written, '{"traceEvents":[{"foo":"bar"}]}');
      });

      test('writes summary to JSON file', () async {
pq's avatar
pq committed
238
        await summarize(<Map<String, dynamic>>[
239 240 241 242 243 244
          begin(1000), end(11000),
          begin(11000), end(13000),
          begin(13000), end(25000),
          build(1000, 9000),
          build(11000, 1000),
          build(13000, 11000),
245
        ]).writeSummaryToFile('test', destinationDirectory: tempDir.path);
246
        final String written =
247
            await fs.file(path.join(tempDir.path, 'test.timeline_summary.json')).readAsString();
248
        expect(json.decode(written), <String, dynamic>{
249
          'average_frame_build_time_millis': 7.0,
250
          'worst_frame_build_time_millis': 11.0,
251
          'missed_frame_build_budget_count': 2,
252 253 254
          'average_frame_rasterizer_time_millis': 8.0,
          'worst_frame_rasterizer_time_millis': 12.0,
          'missed_frame_rasterizer_budget_count': 2,
255
          'frame_count': 3,
256
          'frame_build_times': <int>[9000, 1000, 11000],
257
          'frame_rasterizer_times': <int>[10000, 2000, 12000],
258 259 260 261 262
        });
      });
    });
  });
}