mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
streamline and improve benchmarks.
This commit is contained in:
@@ -269,6 +269,7 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// this method simulates receiving a message from the websocket
|
// this method simulates receiving a message from the websocket
|
||||||
|
// for testing purposes only
|
||||||
public _push(msg: string) {
|
public _push(msg: string) {
|
||||||
this.incomingMessageQueue.enqueue(msg)
|
this.incomingMessageQueue.enqueue(msg)
|
||||||
if (!this.queueRunning) {
|
if (!this.queueRunning) {
|
||||||
|
|||||||
37
benchmark.ts
37
benchmark.ts
@@ -1,3 +1,4 @@
|
|||||||
|
import { run, bench, group, baseline } from 'mitata'
|
||||||
import { initNostrWasm } from 'nostr-wasm'
|
import { initNostrWasm } from 'nostr-wasm'
|
||||||
import { NostrEvent } from './core'
|
import { NostrEvent } from './core'
|
||||||
import { finalizeEvent, generateSecretKey } from './pure'
|
import { finalizeEvent, generateSecretKey } from './pure'
|
||||||
@@ -6,17 +7,17 @@ import { AbstractRelay } from './abstract-relay.ts'
|
|||||||
import { Relay as PureRelay } from './relay.ts'
|
import { Relay as PureRelay } from './relay.ts'
|
||||||
import { alwaysTrue } from './helpers.ts'
|
import { alwaysTrue } from './helpers.ts'
|
||||||
|
|
||||||
const EVENTS = 1000
|
const EVENTS = 100
|
||||||
|
|
||||||
let messages: string[] = []
|
let messages: string[] = []
|
||||||
let baseContent = ''
|
let baseContent = ''
|
||||||
for (let i = 0; i < EVENTS; i++) {
|
for (let i = 0; i < EVENTS / 100; i++) {
|
||||||
baseContent += 'a'
|
baseContent += 'a'
|
||||||
}
|
}
|
||||||
const secretKey = generateSecretKey()
|
const secretKey = generateSecretKey()
|
||||||
for (let i = 0; i < EVENTS / 200; i++) {
|
for (let i = 0; i < EVENTS; i++) {
|
||||||
const tags = []
|
const tags = []
|
||||||
for (let t = 0; t < i; t++) {
|
for (let t = 0; t < i / 100; t++) {
|
||||||
tags.push(['t', 'nada'])
|
tags.push(['t', 'nada'])
|
||||||
}
|
}
|
||||||
const event = { created_at: Math.round(Date.now()) / 1000, kind: 1, content: baseContent.slice(0, EVENTS - i), tags }
|
const event = { created_at: Math.round(Date.now()) / 1000, kind: 1, content: baseContent.slice(0, EVENTS - i), tags }
|
||||||
@@ -30,10 +31,11 @@ const pureRelay = new PureRelay('wss://pure.com/')
|
|||||||
const trustedRelay = new AbstractRelay('wss://trusted.com/', { verifyEvent: alwaysTrue })
|
const trustedRelay = new AbstractRelay('wss://trusted.com/', { verifyEvent: alwaysTrue })
|
||||||
const wasmRelay = new AbstractRelay('wss://wasm.com/', { verifyEvent })
|
const wasmRelay = new AbstractRelay('wss://wasm.com/', { verifyEvent })
|
||||||
|
|
||||||
const run = (relay: AbstractRelay) => async () => {
|
const runWith = (relay: AbstractRelay) => async () => {
|
||||||
return new Promise<void>(resolve => {
|
return new Promise<void>(resolve => {
|
||||||
let received = 0
|
let received = 0
|
||||||
let sub = relay.prepareSubscription([{}], {
|
let sub = relay.prepareSubscription([{}], {
|
||||||
|
id: '_',
|
||||||
onevent(_: NostrEvent) {
|
onevent(_: NostrEvent) {
|
||||||
received++
|
received++
|
||||||
if (received === messages.length - 1) {
|
if (received === messages.length - 1) {
|
||||||
@@ -42,7 +44,6 @@ const run = (relay: AbstractRelay) => async () => {
|
|||||||
sub.close()
|
sub.close()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
id: '_',
|
|
||||||
})
|
})
|
||||||
for (let e = 0; e < messages.length; e++) {
|
for (let e = 0; e < messages.length; e++) {
|
||||||
relay._push(messages[e])
|
relay._push(messages[e])
|
||||||
@@ -50,22 +51,10 @@ const run = (relay: AbstractRelay) => async () => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const benchmarks: Record<string, { test: () => Promise<void>; runs: number[] }> = {
|
group('relay read message and verify event', () => {
|
||||||
trusted: { test: run(trustedRelay), runs: [] },
|
baseline('wasm', runWith(wasmRelay))
|
||||||
pure: { test: run(pureRelay), runs: [] },
|
bench('pure js', runWith(pureRelay))
|
||||||
wasm: { test: run(wasmRelay), runs: [] },
|
bench('trusted', runWith(trustedRelay))
|
||||||
}
|
})
|
||||||
|
|
||||||
for (let b = 0; b < 20; b++) {
|
await run()
|
||||||
for (let name in benchmarks) {
|
|
||||||
const { test, runs } = benchmarks[name]
|
|
||||||
const before = performance.now()
|
|
||||||
await test()
|
|
||||||
runs.push(performance.now() - before)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let name in benchmarks) {
|
|
||||||
const { runs } = benchmarks[name]
|
|
||||||
console.log(name, runs.reduce((a, b) => a + b, 0) / runs.length)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { verifiedSymbol, type Event, type Nostr } from './core.ts'
|
import { verifiedSymbol, type Event, type Nostr, VerifiedEvent } from './core.ts'
|
||||||
|
|
||||||
export async function yieldThread() {
|
export async function yieldThread() {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
@@ -10,7 +10,7 @@ export async function yieldThread() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const alwaysTrue: Nostr['verifyEvent'] = (t: Event) => {
|
export const alwaysTrue: Nostr['verifyEvent'] = (t: Event): t is VerifiedEvent => {
|
||||||
t[verifiedSymbol] = true
|
t[verifiedSymbol] = true
|
||||||
return t[verifiedSymbol]
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
6
justfile
6
justfile
@@ -26,7 +26,7 @@ lint:
|
|||||||
|
|
||||||
benchmark:
|
benchmark:
|
||||||
bun build --target=node --outfile=bench.js benchmark.ts
|
bun build --target=node --outfile=bench.js benchmark.ts
|
||||||
bun run benchmark.ts
|
timeout 25s deno run --allow-read bench.js || true
|
||||||
timeout 2s deno run bench.js || true
|
timeout 25s node bench.js || true
|
||||||
timeout 2s node bench.js || true
|
timeout 25s bun run benchmark.ts || true
|
||||||
rm bench.js
|
rm bench.js
|
||||||
|
|||||||
Reference in New Issue
Block a user