-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathMemTestHelper.ts
160 lines (140 loc) · 4.95 KB
/
MemTestHelper.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS202: Simplify dynamic range loops
* DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
import Bacon from "../dist/Bacon";
const toKb = x => (x / 1024).toFixed(2) + ' KiB';
const toMb = x => (x / 1024 / 1024).toFixed(2) + ' MiB';
const byteFormat = function(bytes, comparison) {
if (Math.abs(comparison || bytes) > (512 * 1024)) {
return toMb(bytes);
} else {
return toKb(bytes);
}
};
const lpad = function(string, length) {
if (length == null) { length = 12; }
while(string.length < length) {
string = ` ${string}`;
}
return string;
};
const rpad = function(string, length) {
if (length == null) { length = 12; }
while(string.length < length) {
string = `${string} `;
}
return string;
};
const measure = function(fun) {
global.gc();
const lastMemoryUsage = process.memoryUsage().heapUsed;
const startTime = Date.now();
fun();
const duration = Date.now() - startTime;
global.gc();
return [process.memoryUsage().heapUsed - lastMemoryUsage, duration];
};
const sum = xs => xs.reduce((sum, x) => sum + x);
const mean = xs => sum(xs) / xs.length;
const stddev = function(xs) {
const avg = mean(xs);
return Math.pow(mean(Array.from(xs).map((x) => Math.pow((x - avg), 2))), 0.5);
};
const processResults = function(results, i) {
const values = (Array.from(results).map((x) => x[i]));
return {
mean: mean(values.slice(2)),
stddev: stddev(values.slice(2))
};
};
const printResult = function(label, result, forcePrefix) {
if (forcePrefix == null) { forcePrefix = false; }
var prefix = prefix && (result.mean > 0) ? '+' : '';
return console.log(` ${rpad(label, 20)}`, lpad(prefix + byteFormat(result.mean), 12), '\u00b1', byteFormat(result.stddev, result.mean));
};
const createNObservable = function(count, generator) {
let withoutSubscriber, withSubscriber, afterCleanup, reSubscribe, afterReCleanup;
const n = Math.floor(count / 10);
const m = 10;
const results = (() => {
const result = [];
for (var j = 0, i = j, end = m, asc = 0 <= end; asc ? j < end : j > end; asc ? j++ : j--, i = j) {
global.gc();
var objects = new Array(n); // Preallocate array of n elements
var unsubscribers = new Array(n);
const subscribe = () =>
(() => {
let asc1, end1;
const result1 = [];
for (i = 0, end1 = objects.length, asc1 = 0 <= end1; asc1 ? i < end1 : i > end1; asc1 ? i++ : i--) {
result1.push(unsubscribers[i] = objects[i].onValue(noop));
}
return result1;
})()
;
const unsubscribe = () =>
(() => {
let asc1, end1;
const result1 = [];
for (i = 0, end1 = objects.length, asc1 = 0 <= end1; asc1 ? i < end1 : i > end1; asc1 ? i++ : i--) {
unsubscribers[i]();
result1.push(unsubscribers[i] = null);
}
return result1;
})()
;
global.gc();
withoutSubscriber = measure(() =>
(() => {
let asc1, end1;
const result1 = [];
for (i = 0, end1 = objects.length, asc1 = 0 <= end1; asc1 ? i < end1 : i > end1; asc1 ? i++ : i--) {
result1.push(objects[i] = generator(i));
}
return result1;
})());
withSubscriber = measure(subscribe);
afterCleanup = measure(unsubscribe);
reSubscribe = measure(subscribe);
afterReCleanup = measure(unsubscribe);
objects = null;
unsubscribers = null;
result.push([withoutSubscriber[0]/n, withSubscriber[0]/n, afterCleanup[0]/n, reSubscribe[0]/n, afterReCleanup[0]/n]);
}
return result;
})();
withoutSubscriber = processResults(results, 0);
withSubscriber = processResults(results, 1);
afterCleanup = processResults(results, 2);
reSubscribe = processResults(results, 3);
afterReCleanup = processResults(results, 4);
printResult('w/o subscription', withoutSubscriber);
printResult('with subscription', withSubscriber, true);
printResult('unsubscribe', afterCleanup, true);
printResult('subscribe again', reSubscribe, true);
return printResult('unsubscribe again', afterReCleanup, true);
};
const title = text => console.log(`\n${text}`);
var noop = function() {};
// Keep reference to listeners during test run
const fakeSource = {
listeners: [],
subscribe(listener) { return this.listeners.push(listener); },
unsubscribe(listener) {
const index = this.listeners.indexOf(listener);
if (index !== -1) { return this.listeners.splice(index, 1); }
}
};
const eventStream = () =>
new Bacon.EventStream(function(sink) {
fakeSource.subscribe(sink);
return () => fakeSource.unsubscribe(sink);
})
;
export default { createNObservable, eventStream, title, noop };