-
Notifications
You must be signed in to change notification settings - Fork 12
/
benchmark.js
191 lines (179 loc) · 5.5 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
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
/* eslint no-unused-vars: 0 */
const Benchmark = require("benchmark");
const native = require("./src/native.js");
const addon = require("./build/Release/addon.node");
const wasm = require("./src/wasm.js");
const randomInRange = (max, min = 0) =>
Math.floor(Math.random() * (max - min + 1 + min));
const generateString = (length) => {
const text = [];
const possible =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < length; i++) {
text.push(possible.charAt(randomInRange(possible.length)));
}
return text.join("");
};
const generateRegressionData = (amount, length, isTyped) => {
const result = new Array(amount);
for (let i = 0; i < amount; i++) {
const y = isTyped ? new Float64Array(length) : new Array(length);
const x = isTyped ? new Float64Array(length) : new Array(length);
const slope = parseFloat((Math.random() * 10).toPrecision(2));
const intercept = parseFloat((Math.random() * 10).toPrecision(2));
for (let i = 0; i < length; i++) {
x[i] = Math.floor(Math.random() * 100);
y[i] = intercept + slope * x[i] + Math.random();
}
result[i] = [y, x];
}
return result;
};
const generateStringsArray = (amount, length) => {
const result = new Array(amount);
for (let i = 0; i < amount; i++) {
result[i] = generateString(randomInRange(length));
}
return result;
};
const levensteinData = generateStringsArray(100, 40);
const SHAData = generateStringsArray(1000, 1000);
const regressionData = generateRegressionData(10, 1000);
const regressionDataTyped = generateRegressionData(10, 1000, true);
const levensteinSuite = new Benchmark.Suite("Levenstein Distance:");
levensteinSuite
.add("Native", () => {
const result = native.levenstein(
levensteinData[randomInRange(99)],
levensteinData[randomInRange(99)]
);
})
.add("N-API Addon", () => {
const result = addon.levenstein(
levensteinData[randomInRange(99)],
levensteinData[randomInRange(99)]
);
})
.add("Web Assembly", () => {
const result = wasm.levenstein(
levensteinData[randomInRange(99)],
levensteinData[randomInRange(99)]
);
})
.on("start", (event) => {
console.log(event.currentTarget.name);
})
.on("cycle", (event) => {
console.log(` ${String(event.target)}`);
})
.on("complete", (event) => {
console.log(
` Fastest is ${event.currentTarget.filter("fastest").map("name")}`
);
console.log("");
});
const fibonacciSuite = new Benchmark.Suite("Fibonacci:");
fibonacciSuite
.add("Native", () => {
const result = native.fibonacci(randomInRange(45, 30));
})
.add("N-API Addon", () => {
const result = addon.fibonacci(randomInRange(45, 30));
})
.add("Web Assembly", () => {
const result = wasm.fibonacci(randomInRange(45, 30));
})
.on("start", (event) => {
console.log(event.currentTarget.name);
})
.on("cycle", (event) => {
console.log(` ${String(event.target)}`);
})
.on("complete", (event) => {
console.log(
` Fastest is ${event.currentTarget.filter("fastest").map("name")}`
);
console.log("");
});
const fermatSuite = new Benchmark.Suite("Fermat Primality Test:");
fermatSuite
.add("Native", () => {
const result = native.fermat(randomInRange(1000000), 3);
})
.add("N-API Addon", () => {
const result = addon.fermat(randomInRange(1000000), 3);
})
.add("Web Assembly", () => {
const result = wasm.fermat(randomInRange(1000000), 3);
})
.on("start", (event) => {
console.log(event.currentTarget.name);
})
.on("cycle", (event) => {
console.log(` ${String(event.target)}`);
})
.on("complete", (event) => {
console.log(
` Fastest is ${event.currentTarget.filter("fastest").map("name")}`
);
console.log("");
});
const regressionSuite = new Benchmark.Suite("Simple Linear Regression:");
regressionSuite
.add("Native", () => {
const result = native.regression(...regressionData[randomInRange(9)]);
})
.add("N-API Addon", () => {
const result = addon.regression(...regressionData[randomInRange(9)]);
})
.add("N-API Addon using TypedArrays", () => {
const result = addon.regression(...regressionDataTyped[randomInRange(9)]);
})
.add("Web Assembly", () => {
const result = wasm.regression(...regressionData[randomInRange(9)]);
})
.add("Web Assembly using TypedArrays", () => {
const result = wasm.regression(...regressionDataTyped[randomInRange(9)]);
})
.on("start", (event) => {
console.log(event.currentTarget.name);
})
.on("cycle", (event) => {
console.log(` ${String(event.target)}`);
})
.on("complete", (event) => {
console.log(
` Fastest is ${event.currentTarget.filter("fastest").map("name")}`
);
console.log("");
});
const sha256Suite = new Benchmark.Suite("SHA256:");
sha256Suite
.add("Native", () => {
const result = native.sha256(SHAData[randomInRange(999)]);
})
.add("N-API Addon", () => {
const result = addon.sha256(SHAData[randomInRange(999)]);
})
.add("Web Assembly", () => {
const result = wasm.sha256(SHAData[randomInRange(999)]);
})
.on("start", (event) => {
console.log(event.currentTarget.name);
})
.on("cycle", (event) => {
console.log(` ${String(event.target)}`);
})
.on("complete", (event) => {
console.log(
` Fastest is ${event.currentTarget.filter("fastest").map("name")}`
);
console.log("");
});
wasm.onRuntimeInitialized = () => {
levensteinSuite.run();
fibonacciSuite.run();
fermatSuite.run();
regressionSuite.run();
sha256Suite.run();
};