1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 21:00:23 +01:00
CanvasBlocker/test/performanceTest.js

178 lines
4.5 KiB
JavaScript
Raw Normal View History

2020-01-06 15:15:04 +01:00
/* globals canvasAPI */
2019-12-16 19:27:28 +01:00
const createLog = function(){
2017-10-03 15:35:31 +02:00
"use strict";
2019-12-16 19:27:28 +01:00
const div = document.getElementById("log");
2017-08-13 23:44:31 +02:00
return function createLog(){
2019-12-16 19:27:28 +01:00
const logDiv = document.createElement("div");
2017-08-13 23:44:31 +02:00
logDiv.className = "log";
div.appendChild(logDiv);
return {
createButton: function createButton(text, callback){
2019-12-16 19:27:28 +01:00
const button = document.createElement("button");
button.className = "logButton";
logDiv.appendChild(button);
button.textContent = text;
button.addEventListener("click", callback);
},
createLine: function createLine(str, type = "div"){
2019-12-16 19:27:28 +01:00
const logLine = document.createElement(type);
logLine.className = "logLine";
logDiv.appendChild(logLine);
2017-08-13 23:44:31 +02:00
logLine.textContent = str;
return function updateLine(str){
logLine.textContent = str;
};
}
2017-08-13 23:44:31 +02:00
};
};
}();
2019-12-16 19:27:28 +01:00
const performTest = function(){
2017-10-03 15:35:31 +02:00
"use strict";
2017-08-13 23:44:31 +02:00
return function performTest(name, func, innerRunLength, outerRunLength){
2019-12-16 19:27:28 +01:00
const log = createLog();
log.createLine("test " + name, "h3");
2019-12-16 19:27:28 +01:00
const line = log.createLine("");
let line2;
let time = 0;
let time2 = 0;
let min = Number.POSITIVE_INFINITY;
let max = 0;
let outerI = 0;
const outerRunIncrease = outerRunLength;
2018-06-30 15:16:10 +02:00
if (func.prepareOnce){
func.prepareOnce();
}
log.createButton("measure", function(){
line("starting");
line2("");
function run(){
2019-12-16 19:27:28 +01:00
for (let i = 0; i < innerRunLength; i += 1){
if (func.prepare){
func.prepare();
}
2019-12-16 19:27:28 +01:00
const start = performance.now();
func.test();
2019-12-16 19:27:28 +01:00
const end = performance.now();
const duration = end - start;
min = Math.min(min, duration);
max = Math.max(max, duration);
time2 += duration * duration;
time += duration;
}
outerI += 1;
2019-12-16 19:27:28 +01:00
const totalRunI = outerI * innerRunLength;
line(
"finished run " + totalRunI + " from " + (innerRunLength * outerRunLength) +
" -> average: " + (time / totalRunI).toFixed(2) +
"(\u00B1" + Math.sqrt((time2 - time * time / totalRunI) / totalRunI).toFixed(2) + ") ms " +
"(min: " + min.toFixed(2) + "ms, max: " + max.toFixed(2) + "ms)"
);
if (outerI < outerRunLength){
window.setTimeout(run, 10);
}
else {
outerRunLength += outerRunIncrease;
line2("finished");
2018-06-30 15:16:10 +02:00
}
2017-08-13 23:44:31 +02:00
}
window.setTimeout(run, 10);
});
2019-11-28 01:26:35 +01:00
line2 = log.createLine("");
2017-08-13 23:44:31 +02:00
};
}();
2019-12-16 19:27:28 +01:00
const fingerprintTest = function(){
2017-10-03 15:35:31 +02:00
"use strict";
2019-12-16 19:27:28 +01:00
let canvas;
2017-08-13 23:44:31 +02:00
return {
prepare: function(){
// create window canvas
2017-10-03 15:35:31 +02:00
canvas = document.createElement("canvas");
2017-08-13 23:44:31 +02:00
// draw image in window canvas
2020-01-06 15:15:04 +01:00
canvasAPI.draw(canvas);
2017-08-13 23:44:31 +02:00
},
test: function fingerprintTest(){
return canvas.toDataURL();
}
};
}();
2019-12-16 19:27:28 +01:00
const randomImageTest = function(){
2017-10-03 15:35:31 +02:00
"use strict";
2019-12-16 19:27:28 +01:00
let canvas;
2017-08-13 23:44:31 +02:00
return {
prepare: function(){
canvas = document.createElement("canvas");
canvas.width = 1000;
canvas.height = 100;
2019-12-16 19:27:28 +01:00
const imageData = new ImageData(canvas.width, canvas.height);
const data = imageData.data;
2017-08-13 23:44:31 +02:00
2019-12-16 19:27:28 +01:00
for (let i = 0; i < data.length; i += 4){
2017-08-13 23:44:31 +02:00
data[i + 0] = Math.floor(256 * Math.random());
data[i + 1] = Math.floor(256 * Math.random());
data[i + 2] = Math.floor(256 * Math.random());
data[i + 3] = 255;
}
canvas.getContext("2d").putImageData(imageData, 0, 0);
},
test: function randomImageTest(){
return canvas.toDataURL();
}
};
}();
2019-12-16 19:27:28 +01:00
const innerHTMlTest = function(html, repeats){
2019-05-22 23:43:26 +02:00
"use strict";
2019-12-16 19:27:28 +01:00
let div;
2019-05-22 23:43:26 +02:00
return {
prepareOnce: function(){
div = document.createElement("div");
div.style.visibility = "hidden";
document.body.appendChild(div);
},
test: function randomImageTest(){
2019-12-16 19:27:28 +01:00
for (let i = repeats; i--;){
2019-05-22 23:43:26 +02:00
div.innerHTML = html;
div.innerHTML = "";
}
}
};
};
2020-01-06 14:56:49 +01:00
const appendChildTest = function(tagName, repeats){
"use strict";
let div;
return {
prepareOnce: function(){
div = document.createElement("div");
div.style.visibility = "hidden";
document.body.appendChild(div);
},
test: function(){
for (let i = repeats; i--;){
const element = document.createElement(tagName);
div.appendChild(element);
div.removeChild(element);
}
}
};
};
2019-05-22 23:43:26 +02:00
2017-08-13 23:44:31 +02:00
performTest("fingerprinting", fingerprintTest, 10, 100);
2019-05-22 23:43:26 +02:00
performTest("big random image", randomImageTest, 10, 10);
2020-01-06 14:56:49 +01:00
performTest("innerHTML (1000 times)", innerHTMlTest("text <br>no iframe", 1000), 10, 30);
performTest("innerHTML with iframe (20 times)", innerHTMlTest("text <br>iframe: <iframe></iframe>", 20), 10, 10);
performTest("appendChild (300 times)", appendChildTest("span", 300), 10, 30);
performTest("appendChild with iframe (20 times)", appendChildTest("iframe", 20), 10, 30);