forked from mhevery/AngularConnect-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.ts
96 lines (91 loc) · 3.64 KB
/
benchmark.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import {performance} from 'perf_hooks';
const MIN_SAMPLE_COUNT_NO_IMPROVEMENT = 100;
const MIN_SAMPLE_DURATION = 2;
const UNITS = ['ms', 'us', 'ns', 'ps'];
export interface Benchmark {
(versionName: string): Profile;
report(fn?: (report: string) => void): void;
}
export interface Profile {
(): boolean;
profileName: string;
bestTime: number;
iterationCount: number;
sampleCount: number;
noImprovementCount: number;
}
export function createBenchmark(benchmarkName: string): Benchmark {
const profiles: Profile[] = [];
const benchmark = function Benchmark(profileName: string): Profile {
let iterationCounter: number = 0;
let timestamp: number = 0;
const profile: Profile = function Profile(): boolean {
if (iterationCounter === 0) {
let runAgain = false;
// if we reached the end of the iteration count than we should decide what to do next.
if (timestamp === 0) {
// this is the first time we are executing
iterationCounter = profile.iterationCount;
runAgain = true;
//console.log('profiling', profileName, '...');
} else {
profile.sampleCount++;
// we came to an end of a sample, compute the time.
const duration_ms = performance.now() - timestamp;
const iterationTime_ms = Math.max((duration_ms / profile.iterationCount), 0);
if (profile.bestTime > iterationTime_ms) {
profile.bestTime = iterationTime_ms;
profile.noImprovementCount = 0;
runAgain = true;
} else {
runAgain = (profile.noImprovementCount++) < MIN_SAMPLE_COUNT_NO_IMPROVEMENT;
}
if (duration_ms < MIN_SAMPLE_DURATION) {
// we have not ran for long enough so increase the iteration count.
profile.iterationCount = Math.max(
// As a sanity if duration_ms is 0 just double the count.
profile.iterationCount << 1,
// Otherwise try to guess how many iterations we have to do to get the right time.
Math.round(MIN_SAMPLE_DURATION / duration_ms * profile.iterationCount)
);
profile.noImprovementCount = 0;
runAgain = true;
}
// console.log(' Sample count:', profile.sampleCount, 'iterations', profile.iterationCount, 'time (ms):', iterationTime_ms);
}
iterationCounter = profile.iterationCount;
timestamp = performance.now();
return runAgain;
} else {
// this is the common path and it needs te be quick!
iterationCounter--;
return true;
}
} as Profile;
profile.profileName = profileName;
profile.bestTime = Number.MAX_SAFE_INTEGER;
profile.iterationCount = 1;
profile.noImprovementCount = 0;
profile.sampleCount = 0;
profiles.push(profile);
return profile;
} as Benchmark;
benchmark.report = function(fn?: (report: string) => void) {
const fastest = profiles.reduce((previous: Profile, current: Profile) => {
return (previous.bestTime < current.bestTime) ? previous : current;
});
let unitOffset = 0;
let time = fastest.bestTime;
while (time < 1 && time !== 0) {
time = time * 1000;
unitOffset++;
}
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')}`);
};
return benchmark;
}