Sub topic 11
Adding Your Own Timeline Events
Assume that you can run a Flutter app in profile mode and use the DevTools Performance view. This chapter shows how to label your own synchronous work so that it is easier to find in Timeline Events.
Why add custom events?
Flutter and Dart already record many framework and runtime events. However, application-specific work can be difficult to recognize. A method named parseCatalog might appear as an anonymous callback or as a group of small operations.
A custom timeline event gives that work a clear name. The event appears in the Performance timeline while the code runs. You can then compare its duration with the slow frame that contains it.
Custom events help answer questions such as:
- Did parsing happen during the slow frame?
- How long did parsing take?
- Did a change reduce the parsing time?
- Is the work running once, or on every rebuild?
A custom event labels work. It does not move the work to another thread or make it faster by itself.
Marking synchronous work
Use Timeline.timeSync from dart:developer when the operation starts and finishes during one synchronous call. The method records the duration and returns the callback's result.
import 'dart:developer' as developer;
List<String> parseCatalog(String raw) {
return developer.Timeline.timeSync(
'parse_catalog',
() {
return raw
.split('\n')
.where((line) => line.isNotEmpty)
.toList();
},
);
}
void main() {
final catalog = parseCatalog('apples\noranges\nbananas');
print(catalog);
}In a Flutter app, call parseCatalog from the part of the application that loads or processes the catalog. Then run the app in profile mode, start recording in the Performance view, and reproduce the slow interaction.
Select the relevant frame in the Flutter frames chart. In Timeline Events, search for parse_catalog or locate the event under the UI thread. The event width represents its duration. A wide event means that the operation occupied the thread for longer.
Recording nested work
You can mark a larger operation and its sub-operations. Nested events show which part of the larger operation consumed time.
import 'dart:developer' as developer;
List<String> loadCatalog(String raw) {
return developer.Timeline.timeSync(
'load_catalog',
() {
return developer.Timeline.timeSync(
'parse_catalog',
() => raw
.split('\n')
.where((line) => line.isNotEmpty)
.toList(),
);
},
);
}The load_catalog event includes the parsing event. This structure helps you separate parsing from other work, such as validation or conversion.
Do not wrap every small function in a timeline event. Excessive labels make a trace harder to read and add tracing overhead. Mark operations that are large enough to affect a frame or important enough to measure.
Handling asynchronous work
Timeline.timeSync measures synchronous code. It does not keep one event open while an asynchronous operation waits for a future. Mark the synchronous sections around asynchronous work instead.
For example, you might create one event for preparing a request and another for processing the response. The network wait itself does not block the UI thread and usually does not explain UI-thread jank.
flowchart TD
A[User interaction] --> B[Start recording]
B --> C[Reproduce slow action]
C --> D[Select slow frame]
D --> E[Find custom event]
E --> F[Change targeted code]
F --> G[Record the same action again]
G --> H[Compare event duration]caption: Finding a custom timeline event during a profiling loop alt: A flowchart showing recording, reproducing a slow action, locating a custom event, changing code, and measuring again.
Compare before and after
Record a baseline before changing the code. Note the selected frame and the duration of your custom event. After making one targeted change, repeat the same interaction under similar conditions.
A shorter custom event is useful evidence, but also check the frames chart. The frame must fit the device's frame budget, and no new raster-thread bottleneck should replace the original UI-thread problem.
Exercise
What does a custom timeline event tell you, and what does it not do?
Show answer
It labels and measures a section of code in DevTools. It does not move the code to another thread or improve its performance automatically.