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

[integration_test] Add watchPerformance #3116

Merged
merged 6 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/integration_test/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.2

* Add `watchPerformance` for performance test.

## 0.9.1

* Keep handling deprecated Android v1 classes for backward compatibility.
Expand Down
44 changes: 44 additions & 0 deletions packages/integration_test/lib/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:async';
import 'dart:developer' as developer;
import 'dart:ui';

import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -81,6 +82,10 @@ class IntegrationTestWidgetsFlutterBinding extends LiveTestWidgetsFlutterBinding

Size _surfaceSize;

// This flag is used to print warning messages when tracking performance
// under debug mode.
static bool _firstRun = false;

/// Artificially changes the surface size to `size` on the Widget binding,
/// then flushes microtasks.
///
Expand Down Expand Up @@ -282,4 +287,43 @@ class IntegrationTestWidgetsFlutterBinding extends LiveTestWidgetsFlutterBinding
reportData ??= <String, dynamic>{};
reportData[reportKey] = timeline.toJson();
}

/// Watches the [FrameTiming] during `action` and report it to the binding
/// with key `reportKey`.
///
/// This can be used to implement performance tests previously using
/// [traceAction] and [TimelineSummary] from [flutter_driver]
Future<void> watchPerformance(
Future<void> action(), {
String reportKey = 'performance',
}) async {
assert(() {
if (_firstRun) {
debugPrint(kDebugWarning);
_firstRun = false;
}
return true;
}());

// The engine could batch FrameTimings and send them only once per second.
// Delay for a sufficient time so either old FrameTimings are flushed and not
// interfering our measurements here, or new FrameTimings are all reported.
// TODO(CareF): remove this when flush FrameTiming is readly in engine.
// See https://github.com/flutter/flutter/issues/64808
// and https://github.com/flutter/flutter/issues/67593
Future<void> delayForFrameTimings() =>
Future<void>.delayed(const Duration(seconds: 2));

await delayForFrameTimings(); // flush old FrameTimings
final List<FrameTiming> frameTimings = <FrameTiming>[];
final TimingsCallback watcher = frameTimings.addAll;
addTimingsCallback(watcher);
await action();
await delayForFrameTimings(); // make sure all FrameTimings are reported
removeTimingsCallback(watcher);
final FrameTimingSummarizer frameTimes =
FrameTimingSummarizer(frameTimings);
reportData ??= <String, dynamic>{};
reportData[reportKey] = frameTimes.summary;
}
}
2 changes: 1 addition & 1 deletion packages/integration_test/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: integration_test
description: Runs tests that use the flutter_test API as integration tests.
version: 0.9.1
version: 0.9.2
homepage: https://github.com/flutter/plugins/tree/master/packages/integration_test

environment:
Expand Down