Skip to content

Commit a0780e4

Browse files
committed
chore: create bench dir
1 parent 1a34c4a commit a0780e4

File tree

7 files changed

+200
-3
lines changed

7 files changed

+200
-3
lines changed

bench/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Benchmarks
2+
3+
We are still trying to find a fair way to compare the performance of various
4+
Nostr libraries. You should NOT consider the current results reliable.
5+
6+
### Method
7+
8+
WIP
9+
10+
### Results
11+
12+
```sh
13+
cpu: Intel(R) Core(TM) i7-7600U CPU @ 2.80GHz
14+
runtime: deno 1.41.3 (x86_64-unknown-linux-gnu)
15+
16+
file:///home/hasundue/lophus/bench/bench.ts
17+
benchmark time (avg) iter/s (min … max) p75 p99 p995
18+
----------------------------------------------------------------- -----------------------------
19+
20+
# Time to deliver a request to a relay
21+
group subscribe
22+
lophus 52.32 µs/iter 19,111.7 (18.74 µs … 6.17 ms) 52.78 µs 132.19 µs 165.39 µs
23+
nostr_tools 49.33 µs/iter 20,270.0 (38.33 µs … 182.21 µs) 49.32 µs 111.07 µs 133.75 µs
24+
25+
summary
26+
lophus
27+
1.06x slower than nostr_tools
28+
29+
# Time to get an event from a relay
30+
group get an event
31+
lophus 56.3 µs/iter 17,761.7 (27.8 µs … 8.69 ms) 44.75 µs 119.7 µs 166.07 µs
32+
nostr_tools 2.44 ms/iter 409.6 (2.11 ms … 4.93 ms) 2.34 ms 4.4 ms 4.93 ms
33+
34+
summary
35+
lophus
36+
43.36x faster than nostr_tools
37+
```

bench/bench.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { MockWebSocket } from "@lophus/lib/testing";
2+
import { TextNoteComposer } from "@lophus/std/notes";
3+
import { generatePrivateKey, Signer } from "@lophus/std/signs";
4+
5+
const LIBS = ["lophus", "nostr_tools"] as const;
6+
7+
const nsec = generatePrivateKey();
8+
const note = new TextNoteComposer().compose({
9+
content: "bench",
10+
});
11+
const event = new Signer(nsec).sign(note);
12+
13+
for (const lib of LIBS) {
14+
Deno.bench({
15+
name: lib,
16+
baseline: lib === "lophus",
17+
group: "subscribe",
18+
async fn({ start, end }) {
19+
const { setup, subscribe } = await import(`./${lib}.ts`);
20+
const done = (async () => {
21+
const { value: ws } = await MockWebSocket.instances().next();
22+
return new Promise((resolve) =>
23+
ws!.remote.addEventListener("message", resolve)
24+
);
25+
})();
26+
setup({ WebSocket: MockWebSocket });
27+
start();
28+
subscribe();
29+
await done;
30+
end();
31+
},
32+
});
33+
34+
Deno.bench({
35+
name: lib,
36+
baseline: lib === "lophus",
37+
group: "get an event",
38+
async fn({ start, end }) {
39+
const { setup, subscribe, receive } = await import(`./${lib}.ts`);
40+
setup({ WebSocket: MockWebSocket });
41+
const sent = (async () => {
42+
const { value: ws } = await MockWebSocket.instances().next();
43+
return new Promise<void>((resolve) => {
44+
ws!.remote.addEventListener("message", (msg) => {
45+
const [, id] = JSON.parse(msg.data);
46+
ws!.remote.send(
47+
JSON.stringify(["EVENT", id, event]),
48+
);
49+
resolve();
50+
});
51+
});
52+
})();
53+
start();
54+
const sub = await subscribe();
55+
const received = receive(sub);
56+
await sent;
57+
await received;
58+
end();
59+
},
60+
});
61+
}

bench/lophus.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { NostrEvent, Relay } from "@lophus/nips";
2+
import { BenchContext } from "./types.ts";
3+
4+
export function setup(c: BenchContext) {
5+
globalThis.WebSocket = c.WebSocket;
6+
}
7+
8+
export function subscribe() {
9+
const relay = new Relay("ws://localhost:80");
10+
return relay.subscribe({ kinds: [1] }, { id: "bench" });
11+
}
12+
13+
export async function receive(sub: ReadableStream<NostrEvent<1>>) {
14+
await sub.getReader().read();
15+
}

bench/nostr_tools.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Relay, useWebSocketImplementation } from "nostr-tools/relay";
2+
import { BenchContext } from "./types.ts";
3+
4+
let received: (() => void) | undefined;
5+
6+
export function setup(c: BenchContext) {
7+
useWebSocketImplementation(c.WebSocket);
8+
}
9+
10+
export async function subscribe() {
11+
const relay = await Relay.connect("ws://localhost:80");
12+
return relay.subscribe([{ kinds: [1] }], { onevent: () => received?.() });
13+
}
14+
15+
export async function receive() {
16+
await new Promise<void>((resolve) => {
17+
received = resolve;
18+
});
19+
}

bench/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface BenchContext {
2+
WebSocket: typeof WebSocket;
3+
}

deno.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"@lophus/std/watch": "./std/watch.ts",
3232
"@std/assert": "jsr:@std/assert@^0.219.1",
3333
"@std/streams": "jsr:@std/streams@^0.219.1",
34-
"@std/testing": "jsr:@std/testing@^0.219.1"
34+
"@std/testing": "jsr:@std/testing@^0.219.1",
35+
"nostr-tools": "npm:nostr-tools@2.3.2"
3536
},
3637
"tasks": {
3738
"build": "mkdir -p ./dist && deno run -A ./bin/bundle.ts",

deno.lock

+63-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)