]> zoso.dev Git - libnemo.git/commitdiff
Add function to calculate different types of average times. Expand existing performan...
authorChris Duncan <chris@zoso.dev>
Sun, 8 Dec 2024 11:19:59 +0000 (03:19 -0800)
committerChris Duncan <chris@zoso.dev>
Sun, 8 Dec 2024 11:19:59 +0000 (03:19 -0800)
GLOBALS.mjs
perf/account.perf.js
perf/block.perf.js [new file with mode: 0644]
perf/main.mjs
perf/wallet.perf.js
src/lib/convert.ts

index 4d269f254b424e452b9375eb9397c0a0cb83f9ba..563ff8ecdc8b2a92f4bacde9723933faca80fe99 100644 (file)
@@ -16,6 +16,21 @@ if (globalThis.sessionStorage == null) {
        })
 }
 
+export function average (times) {
+       let sum = 0, reciprocals = 0, product = 1, count = times.length
+       for (let i = 0; i < count; i++) {
+               sum += times[i]
+               reciprocals += 1 / times[i]
+               product *= times[i]
+       }
+       return {
+               total: sum,
+               arithmetic: sum / count,
+               harmonic: count / reciprocals,
+               geometric: Math.pow(product, 1 / count)
+       }
+}
+
 const failures = []
 const passes = []
 function fail (...args) {
index 612414ee085115f305b857686b3878de214c9654..32a0d0e3a834ba2395320e3d2d9d916b2326a30b 100644 (file)
@@ -3,47 +3,64 @@
 
 'use strict'
 
-import { assert, skip, test } from '#GLOBALS.mjs'
+import { assert, average, skip, suite, test } from '#GLOBALS.mjs'
 import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js'
 import { Bip44Wallet, Blake2bWallet } from '#dist/main.js'
 
-await test('total time to create 0x8000 BIP-44 accounts', 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(`${performance.now() - now} ms`)
-       assert.equals(accounts.length, 0x8000)
-})
-
-await test('average time to create 1 BIP-44 account 0x80 times', async () => {
-       const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD)
-       await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)
-       let now = performance.now()
-       for (let i = 0; i < 0x80; i++) {
-               await wallet.accounts(i)
-       }
-       const avg = (performance.now() - now) / 0x80
-       console.log(`${avg} ms`)
-})
+await suite('Account performance', async () => {
+       await test('Time to create 0x2000 BIP-44 accounts', async () => {
+               const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD)
+               await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)
+               const start = performance.now()
+               const accounts = await wallet.accounts(0, 0x1fff)
+               const end = performance.now()
+               console.log(`Total: ${end - start} ms`)
+               console.log(`Average: ${(end - start) / 0x2000} ms`)
+               assert.equals(accounts.length, 0x2000)
+       })
 
-await test('create 0x8000 BLAKE2b accounts', 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(`${performance.now() - now} ms`)
-       assert.equals(accounts.length, 0x8000)
-})
+       await test('Time to create 1 BIP-44 account 0x20 times', async () => {
+               const times = []
+               const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD)
+               await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)
+               for (let i = 0; i < 0x20; i++) {
+                       const start = performance.now()
+                       await wallet.accounts(i)
+                       const end = performance.now()
+                       times.push(end - start)
+               }
+               const { total, arithmetic, harmonic, geometric } = average(times)
+               console.log(`Total: ${total} ms`)
+               console.log(`Average: ${arithmetic} ms`)
+               console.log(`Harmonic: ${harmonic} ms`)
+               console.log(`Geometric: ${geometric} ms`)
+       })
 
+       await test('Time to create 0x2000 BLAKE2b accounts', async () => {
+               const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)
+               await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)
+               const start = performance.now()
+               const accounts = await wallet.accounts(0, 0x1fff)
+               const end = performance.now()
+               console.log(`Total: ${end - start} ms`)
+               console.log(`Average: ${(end - start) / 0x2000} ms`)
+               assert.equals(accounts.length, 0x2000)
+       })
 
-await test('average time to create 1 BLAKE2b account 0x80 times', async () => {
-       const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)
-       await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)
-       let now = performance.now()
-       for (let i = 0; i < 0x80; i++) {
-               await wallet.accounts(i)
-       }
-       const avg = (performance.now() - now) / 0x80
-       console.log(`${avg} ms`)
+       await test('Average time to create 1 BLAKE2b account 0x20 times', async () => {
+               const times = []
+               const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)
+               await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)
+               for (let i = 0; i < 0x20; i++) {
+                       const start = performance.now()
+                       await wallet.accounts(i)
+                       const end = performance.now()
+                       times.push(end - start)
+               }
+               const { total, arithmetic, harmonic, geometric } = average(times)
+               console.log(`Total: ${total} ms`)
+               console.log(`Average: ${arithmetic} ms`)
+               console.log(`Harmonic: ${harmonic} ms`)
+               console.log(`Geometric: ${geometric} ms`)
+       })
 })
