Change Date.now to performance.now (#4963)
* Change Date.now to performance.now * Additional logs
This commit is contained in:
@@ -5,7 +5,7 @@ import { TypeRegistry } from '@polkadot/types';
|
||||
import { CodecDate, Compact, U32 } from '@polkadot/types-codec';
|
||||
import { BN } from '@polkadot/util';
|
||||
|
||||
import { performance } from '../test/performance';
|
||||
import { perf } from '../test/performance';
|
||||
|
||||
const CompactU32 = Compact.with(U32);
|
||||
|
||||
@@ -93,5 +93,5 @@ describe('Compact', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
performance('Compact', 75_000, [[new Uint8Array([63 << 2])]], (v: Uint8Array) => new CompactU32(registry, v));
|
||||
perf('Compact', 75_000, [[new Uint8Array([63 << 2])]], (v: Uint8Array) => new CompactU32(registry, v));
|
||||
});
|
||||
|
||||
@@ -7,7 +7,7 @@ import { TypeRegistry } from '@polkadot/types';
|
||||
import { Enum, Null, Text, U32 } from '@polkadot/types-codec';
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { performance } from '../test/performance';
|
||||
import { perf } from '../test/performance';
|
||||
|
||||
const PEnum = Enum.with({ a: U32, b: U32 });
|
||||
|
||||
@@ -454,5 +454,5 @@ describe('Enum', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
performance('Enum', 20_000, [[new Uint8Array([0, 31, 32, 33, 34])]], (v: Uint8Array) => new PEnum(registry, v));
|
||||
perf('Enum', 20_000, [[new Uint8Array([0, 31, 32, 33, 34])]], (v: Uint8Array) => new PEnum(registry, v));
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@ import { TypeRegistry } from '@polkadot/types';
|
||||
import { UInt } from '@polkadot/types-codec';
|
||||
import { BN, BN_TWO, isBn } from '@polkadot/util';
|
||||
|
||||
import { performance } from '../test/performance';
|
||||
import { perf } from '../test/performance';
|
||||
|
||||
describe('UInt', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
@@ -183,5 +183,5 @@ describe('UInt', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
performance('UInt', 75_000, [[new Uint8Array([31, 32, 33, 34])]], (v: Uint8Array) => new UInt(registry, v));
|
||||
perf('UInt', 75_000, [[new Uint8Array([31, 32, 33, 34])]], (v: Uint8Array) => new UInt(registry, v));
|
||||
});
|
||||
|
||||
@@ -9,7 +9,7 @@ import { Text, u32, Vec } from '@polkadot/types-codec';
|
||||
import rpcMetadata from '@polkadot/types-support/metadata/static-substrate';
|
||||
import { decodeAddress, randomAsU8a } from '@polkadot/util-crypto';
|
||||
|
||||
import { performance } from '../test/performance';
|
||||
import { perf } from '../test/performance';
|
||||
|
||||
const registry = new TypeRegistry();
|
||||
const metadata = new Metadata(registry, rpcMetadata);
|
||||
@@ -194,5 +194,5 @@ describe('Vec', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
performance('Vec<U32>', 40_000, [[new Uint8Array([3 << 2, 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34])]], (v: Uint8Array) => new VecU32(registry, v));
|
||||
perf('Vec<U32>', 40_000, [[new Uint8Array([3 << 2, 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34])]], (v: Uint8Array) => new VecU32(registry, v));
|
||||
});
|
||||
|
||||
@@ -7,7 +7,7 @@ import { TypeRegistry } from '@polkadot/types';
|
||||
import { Bytes, Raw, Text } from '@polkadot/types-codec';
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
|
||||
import { performance } from '../test/performance';
|
||||
import { perf } from '../test/performance';
|
||||
|
||||
describe('Text', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
@@ -79,5 +79,5 @@ describe('Text', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
performance('Text', 100_000, [[new Uint8Array([6 << 2, 102, 111, 111, 102, 111, 111])]], (v: Uint8Array) => new Text(registry, v));
|
||||
perf('Text', 100_000, [[new Uint8Array([6 << 2, 102, 111, 111, 102, 111, 111])]], (v: Uint8Array) => new Text(registry, v));
|
||||
});
|
||||
|
||||
@@ -12,7 +12,7 @@ const NUM_PAD = 16;
|
||||
const PRE_PAD = 32;
|
||||
|
||||
function loop (count: number, inputs: unknown[][], exec: ExecFn): [number, unknown[]] {
|
||||
const start = Date.now();
|
||||
const start = performance.now();
|
||||
const results = new Array<unknown>(inputs.length);
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
@@ -23,7 +23,7 @@ function loop (count: number, inputs: unknown[][], exec: ExecFn): [number, unkno
|
||||
}
|
||||
}
|
||||
|
||||
return [Date.now() - start, results];
|
||||
return [performance.now() - start, results];
|
||||
}
|
||||
|
||||
export function formatFixed (value: number): string {
|
||||
@@ -41,37 +41,14 @@ ${formatFixed(ops).padStart(NUM_PAD + PRE_PAD + 1)} ops/s
|
||||
${formatFixed(micro).padStart(NUM_PAD + PRE_PAD + 1)} μs/op`;
|
||||
}
|
||||
|
||||
export function performance (name: string, count: number, inputs: unknown[][], exec: ExecFn): void {
|
||||
export function perf (name: string, count: number, inputs: unknown[][], exec: ExecFn): void {
|
||||
it(`performance: ${name}`, (): void => {
|
||||
const [time] = loop(count, inputs, exec);
|
||||
|
||||
console.log(`
|
||||
performance run for ${name} completed with ${formatNumber(count)} iterations.
|
||||
|
||||
${`${name}:`.padStart(PRE_PAD)} ${time.toString().padStart(NUM_PAD)} ms${formatOps(count, time)}
|
||||
${`${name}:`.padStart(PRE_PAD)} ${time.toFixed(2).padStart(NUM_PAD)} ms${formatOps(count, time)}
|
||||
`);
|
||||
});
|
||||
}
|
||||
|
||||
export function performanceCmp (name: string, [first, second]: [string, string], count: number, inputs: unknown[][], exec: ExecFn): void {
|
||||
it(`performance: ${name}`, (): void => {
|
||||
const pa = inputs.map((values) => [...values, false]);
|
||||
const pb = inputs.map((values) => [...values, true]);
|
||||
const [ta, ra] = loop(count, pa, exec);
|
||||
const [tb, rb] = loop(count, pb, exec);
|
||||
|
||||
console.log(`
|
||||
performance run for ${name} completed with ${formatNumber(count)} iterations.
|
||||
|
||||
${`${first}:`.padStart(PRE_PAD)} ${ta.toString().padStart(NUM_PAD)} ms ${ta < tb ? '(fastest)' : `(slowest, ${(ta / tb).toFixed(2)}x)`}${formatOps(count, ta)}
|
||||
|
||||
${`${second}:`.padStart(PRE_PAD)} ${tb.toString().padStart(NUM_PAD)} ms ${ta > tb ? '(fastest)' : `(slowest, ${(tb / ta).toFixed(2)}x)`}${formatOps(count, tb)}
|
||||
`);
|
||||
|
||||
const unmatched = ra.filter((r, i) =>
|
||||
JSON.stringify(r) !== JSON.stringify(rb[i])
|
||||
);
|
||||
|
||||
expect(unmatched.length).toEqual(0);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ function decodeU8a (registry: Registry, value: Uint8Array): MetadataVersioned |
|
||||
*/
|
||||
export class Metadata extends MetadataVersioned {
|
||||
constructor (registry: Registry, value?: Uint8Array | HexString | Map<string, unknown> | Record<string, unknown>) {
|
||||
// console.time('Metadata')
|
||||
// const timeStart = performance.now()
|
||||
|
||||
super(
|
||||
registry,
|
||||
@@ -52,6 +52,6 @@ export class Metadata extends MetadataVersioned {
|
||||
: value
|
||||
);
|
||||
|
||||
// console.timeEnd('Metadata')
|
||||
// console.log('Metadata', `${(performance.now() - timeStart).toFixed(2)}ms`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,14 +32,14 @@ export class MetadataVersioned extends Struct {
|
||||
readonly #converted = new Map<MetaVersions, MetaMapped>();
|
||||
|
||||
constructor (registry: Registry, value?: Uint8Array | HexString | Map<string, unknown> | Record<string, unknown>) {
|
||||
// console.time('MetadataVersioned')
|
||||
// const timeStart = performance.now()
|
||||
|
||||
super(registry, {
|
||||
magicNumber: MagicNumber,
|
||||
metadata: 'MetadataAll'
|
||||
}, value);
|
||||
|
||||
// console.timeEnd('MetadataVersioned')
|
||||
// console.log('MetadataVersioned', `${(performance.now() - timeStart).toFixed(2)}ms`)
|
||||
}
|
||||
|
||||
#assertVersion = (version: number): boolean => {
|
||||
|
||||
@@ -478,7 +478,7 @@ export class PortableRegistry extends Struct implements ILookup {
|
||||
#types: Record<number, PortableType>;
|
||||
|
||||
constructor (registry: Registry, value?: Uint8Array, isContract?: boolean) {
|
||||
// console.time('PortableRegistry')
|
||||
// const timeStart = performance.now()
|
||||
|
||||
super(registry, {
|
||||
types: 'Vec<PortableType>'
|
||||
@@ -492,7 +492,7 @@ export class PortableRegistry extends Struct implements ILookup {
|
||||
this.#params = params;
|
||||
this.#types = types;
|
||||
|
||||
// console.timeEnd('PortableRegistry')
|
||||
// console.log('PortableRegistry', `${(performance.now() - timeStart).toFixed(2)}ms`)
|
||||
}
|
||||
|
||||
public get names (): string[] {
|
||||
|
||||
Reference in New Issue
Block a user