]> git.proxmox.com Git - mirror_novnc.git/blob - tests/arrays.js
Change noVNC license to from LGPLv3 to MPL 2.0
[mirror_novnc.git] / tests / arrays.js
1 /*
2 * Javascript binary array performance tests
3 * Copyright (C) 2012 Joel Martin
4 * Licensed under MPL 2.0 (see LICENSE.txt)
5 */
6
7 var ctx, i, j, randlist,
8 new_normal, new_imageData, new_arrayBuffer,
9 browser = Browser.browser + " " +
10 Browser.version + " on " +
11 Browser.OS,
12 do_imageData = false,
13 do_arrayBuffer = false,
14 conf = {
15 'create_cnt' : 2000,
16 'read_cnt' : 5000000,
17 'write_cnt' : 5000000,
18 'iterations' : 0,
19 'order_l1' : [browser],
20 'order_l2' : ['normal',
21 'imageData',
22 'arrayBuffer'],
23 'order_l3' : ['create',
24 'sequentialRead',
25 'randomRead',
26 'sequentialWrite']
27 },
28 stats = {},
29 testFunc = {},
30 iteration, arraySize;
31
32 var newline = "\n";
33 if (Util.Engine.trident) {
34 var newline = "<br>\n";
35 }
36 function message(str) {
37 //console.log(str);
38 cell = $D('messages');
39 cell.innerHTML += str + newline;
40 cell.scrollTop = cell.scrollHeight;
41 }
42
43 function vmessage(str) {
44 if (verbose) {
45 message(str);
46 } else {
47 console.log(str);
48 }
49 }
50
51 new_normal = function() {
52 var arr = [], i;
53 for (i = 0; i < arraySize; i++) {
54 arr[i] = 0;
55 }
56 return arr;
57 }
58
59 /* Will be overridden with real function */
60 new_imageData = function() {
61 throw("imageData not supported");
62 };
63
64 new_imageData_createImageData = function() {
65 var imageData = ctx.createImageData(1024/4, arraySize / 1024);
66 return imageData.data;
67 };
68
69 new_imageData_getImageData = function() {
70 var imageData = ctx.getImageData(0, 0, 1024/4, arraySize / 1024),
71 arr = imageData.data;
72 for (i = 0; i < arraySize; i++) {
73 arr[i] = 0;
74 }
75 return arr;
76 };
77
78 new_arrayBuffer = function() {
79 var arr = new ArrayBuffer(arraySize);
80 return new Uint8Array(arr);
81 }
82
83 function init_randlist() {
84 randlist = [];
85 for (var i=0; i < arraySize; i++) {
86 randlist[i] = parseInt(Math.random() * 256, 10);
87 }
88 }
89 function copy_randlist(arr) {
90 for (var i=0; i < arraySize; i++) {
91 arr[i] = randlist[i];
92 }
93 }
94
95 function begin() {
96 var i, j;
97 conf.iterations = parseInt($D('iterations').value, 10);
98 arraySize = parseInt($D('arraySize').value, 10) * 1024;
99
100 init_randlist();
101
102 // TODO: randomize test_list
103
104 stats = {};
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]] = [];
109 }
110 }
111
112 $D('startButton').value = "Running";
113 $D('startButton').disabled = true;
114
115 message("running " + conf.iterations + " test iterations");
116 iteration = 1;
117 setTimeout(run_next_iteration, 250);
118 }
119
120 function finish() {
121 var totalTime, arrayType, testType, times;
122 message("tests finished");
123
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/" +
134 times.max() + "ms");
135 message(" StdDev : " + times.stdDev() + "ms");
136 }
137 }
138
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));
144
145 $D('startButton').disabled = false;
146 $D('startButton').value = "Run Tests";
147 }
148
149 function run_next_iteration() {
150 var arrayType, testType, deltaTime;
151
152 for (i = 0; i < conf.order_l2.length; i++) {
153 arrayType = conf.order_l2[i];
154 if (arrayType === 'imageData' && (!do_imageData)) {
155 continue;
156 }
157 if (arrayType === 'arrayBuffer' && (!do_arrayBuffer)) {
158 continue;
159 }
160 for (j = 0; j < conf.order_l3.length; j++) {
161 testType = conf.order_l3[j];
162
163 deltaTime = testFunc[arrayType + "_" + testType]();
164
165 stats[arrayType][testType].push(deltaTime);
166 vmessage("test " + (arrayType + "_" + testType) +
167 " time: " + (deltaTime) + "ms");
168 }
169 }
170
171 message("finished test iteration " + iteration);
172 if (iteration >= conf.iterations) {
173 setTimeout(finish, 1);
174 return;
175 }
176 iteration++;
177 setTimeout(run_next_iteration, 1);
178 }
179
180 /*
181 * Test functions
182 */
183
184 testFunc["normal_create"] = function() {
185 var cnt, arrNormal, startTime, endTime;
186 vmessage("create normal array " + conf.create_cnt + "x, initialized to 0");
187
188 startTime = (new Date()).getTime();
189 for (cnt = 0; cnt < conf.create_cnt; cnt++) {
190 arrNormal = new_normal();
191 }
192 endTime = (new Date()).getTime();
193
194 return endTime - startTime;
195 };
196
197 testFunc["imageData_create"] = function() {
198 var cnt, arrImage, startTime, endTime;
199 vmessage("create imageData array " + conf.create_cnt + "x, initialized to 0");
200
201 startTime = (new Date()).getTime();
202 for (cnt = 0; cnt < conf.create_cnt; cnt++) {
203 arrImage = new_imageData();
204 }
205 endTime = (new Date()).getTime();
206
207 if (arrImage[103] !== 0) {
208 message("Initialization failed, arrImage[103] is: " + arrImage[103]);
209 throw("Initialization failed, arrImage[103] is: " + arrImage[103]);
210 }
211 return endTime - startTime;
212 };
213
214 testFunc["arrayBuffer_create"] = function() {
215 var cnt, arrBuffer, startTime, endTime;
216 vmessage("create arrayBuffer array " + conf.create_cnt + "x, initialized to 0");
217
218 startTime = (new Date()).getTime();
219 for (cnt = 0; cnt < conf.create_cnt; cnt++) {
220 arrBuffer = new_arrayBuffer();
221 }
222 endTime = (new Date()).getTime();
223
224 if (arrBuffer[103] !== 0) {
225 message("Initialization failed, arrBuffer[103] is: " + arrBuffer[103]);
226 throw("Initialization failed, arrBuffer[103] is: " + arrBuffer[103]);
227 }
228 return endTime - startTime;
229 };
230
231 function test_sequentialRead(arr) {
232 var i, j, cnt, startTime, endTime;
233 /* Initialize the array */
234 copy_randlist(arr);
235
236 startTime = (new Date()).getTime();
237 i = 0;
238 j = 0;
239 for (cnt = 0; cnt < conf.read_cnt; cnt++) {
240 j = arr[i];
241 i++;
242 if (i >= arraySize) {
243 i = 0;
244 }
245 }
246 endTime = (new Date()).getTime();
247
248 return endTime - startTime;
249 }
250
251 function test_randomRead(arr) {
252 var i, cnt, startTime, endTime;
253 /* Initialize the array */
254 copy_randlist(arr); // used as jumplist
255
256 startTime = (new Date()).getTime();
257 i = 0;
258 for (cnt = 0; cnt < conf.read_cnt; cnt++) {
259 i = (arr[i] + cnt) % arraySize;
260 }
261 endTime = (new Date()).getTime();
262
263 return endTime - startTime;
264 }
265
266 function test_sequentialWrite(arr) {
267 var i, cnt, startTime, endTime;
268 /* Initialize the array */
269 copy_randlist(arr);
270
271 startTime = (new Date()).getTime();
272 i = 0;
273 for (cnt = 0; cnt < conf.write_cnt; cnt++) {
274 arr[i] = (cnt % 256);
275 i++;
276 if (i >= arraySize) {
277 i = 0;
278 }
279 }
280 endTime = (new Date()).getTime();
281
282 return endTime - startTime;
283 }
284
285 /* Sequential Read Tests */
286 testFunc["normal_sequentialRead"] = function() {
287 vmessage("read normal array " + conf.read_cnt + "x");
288 return test_sequentialRead(new_normal());
289 };
290
291 testFunc["imageData_sequentialRead"] = function() {
292 vmessage("read imageData array " + conf.read_cnt + "x");
293 return test_sequentialRead(new_imageData());
294 };
295
296 testFunc["arrayBuffer_sequentialRead"] = function() {
297 vmessage("read arrayBuffer array " + conf.read_cnt + "x");
298 return test_sequentialRead(new_arrayBuffer());
299 };
300
301
302 /* Random Read Tests */
303 testFunc["normal_randomRead"] = function() {
304 vmessage("read normal array " + conf.read_cnt + "x");
305 return test_randomRead(new_normal());
306 };
307
308 testFunc["imageData_randomRead"] = function() {
309 vmessage("read imageData array " + conf.read_cnt + "x");
310 return test_randomRead(new_imageData());
311 };
312
313 testFunc["arrayBuffer_randomRead"] = function() {
314 vmessage("read arrayBuffer array " + conf.read_cnt + "x");
315 return test_randomRead(new_arrayBuffer());
316 };
317
318
319 /* Sequential Write Tests */
320 testFunc["normal_sequentialWrite"] = function() {
321 vmessage("write normal array " + conf.write_cnt + "x");
322 return test_sequentialWrite(new_normal());
323 };
324
325 testFunc["imageData_sequentialWrite"] = function() {
326 vmessage("write imageData array " + conf.write_cnt + "x");
327 return test_sequentialWrite(new_imageData());
328 };
329
330 testFunc["arrayBuffer_sequentialWrite"] = function() {
331 vmessage("write arrayBuffer array " + conf.write_cnt + "x");
332 return test_sequentialWrite(new_arrayBuffer());
333 };
334
335 init = function() {
336 vmessage(">> init");
337
338 $D('iterations').value = 10;
339 $D('arraySize').value = 10;
340 arraySize = parseInt($D('arraySize').value, 10) * 1024;
341
342 message("Browser: " + browser);
343
344 /* Determine browser binary array support */
345 try {
346 ctx = $D('canvas').getContext('2d');
347 new_imageData = new_imageData_createImageData;
348 new_imageData();
349 do_imageData = true;
350 } catch (exc) {
351 vmessage("createImageData not supported: " + exc);
352 try {
353 ctx = $D('canvas').getContext('2d');
354 new_imageData = new_imageData_getImageData;
355 blah = new_imageData();
356 do_imageData = true;
357 } catch (exc) {
358 vmessage("getImageData not supported: " + exc);
359 }
360 }
361 if (! do_imageData) {
362 message("imageData arrays not supported");
363 }
364
365 try {
366 new_arrayBuffer();
367 do_arrayBuffer = true;
368 } catch (exc) {
369 vmessage("Typed Arrays not supported: " + exc);
370 }
371 if (! do_arrayBuffer) {
372 message("Typed Arrays (ArrayBuffers) not suppoted");
373 }
374 vmessage("<< init");
375 }