diff --git a/perf/block.perf.js b/perf/block.perf.js
new file mode 100644 (file)
index 0000000..adbb37d
--- /dev/null
@@ -0,0 +1,60 @@
+// SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+'use strict'
+
+import { assert, average, skip, suite, test } from '#GLOBALS.mjs'
+import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js'
+import { SendBlock, ReceiveBlock } from '#dist/main.js'
+
+await suite('Block performance', async () => {
+       const COUNT = 0x10
+
+       await test('Time to calculate proof-of-work for a send block 0x10 times', async () => {
+               const times = []
+               const block = new SendBlock(
+                       NANO_TEST_VECTORS.SEND_BLOCK.account,
+                       NANO_TEST_VECTORS.SEND_BLOCK.balance,
+                       NANO_TEST_VECTORS.SEND_BLOCK.link,
+                       '0',
+                       NANO_TEST_VECTORS.SEND_BLOCK.representative,
+                       NANO_TEST_VECTORS.SEND_BLOCK.previous
+               )
+               for (let i = 0; i < COUNT; i++) {
+                       const start = performance.now()
+                       await block.pow()
+                       const end = performance.now()
+                       times.push(end - start)
+                       console.log(`${end - start} ms`)
+               }
+               const { total, arithmetic, harmonic, geometric } = average(times)
+               console.log(`Total: ${total} ms`)
+               console.log(`Average: ${arithmetic} ms`)
+               console.log(`Harmonic: ${harmonic} ms`)
+               console.log(`Geometric: ${geometric} ms`)
+       })
+
+       await test('Time to calculate proof-of-work for a send block 0x10 times', async () => {
+               const times = []
+               const block = new ReceiveBlock(
+                       NANO_TEST_VECTORS.RECEIVE_BLOCK.account,
+                       NANO_TEST_VECTORS.RECEIVE_BLOCK.balance,
+                       NANO_TEST_VECTORS.RECEIVE_BLOCK.link,
+                       '0',
+                       NANO_TEST_VECTORS.RECEIVE_BLOCK.representative,
+                       NANO_TEST_VECTORS.RECEIVE_BLOCK.previous
+               )
+               for (let i = 0; i < COUNT; i++) {
+                       const start = performance.now()
+                       await block.pow()
+                       const end = performance.now()
+                       times.push(end - start)
+                       console.log(`${end - start} ms`)
+               }
+               const { total, arithmetic, harmonic, geometric } = average(times)
+               console.log(`Total: ${total} ms`)
+               console.log(`Average: ${arithmetic} ms`)
+               console.log(`Harmonic: ${harmonic} ms`)
+               console.log(`Geometric: ${geometric} ms`)
+       })
+})
index b80898f238ab90128c5ccab2d8a2b0a416d8fff2..23ff8a94ad03e628b980ac835a53dfa887a06045 100644 (file)
@@ -1,8 +1,9 @@
 // SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>
 // SPDX-License-Identifier: GPL-3.0-or-later
 
-import './account.perf.js'
 import './wallet.perf.js'
+import './account.perf.js'
+import './block.perf.js'
 
 document.addEventListener('load', () => {
        console.log('> TESTING COMPLETE <')
index 0e926184d419822143d5827e5cc6f175caedd02c..7ed3b14666d4015dd348293a5274c9824ec974ac 100644 (file)
@@ -3,28 +3,40 @@
 
 'use strict'
 
-import { assert, skip, test } from '#GLOBALS.mjs'
+import { assert, average, skip, suite, test } from '#GLOBALS.mjs'
 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 seeds = []
-       let now = performance.now()
-       for (let i = 0x80; i > 0; i--) {
-               const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD)
-               seeds.push(wallet.seed)
-       }
-       console.log(`${performance.now() - now} ms`)
-       assert.equals(seeds.length, 0x80)
-})
+await suite(`Wallet performance`, async () => {
+       const COUNT = 0x20
+
+       await test(`Time to create ${COUNT} BIP-44 wallets`, async () => {
+               const times = []
+               for (let i = 0; i < COUNT; i++) {
+                       const start = performance.now()
+                       const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD)
+                       const end = performance.now()
+                       times.push(end - start)
+               }
+               const { total, arithmetic, harmonic, geometric } = average(times)
+               console.log(`Total: ${total} ms`)
+               console.log(`Average: ${arithmetic} ms`)
+               console.log(`Harmonic: ${harmonic} ms`)
+               console.log(`Geometric: ${geometric} ms`)
+       })
 
-await test('creating BLAKE2b wallets performance test', async () => {
-       const seeds = []
-       let now = performance.now()
-       for (let i = 0x80; i > 0; i--) {
-               const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)
-               seeds.push(wallet.seed)
-       }
-       console.log(`${performance.now() - now} ms`)
-       assert.equals(seeds.length, 0x80)
+       await test(`Time to create ${COUNT} BLAKE2b wallets`, async () => {
+               const times = []
+               for (let i = 0; i < COUNT; i++) {
+                       const start = performance.now()
+                       const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)
+                       const end = performance.now()
+                       times.push(end - start)
+               }
+               const { total, arithmetic, harmonic, geometric } = average(times)
+               console.log(`Total: ${total} ms`)
+               console.log(`Average: ${arithmetic} ms`)
+               console.log(`Harmonic: ${harmonic} ms`)
+               console.log(`Geometric: ${geometric} ms`)
+       })
 })
index 80c8003263fbdc73cbfbc46cd56c7558145e87cf..08fe6854803f8c80e2f615ccd76290a7c9807ba0 100644 (file)
@@ -169,9 +169,10 @@ export const bytes = {
        toDec (bytes: Uint8Array): bigint | number {\r
                const integers: bigint[] = []\r
                bytes.reverse().forEach(b => integers.push(BigInt(b)))\r
-               const decimal = integers.reduce((sum, byte, index) => {\r
-                       return sum + (byte << BigInt(index * 8))\r
-               })\r
+               let decimal = 0n\r
+               for (let i = 0; i < integers.length; i++) {\r
+                       decimal += integers[i] << BigInt(i * 8)\r
+               }\r
                if (decimal > 9007199254740991n) {\r
                        return decimal\r
                } else {\r