Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular/ssr): add performance profiler to
CommonEngine
This commit adds an option to the `CommonEngine` to enable performance profiling. When enabled, timings of a number of steps will be outputted in the server console. Example: ``` ********** Performance results ********** Retrieve SSG Page: 0.3ms Render Page: 25.4ms Inline Critical CSS: 2.3ms ***************************************** ``` To enable profiling set `enablePeformanceProfiler: true` in the `CommonEngine` options. ```ts const commonEngine = new CommonEngine({ enablePeformanceProfiler: true }); ```
- Loading branch information
1 parent
e516a4b
commit dcf3fdd
Showing
4 changed files
with
170 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
const PERFORMANCE_MARK_PREFIX = '🅰️'; | ||
|
||
export function printPerformanceLogs(): void { | ||
let maxWordLength = 0; | ||
const benchmarks: [step: string, value: string][] = []; | ||
|
||
for (const { name, duration } of performance.getEntriesByType('measure')) { | ||
if (!name.startsWith(PERFORMANCE_MARK_PREFIX)) { | ||
continue; | ||
} | ||
|
||
// `🅰️:Retrieve SSG Page` -> `Retrieve SSG Page:` | ||
const step = name.slice(PERFORMANCE_MARK_PREFIX.length + 1) + ':'; | ||
if (step.length > maxWordLength) { | ||
maxWordLength = step.length; | ||
} | ||
|
||
benchmarks.push([step, `${duration.toFixed(1)}ms`]); | ||
performance.clearMeasures(name); | ||
} | ||
|
||
/* eslint-disable no-console */ | ||
console.log('********** Performance results **********'); | ||
for (const [step, value] of benchmarks) { | ||
const spaces = maxWordLength - step.length + 5; | ||
console.log(step + ' '.repeat(spaces) + value); | ||
} | ||
console.log('*****************************************'); | ||
/* eslint-enable no-console */ | ||
} | ||
|
||
export async function runMethodAndMeasurePerf<T>( | ||
label: string, | ||
asyncMethod: () => Promise<T>, | ||
): Promise<T> { | ||
const labelName = `${PERFORMANCE_MARK_PREFIX}:${label}`; | ||
const startLabel = `start:${labelName}`; | ||
const endLabel = `end:${labelName}`; | ||
|
||
try { | ||
performance.mark(startLabel); | ||
|
||
return await asyncMethod(); | ||
} finally { | ||
performance.mark(endLabel); | ||
performance.measure(labelName, startLabel, endLabel); | ||
performance.clearMarks(startLabel); | ||
performance.clearMarks(endLabel); | ||
} | ||
} | ||
|
||
export function noopRunMethodAndMeasurePerf<T>( | ||
label: string, | ||
asyncMethod: () => Promise<T>, | ||
): Promise<T> { | ||
return asyncMethod(); | ||
} |