Measure Angular Performance
In this guide, we'll show you how to measure the performance of your Angular application using the Polaris SDK.
- First, we'll measure the intial render time of a component.
- Next, we'll measure the re-render time of a component.
- Finally, we'll measure the performance of a critical workflow in our app.
Measure Initial Render Time
First, let's measure the time it takes to render a component for the first time.
import { measureFirstRender } from '@getpolaris.ai/sdk-angular';
export class AppComponent {
constructor() {
measureFirstRender();
}
}
Let's review the code above.
- We import the
measureFirstRenderfunction from the@getpolaris.ai/sdk-angularpackage. - We call the
measureFirstRenderfunction in the constructor of theAppComponent.
You can invoke the measureFirstRender function in any component's constructor() function to measure the time it takes to render that component for the first time.
Measure Re-render Time
Next, let's measure the time it takes to re-render a component.
import { measureRender } from '@getpolaris.ai/sdk-angular';
export class AppComponent {
constructor() {
measureRender();
}
}
Let's review the code above.
- We import the
measureRenderfunction from the@getpolaris.ai/sdk-angularpackage. - We call the
measureRenderfunction in the constructor of theAppComponent.
You can invoke the measureRender function in any component's constructor() function to measure the time it takes to re-render that component.
Measure Critical Workflows
We want to measure the performance of a critical workflow in our app, such as the authentication flow.
First, we need to create an instrument in Polaris using the getInstrument() method on the PolarisService class.
export class AppComponent {
const polaris = inject(PolarisService);
onAuthenticate() {
const instrument = polaris.getInstrument('auth-flow');
instrument.start();
}
}
Let's review the code above.
- We import the
PolarisServiceclass from the@getpolaris.ai/sdk-angularpackage. - We call the
getInstrument()method on thePolarisServiceclass to create an instrument with the nameauth-flow. - We call the
start()method on the instrument to start measuring the performance of the authentication flow.
Next, we need to stop the instrument when the authentication flow is complete.
export class DashboardComponent {
const polaris = inject(PolarisService);
ngAfterViewInit() {
const instrument = polaris.getInstrument('auth-flow');
instrument.done())
}
}
We call the done() method on the instrument to stop measuring the performance of the authentication flow.