summaryrefslogtreecommitdiff
path: root/school/node_modules/node-forge/tests/performance.html
blob: 9acbcc5ae35c3a4d3a21257b66b68047c9391262 (plain)
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <head>
      <title>Forge Performance Tests</title>
      <link type="text/css" rel="stylesheet" media="all" href="screen.css" />
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
      <script type="text/javascript" src="forge/debug.js"></script>
      <script type="text/javascript" src="forge/util.js"></script>
      <script type="text/javascript" src="forge/log.js"></script>
      <script type="text/javascript" src="forge/task.js"></script>
      <script type="text/javascript" src="forge/md5.js"></script>
      <script type="text/javascript" src="forge/sha1.js"></script>
      <script type="text/javascript" src="forge/sha256.js"></script>
      <script type="text/javascript" src="forge/hmac.js"></script>
      <script type="text/javascript" src="forge/aes.js"></script>
      <script type="text/javascript" src="forge/pem.js"></script>
      <script type="text/javascript" src="forge/asn1.js"></script>
      <script type="text/javascript" src="forge/jsbn.js"></script>
      <script type="text/javascript" src="forge/prng.js"></script>
      <script type="text/javascript" src="forge/random.js"></script>
      <script type="text/javascript" src="forge/oids.js"></script>
      <script type="text/javascript" src="forge/rsa.js"></script>
      <script type="text/javascript" src="forge/pbe.js"></script>
      <script type="text/javascript" src="forge/x509.js"></script>
      <script type="text/javascript" src="forge/pki.js"></script>
      <script type="text/javascript" src="forge/tls.js"></script>
      <script type="text/javascript" src="forge/aesCipherSuites.js"></script>

      <script type="text/javascript">
      //<![CDATA[
      // logging category
      var cat = 'forge.tests.performance';

      var test_random = function()
      {
         forge.log.debug(cat, 'painting canvas...');
         setTimeout(function()
         {
	         var canvas = document.getElementById("canvas");
	         var ctx = canvas.getContext("2d");
	         var imgData = ctx.createImageData(canvas.width, canvas.height);

	         // generate random bytes
	         var bytes = forge.random.getBytes(canvas.width * canvas.height * 3);
	         var n = 0;
	         for(var x = 0; x < imgData.width; x++)
	         {
	            for(var y = 0; y < imgData.height; y++)
	            {
	               // index of the pixel in the array
	               var idx = (x + y * imgData.width) * 4;

	               // set values
	               imgData.data[idx + 0] = bytes.charCodeAt(n++); // Red
	               imgData.data[idx + 1] = bytes.charCodeAt(n++); // Green
	               imgData.data[idx + 2] = bytes.charCodeAt(n++); // Blue
	               imgData.data[idx + 3] = 255;                   // Alpha
	            }
	         }

	         ctx.putImageData(imgData, 0, 0);
	         forge.log.debug(cat, 'done');
         }, 0);
      };

      var canvas_clear = function()
      {
         var canvas = document.getElementById("canvas");
         var ctx = canvas.getContext("2d");
         ctx.clearRect(0, 0, canvas.width, canvas.height);
      };

      var test_buffer_fill = function()
      {
         forge.log.debug(cat,
            'buffer fill optimized vs. slow running for about 5 seconds...');

         setTimeout(function()
         {
	         // run slow fill for 2.5 seconds
	         var st, et;
	         var b = '';
	         var passed = 0;
	         while(passed < 2500)
	         {
	            st = +new Date();
	            b += 'b';
	            et = +new Date();
	            passed += (et - st);
	         }

	         // do optimized fill
	         var buf = forge.util.createBuffer();
	         st = +new Date();
	         buf.fillWithByte('b'.charCodeAt(0), b.length);
	         et = +new Date();

	         forge.log.debug(cat, 'fill times', (et - st) + ' < ' + passed);
         });
      };

      var test_buffer_xor = function()
      {
         forge.log.debug(cat,
            'buffer xor optimized vs. slow running for about 5 seconds...');

         setTimeout(function()
         {
	         // run slow xor for 2.5 seconds
	         var st, et;
	         var output = forge.util.createBuffer();
	         var passed = 0;
	         while(passed < 2500)
	         {
	            st = +new Date();
	            output.putByte(0x01 ^ 0x02);
	            et = +new Date();
	            passed += (et - st);
	         }

	         // do optimized xor
	         var count = output.length();
	         var b1 = forge.util.createBuffer();
	         b1.fillWithByte(0x01, count);
	         var b2 = forge.util.createBuffer();
	         b2.fillWithByte(0x02, count);

	         st = +new Date();
	         output = forge.util.xorBytes(b1.getBytes(), b2.getBytes(), count);
	         et = +new Date();

	         forge.log.debug(cat, 'xor times', (et - st) + ' < ' + passed);
         });
      };

      // TODO: follow the same format as the hash tests
      var test_base64_encode = function()
      {
         forge.log.debug(cat, 'base64 encode running for about 5 seconds...');

         setTimeout(function()
         {
	         // get starting time to make test run for only 5 seconds
	         var start = +new Date();

	         // build data to encode
	         var str = '';
	         for(var i = 0; i < 100; i++)
	         {
	            str += '00010203050607080A0B0C0D0F1011121415161719';
	         }

	         // keep encoding for 5 seconds, keep track of total and count
	         var total = 0, count = 0, st, et;
	         var passed = 0;
	         while(passed < 5000)
	         {
	            st = +new Date();
	            forge.util.encode64(str);
	            et = +new Date();
	            ++count;
	            total += (et - st);
	            passed = +new Date() - start;
	         }

	         total /= 1000;
	         var kb = 4200/1024;
	         forge.log.debug(cat,
	            'encode:', (count*kb/total) + ' KiB/s');
         });
      };

      // TODO: follow the same format as the hash tests
      var test_base64_decode = function()
      {
         forge.log.debug(cat, 'base64 decode running for about 5 seconds...');

         setTimeout(function()
         {
	         // get starting time to make test run for only 5 seconds
	         var start = +new Date();

	         // build data to decode
	         var str = '';
	         for(var i = 0; i < 100; i++)
	         {
	            str +=
		            'MDAwMTAyMDMwNTA2MDcwODBBMEIwQzBEMEYxMDExMTIxNDE1MTYxNzE5';
	         }

	         // keep encoding for 5 seconds, keep track of total and count
	         var total = 0, count = 0, st, et;
	         var passed = 0;
	         while(passed < 5000)
	         {
	            st = +new Date();
	            forge.util.decode64(str);
	            et = +new Date();
	            ++count;
	            total += (et - st);
	            passed = +new Date() - start;
	         }

	         total /= 1000;
	         var kb = 5600/1024;
	         forge.log.debug(cat,
	            'decode:', (count*kb/total) + ' KiB/s');
         });
      };

      var test_md5 = function()
      {
         // create input data
         var input = ['0123456789abcdef', '', '', '', ''];
         for(var i = 0; i < 4; ++i)
         {
            input[1] += input[0];
         }
         for(var i = 0; i < 4; ++i)
         {
            input[2] += input[1];
         }
         for(var i = 0; i < 4; ++i)
         {
            input[3] += input[2];
         }
         for(var i = 0; i < 8; ++i)
         {
            input[4] += input[3];
         }

         var md = forge.md.md5.create();

         forge.log.debug(cat, 'md5 times in 1000s of bytes/sec processed:');

         var st, et;
         var output =
            ['  16 bytes: ',
             '  64 bytes: ',
             ' 256 bytes: ',
             '1024 bytes: ',
             '8192 bytes: '];
         var s = [16, 64, 256, 1024, 8192];
         var t = [0, 0, 0, 0, 0];
         for(var n = 0; n < 5; ++n)
         {
            var f = function(n)
            {
               setTimeout(function()
               {
                  // run for 2 seconds each
                  var count = 0;
                  while(t[n] < 2000)
                  {
                     md.start();
                     st = +new Date();
                     md.update(input[n]);
                     md.digest();
                     et = +new Date();
                     t[n] = t[n] + (et - st);
                     ++count;
                  }
                  t[n] /= 1000;
                  forge.log.debug(cat,
                     output[n], (count*s[n]/t[n]/1000) + 'k/sec');
               }, 0);
            }(n);
         }
      };

      var test_sha1 = function()
      {
         // create input data
         var input = ['0123456789abcdef', '', '', '', ''];
         for(var i = 0; i < 4; ++i)
         {
            input[1] += input[0];
         }
         for(var i = 0; i < 4; ++i)
         {
            input[2] += input[1];
         }
         for(var i = 0; i < 4; ++i)
         {
            input[3] += input[2];
         }
         for(var i = 0; i < 8; ++i)
         {
            input[4] += input[3];
         }

         var md = forge.md.sha1.create();

         forge.log.debug(cat, 'sha-1 times in 1000s of bytes/sec processed:');

         var st, et;
         var output =
            ['  16 bytes: ',
             '  64 bytes: ',
             ' 256 bytes: ',
             '1024 bytes: ',
             '8192 bytes: '];
         var s = [16, 64, 256, 1024, 8192];
         var t = [0, 0, 0, 0, 0];
         for(var n = 0; n < 5; ++n)
         {
            var f = function(n)
            {
               setTimeout(function()
               {
                  // run for 2 seconds each
                  var count = 0;
                  while(t[n] < 2000)
                  {
                     md.start();
                     st = +new Date();
                     md.update(input[n]);
                     md.digest();
                     et = +new Date();
                     t[n] = t[n] + (et - st);
                     ++count;
                  }
                  t[n] /= 1000;
                  forge.log.debug(cat,
                     output[n], (count*s[n]/t[n]/1000) + 'k/sec');
               }, 0);
            }(n);
         }
      };

      var test_sha256 = function()
      {
         // create input data
         var input = ['0123456789abcdef', '', '', '', ''];
         for(var i = 0; i < 4; ++i)
         {
            input[1] += input[0];
         }
         for(var i = 0; i < 4; ++i)
         {
            input[2] += input[1];
         }
         for(var i = 0; i < 4; ++i)
         {
            input[3] += input[2];
         }
         for(var i = 0; i < 8; ++i)
         {
            input[4] += input[3];
         }

         var md = forge.md.sha256.create();

         forge.log.debug(cat, 'sha-256 times in 1000s of bytes/sec processed:');

         var st, et;
         var output =
            ['  16 bytes: ',
             '  64 bytes: ',
             ' 256 bytes: ',
             '1024 bytes: ',
             '8192 bytes: '];
         var s = [16, 64, 256, 1024, 8192];
         var t = [0, 0, 0, 0, 0];
         for(var n = 0; n < 5; ++n)
         {
            var f = function(n)
            {
               setTimeout(function()
               {
                  // run for 2 seconds each
                  var count = 0;
                  while(t[n] < 2000)
                  {
                     md.start();
                     st = +new Date();
                     md.update(input[n]);
                     md.digest();
                     et = +new Date();
                     t[n] = t[n] + (et - st);
                     ++count;
                  }
                  t[n] /= 1000;
                  forge.log.debug(cat,
                     output[n], (count*s[n]/t[n]/1000) + 'k/sec');
               }, 0);
            }(n);
         }
      };

      // TODO: follow the same format as the hash tests
      var aes_128 = function()
      {
         forge.log.debug(cat, 'running AES-128 for 5 seconds...');

         var block = [];
         block.push(0x00112233);
         block.push(0x44556677);
         block.push(0x8899aabb);
         block.push(0xccddeeff);

         var key = [];
         key.push(0x00010203);
         key.push(0x04050607);
         key.push(0x08090a0b);
         key.push(0x0c0d0e0f);

         setTimeout(function()
         {
            // run for 5 seconds
            var start = +new Date();
	         var now;
	         var totalEncrypt = 0;
	         var totalDecrypt = 0;
	         var count = 0;
            var passed = 0;
	         while(passed < 5000)
	         {
	            var output = [];
	            var w = forge.aes._expandKey(key, false);
	            now = +new Date();
	            forge.aes._updateBlock(w, block, output, false);
	            totalEncrypt += +new Date() - now;

	            block = output;
	            output = [];
	            w = forge.aes._expandKey(key, true);
	            now = +new Date();
	            forge.aes._updateBlock(w, block, output, true);
	            totalDecrypt += +new Date() - now;

               ++count;
	            passed = +new Date() - start;
	         }

	         count = count * 16 / 1000;
	         totalEncrypt /= 1000;
	         totalDecrypt /= 1000;
	         forge.log.debug(cat, 'times in 1000s of bytes/sec processed.');
	         forge.log.debug(cat,
	   	      'encrypt: ' + (count*16 / totalEncrypt) + ' k/sec');
	         forge.log.debug(cat,
	   	      'decrypt: ' + (count*16 / totalDecrypt) + ' k/sec');
         }, 0);
      };

      // TODO: follow the same format as the hash tests
      var aes_128_cbc = function()
      {
         forge.log.debug(cat, 'running AES-128 CBC for 5 seconds...');

         var key = forge.random.getBytes(16);
         var iv = forge.random.getBytes(16);
         var plain = forge.random.getBytes(16);

         setTimeout(function()
         {
            // run for 5 seconds
            var start = +new Date();
	         var now;
	         var totalEncrypt = 0;
	         var totalDecrypt = 0;
	         var cipher;
	         var count = 0;
	         var passed = 0;
	         while(passed < 5000)
	         {
               var input = forge.util.createBuffer(plain);

               // encrypt, only measuring update() and finish()
               cipher = forge.aes.startEncrypting(key, iv);
               now = +new Date();
               cipher.update(input);
               cipher.finish();
               totalEncrypt += +new Date() - now;

               // decrypt, only measuring update() and finish()
               var ct = cipher.output;
               cipher = forge.aes.startDecrypting(key, iv);
               now = +new Date();
               cipher.update(ct);
               cipher.finish();
               totalDecrypt += +new Date() - now;

               ++count;
               passed = +new Date() - start;
	         }

	         // 32 bytes encrypted because of 16 bytes of padding
	         count = count * 32 / 1000;
            totalEncrypt /= 1000;
            totalDecrypt /= 1000;
            forge.log.debug(cat, 'times in 1000s of bytes/sec processed.');
            forge.log.debug(cat,
               'encrypt: ' + (count / totalEncrypt) + ' k/sec');
            forge.log.debug(cat,
               'decrypt: ' + (count / totalDecrypt) + ' k/sec');
         }, 0);
      };

      //]]>
      </script>
   </head>
   <body>
      <div class="nav"><a href="index.html">Forge Tests</a> / Performance</div>

      <div class="header">
         <h1>Performance Tests</h1>
      </div>

      <div class="content">

         <fieldset class="section">
            <ul>
               <li>Use the controls below to run Forge performance tests.</li>
               <li>You currently need a JavaScript console to view the output.</li>
            </ul>
         </fieldset>

         <fieldset class="section">
            <legend>Tests</legend>

         <div id="random_controls">
            <button id="random" onclick="javascript:return test_random();">paint random</button>
            <button id="clear" onclick="javascript:return canvas_clear();">clear</button>
         </div>
         <canvas id="canvas" width="100" height="100"></canvas>
         <div id="buffer_controls">
            <button id="buffer_fill" onclick="javascript:return test_buffer_fill();">buffer fill</button>
            <button id="buffer_xor" onclick="javascript:return test_buffer_xor();">buffer xor</button>
         </div>
         <div id="base64_controls">
            <button id="base64_encode" onclick="javascript:return test_base64_encode();">base64 encode</button>
            <button id="base64_decode" onclick="javascript:return test_base64_decode();">base64 decode</button>
         </div>
         <div id="hash_controls">
            <button id="md5" onclick="javascript:return test_md5();">md5</button>
            <button id="sha1" onclick="javascript:return test_sha1();">sha1</button>
            <button id="sha256" onclick="javascript:return test_sha256();">sha256</button>
         </div>
         <div id="aes_controls">
            <button id="aes_128" onclick="javascript:return aes_128();">aes-128</button>
            <button id="aes_128_cbc" onclick="javascript:return aes_128_cbc();">aes-128 cbc</button>
         </div>
         </fieldset>
      </div>
   </body>
</html>