-
Notifications
You must be signed in to change notification settings - Fork 57
/
benchmark.html
99 lines (96 loc) · 2.59 KB
/
benchmark.html
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
<!DOCTYPE html>
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
<html>
<head>
<script src="jpg.js"></script>
<script>
function Benchmark(config) {
this.config = config;
this.imageIndex = 0;
this.times = [];
}
Benchmark.prototype.start = function() {
console.log('Running ' + this.config.samples + ' times' +
' on ' + this.config.images.length + ' images.');
this.nextImage();
}
Benchmark.prototype.nextImage = function() {
if (this.imageIndex >= this.config.images.length) {
this.finish();
return;
}
var times = [];
var path = this.config.images[this.imageIndex];
var xhr = new XMLHttpRequest();
xhr.open("GET", path, true);
xhr.responseType = "arraybuffer";
xhr.onload = (function() {
// TODO catch parse error
var data = new Uint8Array(xhr.response || xhr.mozResponseArrayBuffer);
for (var i = 0; i < this.config.samples; i++) {
times.push(performance.now());
var j = new JpegImage();
j.parse(data);
times.push(performance.now());
}
this.times.push(times);
this.displayImage(j);
this.imageIndex++;
this.nextImage();
}).bind(this);
xhr.send(null);
}
Benchmark.prototype.displayImage = function(j) {
var canvases = document.getElementById('canvases');
var wrapper = document.createElement('span');
wrapper.innerHTML = '<h1>' + this.config.images[this.imageIndex] + '</h1>';
var c = document.createElement('canvas');
wrapper.appendChild(c);
var br = document.createElement('br');
wrapper.appendChild(br);
canvases.appendChild(wrapper);
c.width = j.width;
c.height = j.height;
var ctx = c.getContext("2d");
var d = ctx.getImageData(0,0,j.width,j.height);
j.copyToImageData(d);
ctx.putImageData(d, 0, 0);
}
Benchmark.prototype.finish = function() {
console.log('Finished');
var averages = [];
var avgSum = 0;
for (var i = 0; i < this.times.length; i++) {
var times = this.times[i];
var sum = 0;
for (var j = 0; j < times.length; j += 2) {
var took = times[j + 1] - times[j];
sum += took;
}
var average = (sum / (times.length / 2));
avgSum += average;
console.log("Average: " + average.toFixed(2) + ' ms');
averages.push(average);
}
console.log("Overall average: " + (avgSum / this.times.length).toFixed(2) + ' ms');
}
function go() {
var config = {
images: [
'images/j1.jpg',
'images/j2.jpg',
'images/j3.jpg'
],
samples: 50
};
var b = new Benchmark(config);
b.start();
}
</script>
</head>
<body onload="go()">
<div id="canvases">
</div>
</body>
</html>