|
| 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 | +} |
0 commit comments