timeline_summary_test.dart 5.5 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

import 'dart:convert' show JSON;

import 'package:test/test.dart';
import 'package:flutter_driver/src/common.dart';
9
import 'package:flutter_driver/flutter_driver.dart';
10 11 12 13 14

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

    TimelineSummary summarize(List<Map<String, dynamic>> testEvents) {
15
      return new TimelineSummary.summarize(new Timeline.fromJson(<String, dynamic>{
16
        'traceEvents': testEvents,
17
      }));
18 19 20 21 22 23 24 25 26 27 28 29 30
    }

    Map<String, dynamic> begin(int timeStamp) => <String, dynamic>{
      'name': 'Engine::BeginFrame', 'ph': 'B', 'ts': timeStamp
    };

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

    group('frame_count', () {
      test('counts frames', () {
        expect(
pq's avatar
pq committed
31
          summarize(<Map<String, dynamic>>[
32 33 34 35 36 37 38 39 40 41
            begin(1000), end(2000),
            begin(3000), end(5000),
          ]).countFrames(),
          2
        );
      });
    });

    group('average_frame_build_time_millis', () {
      test('returns null when there is no data', () {
pq's avatar
pq committed
42
        expect(summarize(<Map<String, dynamic>>[]).computeAverageFrameBuildTimeMillis(), isNull);
43 44 45 46
      });

      test('computes average frame build time in milliseconds', () {
        expect(
pq's avatar
pq committed
47
          summarize(<Map<String, dynamic>>[
48 49 50 51 52 53 54 55 56
            begin(1000), end(2000),
            begin(3000), end(5000),
          ]).computeAverageFrameBuildTimeMillis(),
          1.5
        );
      });

      test('skips leading "end" events', () {
        expect(
pq's avatar
pq committed
57
          summarize(<Map<String, dynamic>>[
58 59 60
            end(1000),
            begin(2000), end(4000),
          ]).computeAverageFrameBuildTimeMillis(),
61
          2.0
62 63 64 65 66
        );
      });

      test('skips trailing "begin" events', () {
        expect(
pq's avatar
pq committed
67
          summarize(<Map<String, dynamic>>[
68 69 70
            begin(2000), end(4000),
            begin(5000),
          ]).computeAverageFrameBuildTimeMillis(),
71 72 73 74 75 76 77
          2.0
        );
      });
    });

    group('worst_frame_build_time_millis', () {
      test('returns null when there is no data', () {
pq's avatar
pq committed
78
        expect(summarize(<Map<String, dynamic>>[]).computeWorstFrameBuildTimeMillis(), isNull);
79 80 81 82
      });

      test('computes worst frame build time in milliseconds', () {
        expect(
pq's avatar
pq committed
83
          summarize(<Map<String, dynamic>>[
84 85 86 87 88 89
            begin(1000), end(2000),
            begin(3000), end(5000),
          ]).computeWorstFrameBuildTimeMillis(),
          2.0
        );
        expect(
pq's avatar
pq committed
90
          summarize(<Map<String, dynamic>>[
91 92 93 94 95 96 97 98 99
            begin(3000), end(5000),
            begin(1000), end(2000),
          ]).computeWorstFrameBuildTimeMillis(),
          2.0
        );
      });

      test('skips leading "end" events', () {
        expect(
pq's avatar
pq committed
100
          summarize(<Map<String, dynamic>>[
101 102 103 104 105 106 107 108 109
            end(1000),
            begin(2000), end(4000),
          ]).computeWorstFrameBuildTimeMillis(),
          2.0
        );
      });

      test('skips trailing "begin" events', () {
        expect(
pq's avatar
pq committed
110
          summarize(<Map<String, dynamic>>[
111 112 113 114
            begin(2000), end(4000),
            begin(5000),
          ]).computeWorstFrameBuildTimeMillis(),
          2.0
115 116 117 118 119 120
        );
      });
    });

    group('computeMissedFrameBuildBudgetCount', () {
      test('computes the number of missed build budgets', () {
pq's avatar
pq committed
121
        TimelineSummary summary = summarize(<Map<String, dynamic>>[
122 123 124 125 126 127 128 129 130 131 132 133 134
          begin(1000), end(10000),
          begin(11000), end(12000),
          begin(13000), end(23000),
        ]);

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

    group('summaryJson', () {
      test('computes and returns summary as JSON', () {
        expect(
pq's avatar
pq committed
135
          summarize(<Map<String, dynamic>>[
136 137 138 139
            begin(1000), end(10000),
            begin(11000), end(12000),
            begin(13000), end(24000),
          ]).summaryJson,
pq's avatar
pq committed
140
          <String, dynamic>{
141
            'average_frame_build_time_millis': 7.0,
142
            'worst_frame_build_time_millis': 11.0,
143 144
            'missed_frame_build_budget_count': 2,
            'frame_count': 3,
145
            'frame_build_times': <int>[9000, 1000, 11000],
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
          }
        );
      });
    });

    group('writeTimelineToFile', () {
      setUp(() {
        useMemoryFileSystemForTesting();
      });

      tearDown(() {
        restoreFileSystem();
      });

      test('writes timeline to JSON file', () async {
pq's avatar
pq committed
161
        await summarize(<Map<String, String>>[<String, String>{'foo': 'bar'}])
162 163 164 165 166 167 168
          .writeTimelineToFile('test', destinationDirectory: '/temp');
        String written =
            await fs.file('/temp/test.timeline.json').readAsString();
        expect(written, '{"traceEvents":[{"foo":"bar"}]}');
      });

      test('writes summary to JSON file', () async {
pq's avatar
pq committed
169
        await summarize(<Map<String, dynamic>>[
170 171 172 173 174 175
          begin(1000), end(10000),
          begin(11000), end(12000),
          begin(13000), end(24000),
        ]).writeSummaryToFile('test', destinationDirectory: '/temp');
        String written =
            await fs.file('/temp/test.timeline_summary.json').readAsString();
pq's avatar
pq committed
176
        expect(JSON.decode(written), <String, dynamic>{
177
          'average_frame_build_time_millis': 7.0,
178
          'worst_frame_build_time_millis': 11.0,
179 180
          'missed_frame_build_budget_count': 2,
          'frame_count': 3,
181
          'frame_build_times': <int>[9000, 1000, 11000],
182 183 184 185 186
        });
      });
    });
  });
}