-
Notifications
You must be signed in to change notification settings - Fork 0
/
bun_io_bench.js
57 lines (45 loc) · 1.5 KB
/
bun_io_bench.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
const NUM_EXPERIMENTS = 10;
const NUM_ITERATIONS = 100000;
const PATHS = ["./path/1/test.txt", "./path/2/test.txt", "./path/3/test.txt", "./path/4/test.txt", "./path/5/test.txt"];
let count;
function mean_std(vals) {
let sum = 0, mean, std = 0;
for (let i = 0; i < vals.length; i++) {
sum += vals[i];
}
mean = sum / vals.length;
for (let i = 0; i < vals.length; i++) {
std += Math.pow((vals[i] - mean), 2);
}
std = Math.sqrt(std / (vals.length));
return [mean, std];
}
let time_measure = [];
function bench(k) {
count = 0;
const t0 = performance.now();
for (let i = 0; i < NUM_ITERATIONS; i++) {
const f = Bun.file(PATHS[Math.floor(Math.random() * 5)]);
f.text().then(res => {
count++;
if (count === NUM_ITERATIONS) {
const t1 = performance.now();
const t = (t1 - t0);
time_measure.push(t);
if (k === NUM_EXPERIMENTS) {
let c = mean_std(time_measure);
let mean = c[0];
let stdev = c[1];
console.log("Number of runs: " + NUM_EXPERIMENTS);
console.log("Mean running time: " + mean + " ms.");
console.log("Standard deviation running time: " + stdev);
} else {
bench(k + 1);
}
}
}).catch(err => {
console.log("err")
});
}
}
bench(1);