Skip to content

Commit 7afc833

Browse files
committed
improve micro benchmarks
1 parent ade40a4 commit 7afc833

File tree

5 files changed

+188
-49
lines changed

5 files changed

+188
-49
lines changed

benchmark/md4-cache.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const createHash = require("../lib/util/createHash");
2+
3+
const compare = require("./micro-compare");
4+
5+
const size = 50;
6+
7+
const strings = [];
8+
for (let count = 1; ; count *= 10) {
9+
while (strings.length < count) {
10+
const s = require("crypto").randomBytes(size).toString("hex");
11+
strings.push(s);
12+
const hash = createHash("native-md4");
13+
hash.update(s);
14+
hash.update(s);
15+
hash.digest("hex");
16+
}
17+
let i = 0;
18+
console.log(
19+
`${count} different 200 char strings: ` +
20+
compare(
21+
"native md4",
22+
() => {
23+
const hash = createHash("native-md4");
24+
const s = strings[(i = (i + 1) % strings.length)];
25+
hash.update(s);
26+
hash.update(s);
27+
return hash.digest("hex");
28+
},
29+
"wasm md4",
30+
() => {
31+
const hash = createHash("md4");
32+
const s = strings[(i = (i + 1) % strings.length)];
33+
hash.update(s);
34+
hash.update(s);
35+
return hash.digest("hex");
36+
}
37+
)
38+
);
39+
}

benchmark/md4.js

+11-49
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,47 @@
1-
const crypto = require("crypto");
21
const createHash = require("../lib/util/createHash");
32

4-
let result;
5-
6-
const measure = (fn, count) => {
7-
const start = process.hrtime.bigint();
8-
for (let i = 0; i < count; i++) result = fn();
9-
return Number(process.hrtime.bigint() - start);
10-
};
11-
12-
const NS_PER_MS = 1000000; // 1ms
13-
const MIN_DURATION = 100 * NS_PER_MS; // 100ms
14-
const MAX_DURATION = 1000 * NS_PER_MS; // 1000ms
15-
const MAX_WARMUP_DURATION = 1 * NS_PER_MS; // 1ms
16-
17-
const format = (fast, slow, fastName, slowName, count) => {
18-
return `${fastName} is ${
19-
Math.round(((slow - fast) * 1000) / slow) / 10
20-
}% faster than ${slowName} (${Math.round(fast / 100 / count) / 10} µs vs ${
21-
Math.round(slow / 100 / count) / 10
22-
} µs, ${count}x)`;
23-
};
24-
25-
const compare = (n1, f1, n2, f2) => {
26-
let count = 1;
27-
while (true) {
28-
const timings = [f1, f2, f1, f2, f1, f2].map(f => measure(f, count));
29-
const t1 = Math.min(timings[0], timings[2], timings[4]);
30-
const t2 = Math.min(timings[1], timings[3], timings[5]);
31-
if (count === 1 && (t1 > MAX_WARMUP_DURATION || t2 > MAX_WARMUP_DURATION)) {
32-
continue;
33-
}
34-
if (
35-
(t1 > MIN_DURATION && t2 > MIN_DURATION) ||
36-
t1 > MAX_DURATION ||
37-
t2 > MAX_DURATION
38-
) {
39-
return t1 > t2
40-
? format(t2, t1, n2, n1, count)
41-
: format(t1, t2, n1, n2, count);
42-
}
43-
count *= 2;
44-
}
45-
};
3+
const compare = require("./micro-compare");
464

