Skip to content

Commit d26a91f

Browse files
and-olithePunderWoman
authored andcommitted
refactor(devtools): Use Chrome DevTools Performance extension API (angular#55805)
This change is a proof of concept of how the new Chrome DevTools Performance extension API (https://bit.ly/rpp-e11y) can be used to surface Angular runtime data directly in the Chrome DevTools Performance panel. Specifically, it implements the following changes: 1. Use the profiling status notification API to toggle the Timing API: The notification API is implemented under the chrome.devtools.performance extension namespace and consits of two events: ProfilingStarted and ProfilingStopped, dispatched when the Performance panel has started and stopped recording, respectively. This API is used to enable the Timings API when the recording has started in the Performance panel and disable it when recording has stopped. 2. Use the User Timings `detail` field format specification of the Performance extension API (https://developer.mozilla.org/en-US/docs/Web/API/Performance_API/User_timing) to inject data collected by the Angular Profiler into the Performance panel timeline. Angular Profiler uses several hooks to measure framework tasks like change detection. With this change, this measurements are visible in the same context as the runtime data collected by the browser in the Performance Panel timeline. Note: to enable the user timings to be collected in the first place, one needs to open the Angular DevTools panel so that the related artifacts are loaded in the page. This shortcoming can be fixed in a follow up so that the extra step isn't necessary. PR Close angular#55805
1 parent a49f7c9 commit d26a91f

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

devtools/projects/ng-devtools-backend/src/lib/hooks/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,19 @@ const endMark = (nodeName: string, method: Method) => {
3434
if (performance.getEntriesByName(start).length > 0) {
3535
// tslint:disable-next-line:ban
3636
performance.mark(end);
37-
performance.measure(name, start, end);
37+
38+
const measureOptions = {
39+
start,
40+
end,
41+
detail: {
42+
devtools: {
43+
dataType: 'track-entry',
44+
color: 'primary',
45+
track: '🅰️ Angular DevTools',
46+
},
47+
},
48+
};
49+
performance.measure(name, measureOptions);
3850
}
3951
performance.clearMarks(start);
4052
performance.clearMarks(end);

devtools/projects/ng-devtools/src/lib/devtools-tabs/devtools-tabs.component.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@
7373
</mat-tab-nav-panel>
7474

7575
<mat-menu #menu="matMenu">
76-
<div mat-menu-item disableRipple (click)="$event.stopPropagation(); toggleTimingAPI()">
77-
<mat-slide-toggle [checked]="timingAPIEnabled">
78-
Enable timing API
79-
</mat-slide-toggle>
80-
</div>
76+
@if (!profilingNotificationsSupported) {
77+
<div mat-menu-item disableRipple (click)="$event.stopPropagation(); toggleTimingAPI()">
78+
<mat-slide-toggle [checked]="timingAPIEnabled">
79+
Enable timing API
80+
</mat-slide-toggle>
81+
</div>
82+
}
8183
<div mat-menu-item disableRipple (click)="$event.stopPropagation(); themeService.toggleDarkMode(currentTheme === 'light-theme')">
8284
<mat-slide-toggle [checked]="currentTheme === 'dark-theme'">
8385
Dark Mode

devtools/projects/ng-devtools/src/lib/devtools-tabs/devtools-tabs.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ export class DevToolsTabsComponent implements OnInit, AfterViewInit {
7373
routerTreeEnabled = false;
7474
showCommentNodes = false;
7575
timingAPIEnabled = false;
76+
profilingNotificationsSupported = Boolean(
77+
(window.chrome?.devtools as any)?.performance?.onProfilingStarted,
78+
);
7679

7780
currentTheme!: Theme;
7881
routes: Route[] = [];

devtools/projects/shell-browser/src/app/app.component.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {ChangeDetectorRef, Component, inject, OnInit} from '@angular/core';
10+
import {Events, MessageBus} from 'protocol';
1011

1112
@Component({
1213
selector: 'app-root',
@@ -15,12 +16,34 @@ import {ChangeDetectorRef, Component, inject, OnInit} from '@angular/core';
1516
})
1617
export class AppComponent implements OnInit {
1718
private _cd = inject(ChangeDetectorRef);
18-
19+
private readonly _messageBus = inject<MessageBus<Events>>(MessageBus);
20+
private onProfilingStartedListener = () => {
21+
this._messageBus.emit('enableTimingAPI');
22+
};
23+
private onProfilingStoppedListener = () => {
24+
this._messageBus.emit('disableTimingAPI');
25+
};
1926
ngOnInit(): void {
2027
chrome.devtools.network.onNavigated.addListener(() => {
2128
window.location.reload();
2229
});
30+
// At the moment the chrome.devtools.performance namespace does not
31+
// have an entry in DefinitelyTyped, so this is a temporary
32+
// workaround to prevent TypeScript failures while the corresponding
33+
// type is added upstream.
34+
const chromeDevToolsPerformance = (chrome.devtools as any).performance;
35+
chromeDevToolsPerformance?.onProfilingStarted?.addListener?.(this.onProfilingStartedListener);
36+
chromeDevToolsPerformance?.onProfilingStopped?.addListener?.(this.onProfilingStoppedListener);
2337

2438
this._cd.detectChanges();
2539
}
40+
ngOnDestroy(): void {
41+
const chromeDevToolsPerformance = (chrome.devtools as any).performance;
42+
chromeDevToolsPerformance?.onProfilingStarted?.removeListener?.(
43+
this.onProfilingStartedListener,
44+
);
45+
chromeDevToolsPerformance?.onProfilingStopped?.removeListener?.(
46+
this.onProfilingStoppedListener,
47+
);
48+
}
2649
}

0 commit comments

Comments
 (0)