forked from tonyg/js-nacl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.js
73 lines (58 loc) · 2.29 KB
/
benchmark.js
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
function output(x) {
document.getElementById("output").innerHTML += x + "\n";
}
function main () {
try {
do_tests();
} catch (e) {
alert(JSON.stringify(e));
}
}
var TIMELIMIT = 1000;
function measure(desc, f) {
var startTime = new Date().getTime();
var stopTime;
var delta;
var i = 0;
var result;
while (1) {
result = f();
i++;
stopTime = new Date().getTime();
delta = stopTime - startTime;
if (delta > TIMELIMIT) break;
}
var iter_per_sec = i / (delta / 1000);
var sec_per_iter = (delta / 1000) / i;
output(desc + ", " + iter_per_sec + " Hz, " + sec_per_iter + " seconds per iteration");
return result;
}
function do_tests() {
var hello = nacl.encode_utf8("hello");
var kp = nacl.crypto_box_keypair_from_seed(hello);
var selfShared = nacl.crypto_box_precompute(kp.boxPk, kp.boxSk);
var n = nacl.crypto_box_random_nonce();
var c = nacl.crypto_box_precomputed(hello, n, selfShared);
var m = nacl.crypto_box_open_precomputed(c, n, selfShared);
var c2 = nacl.crypto_box(hello, n, kp.boxPk, kp.boxSk);
var m2 = nacl.crypto_box_open(c2, n, kp.boxPk, kp.boxSk);
measure('nacl.crypto_hash_string("hello")',
function () { return nacl.crypto_hash_string("hello") });
measure('nacl.crypto_hash(hello)',
function () { return nacl.crypto_hash(hello) });
measure('nacl.crypto_box_keypair_from_seed(hello)',
function () { return nacl.crypto_box_keypair_from_seed(hello) });
measure('nacl.crypto_box_precompute(kp.boxPk, kp.boxSk)',
function () { return nacl.crypto_box_precompute(kp.boxPk, kp.boxSk) });
measure('nacl.crypto_box_random_nonce()',
function () { return nacl.crypto_box_random_nonce() });
measure('nacl.crypto_box_precomputed(hello, n, selfShared)',
function () { return nacl.crypto_box_precomputed(hello, n, selfShared) });
measure('nacl.crypto_box_open_precomputed(c, n, selfShared)',
function () { return nacl.crypto_box_open_precomputed(c, n, selfShared) });
measure('nacl.crypto_box(hello, n, kp.boxPk, kp.boxSk)',
function () { return nacl.crypto_box(hello, n, kp.boxPk, kp.boxSk) });
measure('nacl.crypto_box_open(c2, n, kp.boxPk, kp.boxSk)',
function () { return nacl.crypto_box_open(c2, n, kp.boxPk, kp.boxSk) });
}
window.onload = main;