From 3f514eedc73cfd997145ccd1839651b93c99f9bb Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 6 Dec 2024 16:39:58 -0800 Subject: [PATCH] Add more info to test logging. Comment out some portions of the test webpage to check performance. Add timers to performance tests. Initialize Pools when the wallet needs it instead of from the get-go. --- GLOBALS.mjs | 19 ++++--- perf/account.perf.js | 10 ++-- perf/wallet.perf.js | 18 ++++-- src/lib/pool.ts | 2 - src/lib/wallet.ts | 12 ++-- test.html | 132 +++++++++++++++++++++---------------------- 6 files changed, 98 insertions(+), 95 deletions(-) diff --git a/GLOBALS.mjs b/GLOBALS.mjs index dea5a8c..e2657fb 100644 --- a/GLOBALS.mjs +++ b/GLOBALS.mjs @@ -65,34 +65,35 @@ export function skip (name, ...args) { } export function test (name, fn) { + console.log(`=> Test: ${name}`) if (fn instanceof Promise) { try { return fn - .then(() => log(`PASS: ${name}`)) - .catch((err) => { error(`FAIL: ${name}: ${err}`) }) + .then(() => log(` -> PASS: ${name}`)) + .catch((err) => { error(` -> FAIL: ${name}: ${err}`) }) } catch (err) { - error(`FAIL: ${name}: ${err.message}`) + error(` -> FAIL: ${name}: ${err.message}`) error(err) } } else if (fn?.constructor?.name === 'AsyncFunction') { try { return fn() - .then(() => log(`PASS: ${name}`)) - .catch((err) => error(`FAIL: ${name}: ${err.message}`)) + .then(() => log(` -> PASS: ${name}`)) + .catch((err) => error(` -> FAIL: ${name}: ${err.message}`)) } catch (err) { - error(`FAIL: ${name}: ${err.message}`) + error(` -> FAIL: ${name}: ${err.message}`) error(err) } } else if (typeof fn === 'function') { try { fn() - log(`PASS: ${name}`) + log(` -> PASS: ${name}`) } catch (err) { - error(`FAIL: ${name}: ${err.message}`) + error(` -> FAIL: ${name}: ${err.message}`) error(err) } } else { - error(`FAIL: ${name}: test cannot execute on ${typeof fn} ${fn}`) + error(` -> FAIL: ${name}: test cannot execute on ${typeof fn} ${fn}`) } } diff --git a/perf/account.perf.js b/perf/account.perf.js index 4ce0a81..4f97b9c 100644 --- a/perf/account.perf.js +++ b/perf/account.perf.js @@ -10,19 +10,17 @@ import { Bip44Wallet, Blake2bWallet } from '#dist/main.js' await test('BIP-44 ckd performance test', async () => { const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - + let now = performance.now() const accounts = await wallet.accounts(0, 0x7fff) - - console.log(`HERE`) + console.log(`${performance.now() - now} ms`) assert.equals(accounts.length, 0x8000) }) await test('BLAKE2b ckd performance test', async () => { const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - + let now = performance.now() const accounts = await wallet.accounts(0, 0x7fff) - - console.log(`HERE 2`) + console.log(`${performance.now() - now} ms`) assert.equals(accounts.length, 0x8000) }) diff --git a/perf/wallet.perf.js b/perf/wallet.perf.js index 4b845a1..0e92618 100644 --- a/perf/wallet.perf.js +++ b/perf/wallet.perf.js @@ -8,17 +8,23 @@ import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js' import { Bip44Wallet, Blake2bWallet } from '#dist/main.js' await test('creating BIP-44 wallets performance test', async () => { - const wallets = [] + const seeds = [] + let now = performance.now() for (let i = 0x80; i > 0; i--) { - wallets.push(await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD)) + const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD) + seeds.push(wallet.seed) } - assert.equals(wallets.length, 0x80) + console.log(`${performance.now() - now} ms`) + assert.equals(seeds.length, 0x80) }) await test('creating BLAKE2b wallets performance test', async () => { - const wallets = [] + const seeds = [] + let now = performance.now() for (let i = 0x80; i > 0; i--) { - wallets.push(await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)) + const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) + seeds.push(wallet.seed) } - assert.equals(wallets.length, 0x80) + console.log(`${performance.now() - now} ms`) + assert.equals(seeds.length, 0x80) }) diff --git a/src/lib/pool.ts b/src/lib/pool.ts index b44b2ed..7c249b9 100644 --- a/src/lib/pool.ts +++ b/src/lib/pool.ts @@ -12,7 +12,6 @@ type Thread = { export class Pool { #approach: 'converge' | 'divide' = 'divide' #cores: number = Math.max(1, navigator.hardwareConcurrency - 1) - // #cores: number = 1 #queue: object[] = [] #resolve: Function = (value: unknown): void => { } #results: object[] = [] @@ -46,7 +45,6 @@ export class Pool { } #assign (thread: Thread, next: any[]): void { - console.dir(thread.worker) if (next?.length > 0) { thread.isBusy = true const buf = new TextEncoder().encode(JSON.stringify(next)).buffer diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 2c3ae3b..38a93d0 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -29,7 +29,6 @@ abstract class Wallet { #id: Entropy #locked: boolean = true #mnemonic: Bip39Mnemonic | null - #pool: Pool #safe: Safe #seed: string | null get id () { return this.#id.hex } @@ -59,7 +58,6 @@ abstract class Wallet { ? new Entropy(id) : new Entropy(16) this.#mnemonic = mnemonic ?? null - this.#pool = new Pool(`const Blake2b = ${Blake2b}\n${NanoNaCl}`) this.#safe = new Safe() this.#seed = seed ?? null } @@ -97,7 +95,9 @@ abstract class Wallet { let results = await this.ckd(indexes) const data: any = [] results.forEach(r => data.push({ privateKey: r.privateKey, index: r.index })) - const keypairs: KeyPair[] = await this.#pool.work('divide', data) + const pool = new Pool(`const Blake2b = ${Blake2b}\n${NanoNaCl}`) + const keypairs: KeyPair[] = await pool.work('divide', data) + pool.dismiss() for (const keypair of keypairs) { if (keypair.privateKey == null) throw new RangeError('Account private key missing') if (keypair.publicKey == null) throw new RangeError('Account public key missing') @@ -266,7 +266,6 @@ abstract class Wallet { */ export class Bip44Wallet extends Wallet { static #isInternal: boolean = false - #pool: Pool constructor (seed: string, mnemonic?: Bip39Mnemonic, id?: string) { if (!Bip44Wallet.#isInternal) { @@ -274,7 +273,6 @@ export class Bip44Wallet extends Wallet { } Bip44Wallet.#isInternal = false super(seed, mnemonic, id) - this.#pool = new Pool(Bip44Ckd) } /** @@ -423,9 +421,11 @@ export class Bip44Wallet extends Wallet { * @returns {Promise} */ async ckd (indexes: number[]): Promise { + const pool = new Pool(Bip44Ckd) const data: any = [] indexes.forEach(i => data.push({ seed: this.seed, index: i })) - const privateKeys: KeyPair[] = await this.#pool.work('divide', data) + const privateKeys: KeyPair[] = await pool.work('divide', data) + pool.dismiss() return privateKeys } } diff --git a/test.html b/test.html index 7347f65..010cfaa 100644 --- a/test.html +++ b/test.html @@ -2,75 +2,75 @@ - - + + - + // console.log('PoW') + // let blocks = [] + // console.log(`send blocks`) + // now = performance.now() + // for (let i = 0; i < 10; i++) { + // const block = new libnemo.SendBlock( + // 'nano_1xmastiputrdrhnf35jdh4yj1q339tyuk86w3k6oy5knede8zfowpa1rgjpn', + // '100000000000000000000000000', + // 'nano_1xmastreedxwfhpktqxppwgwwhdx1p6hiskpw7jt8g5y19khyy38axg4tohm', + // '1', + // 'nano_3rw4un6ys57hrb39sy1qx8qy5wukst1iiponztrz9qiz6qqa55kxzx4491or', + // '4E5004CA14899B8F9AABA7A76D010F73E6BAE54948912588B8C4FE0A3B558CA5') + // try { + // await block.pow() + // console.log(`block ${i + 1} work: ${block.work}`) + // } catch (err) { + // console.error(err) + // } + // blocks.push(block) + // } + // console.log(`send pow done (${performance.now() - now} ms)`) + // now = performance.now() + // for (let i = 0; i < 10; i++) { + // const block = new libnemo.ReceiveBlock( + // 'nano_1xmastiputrdrhnf35jdh4yj1q339tyuk86w3k6oy5knede8zfowpa1rgjpn', + // '0', + // 'FB0D886948EF4BBC410C0C64C16291E31AB360BEB21B338988A6C37D89C24362', + // '1', + // 'nano_3rw4un6ys57hrb39sy1qx8qy5wukst1iiponztrz9qiz6qqa55kxzx4491or', + // 'B320F9AD2C3341E0FA6EFAF093C2D618036D10DB800F14830A928A44B8DD265C') + // try { + // await block.pow() + // console.log(`block ${i + 1} work: ${block.work}`) + // } catch (err) { + // console.error(err) + // } + // blocks.push(block) + // } + // console.log(`receive done (${performance.now() - now} ms)`) + // console.log(`pow done (${performance.now() - now} ms)`) + -- 2.34.1