Extract performance test loop (& bump wasm) (#1217)

* Extract performance test loop (& bump wasm)

* bitLength for blake preformance

* sha512 naming

* ops/s display
This commit is contained in:
Jaco
2021-11-19 16:16:39 +02:00
committed by GitHub
parent 23d46b063c
commit d20c37ddd0
7 changed files with 76 additions and 101 deletions
+1 -1
View File
@@ -28,7 +28,7 @@
"@babel/runtime": "^7.16.3",
"@polkadot/networks": "7.8.3-37",
"@polkadot/util": "7.8.3-37",
"@polkadot/wasm-crypto": "^4.2.2-17",
"@polkadot/wasm-crypto": "^4.3.1",
"@polkadot/x-randomvalues": "7.8.3-37",
"blakejs": "^1.1.1",
"bn.js": "^4.12.0",
+5 -20
View File
@@ -4,6 +4,7 @@
import { hexToU8a } from '@polkadot/util';
import { waitReady } from '@polkadot/wasm-crypto';
import { performanceTest } from '../../test/performance';
import { blake2AsU8a } from '.';
describe('blake2AsU8a', (): void => {
@@ -59,25 +60,9 @@ describe('blake2AsU8a', (): void => {
expect(a).toEqual(b);
});
it.skip.each([false, true])('performance: onlyJs=%p', (onlyJs): void => {
const label = `blake2AsU8a:onlyJs=${onlyJs.toString()}`;
const test = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8
]);
console.time(label);
for (let i = 0; i < 32768; i++) {
blake2AsU8a(test);
}
console.timeEnd(label);
describe.each([256, 512] as (256 | 512)[])('bitLength=$p', (bitLength): void => {
performanceTest(`blake2AsU8a, bitLength=${bitLength}`, 64000, (input, onlyJs) =>
blake2AsU8a(input, bitLength, null, onlyJs)
);
});
});
+6 -23
View File
@@ -4,6 +4,7 @@
import { hexToU8a, stringToU8a } from '@polkadot/util';
import { waitReady } from '@polkadot/wasm-crypto';
import { performanceTest } from '../../test/performance';
import { keccakAsU8a } from '.';
describe('keccakAsU8a', (): void => {
@@ -21,8 +22,8 @@ describe('keccakAsU8a', (): void => {
)
};
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
describe.each([256, 512] as (256 | 512)[])('bitLength=$p', (bitLength): void => {
describe.each([256, 512] as (256 | 512)[])('bitLength=$p', (bitLength): void => {
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
it('returns an hex representation (string)', (): void => {
expect(
keccakAsU8a(input, bitLength, onlyJs)
@@ -41,27 +42,9 @@ describe('keccakAsU8a', (): void => {
).toEqual(output[bitLength]);
});
});
});
it.skip.each([false, true])('performance: onlyJs=%p', (onlyJs): void => {
const label = `keccakAsU8a:onlyJs=${onlyJs.toString()}`;
const test = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8
]);
console.time(label);
for (let i = 0; i < 128000; i++) {
keccakAsU8a(test, undefined, onlyJs);
}
console.timeEnd(label);
performanceTest(`keccakAsU8a, bitLength=${bitLength}`, 128000, (input, onlyJs) =>
keccakAsU8a(input, bitLength, onlyJs)
);
});
});
+4 -21
View File
@@ -4,6 +4,7 @@
import { hexToU8a } from '@polkadot/util';
import { waitReady } from '@polkadot/wasm-crypto';
import { performanceTest } from '../../test/performance';
import { sha256AsU8a } from '.';
const TESTS = [
@@ -42,25 +43,7 @@ describe('sha256AsU8a', (): void => {
});
});
it.skip.each([false, true])('performance: onlyJs=%p', (onlyJs): void => {
const label = `sha256AsU8a:onlyJs=${onlyJs.toString()}`;
const test = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8
]);
console.time(label);
for (let i = 0; i < 128000; i++) {
sha256AsU8a(test, onlyJs);
}
console.timeEnd(label);
});
performanceTest('sha256AsU8a', 128000, (input, onlyJs) =>
sha256AsU8a(input, onlyJs)
);
});
+4 -21
View File
@@ -3,6 +3,7 @@
import { waitReady } from '@polkadot/wasm-crypto';
import { performanceTest } from '../../test/performance';
import { sha512AsU8a } from '.';
describe('sha512AsU8a', (): void => {
@@ -22,25 +23,7 @@ describe('sha512AsU8a', (): void => {
});
});
it.skip.each([false, true])('performance: onlyJs=%p', (onlyJs): void => {
const label = `sha512AsU8a:onlyJs=${onlyJs.toString()}`;
const test = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8
]);
console.time(label);
for (let i = 0; i < 128000; i++) {
sha512AsU8a(test, onlyJs);
}
console.timeEnd(label);
});
performanceTest('sha512AsU8a', 64000, (input, onlyJs) =>
sha512AsU8a(input, onlyJs)
);
});
+41
View File
@@ -0,0 +1,41 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { arrayRange, formatNumber } from '@polkadot/util';
import { randomAsU8a } from '@polkadot/util-crypto';
const GENERATED = arrayRange(64).map(() => randomAsU8a());
function loop (count: number, onlyJs: boolean, exec: (input: Uint8Array, onlyJs: boolean) => unknown): number {
const start = Date.now();
for (let i = 0; i < count; i++) {
exec(GENERATED[i % GENERATED.length], onlyJs);
}
return Date.now() - start;
}
function perSecond (count: number, time: number): string {
const micro = (time * 1000) / count;
const ops = 1_000_000 / micro;
return `
${formatNumber(ops).padStart(10)} ops/s
${micro.toFixed(2).padStart(10)} μs/op`;
}
export function performanceTest (name: string, count: number, exec: (input: Uint8Array, onlyJs: boolean) => unknown): void {
it(`performance: ${name}`, (): void => {
const ws = loop(count, false, exec);
const js = loop(count, true, exec);
console.log(`
performance run for ${name} completed with ${count} iterations.
WebAssembly: ${ws.toString().padStart(10)} ms ${ws < js ? '(fastest)' : `(slowest, ${(ws / js).toFixed(2)}x)`}${perSecond(count, ws)}
JavaScript: ${js.toString().padStart(10)} ms ${ws > js ? '(fastest)' : `(slowest, ${(js / ws).toFixed(2)}x)`}${perSecond(count, js)}
`);
});
}
+15 -15
View File
@@ -2086,7 +2086,7 @@ __metadata:
"@babel/runtime": ^7.16.3
"@polkadot/networks": 7.8.3-37
"@polkadot/util": 7.8.3-37
"@polkadot/wasm-crypto": ^4.2.2-17
"@polkadot/wasm-crypto": ^4.3.1
"@polkadot/x-randomvalues": 7.8.3-37
"@types/bn.js": ^4.11.6
"@types/create-hash": ^1.2.2
@@ -2124,35 +2124,35 @@ __metadata:
languageName: unknown
linkType: soft
"@polkadot/wasm-crypto-asmjs@npm:^4.2.2-17":
version: 4.2.2-17
resolution: "@polkadot/wasm-crypto-asmjs@npm:4.2.2-17"
"@polkadot/wasm-crypto-asmjs@npm:^4.3.1":
version: 4.3.1
resolution: "@polkadot/wasm-crypto-asmjs@npm:4.3.1"
dependencies:
"@babel/runtime": ^7.16.3
checksum: e458b3a9c97c2739daf131fc0bfb436c11f40d2bfe821e42771e0b239549366d4e4cd184770feb0fe78784178d5f1cfe8e39dd3e379992e5fc64b9414b3f3a58
checksum: 145bff607c0a1da592d9945a902d5ea2373487a14149ed7fb940151c6977660c935d67bf90e06ea39affe3d72e4483a1b66d758447385039b41f45935db8e66a
languageName: node
linkType: hard
"@polkadot/wasm-crypto-wasm@npm:^4.2.2-17":
version: 4.2.2-17
resolution: "@polkadot/wasm-crypto-wasm@npm:4.2.2-17"
"@polkadot/wasm-crypto-wasm@npm:^4.3.1":
version: 4.3.1
resolution: "@polkadot/wasm-crypto-wasm@npm:4.3.1"
dependencies:
"@babel/runtime": ^7.16.3
checksum: cf4a7ca5a2300a41fb5975fd42e251ea7ccc50680e347c7383c21e8e4111377a0189171e29bef1a6edb364553778b1c662a3ab8939ffbecd978c3f380db33b27
checksum: e06c91e267790e100f5c36ff39bc226323cfc19600920ee2dff77e38251b28c8010d4b58372aaaffb60a5611dfc9b904e36c87ace2a40cf1bbee8446ff7282b5
languageName: node
linkType: hard
"@polkadot/wasm-crypto@npm:^4.2.2-17":
version: 4.2.2-17
resolution: "@polkadot/wasm-crypto@npm:4.2.2-17"
"@polkadot/wasm-crypto@npm:^4.3.1":
version: 4.3.1
resolution: "@polkadot/wasm-crypto@npm:4.3.1"
dependencies:
"@babel/runtime": ^7.16.3
"@polkadot/wasm-crypto-asmjs": ^4.2.2-17
"@polkadot/wasm-crypto-wasm": ^4.2.2-17
"@polkadot/wasm-crypto-asmjs": ^4.3.1
"@polkadot/wasm-crypto-wasm": ^4.3.1
peerDependencies:
"@polkadot/util": "*"
"@polkadot/x-randomvalues": "*"
checksum: f441aa5d7e644f36ad4fde231de0f1c148287b5119f1ad776962111647db7f54ee116091a3694a6cfa5083d7d2586faf953f1e931a7d550a099609979cb54bb7
checksum: 76bf64f148b3c8fb32a57ffdd982f88901d6aa17beb685fe79f7a3a0268202cce9da72659539964882c325eef3b04ab01e847f0090c064f412a09a2fb81399de
languageName: node
linkType: hard