summaryrefslogtreecommitdiffstats
path: root/benchmarks/comparison/comparison_bench_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'benchmarks/comparison/comparison_bench_test.go')
-rw-r--r--benchmarks/comparison/comparison_bench_test.go510
1 files changed, 510 insertions, 0 deletions
diff --git a/benchmarks/comparison/comparison_bench_test.go b/benchmarks/comparison/comparison_bench_test.go
new file mode 100644
index 0000000..866b4df
--- /dev/null
+++ b/benchmarks/comparison/comparison_bench_test.go
@@ -0,0 +1,510 @@
1package comparison_test
2
3import (
4 "encoding/hex"
5 "encoding/json"
6 "testing"
7
8 nwio "code.northwest.io/nostr"
9 nbd "github.com/nbd-wtf/go-nostr"
10 fiat "fiatjaf.com/nostr"
11)
12
13// Sample event data for benchmarks
14var (
15 samplePubKey = "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"
16 sampleSig = "230e9d8f0ddaf7eb70b5f7741ccfa37e87a455c9a469282e3464e2052d3192cd63a167e196e381ef9d7e69e9ea43af2b0d0e1b8f1e6e7c4e1c6e8e3e9e8e3e9e8"
17 sampleID = "d42c96ccac39e0113b2ef8df82e82e2e15e0e1e7e9e5e7e1e7e9e5e7e1e7e9e5"
18
19 sampleEventJSON = `{
20 "id": "d42c96ccac39e0113b2ef8df82e82e2e15e0e1e7e9e5e7e1e7e9e5e7e1e7e9e5",
21 "pubkey": "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d",
22 "created_at": 1672531200,
23 "kind": 1,
24 "tags": [["e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36", "wss://nostr.example.com"]],
25 "content": "Hello Nostr!",
26 "sig": "230e9d8f0ddaf7eb70b5f7741ccfa37e87a455c9a469282e3464e2052d3192cd63a167e196e381ef9d7e69e9ea43af2b0d0e1b8f1e6e7c4e1c6e8e3e9e8e3e9e8"
27 }`
28)
29
30// ============================================================================
31// Event Unmarshaling Benchmarks
32// ============================================================================
33
34func BenchmarkEventUnmarshal_NWIO(b *testing.B) {
35 data := []byte(sampleEventJSON)
36 b.ResetTimer()
37 for i := 0; i < b.N; i++ {
38 var evt nwio.Event
39 if err := json.Unmarshal(data, &evt); err != nil {
40 b.Fatal(err)
41 }
42 }
43}
44
45func BenchmarkEventUnmarshal_NBD(b *testing.B) {
46 data := []byte(sampleEventJSON)
47 b.ResetTimer()
48 for i := 0; i < b.N; i++ {
49 var evt nbd.Event
50 if err := json.Unmarshal(data, &evt); err != nil {
51 b.Fatal(err)
52 }
53 }
54}
55
56func BenchmarkEventUnmarshal_Fiat(b *testing.B) {
57 data := []byte(sampleEventJSON)
58 b.ResetTimer()
59 for i := 0; i < b.N; i++ {
60 var evt fiat.Event
61 if err := json.Unmarshal(data, &evt); err != nil {
62 b.Fatal(err)
63 }
64 }
65}
66
67// ============================================================================
68// Event Marshaling Benchmarks
69// ============================================================================
70
71func BenchmarkEventMarshal_NWIO(b *testing.B) {
72 evt := &nwio.Event{
73 ID: sampleID,
74 PubKey: samplePubKey,
75 CreatedAt: 1672531200,
76 Kind: 1,
77 Tags: nwio.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}},
78 Content: "Hello Nostr!",
79 Sig: sampleSig,
80 }
81 b.ResetTimer()
82 for i := 0; i < b.N; i++ {
83 if _, err := json.Marshal(evt); err != nil {
84 b.Fatal(err)
85 }
86 }
87}
88
89func BenchmarkEventMarshal_NBD(b *testing.B) {
90 evt := &nbd.Event{
91 ID: sampleID,
92 PubKey: samplePubKey,
93 CreatedAt: nbd.Timestamp(1672531200),
94 Kind: 1,
95 Tags: nbd.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}},
96 Content: "Hello Nostr!",
97 Sig: sampleSig,
98 }
99 b.ResetTimer()
100 for i := 0; i < b.N; i++ {
101 if _, err := json.Marshal(evt); err != nil {
102 b.Fatal(err)
103 }
104 }
105}
106
107func BenchmarkEventMarshal_Fiat(b *testing.B) {
108 sig, _ := hex.DecodeString(sampleSig)
109 var sigBytes [64]byte
110 copy(sigBytes[:], sig)
111
112 pubkey, _ := fiat.PubKeyFromHex(samplePubKey)
113 id, _ := fiat.IDFromHex(sampleID)
114
115 evt := &fiat.Event{
116 ID: id,
117 PubKey: pubkey,
118 CreatedAt: fiat.Timestamp(1672531200),
119 Kind: 1,
120 Tags: fiat.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}},
121 Content: "Hello Nostr!",
122 Sig: sigBytes,
123 }
124 b.ResetTimer()
125 for i := 0; i < b.N; i++ {
126 if _, err := json.Marshal(evt); err != nil {
127 b.Fatal(err)
128 }
129 }
130}
131
132// ============================================================================
133// Event Serialization (for ID computation) Benchmarks
134// ============================================================================
135
136func BenchmarkEventSerialize_NWIO(b *testing.B) {
137 evt := &nwio.Event{
138 PubKey: samplePubKey,
139 CreatedAt: 1672531200,
140 Kind: 1,
141 Tags: nwio.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}},
142 Content: "Hello Nostr!",
143 }
144 b.ResetTimer()
145 for i := 0; i < b.N; i++ {
146 _ = evt.Serialize()
147 }
148}
149
150func BenchmarkEventSerialize_NBD(b *testing.B) {
151 evt := &nbd.Event{
152 PubKey: samplePubKey,
153 CreatedAt: nbd.Timestamp(1672531200),
154 Kind: 1,
155 Tags: nbd.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}},
156 Content: "Hello Nostr!",
157 }
158 b.ResetTimer()
159 for i := 0; i < b.N; i++ {
160 _ = evt.Serialize()
161 }
162}
163
164func BenchmarkEventSerialize_Fiat(b *testing.B) {
165 pubkey, _ := fiat.PubKeyFromHex(samplePubKey)
166 evt := &fiat.Event{
167 PubKey: pubkey,
168 CreatedAt: fiat.Timestamp(1672531200),
169 Kind: 1,
170 Tags: fiat.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}},
171 Content: "Hello Nostr!",
172 }
173 b.ResetTimer()
174 for i := 0; i < b.N; i++ {
175 _ = evt.Serialize()
176 }
177}
178
179// ============================================================================
180// Event ID Computation Benchmarks
181// ============================================================================
182
183func BenchmarkComputeID_NWIO(b *testing.B) {
184 evt := &nwio.Event{
185 PubKey: samplePubKey,
186 CreatedAt: 1672531200,
187 Kind: 1,
188 Tags: nwio.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}},
189 Content: "Hello Nostr!",
190 }
191 b.ResetTimer()
192 for i := 0; i < b.N; i++ {
193 _ = evt.ComputeID()
194 }
195}
196
197func BenchmarkComputeID_NBD(b *testing.B) {
198 evt := &nbd.Event{
199 PubKey: samplePubKey,
200 CreatedAt: nbd.Timestamp(1672531200),
201 Kind: 1,
202 Tags: nbd.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}},
203 Content: "Hello Nostr!",
204 }
205 b.ResetTimer()
206 for i := 0; i < b.N; i++ {
207 _ = evt.GetID()
208 }
209}
210
211func BenchmarkComputeID_Fiat(b *testing.B) {
212 pubkey, _ := fiat.PubKeyFromHex(samplePubKey)
213 evt := &fiat.Event{
214 PubKey: pubkey,
215 CreatedAt: fiat.Timestamp(1672531200),
216 Kind: 1,
217 Tags: fiat.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}},
218 Content: "Hello Nostr!",
219 }
220 b.ResetTimer()
221 for i := 0; i < b.N; i++ {
222 _ = evt.GetID()
223 }
224}
225
226// ============================================================================
227// Key Generation Benchmarks
228// ============================================================================
229
230func BenchmarkGenerateKey_NWIO(b *testing.B) {
231 for i := 0; i < b.N; i++ {
232 if _, err := nwio.GenerateKey(); err != nil {
233 b.Fatal(err)
234 }
235 }
236}
237
238func BenchmarkGenerateKey_NBD(b *testing.B) {
239 for i := 0; i < b.N; i++ {
240 _ = nbd.GeneratePrivateKey()
241 }
242}
243
244func BenchmarkGenerateKey_Fiat(b *testing.B) {
245 for i := 0; i < b.N; i++ {
246 _ = fiat.Generate()
247 }
248}
249
250// ============================================================================
251// Event Signing Benchmarks
252// ============================================================================
253
254func BenchmarkEventSign_NWIO(b *testing.B) {
255 key, err := nwio.GenerateKey()
256 if err != nil {
257 b.Fatal(err)
258 }
259 b.ResetTimer()
260
261 for i := 0; i < b.N; i++ {
262 evt := &nwio.Event{
263 CreatedAt: 1672531200,
264 Kind: 1,
265 Tags: nwio.Tags{},
266 Content: "Hello Nostr!",
267 }
268 if err := key.Sign(evt); err != nil {
269 b.Fatal(err)
270 }
271 }
272}
273
274func BenchmarkEventSign_NBD(b *testing.B) {
275 sk := nbd.GeneratePrivateKey()
276 b.ResetTimer()
277
278 for i := 0; i < b.N; i++ {
279 evt := &nbd.Event{
280 CreatedAt: nbd.Timestamp(1672531200),
281 Kind: 1,
282 Tags: nbd.Tags{},
283 Content: "Hello Nostr!",
284 }
285 if err := evt.Sign(sk); err != nil {
286 b.Fatal(err)
287 }
288 }
289}
290
291func BenchmarkEventSign_Fiat(b *testing.B) {
292 sk := fiat.Generate()
293 b.ResetTimer()
294
295 for i := 0; i < b.N; i++ {
296 evt := &fiat.Event{
297 CreatedAt: fiat.Timestamp(1672531200),
298 Kind: 1,
299 Tags: fiat.Tags{},
300 Content: "Hello Nostr!",
301 }
302 if err := evt.Sign(sk); err != nil {
303 b.Fatal(err)
304 }
305 }
306}
307
308// ============================================================================
309// Event Verification Benchmarks
310// ============================================================================
311
312func BenchmarkEventVerify_NWIO(b *testing.B) {
313 // Create and sign an event
314 key, _ := nwio.GenerateKey()
315 evt := &nwio.Event{
316 CreatedAt: 1672531200,
317 Kind: 1,
318 Tags: nwio.Tags{},
319 Content: "Hello Nostr!",
320 }
321 key.Sign(evt)
322
323 b.ResetTimer()
324 for i := 0; i < b.N; i++ {
325 if !evt.Verify() {
326 b.Fatal("verification failed")
327 }
328 }
329}
330
331func BenchmarkEventVerify_NBD(b *testing.B) {
332 // Create and sign an event
333 sk := nbd.GeneratePrivateKey()
334 evt := &nbd.Event{
335 CreatedAt: nbd.Timestamp(1672531200),
336 Kind: 1,
337 Tags: nbd.Tags{},
338 Content: "Hello Nostr!",
339 }
340 evt.Sign(sk)
341
342 b.ResetTimer()
343 for i := 0; i < b.N; i++ {
344 if ok, _ := evt.CheckSignature(); !ok {
345 b.Fatal("verification failed")
346 }
347 }
348}
349
350func BenchmarkEventVerify_Fiat(b *testing.B) {
351 // Create and sign an event
352 sk := fiat.Generate()
353 evt := &fiat.Event{
354 CreatedAt: fiat.Timestamp(1672531200),
355 Kind: 1,
356 Tags: fiat.Tags{},
357 Content: "Hello Nostr!",
358 }
359 evt.Sign(sk)
360
361 b.ResetTimer()
362 for i := 0; i < b.N; i++ {
363 if !evt.VerifySignature() {
364 b.Fatal("verification failed")
365 }
366 }
367}
368
369// ============================================================================
370// Filter Matching Benchmarks
371// ============================================================================
372
373func BenchmarkFilterMatch_NWIO(b *testing.B) {
374 filter := &nwio.Filter{
375 Kinds: []int{1},
376 Authors: []string{samplePubKey},
377 }
378 evt := &nwio.Event{
379 PubKey: samplePubKey,
380 CreatedAt: 1672531200,
381 Kind: 1,
382 Content: "Hello Nostr!",
383 }
384
385 b.ResetTimer()
386 for i := 0; i < b.N; i++ {
387 if !filter.Matches(evt) {
388 b.Fatal("filter should match")
389 }
390 }
391}
392
393func BenchmarkFilterMatch_NBD(b *testing.B) {
394 filter := nbd.Filter{
395 Kinds: []int{1},
396 Authors: []string{samplePubKey},
397 }
398 evt := &nbd.Event{
399 PubKey: samplePubKey,
400 CreatedAt: nbd.Timestamp(1672531200),
401 Kind: 1,
402 Content: "Hello Nostr!",
403 }
404
405 b.ResetTimer()
406 for i := 0; i < b.N; i++ {
407 if !filter.Matches(evt) {
408 b.Fatal("filter should match")
409 }
410 }
411}
412
413func BenchmarkFilterMatch_Fiat(b *testing.B) {
414 pubkey, _ := fiat.PubKeyFromHex(samplePubKey)
415 filter := fiat.Filter{
416 Kinds: []fiat.Kind{1},
417 Authors: []fiat.PubKey{pubkey},
418 }
419 evt := &fiat.Event{
420 PubKey: pubkey,
421 CreatedAt: fiat.Timestamp(1672531200),
422 Kind: 1,
423 Content: "Hello Nostr!",
424 }
425
426 b.ResetTimer()
427 for i := 0; i < b.N; i++ {
428 if !filter.Matches(*evt) {
429 b.Fatal("filter should match")
430 }
431 }
432}
433
434// ============================================================================
435// Complex Filter Matching Benchmarks (with tags)
436// ============================================================================
437
438func BenchmarkFilterMatchComplex_NWIO(b *testing.B) {
439 filter := &nwio.Filter{
440 Kinds: []int{1, 6, 7},
441 Authors: []string{samplePubKey[:8]}, // Prefix match
442 Tags: map[string][]string{
443 "e": {"5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"},
444 },
445 }
446 evt := &nwio.Event{
447 PubKey: samplePubKey,
448 CreatedAt: 1672531200,
449 Kind: 1,
450 Tags: nwio.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}},
451 Content: "Hello Nostr!",
452 }
453
454 b.ResetTimer()
455 for i := 0; i < b.N; i++ {
456 if !filter.Matches(evt) {
457 b.Fatal("filter should match")
458 }
459 }
460}
461
462func BenchmarkFilterMatchComplex_NBD(b *testing.B) {
463 filter := nbd.Filter{
464 Kinds: []int{1, 6, 7},
465 Authors: []string{samplePubKey}, // NBD also supports prefix, use full key for simplicity
466 Tags: nbd.TagMap{
467 "e": []string{"5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"},
468 },
469 }
470 evt := &nbd.Event{
471 PubKey: samplePubKey,
472 CreatedAt: nbd.Timestamp(1672531200),
473 Kind: 1,
474 Tags: nbd.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}},
475 Content: "Hello Nostr!",
476 }
477
478 b.ResetTimer()
479 for i := 0; i < b.N; i++ {
480 if !filter.Matches(evt) {
481 b.Fatal("filter should match")
482 }
483 }
484}
485
486func BenchmarkFilterMatchComplex_Fiat(b *testing.B) {
487 pubkey, _ := fiat.PubKeyFromHex(samplePubKey)
488
489 filter := fiat.Filter{
490 Kinds: []fiat.Kind{1, 6, 7},
491 Authors: []fiat.PubKey{pubkey}, // Use full pubkey for simplicity
492 Tags: fiat.TagMap{
493 "e": []string{"5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"},
494 },
495 }
496 evt := &fiat.Event{
497 PubKey: pubkey,
498 CreatedAt: fiat.Timestamp(1672531200),
499 Kind: 1,
500 Tags: fiat.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}},
501 Content: "Hello Nostr!",
502 }
503
504 b.ResetTimer()
505 for i := 0; i < b.N; i++ {
506 if !filter.Matches(*evt) {
507 b.Fatal("filter should match")
508 }
509 }
510}