test(ivy): improve microbenchmark reporting progress (#33386)

PR Close #33386
This commit is contained in:
Miško Hevery 2019-10-24 16:06:42 -07:00 committed by Andrew Kushnir
parent a17cc9beee
commit 3f195fefa9
1 changed files with 38 additions and 15 deletions

View File

@ -15,7 +15,6 @@ const MIN_SAMPLE_COUNT_NO_IMPROVEMENT = 500;
// that it is less likely that we will be bothered by GC or preemptive multi tasking. // that it is less likely that we will be bothered by GC or preemptive multi tasking.
const MIN_SAMPLE_DURATION = 3; const MIN_SAMPLE_DURATION = 3;
const UNITS = ['ms', 'us', 'ns', 'ps'];
export interface Benchmark { export interface Benchmark {
(versionName: string): Profile; (versionName: string): Profile;
report(fn?: (report: string) => void): void; report(fn?: (report: string) => void): void;
@ -43,7 +42,8 @@ export function createBenchmark(benchmarkName: string): Benchmark {
// this is the first time we are executing // this is the first time we are executing
iterationCounter = profile.iterationCount; iterationCounter = profile.iterationCount;
runAgain = true; runAgain = true;
// console.log('profiling', profileName, '...'); // tslint:disable-next-line:no-console
console.log(profileName, '...');
} else { } else {
profile.sampleCount++; profile.sampleCount++;
// we came to an end of a sample, compute the time. // we came to an end of a sample, compute the time.
@ -63,8 +63,9 @@ export function createBenchmark(benchmarkName: string): Benchmark {
runAgain = true; runAgain = true;
} }
if (!runAgain) { if (!runAgain) {
// console.log(' Sample count:', profile.sampleCount, 'iterations', // tslint:disable-next-line:no-console
// profile.iterationCount, 'time (ms):', iterationTime_ms); console.log(
` ${formatTime(iterationTime_ms)} (count: ${profile.sampleCount}, iterations: ${profile.iterationCount})`);
} }
} }
iterationCounter = profile.iterationCount; iterationCounter = profile.iterationCount;
@ -89,18 +90,40 @@ export function createBenchmark(benchmarkName: string): Benchmark {
const fastest = profiles.reduce((previous: Profile, current: Profile) => { const fastest = profiles.reduce((previous: Profile, current: Profile) => {
return (previous.bestTime < current.bestTime) ? previous : current; return (previous.bestTime < current.bestTime) ? previous : current;
}); });
let unitOffset = 0; const unitOffset = findUnit(fastest.bestTime);
let time = fastest.bestTime; (fn || console.log)(`\nBenchmark: ${benchmarkName}\n${profiles.map((profile: Profile) => {
while (time < 1 && time !== 0) { const time = formatTime(profile.bestTime, unitOffset);
time = time * 1000; const percent = formatPercent(1 - profile.bestTime / fastest.bestTime);
unitOffset++; return ` ${profile.profileName}: ${time}(${percent}) `;
}
let unit: string = UNITS[unitOffset];
(fn || console.log)(`Benchmark: ${benchmarkName}\n${profiles.map((profile: Profile) => {
const time = (profile.bestTime * Math.pow(1000, unitOffset)).toFixed(3);
const percent = (100 - profile.bestTime / fastest.bestTime * 100).toFixed(0);
return ' ' + profile.profileName + ': ' + time + ' ' +unit + '(' + percent + '%)';
}).join('\n')}`); }).join('\n')}`);
}; };
return benchmark; return benchmark;
} }
enum UNITS {
ms = 0,
us = 1,
ns = 2,
ps = 3,
}
function findUnit(time_ms: number): UNITS {
let unitOffset = UNITS.ms;
while (time_ms < 1 && time_ms !== 0) {
time_ms = time_ms * 1000;
unitOffset++;
}
return unitOffset;
}
function formatTime(value: number, unitOffset?: number): string {
if (unitOffset === undefined) {
unitOffset = findUnit(value);
}
const time = (value * Math.pow(1000, unitOffset)).toFixed(3);
return time + ' ' + UNITS[unitOffset];
}
function formatPercent(value: number): string {
return (value * 100).toFixed(0) + '%';
}