perf: report cold and minimal times when profiling in a browser (#33653)

PR Close #33653
This commit is contained in:
Pawel Kozlowski 2019-11-07 16:22:12 +01:00 committed by Andrew Scott
parent 81828ae7f4
commit e8d43d8fde
1 changed files with 18 additions and 4 deletions

View File

@ -42,19 +42,33 @@ export function bindAction(selector: string, callback: () => void) {
export function profile(create: () => void, destroy: () => void, name: string) {
return function() {
window.console.profile(name);
let duration = 0;
const noOfRuns = 150;
let durations: number[] = [];
let count = 0;
while (count++ < 150) {
while (count++ < noOfRuns) {
const start = window.performance.now();
create();
duration += window.performance.now() - start;
const end = window.performance.now() - start;
durations.push(end);
destroy();
}
window.console.profileEnd();
window.console.log(`Iterations: ${count}; time: ${duration / count} ms / iteration`);
reportProfileResults(durations, noOfRuns);
};
}
function reportProfileResults(durations: number[], count: number) {
const totalDuration = durations.reduce((soFar: number, duration: number) => soFar + duration, 0);
const avgDuration = (totalDuration / count).toFixed(2);
const minDuration = durations
.reduce(
(soFar: number, duration: number) => Math.min(soFar, duration),
Number.MAX_SAFE_INTEGER)
.toFixed(2);
window.console.log(
`Iterations: ${count}; cold time: ${durations[0].toFixed(2)} ms; average time: ${avgDuration} ms / iteration; best time: ${minDuration} ms`);
}
// helper script that will read out the url parameters
// and store them in appropriate form fields on the page
function urlParamsToForm() {