Devtools profiler calls by pkozlowski-opensource · Pull Request #59233 · angular/angular · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/core/src/application/application_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import {NgZone} from '../zone/ng_zone';
import {ApplicationInitStatus} from './application_init';
import {TracingAction, TracingService, TracingSnapshot} from './tracing';
import {EffectScheduler} from '../render3/reactivity/root_effect_scheduler';
import {ProfilerEvent} from '../render3/profiler_types';
import {profiler} from '../render3/profiler';

/**
* A DI token that provides a set of callbacks to
Expand Down Expand Up @@ -531,6 +533,8 @@ export class ApplicationRef {
componentOrFactory: ComponentFactory<C> | Type<C>,
rootSelectorOrNode?: string | any,
): ComponentRef<C> {
profiler(ProfilerEvent.BootstrapComponentStart);

(typeof ngDevMode === 'undefined' || ngDevMode) && warnIfDestroyed(this._destroyed);
const isComponentFactory = componentOrFactory instanceof ComponentFactory;
const initStatus = this._injector.get(ApplicationInitStatus);
Expand Down Expand Up @@ -576,6 +580,9 @@ export class ApplicationRef {
const _console = this._injector.get(Console);
_console.log(`Angular is running in development mode.`);
}

profiler(ProfilerEvent.BootstrapComponentEnd, compRef);

return compRef;
}

Expand All @@ -598,6 +605,8 @@ export class ApplicationRef {

/** @internal */
_tick = (): void => {
profiler(ProfilerEvent.ChangeDetectionStart);

if (this.tracingSnapshot !== null) {
const snapshot = this.tracingSnapshot;
this.tracingSnapshot = null;
Expand All @@ -622,7 +631,6 @@ export class ApplicationRef {
try {
this._runningTick = true;
this.synchronize();

if (typeof ngDevMode === 'undefined' || ngDevMode) {
for (let view of this.allViews) {
view.checkNoChanges();
Expand All @@ -635,6 +643,8 @@ export class ApplicationRef {
this._runningTick = false;
setActiveConsumer(prevConsumer);
this.afterTick.next();

profiler(ProfilerEvent.ChangeDetectionEnd);
}
};

Expand All @@ -653,7 +663,9 @@ export class ApplicationRef {

let runs = 0;
while (this.dirtyFlags !== ApplicationRefDirtyFlags.None && runs++ < MAXIMUM_REFRESH_RERUNS) {
profiler(ProfilerEvent.ChangeDetectionSyncStart);
this.synchronizeOnce();
profiler(ProfilerEvent.ChangeDetectionSyncEnd);
}

if ((typeof ngDevMode === 'undefined' || ngDevMode) && runs >= MAXIMUM_REFRESH_RERUNS) {
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/application/create_application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import {Type} from '../interface/type';
import {createOrReusePlatformInjector} from '../platform/platform';
import {assertStandaloneComponentType} from '../render3/errors';
import {EnvironmentNgModuleRefAdapter} from '../render3/ng_module_ref';
import {NgZone} from '../zone/ng_zone';

import {_callAndReportToErrorHandler, ApplicationRef} from './application_ref';
import {ChangeDetectionScheduler} from '../change_detection/scheduling/zoneless_scheduling';
import {ChangeDetectionSchedulerImpl} from '../change_detection/scheduling/zoneless_scheduling_impl';
import {bootstrap} from '../platform/bootstrap';
import {profiler} from '../render3/profiler';
import {ProfilerEvent} from '../render3/profiler_types';

/**
* Internal create application API that implements the core application creation logic and optional
Expand All @@ -37,6 +38,7 @@ export function internalCreateApplication(config: {
appProviders?: Array<Provider | EnvironmentProviders>;
platformProviders?: Provider[];
}): Promise<ApplicationRef> {
profiler(ProfilerEvent.BootstrapApplicationStart);
try {
const {rootComponent, appProviders, platformProviders} = config;

Expand Down Expand Up @@ -69,5 +71,7 @@ export function internalCreateApplication(config: {
});
} catch (e) {
return Promise.reject(e);
} finally {
profiler(ProfilerEvent.BootstrapApplicationEnd);
}
}
6 changes: 6 additions & 0 deletions packages/core/src/defer/rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import {
getTDeferBlockDetails,
getTemplateIndexForState,
} from './utils';
import {profiler} from '../render3/profiler';
import {ProfilerEvent} from '../render3/profiler_types';

/**
* **INTERNAL**, avoid referencing it in application code.
Expand Down Expand Up @@ -243,6 +245,8 @@ function applyDeferBlockState(
tNode: TNode,
hostLView: LView<unknown>,
) {
profiler(ProfilerEvent.DeferBlockStateStart);

const stateTmplIndex = getTemplateIndexForState(newState, hostLView, tNode);

if (stateTmplIndex !== null) {
Expand Down Expand Up @@ -306,6 +310,8 @@ function applyDeferBlockState(
lDetails[ON_COMPLETE_FNS] = null;
}
}

profiler(ProfilerEvent.DeferBlockStateEnd);
}

/**
Expand Down
21 changes: 17 additions & 4 deletions packages/core/src/render3/after_render/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
} from '../../change_detection/scheduling/zoneless_scheduling';
import {type DestroyRef} from '../../linker/destroy_ref';
import {TracingAction, TracingService, TracingSnapshot} from '../../application/tracing';
import {profiler} from '../profiler';
import {ProfilerEvent} from '../profiler_types';

export class AfterRenderManager {
impl: AfterRenderImpl | null = null;
Expand Down Expand Up @@ -65,6 +67,12 @@ export class AfterRenderImpl {
* might be scheduled.
*/
execute(): void {
const hasSequencesToExecute = this.sequences.size > 0;

if (hasSequencesToExecute) {
profiler(ProfilerEvent.AfterRenderHooksStart);
}

this.executing = true;
for (const phase of AFTER_RENDER_PHASES) {
for (const sequence of this.sequences) {
Expand All @@ -74,10 +82,11 @@ export class AfterRenderImpl {

try {
sequence.pipelinedValue = this.ngZone.runOutsideAngular(() =>
this.maybeTrace(
() => sequence.hooks[phase]!(sequence.pipelinedValue),
sequence.snapshot,
),
this.maybeTrace(() => {
const hookFn = sequence.hooks[phase]!;
const value = hookFn(sequence.pipelinedValue);
return value;
}, sequence.snapshot),
);
} catch (err) {
sequence.erroredOrDestroyed = true;
Expand Down Expand Up @@ -105,6 +114,10 @@ export class AfterRenderImpl {
this.scheduler.notify(NotificationSource.DeferredRenderHook);
}
this.deferredRegistrations.clear();

if (hasSequencesToExecute) {
profiler(ProfilerEvent.AfterRenderHooksEnd);
}
}

register(sequence: AfterRenderSequence): void {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/render3/component_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ import {getComponentLViewByIndex, getNativeByTNode, getTNode} from './util/view_
import {ViewRef} from './view_ref';
import {ChainedInjector} from './chained_injector';
import {unregisterLView} from './interfaces/lview_tracking';
import {profiler} from './profiler';
import {ProfilerEvent} from './profiler_types';

export class ComponentFactoryResolver extends AbstractComponentFactoryResolver {
/**
Expand Down Expand Up @@ -214,6 +216,8 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
rootSelectorOrNode?: any,
environmentInjector?: NgModuleRef<any> | EnvironmentInjector | undefined,
): AbstractComponentRef<T> {
profiler(ProfilerEvent.DynamicComponentStart);

const prevConsumer = setActiveConsumer(null);
try {
// Check if the component is orphan
Expand Down Expand Up @@ -397,6 +401,7 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
unregisterLView(rootLView);
throw e;
} finally {
profiler(ProfilerEvent.DynamicComponentEnd);
leaveView();
}

Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/render3/instructions/change_detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ import {
} from './shared';
import {runEffectsInView} from '../reactivity/view_effect_runner';
import {isDestroyed} from '../interfaces/type_checks';
import {ProfilerEvent} from '../profiler_types';
import {profiler} from '../profiler';

/**
* The maximum number of times the change detection traversal will rerun before throwing an error.
Expand Down Expand Up @@ -428,8 +430,12 @@ function detectChangesInComponent(
mode: ChangeDetectionMode,
): void {
ngDevMode && assertEqual(isCreationMode(hostLView), false, 'Should be run in update mode');
profiler(ProfilerEvent.ComponentStart);

const componentView = getComponentLViewByIndex(componentHostIdx, hostLView);
detectChangesInViewIfAttached(componentView, mode);

profiler(ProfilerEvent.ComponentEnd, componentView[CONTEXT] as any as {});
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/render3/instructions/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
TVIEW,
TView,
} from '../interfaces/view';
import {profiler} from '../profiler';
import {ProfilerEvent} from '../profiler_types';
import {enterView, leaveView} from '../state';
import {getComponentLViewByIndex, isCreationMode} from '../util/view_utils';

Expand All @@ -38,7 +40,11 @@ export function renderComponent(hostLView: LView, componentHostIdx: number) {
componentView[HYDRATION] = retrieveHydrationInfo(hostRNode, componentView[INJECTOR]);
}

profiler(ProfilerEvent.ComponentStart);

renderView(componentTView, componentView, componentView[CONTEXT]);

profiler(ProfilerEvent.ComponentEnd, componentView[CONTEXT] as any as {});
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/render3/instructions/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ export function processHostBindingOpCodes(tView: TView, lView: LView): void {
const hostBindingFn = hostBindingOpCodes[++i] as HostBindingsFunction<any>;
setBindingRootForHostBindings(bindingRootIndx, directiveIdx);
const context = lView[directiveIdx];

profiler(ProfilerEvent.HostBindingsUpdateStart, context);
hostBindingFn(RenderFlags.Update, context);
profiler(ProfilerEvent.HostBindingsUpdateEnd, context);
}
}
} finally {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/render3/profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const setProfiler = (profiler: Profiler | null) => {
* execution context
* @returns
*/
export const profiler: Profiler = function (event, instance, hookOrListener) {
export const profiler: Profiler = function (event, instance = null, hookOrListener) {
if (profilerCallback != null /* both `null` and `undefined` */) {
profilerCallback(event, instance, hookOrListener);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/render3/profiler_types.ts
Loading