-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathcommon.spec.ts
37 lines (30 loc) · 1.35 KB
/
common.spec.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
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Memory Leak Unit Testing
*/
interface Window {
performance: { memory: any };
}
declare let window: Window;
export const inMB: (n: any) => number = (n: any) => n / 1000000;
const runningAverage: any = (arr: any, newVal: any, oldAvg: any) => ((oldAvg * (arr.length - 1) + newVal) / arr.length);
export const profile: any = {
samples: [] as any,
diffs: [] as any,
averageUsage: 0,
averageChange: 0, //Collects a sample of memory and updates all the values in the profile object
sample(): void {
const newSample: any = getMemoryProfile();
this.samples.push(newSample);
this.averageUsage = runningAverage(this.samples, newSample, this.averageUsage);
const sampleLen: any = this.samples.length;
if (sampleLen >= 2) {
const newDiff: number = this.samples[sampleLen - 1] - this.samples[sampleLen - 2];
this.diffs.push(newDiff);
this.averageChange = runningAverage(this.diffs, newDiff, this.averageChange);
}
}
};
export const getMemoryProfile: any = () => window.performance.memory.usedJSHeapSize; //Return used memory
//Check average change in memory samples to not be over 10MB
//Check the final memory usage against the first usage, there should be little change if everything was properly deallocated