Sub topic 11
Measuring Flutter Performance with `traceAction`
This chapter assumes you know basic Dart and Flutter, have written a widget test, and can run Flutter commands in a terminal. You also need a working integration test and a connected device, emulator, or simulator.
A functional integration test answers whether an interaction works. A performance test answers how much time Flutter spends rendering that interaction. The traceAction method records a performance timeline while a callback runs. The timeline can then be inspected for slow frames, expensive build work, layout work, and rasterization.
What `traceAction` measures
Flutter renders the screen in frames. If a frame takes too long, the user may see dropped frames or stuttering. A common target is about 16 milliseconds per frame for a 60 Hz display, although the exact budget depends on the device and display refresh rate.
traceAction surrounds a user interaction with timeline recording:
- The app reaches a known starting state.
- Timeline recording begins.
- The test performs an interaction, such as scrolling.
- Timeline recording ends.
- Flutter writes the result using a report key.
The report key identifies the recorded timeline. Choose a name that describes the interaction, such as scrolling_timeline or checkout_timeline.
Recording a timeline
The binding returned by ensureInitialized exposes traceAction. Keep the recorded callback focused. If the callback includes setup work, navigation, or unrelated delays, the resulting timeline becomes harder to interpret.
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:my_app/main.dart' as app;
void main() {
final binding =
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('scrolling performance', (tester) async {
app.main();
await tester.pumpAndSettle();
final list = find.byType(Scrollable).first;
await binding.traceAction(() async {
await tester.fling(
list,
const Offset(0, -500),
10000,
);
await tester.pumpAndSettle();
}, reportKey: 'scrolling_timeline');
});
}The test should use a release-like build when performance results matter. Debug mode adds instrumentation and may produce timings that do not represent a shipped application. Run the same scenario repeatedly on the same device when comparing changes.
Running through `flutter drive`
The most useful timeline output is collected through flutter drive. Create a driver file under test_driver:
import 'package:integration_test/integration_test_driver.dart';
Future<void> main() => integrationDriver();Run the test with the driver and target the integration test file:
flutter drive \
--driver=test_driver/integration_test.dart \
--target=integration_test/app_test.dart \
-d <device_id>The <device_id> value comes from flutter devices. The driver connects the test process to the application and collects results produced by the integration test. Depending on the Flutter version and platform, the timeline report may be emitted as a JSON performance artifact. Store that artifact with the build so later runs can be compared.
Reading and improving results
Look for repeated slow frames rather than one isolated outlier. A useful investigation asks:
- Is the work in the framework build or layout phases?
- Is rasterization slow because of shadows, clipping, images, or shaders?
- Does the problem occur only on a low-end device?
- Does the test load the same data and image sizes as production?
- Did the change reduce frame time across repeated runs?
Do not add arbitrary delays to make a performance test pass. Delays hide timing problems. Instead, make the starting data controlled, use the same interaction each time, and record enough iterations to reveal a pattern.
Exercise
Why should setup happen before traceAction instead of inside its callback?
Show answer
Setup work would be included in the timeline, making it difficult to tell whether the recorded cost came from the interaction being measured or from launching, loading, or navigating to the test state.