-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathuseBenchmarks.ts
More file actions
110 lines (100 loc) · 3.34 KB
/
useBenchmarks.ts
File metadata and controls
110 lines (100 loc) · 3.34 KB
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
import { useEffect, useState } from 'react';
import { BenchmarkSuite } from '../benchmarks/benchmarks';
import blake3 from '../benchmarks/blake3/blake3';
import cipher from '../benchmarks/cipher/cipher';
import ed from '../benchmarks/ed/ed25519';
import hkdf from '../benchmarks/hkdf/hkdf';
import hash from '../benchmarks/hash/hash';
import hmac from '../benchmarks/hmac/hmac';
import pbkdf2 from '../benchmarks/pbkdf2/pbkdf2';
import ecdh from '../benchmarks/ecdh/ecdh';
import dh from '../benchmarks/dh/dh';
import random from '../benchmarks/random/randomBytes';
import scrypt from '../benchmarks/scrypt/scrypt';
import encoding from '../benchmarks/encoding/encoding';
import xsalsa20 from '../benchmarks/cipher/xsalsa20';
export const useBenchmarks = (): [
BenchmarkSuite[],
(name: string) => void,
() => void,
() => void,
() => void,
() => void,
] => {
const [suites, setSuites] = useState<BenchmarkSuite[]>([]);
const [runCurrent, setRunCurrent] = useState<number>(-1);
// initial load of benchmark suites
useEffect(() => {
const newSuites: BenchmarkSuite[] = [];
newSuites.push(new BenchmarkSuite('blake3', blake3));
newSuites.push(new BenchmarkSuite('cipher', [...xsalsa20, ...cipher]));
newSuites.push(new BenchmarkSuite('ed', ed));
newSuites.push(new BenchmarkSuite('pbkdf2', pbkdf2));
newSuites.push(new BenchmarkSuite('hash', hash));
newSuites.push(new BenchmarkSuite('hmac', hmac));
newSuites.push(new BenchmarkSuite('hkdf', hkdf));
newSuites.push(new BenchmarkSuite('ecdh', ecdh));
newSuites.push(new BenchmarkSuite('dh', dh));
newSuites.push(
new BenchmarkSuite('random', random, {
'browserify/randombytes':
'polyfilled with RNQC, so a somewhat senseless benchmark',
}),
);
newSuites.push(new BenchmarkSuite('scrypt', scrypt));
newSuites.push(
new BenchmarkSuite('encoding', encoding, {
'Buffer polyfill': 'old CraftzdogBuffer.from/toString path',
}),
);
setSuites(newSuites);
}, []);
// This jank is used to trick async functions into running synchronously
// so we run one benchmark at a time and have dedicated resources instead of
// conflicting with other benchmarks.
useEffect(() => {
if (runCurrent < 0) return; // not running benchmarks
// reset to -1 if we're past the end
if (runCurrent >= suites.length) {
setRunCurrent(-1);
return;
}
const s = suites[runCurrent];
if (s?.enabled) {
updateSuites(suite => {
if (suite.name === s.name) {
suite.state = 'running';
}
});
} else {
setRunCurrent(runCurrent + 1);
}
}, [runCurrent]);
const updateSuites = (fn: (suite: BenchmarkSuite) => void) => {
if (!suites.length) return;
const copy = [...suites];
copy.forEach(fn);
setSuites(copy);
};
const toggle = (name: string) =>
updateSuites(suite => {
if (suite.name === name) {
suite.enabled = !suite.enabled;
}
});
const checkAll = () =>
updateSuites(suite => {
suite.enabled = true;
});
const clearAll = () =>
updateSuites(suite => {
suite.enabled = false;
});
const runBenchmarks = () => {
setRunCurrent(0);
};
const bumpRunCurrent = () => {
setRunCurrent(runCurrent + 1);
};
return [suites, toggle, checkAll, clearAll, runBenchmarks, bumpRunCurrent];
};