475
for (const size of [
48-
1, 2, 4, 8, 10, 20, 40, 60, 80, 100, 200, 1000, 5000, 8183, 8184, 8185, 10000,
49-
20000, 32768, 32769, 50000, 100000, 200000, 500000
6+
1, 10, 20, 40, 60, 80, 100, 200, 400, 1000, 1001, 5000, 8183, 8184, 8185,
7+
10000, 20000, 32768, 32769, 50000, 100000, 200000
508
]) {
519
const longString = require("crypto").randomBytes(size).toString("hex");
5210
const buffer = require("crypto").randomBytes(size * 2);
5311
console.log(
5412
`string ${longString.length} chars: ` +
5513
compare(
56-
"crypto md4",
14+
"native md4",
5715
() => {
58-
const hash = crypto.createHash("md4");
16+
const hash = createHash("native-md4");
17+
hash.update(longString);
5918
hash.update(longString);
6019
return hash.digest("hex");
6120
},
6221
"wasm md4",
6322
() => {
6423
const hash = createHash("md4");
6524
hash.update(longString);
25+
hash.update(longString);
6626
return hash.digest("hex");
6727
}
6828
)
6929
);
7030
console.log(
7131
`buffer ${buffer.length} bytes: ` +
7232
compare(
73-
"crypto md4",
33+
"native md4",
7434
() => {
75-
const hash = crypto.createHash("md4");
35+
const hash = createHash("native-md4");
36+
hash.update(buffer);
7637
hash.update(buffer);
7738
return hash.digest("hex");
7839
},
7940
"wasm md4",
8041
() => {
8142
const hash = createHash("md4");
8243
hash.update(buffer);
44+
hash.update(buffer);
8345
return hash.digest("hex");
8446
}
8547
)

benchmark/micro-compare.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
let result;
2+
3+
const measure = (fn, count) => {
4+
const start = process.hrtime.bigint();
5+
for (let i = 0; i < count; i++) result = fn();
6+
return Number(process.hrtime.bigint() - start);
7+
};
8+
9+
const NS_PER_MS = 1000000; // 1ms
10+
const MIN_DURATION = 100 * NS_PER_MS; // 100ms
11+
const MAX_DURATION = 1000 * NS_PER_MS; // 1000ms
12+
const MAX_WARMUP_DURATION = 1 * NS_PER_MS; // 1ms
13+
14+
const format = (fast, slow, fastName, slowName, count) => {
15+
return `${fastName} is ${
16+
Math.round(((slow - fast) * 1000) / slow) / 10
17+
}% faster than ${slowName} (${Math.round(fast / 100 / count) / 10} µs vs ${
18+
Math.round(slow / 100 / count) / 10
19+
} µs, ${count}x)`;
20+
};
21+
22+
const compare = (n1, f1, n2, f2) => {
23+
let count = 1;
24+
while (true) {
25+
const timings = [f1, f2, f1, f2, f1, f2].map(f => measure(f, count));
26+
const t1 = Math.min(timings[0], timings[2], timings[4]);
27+
const t2 = Math.min(timings[1], timings[3], timings[5]);
28+
if (count === 1 && (t1 > MAX_WARMUP_DURATION || t2 > MAX_WARMUP_DURATION)) {
29+
continue;
30+
}
31+
if (
32+
(t1 > MIN_DURATION && t2 > MIN_DURATION) ||
33+
t1 > MAX_DURATION ||
34+
t2 > MAX_DURATION
35+
) {
36+
return t1 > t2
37+
? format(t2, t1, n2, n1, count)
38+
: format(t1, t2, n1, n2, count);
39+
}
40+
count *= 2;
41+
}
42+
};
43+
44+
module.exports = compare;

benchmark/xxhash64-vs-md4.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const createHash = require("../lib/util/createHash");
2+
3+
const compare = require("./micro-compare");
4+
5+
for (const size of [
6+
1, 10, 20, 40, 60, 80, 100, 200, 400, 1000, 1001, 5000, 8183, 8184, 8185,
7+
10000, 20000, 32768, 32769, 50000, 100000, 200000
8+
]) {
9+
const longString = require("crypto").randomBytes(size).toString("hex");
10+
const buffer = require("crypto").randomBytes(size * 2);
11+
console.log(
12+
`string ${longString.length} chars: ` +
13+
compare(
14+
"wasm xxhash64",
15+
() => {
16+
const hash = createHash("xxhash64");
17+
hash.update(longString);
18+
return hash.digest("hex");
19+
},
20+
"wasm md4",
21+
() => {
22+
const hash = createHash("md4");
23+
hash.update(longString);
24+
return hash.digest("hex");
25+
}
26+
)
27+
);
28+
console.log(
29+
`buffer ${buffer.length} bytes: ` +
30+
compare(
31+
"wasm xxhash64",
32+
() => {
33+
const hash = createHash("xxhash64");
34+
hash.update(buffer);
35+
return hash.digest("hex");
36+
},
37+
"wasm md4",
38+
() => {
39+
const hash = createHash("md4");
40+
hash.update(buffer);
41+
return hash.digest("hex");
42+
}
43+
)
44+
);
45+
}

benchmark/xxhash64.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const createHash = require("../lib/util/createHash");
2+
3+
const compare = require("./micro-compare");
4+
5+
for (const size of [
6+
1, 10, 20, 40, 60, 80, 100, 200, 400, 1000, 1001, 5000, 8183, 8184, 8185,
7+
10000, 20000, 32768, 32769, 50000, 100000, 200000
8+
]) {
9+
const longString = require("crypto").randomBytes(size).toString("hex");
10+
const buffer = require("crypto").randomBytes(size * 2);
11+
console.log(
12+
`string ${longString.length} chars: ` +
13+
compare(
14+
"wasm xxhash64",
15+
() => {
16+
const hash = createHash("xxhash64");
17+
hash.update(longString);
18+
hash.update(longString);
19+
return hash.digest("hex");
20+
},
21+
"native md4",
22+
() => {
23+
const hash = createHash("native-md4");
24+
hash.update(longString);
25+
hash.update(longString);
26+
return hash.digest("hex");
27+
}
28+
)
29+
);
30+
console.log(
31+
`buffer ${buffer.length} bytes: ` +
32+
compare(
33+
"wasm xxhash64",
34+
() => {
35+
const hash = createHash("xxhash64");
36+
hash.update(buffer);
37+
hash.update(buffer);
38+
return hash.digest("hex");
39+
},
40+
"native md4",
41+
() => {
42+
const hash = createHash("native-md4");
43+
hash.update(buffer);
44+
hash.update(buffer);
45+
return hash.digest("hex");
46+
}
47+
)
48+
);
49+
}

0 commit comments

Comments
 (0)