Sub topic 3
Recording a trace in the Performance panel
This chapter assumes you can run an Angular app with the Angular CLI, and that you know how to open Chrome DevTools and use the Console. You will record a trace of one user action and read the first things it shows.
What a trace is
A trace is a recording of what the browser did over a span of time. It covers script execution, style calculation, layout, painting, and network activity. The Performance panel draws this recording as a set of tracks on a shared timeline.
A good trace is short and focused. Record one action, such as typing in a search box or opening a dialog. A trace of several minutes of mixed activity is hard to read, and nothing in it tells you which part to look at.
Prepare the recording
- Serve a production-like build of the app, as described in the earlier chapter on production builds.
- Open the app in a fresh Incognito window. This keeps extensions from adding their own work to the trace.
- Open DevTools and switch to the Performance tab.
- Get the page into the state just before the action you want to measure. Load any data first.
The panel's capture settings include a Screenshots option. Turn it on. Screenshots let you see what the user saw at each moment of the trace.
Record the interaction
There are two ways to record.
- Record and reload starts a recording, reloads the page, and stops once the page has loaded. Use it for load performance.
- Record starts and stops on your clicks. Use it for runtime interactions.
For a runtime interaction, follow this sequence:
- Click Record.
- Perform the action once, such as typing three characters into the filter field.
- Wait until the page looks settled.
- Click Stop.
The panel then processes the data and shows the result.
Find your way around the result
The result has three main areas.
| Area | What it shows |
|---|---|
| Overview at the top | CPU activity, screenshots, and a graph over the whole recording. Drag across it to select a time range. |
| Tracks in the middle | Main thread, Network, Frames, Timings, and others. The Main track holds the flame chart. |
| Details at the bottom | Summary, Bottom-Up, Call Tree, and Event Log for the selected range or event. |
Select the part of the overview where the action happened. The tracks and the Summary tab then describe only that range. The Summary shows a pie of time spent in Scripting, Rendering, Painting, and Idle. A large Scripting share on a short interaction is the first sign that JavaScript is the cost.
Add labels to find your own code
Your code is hard to spot in a large trace. You can mark it with the User Timing API, and the marks appear in the Timings track.
filterItems(term: string) {
performance.mark('filter-start');
const result = this.items.filter(i => i.name.includes(term));
performance.measure('filter', 'filter-start');
return result;
}After recording again, look for a bar named filter in the Timings track. Its width is the time between the mark and the measure.
Common mistakes
- Recording too long, which buries the interaction in idle time.
- Recording a development build, which adds checks that distort the numbers.
- Leaving extensions enabled, which adds unrelated work.
- Starting to record before the page has finished loading its data.
Exercise
You want to measure how long a click on a "Sort" button takes. Describe the steps you would take to record that, and name the track where your own performance.measure call would appear.
Show answer
Serve a production-like build, open it in an Incognito window, and open the Performance panel. Load the page and its data first, then click Record, click "Sort" once, wait for the page to settle, and click Stop. Select the range around the click in the overview. A performance.measure call appears in the Timings track.