-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unsplit, backwards-compatibility, wasm relay and pool must be configu…
…red manually from the abstract classes.
- Loading branch information
Showing
16 changed files
with
162 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ package-lock.json | |
.envrc | ||
lib | ||
test.html | ||
bench.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { initNostrWasm } from 'nostr-wasm' | ||
import { NostrEvent } from './core' | ||
import { finalizeEvent, generateSecretKey } from './pure' | ||
import { setNostrWasm, verifyEvent } from './wasm' | ||
import { AbstractRelay } from './abstract-relay.ts' | ||
import { Relay as PureRelay } from './relay.ts' | ||
import { alwaysTrue } from './helpers.ts' | ||
|
||
const RUNS = 400 | ||
|
||
let messages: string[] = [] | ||
let baseContent = '' | ||
for (let i = 0; i < RUNS; i++) { | ||
baseContent += 'a' | ||
} | ||
const secretKey = generateSecretKey() | ||
for (let i = 0; i < RUNS / 100; i++) { | ||
const tags = [] | ||
for (let t = 0; t < i; t++) { | ||
tags.push(['t', 'nada']) | ||
} | ||
const event = { created_at: Math.round(Date.now()) / 1000, kind: 1, content: baseContent.slice(0, RUNS - i), tags } | ||
const signed = finalizeEvent(event, secretKey) | ||
messages.push(JSON.stringify(['EVENT', '_', signed])) | ||
} | ||
|
||
setNostrWasm(await initNostrWasm()) | ||
|
||
const pureRelay = new PureRelay('wss://pure.com/') | ||
const trustedRelay = new AbstractRelay('wss://trusted.com/', { verifyEvent: alwaysTrue }) | ||
const wasmRelay = new AbstractRelay('wss://wasm.com/', { verifyEvent }) | ||
|
||
const run = (relay: AbstractRelay) => async () => { | ||
return new Promise<void>(resolve => { | ||
let received = 0 | ||
let sub = relay.prepareSubscription([{}], { | ||
onevent(_: NostrEvent) { | ||
received++ | ||
if (received === messages.length - 1) { | ||
resolve() | ||
sub.closed = true | ||
sub.close() | ||
} | ||
}, | ||
id: '_', | ||
}) | ||
for (let e = 0; e < messages.length; e++) { | ||
relay._push(messages[e]) | ||
} | ||
}) | ||
} | ||
|
||
const benchmarks: Record<string, { test: () => Promise<void>; runs: number[] }> = { | ||
trusted: { test: run(trustedRelay), runs: [] }, | ||
pure: { test: run(pureRelay), runs: [] }, | ||
wasm: { test: run(wasmRelay), runs: [] }, | ||
} | ||
|
||
for (let b = 0; b < 50; b++) { | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.