Skip to content

Commit b780a52

Browse files
docs: documentation for the Chrome DevTools integration
Initial version of the documentation describing integration with the Chrome DevTools performance panel.
1 parent 42f07ce commit b780a52

File tree

8 files changed

+105
-0
lines changed

8 files changed

+105
-0
lines changed

adev/src/app/sub-navigation-data.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,11 @@ const DOCS_SUB_NAVIGATION_DATA: NavigationItem[] = [
791791
path: 'best-practices/skipping-subtrees',
792792
contentPath: 'best-practices/runtime-performance/skipping-subtrees',
793793
},
794+
{
795+
label: 'Profiling with the Chrome DevTools',
796+
path: 'best-practices/profiling-with-chrome-devtools',
797+
contentPath: 'best-practices/runtime-performance/profiling-with-chrome-devtools',
798+
},
794799
{label: 'Zoneless', path: 'guide/zoneless', contentPath: 'guide/zoneless'},
795800
],
796801
},
891 KB
Loading
742 KB
Loading
413 KB
Loading
154 KB
Loading
427 KB
Loading
485 KB
Loading
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Profiling with the Chrome DevTools
2+
3+
Angular integrates with the [Chrome DevTools extensibility API](https://developer.chrome.com/docs/devtools/performance/extension) to present framework-specific data and insights directly in the [Chrome DevTools performance panel](https://developer.chrome.com/docs/devtools/performance/overview).
4+
5+
With the integration enabled, you can [record a performance profile](https://developer.chrome.com/docs/devtools/performance#record) containing two sets of data:
6+
7+
- Standard performance entries based on Chrome's understanding of your code executing in a browser, and
8+
- Angular-specific entries contributed by the framework's runtime.
9+
10+
Both sets of data are presented together on the same tab, but on separate tracks:
11+
12+
<img alt="Angular custom track in Chrome DevTools profiler" src="assets/images/best-practices/runtime-performance/angular-perf-in-chrome.png">
13+
14+
Angular-specific data are expressed in terms of framework concepts (components, change detection, lifecycle hooks, etc.) alongside lower-level function and method calls captured by a browser. These two data sets are correlated, and you can switch between the different views and level of details.
15+
16+
You can use the Angular track to better understand how your code runs in the browser, including:
17+
18+
- Determining whether a given code block is part of the Angular application, or whether it belongs to another script running on the same page.
19+
- Identifying performance bottlenecks and attribute those to specific components or services.
20+
- Gaining deeper insight into Angular's inner working with a visual breakdown of each change detection cycle.
21+
22+
## Recording a profile
23+
24+
### Enable integration
25+
26+
You can enable Angular profiling in one of two ways:
27+
28+
1. Run `ng.enableProfiling()` in Chrome's console panel, or
29+
1. Include a call to `enableProfiling()` in your application startup code (imported from `@angular/core`).
30+
31+
NOTE:
32+
Angular profiling works exclusively in development mode.
33+
34+
Here is an example of how you can enable the integration in the application bootstrap to capture all possible events:
35+
36+
```ts
37+
import { enableProfiling } from '@angular/core';
38+
import { bootstrapApplication } from '@angular/platform-browser';
39+
import { MyApp } from './my-app';
40+
41+
// Turn on profiling *before* bootstrapping your application
42+
// in order to capture all of the code run on start-up.
43+
enableProfiling();
44+
bootstrapApplication(MyApp);
45+
```
46+
47+
### Record a profile
48+
49+
Use the **Record** button in the Chrome DevTools performance panel:
50+
51+
<img alt="Recording a profile" src="assets/images/best-practices/runtime-performance/recording-profile-in-chrome.png">
52+
53+
See the [Chrome DevTools documentation](https://developer.chrome.com/docs/devtools/performance#record) for more details on recording profiles.
54+
55+
## Interpreting a recorded profile
56+
57+
You can use the "Angular" custom track to quickly identify and diagnose performance issues. The following sections describe some common profiling scenarios.
58+
59+
### Differentiating between your Angular application and other tasks on the same page
60+
61+
As Angular and Chrome data are presented on the separate but correlated tracks, you can see when Angular's application code is executed as opposed to some other browser processing (typically layout and paint) or other scripts running on the same page (in this case the custom Angular track does not have any data):
62+
63+
<img alt="Profile data: Angular vs. 3rd party scripts execution" src="assets/images/best-practices/runtime-performance/profile-angular-vs-3rd-party.png">
64+
65+
This allows you to determine whether further investigations should focus on the Angular application code or on other parts of your codebase or dependencies.
66+
67+
### Color-coding
68+
69+
Angular uses colors in the flame chart graph to distinguish tasks types:
70+
71+
- 🟦 Blue represents TypeScript code written by the application developer (for example: services, component constructors and lifecycle hooks, etc.).
72+
- 🟪 Purple represents templates code written by the application developer and transformed by the Angular compiler.
73+
- 🟩 Green represents entry points to the application code and identifies _reasons_ for executing code.
74+
75+
The following examples illustrate the described color-coding in various, real-life recordings.
76+
77+
#### Example: Application bootstrapping
78+
79+
The application bootstrap process usually consists of:
80+
81+
- Triggers marked in blue, such as the call to the `bootstrapApplication`, instantiation of the root component, and initial change detection
82+
- Various DI services instantiated during bootstrap, marked in green.
83+
84+
<img alt="Profile data: bootstrap application" src="assets/images/best-practices/runtime-performance/profile-bootstrap-application.png">
85+
86+
#### Example: Component execution
87+
88+
One component processing is typically represented as an entry point (blue) followed by its template execution (purple). A template might, in turn, trigger instantiation of directives and execution of lifecycle hooks (green):
89+
90+
<img alt="Profile data: component processing" src="assets/images/best-practices/runtime-performance/profile-component-processing.png">
91+
92+
#### Example: Change detection
93+
94+
A change detection cycle usually consists of one or more data synchronization passes (blue), where each pass traverses a subset of components.
95+
96+
<img alt="Profile data: change detection" src="assets/images/best-practices/runtime-performance/profile-change-detection.png">
97+
98+
With this data visualization, it is possible to immediately identify components that were involved in the change detection and which were skipped (typically the `OnPush` components that were not marked dirty).
99+
100+
Additionally, you can inspect the number of synchronization passes for one change detection. Having more than one synchronization pass suggest that state is updated during change detection. You should avoid this, as it slows down page updates and can even result in infinite loops in the worst cases.

0 commit comments

Comments
 (0)