2 * Javascript binary array performance tests
3 * Copyright (C) 2012 Joel Martin
4 * Licensed under MPL 2.0 (see LICENSE.txt)
7 var ctx
, i
, j
, randlist
,
8 new_normal
, new_imageData
, new_arrayBuffer
,
9 browser
= Browser
.browser
+ " " +
10 Browser
.version
+ " on " +
13 do_arrayBuffer
= false,
17 'write_cnt' : 5000000,
19 'order_l1' : [browser
],
20 'order_l2' : ['normal',
23 'order_l3' : ['create',
33 if (Util
.Engine
.trident
) {
34 var newline
= "<br>\n";
36 function message(str
) {
38 cell
= $D('messages');
39 cell
.innerHTML
+= str
+ newline
;
40 cell
.scrollTop
= cell
.scrollHeight
;
43 function vmessage(str
) {
51 new_normal = function() {
53 for (i
= 0; i
< arraySize
; i
++) {
59 /* Will be overridden with real function */
60 new_imageData = function() {
61 throw("imageData not supported");
64 new_imageData_createImageData = function() {
65 var imageData
= ctx
.createImageData(1024/4, arraySize / 1024);
66 return imageData
.data
;
69 new_imageData_getImageData = function() {
70 var imageData
= ctx
.getImageData(0, 0, 1024/4, arraySize / 1024),
72 for (i
= 0; i
< arraySize
; i
++) {
78 new_arrayBuffer = function() {
79 var arr
= new ArrayBuffer(arraySize
);
80 return new Uint8Array(arr
);
83 function init_randlist() {
85 for (var i
=0; i
< arraySize
; i
++) {
86 randlist
[i
] = parseInt(Math
.random() * 256, 10);
89 function copy_randlist(arr
) {
90 for (var i
=0; i
< arraySize
; i
++) {
97 conf
.iterations
= parseInt($D('iterations').value
, 10);
98 arraySize
= parseInt($D('arraySize').value
, 10) * 1024;
102 // TODO: randomize test_list
105 for (i
= 0; i
< conf
.order_l2
.length
; i
++) {
106 stats
[conf
.order_l2
[i
]] = {};
107 for (j
= 0; j
< conf
.order_l3
.length
; j
++) {
108 stats
[conf
.order_l2
[i
]][conf
.order_l3
[j
]] = [];
112 $D('startButton').value
= "Running";
113 $D('startButton').disabled
= true;
115 message("running " + conf
.iterations
+ " test iterations");
117 setTimeout(run_next_iteration
, 250);
121 var totalTime
, arrayType
, testType
, times
;
122 message("tests finished");
124 for (j
= 0; j
< conf
.order_l3
.length
; j
++) {
125 testType
= conf
.order_l3
[j
];
126 message("Test '" + testType
+ "'");
127 for (i
= 0; i
< conf
.order_l2
.length
; i
++) {
128 arrayType
= conf
.order_l2
[i
];
129 message(" Array Type '" + arrayType
);
130 times
= stats
[arrayType
][testType
];
131 message(" Average : " + times
.mean() + "ms" +
132 " (Total: " + times
.sum() + "ms)");
133 message(" Min/Max : " + times
.min() + "ms/" +
135 message(" StdDev : " + times
.stdDev() + "ms");
139 vmessage("array_chart.py JSON data:");
140 chart_data
= {'conf' : conf
, 'stats' : { } };
141 chart_data
.stats
[browser
] = stats
;
142 chart_data
.stats
['next_browser'] = {};
143 vmessage(JSON
.stringify(chart_data
, null, 2));
145 $D('startButton').disabled
= false;
146 $D('startButton').value
= "Run Tests";
149 function run_next_iteration() {
150 var arrayType
, testType
, deltaTime
;
152 for (i
= 0; i
< conf
.order_l2
.length
; i
++) {
153 arrayType
= conf
.order_l2
[i
];
154 if (arrayType
=== 'imageData' && (!do_imageData
)) {
157 if (arrayType
=== 'arrayBuffer' && (!do_arrayBuffer
)) {
160 for (j
= 0; j
< conf
.order_l3
.length
; j
++) {
161 testType
= conf
.order_l3
[j
];
163 deltaTime
= testFunc
[arrayType
+ "_" + testType
]();
165 stats
[arrayType
][testType
].push(deltaTime
);
166 vmessage("test " + (arrayType
+ "_" + testType
) +
167 " time: " + (deltaTime
) + "ms");
171 message("finished test iteration " + iteration
);
172 if (iteration
>= conf
.iterations
) {
173 setTimeout(finish
, 1);
177 setTimeout(run_next_iteration
, 1);
184 testFunc
["normal_create"] = function() {
185 var cnt
, arrNormal
, startTime
, endTime
;
186 vmessage("create normal array " + conf
.create_cnt
+ "x, initialized to 0");
188 startTime
= (new Date()).getTime();
189 for (cnt
= 0; cnt
< conf
.create_cnt
; cnt
++) {
190 arrNormal
= new_normal();
192 endTime
= (new Date()).getTime();
194 return endTime
- startTime
;
197 testFunc
["imageData_create"] = function() {
198 var cnt
, arrImage
, startTime
, endTime
;
199 vmessage("create imageData array " + conf
.create_cnt
+ "x, initialized to 0");
201 startTime
= (new Date()).getTime();
202 for (cnt
= 0; cnt
< conf
.create_cnt
; cnt
++) {
203 arrImage
= new_imageData();
205 endTime
= (new Date()).getTime();
207 if (arrImage
[103] !== 0) {
208 message("Initialization failed, arrImage[103] is: " + arrImage
[103]);
209 throw("Initialization failed, arrImage[103] is: " + arrImage
[103]);
211 return endTime
- startTime
;
214 testFunc
["arrayBuffer_create"] = function() {
215 var cnt
, arrBuffer
, startTime
, endTime
;
216 vmessage("create arrayBuffer array " + conf
.create_cnt
+ "x, initialized to 0");
218 startTime
= (new Date()).getTime();
219 for (cnt
= 0; cnt
< conf
.create_cnt
; cnt
++) {
220 arrBuffer
= new_arrayBuffer();
222 endTime
= (new Date()).getTime();
224 if (arrBuffer
[103] !== 0) {
225 message("Initialization failed, arrBuffer[103] is: " + arrBuffer
[103]);
226 throw("Initialization failed, arrBuffer[103] is: " + arrBuffer
[103]);
228 return endTime
- startTime
;
231 function test_sequentialRead(arr
) {
232 var i
, j
, cnt
, startTime
, endTime
;
233 /* Initialize the array */
236 startTime
= (new Date()).getTime();
239 for (cnt
= 0; cnt
< conf
.read_cnt
; cnt
++) {
242 if (i
>= arraySize
) {
246 endTime
= (new Date()).getTime();
248 return endTime
- startTime
;
251 function test_randomRead(arr
) {
252 var i
, cnt
, startTime
, endTime
;
253 /* Initialize the array */
254 copy_randlist(arr
); // used as jumplist
256 startTime
= (new Date()).getTime();
258 for (cnt
= 0; cnt
< conf
.read_cnt
; cnt
++) {
259 i
= (arr
[i
] + cnt
) % arraySize
;
261 endTime
= (new Date()).getTime();
263 return endTime
- startTime
;
266 function test_sequentialWrite(arr
) {
267 var i
, cnt
, startTime
, endTime
;
268 /* Initialize the array */
271 startTime
= (new Date()).getTime();
273 for (cnt
= 0; cnt
< conf
.write_cnt
; cnt
++) {
274 arr
[i
] = (cnt
% 256);
276 if (i
>= arraySize
) {
280 endTime
= (new Date()).getTime();
282 return endTime
- startTime
;
285 /* Sequential Read Tests */
286 testFunc
["normal_sequentialRead"] = function() {
287 vmessage("read normal array " + conf
.read_cnt
+ "x");
288 return test_sequentialRead(new_normal());
291 testFunc
["imageData_sequentialRead"] = function() {
292 vmessage("read imageData array " + conf
.read_cnt
+ "x");
293 return test_sequentialRead(new_imageData());
296 testFunc
["arrayBuffer_sequentialRead"] = function() {
297 vmessage("read arrayBuffer array " + conf
.read_cnt
+ "x");
298 return test_sequentialRead(new_arrayBuffer());
302 /* Random Read Tests */
303 testFunc
["normal_randomRead"] = function() {
304 vmessage("read normal array " + conf
.read_cnt
+ "x");
305 return test_randomRead(new_normal());
308 testFunc
["imageData_randomRead"] = function() {
309 vmessage("read imageData array " + conf
.read_cnt
+ "x");
310 return test_randomRead(new_imageData());
313 testFunc
["arrayBuffer_randomRead"] = function() {
314 vmessage("read arrayBuffer array " + conf
.read_cnt
+ "x");
315 return test_randomRead(new_arrayBuffer());
319 /* Sequential Write Tests */
320 testFunc
["normal_sequentialWrite"] = function() {
321 vmessage("write normal array " + conf
.write_cnt
+ "x");
322 return test_sequentialWrite(new_normal());
325 testFunc
["imageData_sequentialWrite"] = function() {
326 vmessage("write imageData array " + conf
.write_cnt
+ "x");
327 return test_sequentialWrite(new_imageData());
330 testFunc
["arrayBuffer_sequentialWrite"] = function() {
331 vmessage("write arrayBuffer array " + conf
.write_cnt
+ "x");
332 return test_sequentialWrite(new_arrayBuffer());
338 $D('iterations').value
= 10;
339 $D('arraySize').value
= 10;
340 arraySize
= parseInt($D('arraySize').value
, 10) * 1024;
342 message("Browser: " + browser
);
344 /* Determine browser binary array support */
346 ctx
= $D('canvas').getContext('2d');
347 new_imageData
= new_imageData_createImageData
;
351 vmessage("createImageData not supported: " + exc
);
353 ctx
= $D('canvas').getContext('2d');
354 new_imageData
= new_imageData_getImageData
;
355 blah
= new_imageData();
358 vmessage("getImageData not supported: " + exc
);
361 if (! do_imageData
) {
362 message("imageData arrays not supported");
367 do_arrayBuffer
= true;
369 vmessage("Typed Arrays not supported: " + exc
);
371 if (! do_arrayBuffer
) {
372 message("Typed Arrays (ArrayBuffers) not suppoted");