From 75b60b999936714e08d70bda228e6ab07f3dde38 Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 23 Nov 2021 15:54:39 +0100 Subject: [PATCH] Adjust and enable old perfromance tests (#1233) --- packages/util-crypto/test/performance.ts | 4 ++-- packages/util/src/hex/toU8a.spec.ts | 23 ++++++++--------------- packages/util/src/hex/toU8aBuffer.spec.ts | 23 ++++++++--------------- packages/util/src/is/hex.spec.ts | 14 +++----------- packages/util/src/u8a/eq.spec.ts | 23 ++++++++--------------- packages/util/src/u8a/toHex.spec.ts | 23 ++++++++--------------- packages/util/src/u8a/toHexBuffer.spec.ts | 23 ++++++++--------------- packages/util/test/performance.ts | 4 ++-- 8 files changed, 47 insertions(+), 90 deletions(-) diff --git a/packages/util-crypto/test/performance.ts b/packages/util-crypto/test/performance.ts index 846a16eb6..117e5042c 100644 --- a/packages/util-crypto/test/performance.ts +++ b/packages/util-crypto/test/performance.ts @@ -1,7 +1,7 @@ // Copyright 2017-2021 @polkadot/util-crypto authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { arrayRange } from '@polkadot/util'; +import { arrayRange, formatNumber } from '@polkadot/util'; import { randomAsU8a } from '@polkadot/util-crypto'; import { formatOps, performanceJs } from '../../util/test/performance'; @@ -31,7 +31,7 @@ export function performanceWasm (name: string, count: number, exec: ExecFn): voi const [js, rjs] = loop(count, true, exec); console.log(` -performance run for ${name} completed with ${count} iterations. +performance run for ${name} completed with ${formatNumber(count)} iterations. ${'WebAssembly:'.padStart(19)} ${ws.toString().padStart(15)} ms ${ws < js ? '(fastest)' : `(slowest, ${(ws / js).toFixed(2)}x)`}${formatOps(count, ws)} diff --git a/packages/util/src/hex/toU8a.spec.ts b/packages/util/src/hex/toU8a.spec.ts index b04a70a10..bff76ea46 100644 --- a/packages/util/src/hex/toU8a.spec.ts +++ b/packages/util/src/hex/toU8a.spec.ts @@ -1,9 +1,16 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { performanceJs } from '../../test/performance'; import { U16_TO_HEX } from './alphabet'; import { hexToU8a } from '.'; +let ptest = '0x'; + +for (let i = 0; i < 640 * 1024; i++) { + ptest += U16_TO_HEX[i % (256 * 256)]; +} + describe('hexToU8a', (): void => { it('returns an empty Uint8Array when null provided', (): void => { expect( @@ -47,19 +54,5 @@ describe('hexToU8a', (): void => { ).toThrow(/hex value to convert/); }); - it.skip('performance', (): void => { - let a = '0x'; - - for (let i = 0; i < 640000; i++) { - a += U16_TO_HEX[i % (256 * 256)]; - } - - console.time('hexToU8a:performance'); - - for (let i = 0; i < 64; i++) { - hexToU8a(a); - } - - console.timeEnd('hexToU8a:performance'); - }); + performanceJs('hexToU8a (640k input)', 10, [[ptest]], hexToU8a); }); diff --git a/packages/util/src/hex/toU8aBuffer.spec.ts b/packages/util/src/hex/toU8aBuffer.spec.ts index 712e28471..024b90154 100644 --- a/packages/util/src/hex/toU8aBuffer.spec.ts +++ b/packages/util/src/hex/toU8aBuffer.spec.ts @@ -1,9 +1,16 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { performanceJs } from '../../test/performance'; import { U16_TO_HEX } from './alphabet'; import { hexToU8a } from './toU8aBuffer'; +let ptest = '0x'; + +for (let i = 0; i < 640 * 1024; i++) { + ptest += U16_TO_HEX[i % (256 * 256)]; +} + describe('hexToU8a', (): void => { it('returns an empty Uint8Array when null provided', (): void => { expect( @@ -41,19 +48,5 @@ describe('hexToU8a', (): void => { ).toThrow(/hex value to convert/); }); - it.skip('performance', (): void => { - let a = '0x'; - - for (let i = 0; i < 640000; i++) { - a += U16_TO_HEX[i % (256 * 256)]; - } - - console.time('hexToU8a:performance'); - - for (let i = 0; i < 64; i++) { - hexToU8a(a); - } - - console.timeEnd('hexToU8a:performance'); - }); + performanceJs('hexToU8a (Node Buffer, 640k input)', 10, [[ptest]], hexToU8a); }); diff --git a/packages/util/src/is/hex.spec.ts b/packages/util/src/is/hex.spec.ts index ef950521b..d14468a92 100644 --- a/packages/util/src/is/hex.spec.ts +++ b/packages/util/src/is/hex.spec.ts @@ -1,10 +1,12 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { performanceJs } from '../../test/performance'; import { isHex } from '.'; describe('isHex', (): void => { const test = '1234abcd'; + const ptest = '0x1234adbcdef5798351773526352876adbcdef2358735adbcdef2623587adbcdef11234adbcdef5798351773526352876adbcdef2358735adbcdef2623587adbcdef11234adbcdef5798351773526352876adbcdef2358735adbcdef2623587adbcdef11234adbcdef5798351773526352876adbcdef2358735adbcdef2623587adbcdef1'; it('returns true on 0x hex values', (): void => { expect( @@ -69,15 +71,5 @@ describe('isHex', (): void => { ).toEqual(true); }); - it.skip('performance', (): void => { - const test = '0x1234adbcdef5798351773526352876adbcdef2358735adbcdef2623587adbcdef11234adbcdef5798351773526352876adbcdef2358735adbcdef2623587adbcdef11234adbcdef5798351773526352876adbcdef2358735adbcdef2623587adbcdef11234adbcdef5798351773526352876adbcdef2358735adbcdef2623587adbcdef1'; - - console.time('performance:isHex'); - - for (let i = 0; i < 512000; i++) { - isHex(test); - } - - console.timeEnd('performance:isHex'); - }); + performanceJs('isHex (1024-bit)', 1000000, [[ptest]], isHex); }); diff --git a/packages/util/src/u8a/eq.spec.ts b/packages/util/src/u8a/eq.spec.ts index b56cbe45e..105dbc235 100644 --- a/packages/util/src/u8a/eq.spec.ts +++ b/packages/util/src/u8a/eq.spec.ts @@ -1,8 +1,15 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { performanceJs } from '../../test/performance'; import { u8aEq } from '.'; +const ptest = new Uint8Array(32768); + +for (let i = 0; i < ptest.length; i++) { + ptest[i] = i % 256; +} + describe('u8aEq', (): void => { it('returns false when the lengths do not match', (): void => { expect( @@ -88,19 +95,5 @@ describe('u8aEq', (): void => { ).toEqual(true); }); - it.skip('performance', (): void => { - const a = new Uint8Array(32768); - - for (let i = 0; i < a.length; i++) { - a[i] = i % 256; - } - - console.time('u8aEq:performance'); - - for (let i = 0; i < 65536; i++) { - u8aEq(a, a); - } - - console.timeEnd('u8aEq:performance'); - }); + performanceJs('u8aEq (32k cmp)', 50000, [[ptest, ptest]], u8aEq); }); diff --git a/packages/util/src/u8a/toHex.spec.ts b/packages/util/src/u8a/toHex.spec.ts index 528dd67a5..c1857d2a0 100644 --- a/packages/util/src/u8a/toHex.spec.ts +++ b/packages/util/src/u8a/toHex.spec.ts @@ -1,8 +1,15 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { performanceJs } from '../../test/performance'; import { u8aToHex } from '.'; +const ptest = new Uint8Array(32768); + +for (let i = 0; i < ptest.length; i++) { + ptest[i] = i % 256; +} + describe('u8aToHex', (): void => { it('returns empty as 0x', (): void => { expect( @@ -58,19 +65,5 @@ describe('u8aToHex', (): void => { ).toEqual('0x8000…0c0d'); }); - it.skip('performance', (): void => { - const a = new Uint8Array(32768); - - for (let i = 0; i < a.length; i++) { - a[i] = i % 256; - } - - console.time('u8aToHex:performance'); - - for (let i = 0; i < 65536; i++) { - u8aToHex(a); - } - - console.timeEnd('u8aToHex:performance'); - }); + performanceJs('u8aToHex (32k input)', 2000, [[ptest]], u8aToHex); }); diff --git a/packages/util/src/u8a/toHexBuffer.spec.ts b/packages/util/src/u8a/toHexBuffer.spec.ts index 77af52cf5..dd8b99a8c 100644 --- a/packages/util/src/u8a/toHexBuffer.spec.ts +++ b/packages/util/src/u8a/toHexBuffer.spec.ts @@ -1,8 +1,15 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { performanceJs } from '../../test/performance'; import { u8aToHex } from './toHexBuffer'; +const ptest = new Uint8Array(32768); + +for (let i = 0; i < ptest.length; i++) { + ptest[i] = i % 256; +} + describe('u8aToHex', (): void => { it('returns empty as 0x', (): void => { expect( @@ -58,19 +65,5 @@ describe('u8aToHex', (): void => { ).toEqual('0x8000…0c0d'); }); - it.skip('performance', (): void => { - const a = new Uint8Array(32768); - - for (let i = 0; i < a.length; i++) { - a[i] = i % 256; - } - - console.time('u8aToHex:performance'); - - for (let i = 0; i < 65536; i++) { - u8aToHex(a); - } - - console.timeEnd('u8aToHex:performance'); - }); + performanceJs('u8aToHex (Node Buffer, 32k input)', 2000, [[ptest]], u8aToHex); }); diff --git a/packages/util/test/performance.ts b/packages/util/test/performance.ts index 7547af24f..efcc143e8 100644 --- a/packages/util/test/performance.ts +++ b/packages/util/test/performance.ts @@ -1,7 +1,7 @@ // Copyright 2017-2021 @polkadot/util-crypto authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { formatDecimal } from '@polkadot/util'; +import { formatDecimal, formatNumber } from '@polkadot/util'; // eslint-disable-next-line @typescript-eslint/no-explicit-any type ExecFn = (...params: any[]) => unknown; @@ -36,7 +36,7 @@ export function performanceJs (name: string, count: number, inputs: unknown[][], const time = loop(count, inputs, exec); console.log(` -performance run for ${name} completed with ${count} iterations. +performance run for ${name} completed with ${formatNumber(count)} iterations. ${'JavaScript:'.padStart(19)} ${time.toString().padStart(15)} ms${formatOps(count, time)} `);