timeline_summary_test.dart 10.8 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

import '../common.dart';
13 14 15 16 17

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

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

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

27 28 29 30 31 32 33 34
    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
    };

35 36 37
    List<Map<String, dynamic>> rasterizeTimeSequenceInMillis(List<int> sequence) {
      final List<Map<String, dynamic>> result = <Map<String, dynamic>>[];
      int t = 0;
38
      for (int duration in sequence) {
39 40 41 42 43 44 45
        result.add(begin(t));
        t += duration * 1000;
        result.add(end(t));
      }
      return result;
    }

46 47 48
    group('frame_count', () {
      test('counts frames', () {
        expect(
pq's avatar
pq committed
49
          summarize(<Map<String, dynamic>>[
50 51
            build(1000, 1000),
            build(3000, 2000),
52 53 54 55 56 57 58
          ]).countFrames(),
          2
        );
      });
    });

    group('average_frame_build_time_millis', () {
59 60 61 62 63
      test('throws when there is no data', () {
        expect(
          () => summarize(<Map<String, dynamic>>[]).computeAverageFrameBuildTimeMillis(),
          throwsA(predicate<ArgumentError>((ArgumentError e) => e.message == 'durations is empty!'))
        );
64 65 66 67
      });

      test('computes average frame build time in milliseconds', () {
        expect(
pq's avatar
pq committed
68
          summarize(<Map<String, dynamic>>[
69 70
            build(1000, 1000),
            build(3000, 2000),
71 72 73 74
          ]).computeAverageFrameBuildTimeMillis(),
          1.5
        );
      });
75 76 77
    });

    group('worst_frame_build_time_millis', () {
78 79 80 81 82
      test('throws when there is no data', () {
        expect(
          () => summarize(<Map<String, dynamic>>[]).computeWorstFrameBuildTimeMillis(),
          throwsA(predicate<ArgumentError>((ArgumentError e) => e.message == 'durations is empty!'))
        );
83 84 85 86
      });

      test('computes worst frame build time in milliseconds', () {
        expect(
pq's avatar
pq committed
87
          summarize(<Map<String, dynamic>>[
88 89
            build(1000, 1000),
            build(3000, 2000),
90 91 92 93
          ]).computeWorstFrameBuildTimeMillis(),
          2.0
        );
        expect(
pq's avatar
pq committed
94
          summarize(<Map<String, dynamic>>[
95 96
            build(3000, 2000),
            build(1000, 1000),
97 98
          ]).computeWorstFrameBuildTimeMillis(),
          2.0
99 100 101 102 103 104
        );
      });
    });

    group('computeMissedFrameBuildBudgetCount', () {
      test('computes the number of missed build budgets', () {
105
        final TimelineSummary summary = summarize(<Map<String, dynamic>>[
106 107 108
          build(1000, 9000),
          build(11000, 1000),
          build(13000, 10000),
109 110 111 112 113 114 115
        ]);

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

116
    group('average_frame_rasterizer_time_millis', () {
117 118 119 120 121
      test('throws when there is no data', () {
        expect(
          () => summarize(<Map<String, dynamic>>[]).computeAverageFrameRasterizerTimeMillis(),
          throwsA(predicate<ArgumentError>((ArgumentError e) => e.message == 'durations is empty!'))
        );
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
      });

      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', () {
156 157 158 159 160
      test('throws when there is no data', () {
        expect(
          () => summarize(<Map<String, dynamic>>[]).computeWorstFrameRasterizerTimeMillis(),
          throwsA(predicate<ArgumentError>((ArgumentError e) => e.message == 'durations is empty!'))
        );
161 162
      });

163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
      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
        );
      });
    });

202
    group('percentile_frame_rasterizer_time_millis', () {
203 204 205 206 207
      test('throws when there is no data', () {
        expect(
          () => summarize(<Map<String, dynamic>>[]).computePercentileFrameRasterizerTimeMillis(90.0),
          throwsA(predicate<ArgumentError>((ArgumentError e) => e.message == 'durations is empty!'))
        );
208 209
      });

210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
      const List<List<int>> sequences = <List<int>>[
        <int>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        <int>[1, 2, 3, 4, 5],
        <int>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
      ];

      const List<int> p90s = <int>[
        9,
        5,
        18
      ];

      test('computes 90th frame rasterizer time in milliseconds', () {
        for(int i = 0; i < sequences.length; ++i) {
          expect(
            summarize(rasterizeTimeSequenceInMillis(sequences[i])).computePercentileFrameRasterizerTimeMillis(90.0),
            p90s[i]
          );
        }
      });

      test('compute 99th frame rasterizer time in milliseconds', () {
        final List<int> sequence = <int>[];
        for(int i = 1; i <= 100; ++i) {
          sequence.add(i);
        }
        expect(
          summarize(rasterizeTimeSequenceInMillis(sequence)).computePercentileFrameRasterizerTimeMillis(99.0),
          99
        );
      });
    });

244 245
    group('computeMissedFrameRasterizerBudgetCount', () {
      test('computes the number of missed rasterizer budgets', () {
246
        final TimelineSummary summary = summarize(<Map<String, dynamic>>[
247 248 249 250 251 252 253 254 255
          begin(1000), end(10000),
          begin(11000), end(12000),
          begin(13000), end(23000),
        ]);

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

256 257 258
    group('summaryJson', () {
      test('computes and returns summary as JSON', () {
        expect(
pq's avatar
pq committed
259
          summarize(<Map<String, dynamic>>[
260 261 262 263 264 265
            begin(1000), end(11000),
            begin(11000), end(13000),
            begin(13000), end(25000),
            build(1000, 9000),
            build(11000, 1000),
            build(13000, 11000),
266
          ]).summaryJson,
pq's avatar
pq committed
267
          <String, dynamic>{
268
            'average_frame_build_time_millis': 7.0,
269 270
            '90th_percentile_frame_build_time_millis': 11.0,
            '99th_percentile_frame_build_time_millis': 11.0,
271
            'worst_frame_build_time_millis': 11.0,
272
            'missed_frame_build_budget_count': 2,
273
            'average_frame_rasterizer_time_millis': 8.0,
274 275
            '90th_percentile_frame_rasterizer_time_millis': 12.0,
            '99th_percentile_frame_rasterizer_time_millis': 12.0,
276 277
            'worst_frame_rasterizer_time_millis': 12.0,
            'missed_frame_rasterizer_budget_count': 2,
278
            'frame_count': 3,
279
            'frame_build_times': <int>[9000, 1000, 11000],
280
            'frame_rasterizer_times': <int>[10000, 2000, 12000],
281 282 283 284 285 286
          }
        );
      });
    });

    group('writeTimelineToFile', () {
287 288 289

      Directory tempDir;

290 291
      setUp(() {
        useMemoryFileSystemForTesting();
292
        tempDir = fs.systemTempDirectory.createTempSync('flutter_driver_test.');
293 294 295
      });

      tearDown(() {
296
        tryToDelete(tempDir);
297 298 299 300
        restoreFileSystem();
      });

      test('writes timeline to JSON file', () async {
pq's avatar
pq committed
301
        await summarize(<Map<String, String>>[<String, String>{'foo': 'bar'}])
302
          .writeTimelineToFile('test', destinationDirectory: tempDir.path);
303
        final String written =
304
            await fs.file(path.join(tempDir.path, 'test.timeline.json')).readAsString();
305 306 307 308
        expect(written, '{"traceEvents":[{"foo":"bar"}]}');
      });

      test('writes summary to JSON file', () async {
pq's avatar
pq committed
309
        await summarize(<Map<String, dynamic>>[
310 311 312 313 314 315
          begin(1000), end(11000),
          begin(11000), end(13000),
          begin(13000), end(25000),
          build(1000, 9000),
          build(11000, 1000),
          build(13000, 11000),
316
        ]).writeSummaryToFile('test', destinationDirectory: tempDir.path);
317
        final String written =
318
            await fs.file(path.join(tempDir.path, 'test.timeline_summary.json')).readAsString();
319
        expect(json.decode(written), <String, dynamic>{
320
          'average_frame_build_time_millis': 7.0,
321
          'worst_frame_build_time_millis': 11.0,
322 323
          '90th_percentile_frame_build_time_millis': 11.0,
          '99th_percentile_frame_build_time_millis': 11.0,
324
          'missed_frame_build_budget_count': 2,
325
          'average_frame_rasterizer_time_millis': 8.0,
326 327
          '90th_percentile_frame_rasterizer_time_millis': 12.0,
          '99th_percentile_frame_rasterizer_time_millis': 12.0,
328 329
          'worst_frame_rasterizer_time_millis': 12.0,
          'missed_frame_rasterizer_budget_count': 2,
330
          'frame_count': 3,
331
          'frame_build_times': <int>[9000, 1000, 11000],
332
          'frame_rasterizer_times': <int>[10000, 2000, 12000],
333 334 335 336 337
        });
      });
    });
  });
}