forked from mozilla/pluotsorbet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.js
403 lines (375 loc) · 12.9 KB
/
benchmark.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
var Benchmark = (function() {
function mean(array) {
function add(a, b) {
return a + b;
}
return array.reduce(add, 0) / array.length;
}
var defaultStorage = {
// 30 is usually considered a large enough sample size for the central limit theorem
// to take effect, unless the distribution is too weird
numRounds: 30,
roundDelay: 5000, // ms to delay starting next round of tests
baseline: {},
current: {},
running: false,
round: 0,
deleteFs: false,
deleteJitCache: false,
buildBaseline: false,
recordMemory: true
};
var NO_SECURITY = typeof netscape !== "undefined" && netscape.security.PrivilegeManager;
function enableSuperPowers() {
// To enable chrome privileges use a separate profile and set the pref
// security.turn_off_all_security_so_that_viruses_can_take_over_this_computer
// to boolean true. To do this on a device, see:
// https://wiki.mozilla.org/B2G/QA/Tips_And_Tricks#For_changing_the_preference:
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
function forceCollectors() {
if (!NO_SECURITY) {
return Promise.resolve();
}
return new Promise(function(resolve, reject) {
enableSuperPowers();
console.log("Starting minimize memory.");
var gMgr = Components.classes["@mozilla.org/memory-reporter-manager;1"].getService(Components.interfaces.nsIMemoryReporterManager);
Components.utils.import("resource://gre/modules/Services.jsm");
Services.obs.notifyObservers(null, "child-mmu-request", null);
gMgr.minimizeMemoryUsage(function() {
console.log("Finished minimize memory.");
resolve();
});
});
}
var STORAGE_KEY = "benchmark";
var storage;
function initStorage(defaults) {
if (!(STORAGE_KEY in localStorage)) {
storage = defaults;
} else {
storage = JSON.parse(localStorage[STORAGE_KEY]);
for (var key in defaults) {
if (key in storage) {
continue;
}
storage[key] = defaults[key];
}
}
}
function saveStorage() {
localStorage[STORAGE_KEY] = JSON.stringify(storage);
}
initStorage(defaultStorage);
var LEFT = 0; var CENTER = 1; var RIGHT = 2;
function prettyTable(rows, alignment) {
function pad(str, repeat, n, align) {
if (align === LEFT) {
return str.padRight(repeat, n);
} else if (align === CENTER) {
var middle = ((n - str.length) / 2) | 0;
return str.padRight(repeat, middle + str.length).padLeft(repeat, n);
} else if (align === RIGHT) {
return str.padLeft(repeat, n);
}
throw new Error("Bad align value." + align);
}
var maxColumnLengths = [];
var numColumns = rows[0].length;
for (var colIndex = 0; colIndex < numColumns; colIndex++) {
var maxLength = 0;
for (var rowIndex = 0; rowIndex < rows.length; rowIndex++) {
maxLength = Math.max(rows[rowIndex][colIndex].toString().length, maxLength);
}
maxColumnLengths[colIndex] = maxLength;
}
var out = "";
for (var rowIndex = 0; rowIndex < rows.length; rowIndex++) {
out += "| ";
for (var colIndex = 0; colIndex < numColumns; colIndex++) {
out += pad(rows[rowIndex][colIndex].toString(), " ", maxColumnLengths[colIndex], rowIndex === 0 ? CENTER : alignment[colIndex]) + " | ";
}
out += "\n";
if (rowIndex === 0) {
out += "|";
for (var colIndex = 0; colIndex < numColumns; colIndex++) {
var align = alignment[colIndex];
if (align === 0) {
out += ":".padRight("-", maxColumnLengths[colIndex] + 2);
} else if (align === 1) {
out += ":".padLeft("-", maxColumnLengths[colIndex] + 1) + ":";
} else if (align === 2) {
out += ":".padLeft("-", maxColumnLengths[colIndex] + 2);
}
out += "|";
}
out += "\n";
}
}
return out;
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function msFormatter(x) {
return numberWithCommas(Math.round(x)) + "ms";
}
function byteFormatter(x) {
return numberWithCommas(Math.round(x / 1024)) + "kb";
}
var valueFormatters = {
startupTime: msFormatter,
totalSize: byteFormatter,
domSize: byteFormatter,
styleSize: byteFormatter,
jsObjectsSize: byteFormatter,
jsStringsSize: byteFormatter,
jsOtherSize: byteFormatter,
otherSize: byteFormatter,
};
function sampleMemory() {
if (!NO_SECURITY) {
return Promise.resolve({});
}
return forceCollectors().then(function() {
var memoryReporter = Components.classes["@mozilla.org/memory-reporter-manager;1"].getService(Components.interfaces.nsIMemoryReporterManager);
var jsObjectsSize = {};
var jsStringsSize = {};
var jsOtherSize = {};
var domSize = {};
var styleSize = {};
var otherSize = {};
var totalSize = {};
var jsMilliseconds = {};
var nonJSMilliseconds = {};
try {
memoryReporter.sizeOfTab(window.parent.window, jsObjectsSize, jsStringsSize, jsOtherSize,
domSize, styleSize, otherSize, totalSize, jsMilliseconds, nonJSMilliseconds);
} catch (e) {
console.log(e);
}
return {
totalSize: totalSize.value,
domSize: domSize.value,
styleSize: styleSize.value,
jsObjectsSize: jsObjectsSize.value,
jsStringsSize: jsStringsSize.value,
jsOtherSize: jsOtherSize.value,
otherSize: otherSize.value,
};
});
}
var startup = {
run: function(settings) {
storage.round = 0;
var current = storage.current = {};
current.startupTime = [];
if (settings.recordMemory) {
storage.recordMemory = true;
current.totalSize = [];
current.domSize = [];
current.styleSize = [];
current.jsObjectsSize = [];
current.jsStringsSize = [];
current.jsOtherSize = [];
current.otherSize = [];
}
storage.running = true;
storage.numRounds = "numRounds" in settings ? settings.numRounds : defaultStorage.numRounds;
storage.roundDelay = "roundDelay" in settings ? settings.roundDelay : defaultStorage.roundDelay;
storage.deleteFs = "deleteFs" in settings ? settings.deleteFs : defaultStorage.deleteFs;
storage.deleteJitCache = "deleteJitCache" in settings ? settings.deleteJitCache : defaultStorage.deleteJitCache;
storage.buildBaseline = "buildBaseline" in settings ? settings.buildBaseline : defaultStorage.buildBaseline;
if (storage.buildBaseline) {
storage.baseline = {};
}
saveStorage();
this.runNextRound();
},
startTimer: function() {
if (!storage.running) {
console.log("startTimer called while benchmark not running");
return;
}
this.startTime = performance.now();
},
stopTimer: function() {
if (!storage.running) {
console.log("stopTimer called while benchmark not running");
return;
}
if (this.startTime === null) {
console.log("stopTimer called without previous call to startTimer");
return;
}
var took = performance.now() - this.startTime;
this.startTime = null;
storage.current.startupTime.push(took);
storage.round++;
saveStorage();
this.runNextRound();
},
sampleMemoryToStorage: function() {
return sampleMemory().then(function(mem) {
for (var p in mem) {
storage.current[p].push(mem[p]);
}
saveStorage();
});
},
runNextRound: function() {
var self = this;
var done = storage.round >= storage.numRounds;
function run() {
var promise;
if (storage.round === 0) {
promise = Promise.resolve();
} else {
promise = self.sampleMemoryToStorage();
}
promise.then(function() {
if (done) {
self.finish();
return;
}
DumbPipe.close(DumbPipe.open("gcReload", {}));
}).catch(function (e) {
console.error(e)
});
}
if (storage.deleteFs) {
console.log("Deleting fs.");
indexedDB.deleteDatabase("asyncStorage");
}
if (storage.deleteJitCache) {
console.log("Deleting jit cache.");
indexedDB.deleteDatabase("CompiledMethodCache");
}
if (storage.round !== 0) {
console.log("Scheduling round " + (storage.round) + " of " + storage.numRounds + " finalization in " + storage.roundDelay + "ms");
setTimeout(run, storage.roundDelay);
} else {
run();
}
},
finish: function() {
storage.running = false;
saveStorage();
var labels = ["Test", "Baseline Mean", "Mean", "+/-", "%", "P", "Min", "Max"];
var rows = [labels];
for (var key in storage.current) {
var samples = storage.current[key];
var baselineSamples = storage.baseline[key] || [];
var hasBaseline = baselineSamples.length > 0;
var formatter = valueFormatters[key];
var row = [key];
rows.push(row);
var currentMean = mean(samples);
var baselineMean = mean(baselineSamples);
row.push(hasBaseline ? formatter(baselineMean) + "" : "n/a");
row.push(formatter(currentMean) + "");
row.push(hasBaseline ? formatter(currentMean - baselineMean) + "" : "n/a");
row.push(hasBaseline ? (100 * (currentMean - baselineMean) / baselineMean).toFixed(2) : "n/a");
var pMessage = "n/a";
if (hasBaseline) {
var p = (baselineSamples.length < 2) ? 1 : ttest(baselineSamples, samples).pValue();
if (p < 0.05) {
pMessage = currentMean < baselineMean ? "BETTER" : "WORSE";
} else {
pMessage = "INSIGNIFICANT";
}
} else {
pMessage = "n/a";
}
row.push(pMessage);
row.push(formatter(Math.min.apply(null, samples)));
row.push(formatter(Math.max.apply(null, samples)));
}
if (storage.buildBaseline) {
storage.baseline = storage.current;
storage.buildBaseline = false;
console.log("FINISHED BUILDING BASELINE");
}
console.log("Raw Values:\n" + "Current: " + JSON.stringify(storage.current) + "\nBaseline: " + JSON.stringify(storage.baseline))
var configRows = [
["Config", "Value"],
["User Agent", window.navigator.userAgent],
["Rounds", storage.numRounds],
["Delay(ms)", storage.roundDelay],
["Delete FS", storage.deleteFs ? "yes" : "no"],
["Delete JIT CACHE", storage.deleteJitCache ? "yes" : "no"],
];
var out = "\n" +
prettyTable(configRows, [LEFT, LEFT]) + "\n" +
prettyTable(rows, [LEFT, RIGHT, RIGHT, RIGHT, RIGHT, RIGHT, RIGHT, RIGHT]);
console.log(out);
saveStorage();
}
};
// Start right away instead of in init() so we can see any speedups in script loading.
if (storage.running) {
startup.startTimer();
}
var numRoundsEl;
var roundDelayEl;
var deleteFsEl;
var deleteJitCacheEl;
var startButton;
var baselineButton;
function getSettings() {
return {
numRounds: numRoundsEl.value | 0,
roundDelay: roundDelayEl.value | 0,
deleteFs: !!deleteFsEl.checked,
deleteJitCache: !!deleteJitCacheEl.checked,
recordMemory: NO_SECURITY
};
}
function start() {
startup.run(getSettings());
}
function buildBaseline() {
var settings = getSettings();
settings.buildBaseline = true;
startup.run(settings);
}
return {
initUI: function() {
numRoundsEl = document.getElementById("benchmark-num-rounds");
roundDelayEl = document.getElementById("benchmark-round-delay");
deleteFsEl = document.getElementById("benchmark-delete-fs");
deleteJitCacheEl = document.getElementById("benchmark-delete-jit-cache");
startButton = document.getElementById("benchmark-startup-run");
baselineButton = document.getElementById("benchmark-startup-baseline");
numRoundsEl.value = storage.numRounds;
roundDelayEl.value = storage.roundDelay;
deleteFsEl.checked = storage.deleteFs;
deleteJitCacheEl.checked = storage.deleteJitCache;
startButton.onclick = start;
baselineButton.onclick = buildBaseline;
},
start: start,
buildBaseline: buildBaseline,
sampleMemory: sampleMemory,
forceCollectors: forceCollectors,
prettyTable: prettyTable,
LEFT: LEFT,
CENTER: CENTER,
RIGHT: RIGHT,
startup: {
init: function() {
if (!storage.running) {
return;
}
var implKey = "com/sun/midp/lcdui/DisplayDevice.gainedForeground0.(II)V";
var originalFn = Native[implKey];
Native[implKey] = function() {
startup.stopTimer();
originalFn.apply(null, arguments);
};
},
run: startup.run.bind(startup),
}
};
})();