Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 65f7260

Browse files
authored
[integration_test] Add watchPerformance (#3116)
* add watchPerformance
1 parent bb4d584 commit 65f7260

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

packages/integration_test/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.9.2
2+
3+
* Add `watchPerformance` for performance test.
4+
15
## 0.9.1
26

37
* Keep handling deprecated Android v1 classes for backward compatibility.

packages/integration_test/lib/integration_test.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66
import 'dart:developer' as developer;
7+
import 'dart:ui';
78

89
import 'package:flutter/rendering.dart';
910
import 'package:flutter_test/flutter_test.dart';
@@ -81,6 +82,10 @@ class IntegrationTestWidgetsFlutterBinding extends LiveTestWidgetsFlutterBinding
8182

8283
Size _surfaceSize;
8384

85+
// This flag is used to print warning messages when tracking performance
86+
// under debug mode.
87+
static bool _firstRun = false;
88+
8489
/// Artificially changes the surface size to `size` on the Widget binding,
8590
/// then flushes microtasks.
8691
///
@@ -282,4 +287,43 @@ class IntegrationTestWidgetsFlutterBinding extends LiveTestWidgetsFlutterBinding
282287
reportData ??= <String, dynamic>{};
283288
reportData[reportKey] = timeline.toJson();
284289
}
290+
291+
/// Watches the [FrameTiming] during `action` and report it to the binding
292+
/// with key `reportKey`.
293+
///
294+
/// This can be used to implement performance tests previously using
295+
/// [traceAction] and [TimelineSummary] from [flutter_driver]
296+
Future<void> watchPerformance(
297+
Future<void> action(), {
298+
String reportKey = 'performance',
299+
}) async {
300+
assert(() {
301+
if (_firstRun) {
302+
debugPrint(kDebugWarning);
303+
_firstRun = false;
304+
}
305+
return true;
306+
}());
307+
308+
// The engine could batch FrameTimings and send them only once per second.
309+
// Delay for a sufficient time so either old FrameTimings are flushed and not
310+
// interfering our measurements here, or new FrameTimings are all reported.
311+
// TODO(CareF): remove this when flush FrameTiming is readly in engine.
312+
// See https://github.com/flutter/flutter/issues/64808
313+
// and https://github.com/flutter/flutter/issues/67593
314+
Future<void> delayForFrameTimings() =>
315+
Future<void>.delayed(const Duration(seconds: 2));
316+
317+
await delayForFrameTimings(); // flush old FrameTimings
318+
final List<FrameTiming> frameTimings = <FrameTiming>[];
319+
final TimingsCallback watcher = frameTimings.addAll;
320+
addTimingsCallback(watcher);
321+
await action();
322+
await delayForFrameTimings(); // make sure all FrameTimings are reported
323+
removeTimingsCallback(watcher);
324+
final FrameTimingSummarizer frameTimes =
325+
FrameTimingSummarizer(frameTimings);
326+
reportData ??= <String, dynamic>{};
327+
reportData[reportKey] = frameTimes.summary;
328+
}
285329
}

packages/integration_test/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: integration_test
22
description: Runs tests that use the flutter_test API as integration tests.
3-
version: 0.9.1
3+
version: 0.9.2
44
homepage: https://github.com/flutter/plugins/tree/master/packages/integration_test
55

66
environment:

0 commit comments

Comments
 (0)