frame_timing_summarizer_test.dart 4.88 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 'dart:ui';

import 'package:flutter_test/flutter_test.dart';

void main() {
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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
  group(FrameTimingSummarizer, () {
    test('calculates all fields', () {
      List<int> vsyncTimes = <int>[
        for (int i = 0; i < 100; i += 1) 100 * (i + 1),
      ];
      List<int> buildTimes = <int>[
        for (int i = 0; i < 100; i += 1) vsyncTimes[i] + 1000 * (i + 1),
      ];
      List<int> rasterTimes = <int>[
        for (int i = 0; i < 100; i += 1) 1000 * (i + 1) + 1000,
      ];
      // reversed to make sure sort is working.
      buildTimes = buildTimes.reversed.toList();
      rasterTimes = rasterTimes.reversed.toList();
      vsyncTimes = vsyncTimes.reversed.toList();
      final List<FrameTiming> inputData = <FrameTiming>[
        for (int i = 0; i < 100; i += 1)
          FrameTiming(
            vsyncStart: 0,
            buildStart: vsyncTimes[i],
            buildFinish: buildTimes[i],
            rasterStart: 500,
            rasterFinish: rasterTimes[i],
            // Wall time should not be used in any profiling metrics.
            // It is primarily to correlate with external tools' measurement.
            rasterFinishWallTime: 0,
          ),
      ];

      final FrameTimingSummarizer summary = FrameTimingSummarizer(inputData);
      expect(summary.averageFrameBuildTime.inMicroseconds, 50500);
      expect(summary.p90FrameBuildTime.inMicroseconds, 90000);
      expect(summary.p99FrameBuildTime.inMicroseconds, 99000);
      expect(summary.worstFrameBuildTime.inMicroseconds, 100000);
      expect(summary.missedFrameBuildBudget, 84);

      expect(summary.averageFrameRasterizerTime.inMicroseconds, 51000);
      expect(summary.p90FrameRasterizerTime.inMicroseconds, 90500);
      expect(summary.p99FrameRasterizerTime.inMicroseconds, 99500);
      expect(summary.worstFrameRasterizerTime.inMicroseconds, 100500);
      expect(summary.missedFrameRasterizerBudget, 85);
      expect(summary.frameBuildTime.length, 100);

      expect(summary.averageVsyncOverhead.inMicroseconds, 5050);
      expect(summary.p90VsyncOverhead.inMicroseconds, 9000);
      expect(summary.p99VsyncOverhead.inMicroseconds, 9900);
      expect(summary.worstVsyncOverhead.inMicroseconds, 10000);
    });

    group('missed budget count', () {
      test('when single element missed budget', () {
        final FrameTimingSummarizer summary = FrameTimingSummarizer(<FrameTiming>[
          FrameTiming(
            buildStart: 0,
            buildFinish: (kBuildBudget + const Duration(microseconds: 1)).inMicroseconds,
            vsyncStart: 0,
            rasterStart: 0,
            rasterFinish: 0,
            rasterFinishWallTime: 0,
          ),
        ]);
        expect(summary.missedFrameBuildBudget, 1);
      });

      test('when single element within budget', () {
        final FrameTimingSummarizer summary = FrameTimingSummarizer(<FrameTiming>[
          FrameTiming(
            buildStart: 0,
            buildFinish: 0,
            vsyncStart: 0,
            rasterStart: 0,
            rasterFinish: 0,
            rasterFinishWallTime: 0,
          ),
        ]);
        expect(summary.missedFrameBuildBudget, 0);
      });

      test('when single element exactly within budget', () {
        final FrameTimingSummarizer summary = FrameTimingSummarizer(<FrameTiming>[
          FrameTiming(
            buildStart: 0,
            buildFinish: kBuildBudget.inMicroseconds,
            vsyncStart: 0,
            rasterStart: 0,
            rasterFinish: 0,
            rasterFinishWallTime: 0,
          ),
        ]);
        expect(summary.missedFrameBuildBudget, 0);
      });

      test('when many missed budget', () {
        final FrameTimingSummarizer summary = FrameTimingSummarizer(<FrameTiming>[
          FrameTiming(
            buildStart: 0,
            buildFinish: 0,
            vsyncStart: 0,
            rasterStart: 0,
            rasterFinish: 0,
            rasterFinishWallTime: 0,
          ),
          FrameTiming(
            buildStart: 0,
            buildFinish: kBuildBudget.inMicroseconds,
            vsyncStart: 0,
            rasterStart: 0,
            rasterFinish: 0,
            rasterFinishWallTime: 0,
          ),
          FrameTiming(
            buildStart: 0,
            buildFinish: (kBuildBudget + const Duration(microseconds: 1)).inMicroseconds,
            vsyncStart: 0,
            rasterStart: 0,
            rasterFinish: 0,
            rasterFinishWallTime: 0,
          ),
          FrameTiming(
            buildStart: 0,
            buildFinish: (kBuildBudget + const Duration(microseconds: 2)).inMicroseconds,
            vsyncStart: 0,
            rasterStart: 0,
            rasterFinish: 0,
            rasterFinishWallTime: 0,
          ),
        ]);
        expect(summary.missedFrameBuildBudget, 2);
      });
    });
140 141
  });
}