package comparison_test import ( "encoding/hex" "encoding/json" "testing" nwio "code.northwest.io/nostr" nbd "github.com/nbd-wtf/go-nostr" fiat "fiatjaf.com/nostr" ) // Sample event data for benchmarks var ( samplePubKey = "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d" sampleSig = "230e9d8f0ddaf7eb70b5f7741ccfa37e87a455c9a469282e3464e2052d3192cd63a167e196e381ef9d7e69e9ea43af2b0d0e1b8f1e6e7c4e1c6e8e3e9e8e3e9e8" sampleID = "d42c96ccac39e0113b2ef8df82e82e2e15e0e1e7e9e5e7e1e7e9e5e7e1e7e9e5" sampleEventJSON = `{ "id": "d42c96ccac39e0113b2ef8df82e82e2e15e0e1e7e9e5e7e1e7e9e5e7e1e7e9e5", "pubkey": "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", "created_at": 1672531200, "kind": 1, "tags": [["e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36", "wss://nostr.example.com"]], "content": "Hello Nostr!", "sig": "230e9d8f0ddaf7eb70b5f7741ccfa37e87a455c9a469282e3464e2052d3192cd63a167e196e381ef9d7e69e9ea43af2b0d0e1b8f1e6e7c4e1c6e8e3e9e8e3e9e8" }` ) // ============================================================================ // Event Unmarshaling Benchmarks // ============================================================================ func BenchmarkEventUnmarshal_NWIO(b *testing.B) { data := []byte(sampleEventJSON) b.ResetTimer() for i := 0; i < b.N; i++ { var evt nwio.Event if err := json.Unmarshal(data, &evt); err != nil { b.Fatal(err) } } } func BenchmarkEventUnmarshal_NBD(b *testing.B) { data := []byte(sampleEventJSON) b.ResetTimer() for i := 0; i < b.N; i++ { var evt nbd.Event if err := json.Unmarshal(data, &evt); err != nil { b.Fatal(err) } } } func BenchmarkEventUnmarshal_Fiat(b *testing.B) { data := []byte(sampleEventJSON) b.ResetTimer() for i := 0; i < b.N; i++ { var evt fiat.Event if err := json.Unmarshal(data, &evt); err != nil { b.Fatal(err) } } } // ============================================================================ // Event Marshaling Benchmarks // ============================================================================ func BenchmarkEventMarshal_NWIO(b *testing.B) { evt := &nwio.Event{ ID: sampleID, PubKey: samplePubKey, CreatedAt: 1672531200, Kind: 1, Tags: nwio.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}}, Content: "Hello Nostr!", Sig: sampleSig, } b.ResetTimer() for i := 0; i < b.N; i++ { if _, err := json.Marshal(evt); err != nil { b.Fatal(err) } } } func BenchmarkEventMarshal_NBD(b *testing.B) { evt := &nbd.Event{ ID: sampleID, PubKey: samplePubKey, CreatedAt: nbd.Timestamp(1672531200), Kind: 1, Tags: nbd.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}}, Content: "Hello Nostr!", Sig: sampleSig, } b.ResetTimer() for i := 0; i < b.N; i++ { if _, err := json.Marshal(evt); err != nil { b.Fatal(err) } } } func BenchmarkEventMarshal_Fiat(b *testing.B) { sig, _ := hex.DecodeString(sampleSig) var sigBytes [64]byte copy(sigBytes[:], sig) pubkey, _ := fiat.PubKeyFromHex(samplePubKey) id, _ := fiat.IDFromHex(sampleID) evt := &fiat.Event{ ID: id, PubKey: pubkey, CreatedAt: fiat.Timestamp(1672531200), Kind: 1, Tags: fiat.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}}, Content: "Hello Nostr!", Sig: sigBytes, } b.ResetTimer() for i := 0; i < b.N; i++ { if _, err := json.Marshal(evt); err != nil { b.Fatal(err) } } } // ============================================================================ // Event Serialization (for ID computation) Benchmarks // ============================================================================ func BenchmarkEventSerialize_NWIO(b *testing.B) { evt := &nwio.Event{ PubKey: samplePubKey, CreatedAt: 1672531200, Kind: 1, Tags: nwio.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}}, Content: "Hello Nostr!", } b.ResetTimer() for i := 0; i < b.N; i++ { _ = evt.Serialize() } } func BenchmarkEventSerialize_NBD(b *testing.B) { evt := &nbd.Event{ PubKey: samplePubKey, CreatedAt: nbd.Timestamp(1672531200), Kind: 1, Tags: nbd.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}}, Content: "Hello Nostr!", } b.ResetTimer() for i := 0; i < b.N; i++ { _ = evt.Serialize() } } func BenchmarkEventSerialize_Fiat(b *testing.B) { pubkey, _ := fiat.PubKeyFromHex(samplePubKey) evt := &fiat.Event{ PubKey: pubkey, CreatedAt: fiat.Timestamp(1672531200), Kind: 1, Tags: fiat.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}}, Content: "Hello Nostr!", } b.ResetTimer() for i := 0; i < b.N; i++ { _ = evt.Serialize() } } // ============================================================================ // Event ID Computation Benchmarks // ============================================================================ func BenchmarkComputeID_NWIO(b *testing.B) { evt := &nwio.Event{ PubKey: samplePubKey, CreatedAt: 1672531200, Kind: 1, Tags: nwio.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}}, Content: "Hello Nostr!", } b.ResetTimer() for i := 0; i < b.N; i++ { _ = evt.ComputeID() } } func BenchmarkComputeID_NBD(b *testing.B) { evt := &nbd.Event{ PubKey: samplePubKey, CreatedAt: nbd.Timestamp(1672531200), Kind: 1, Tags: nbd.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}}, Content: "Hello Nostr!", } b.ResetTimer() for i := 0; i < b.N; i++ { _ = evt.GetID() } } func BenchmarkComputeID_Fiat(b *testing.B) { pubkey, _ := fiat.PubKeyFromHex(samplePubKey) evt := &fiat.Event{ PubKey: pubkey, CreatedAt: fiat.Timestamp(1672531200), Kind: 1, Tags: fiat.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}}, Content: "Hello Nostr!", } b.ResetTimer() for i := 0; i < b.N; i++ { _ = evt.GetID() } } // ============================================================================ // Key Generation Benchmarks // ============================================================================ func BenchmarkGenerateKey_NWIO(b *testing.B) { for i := 0; i < b.N; i++ { if _, err := nwio.GenerateKey(); err != nil { b.Fatal(err) } } } func BenchmarkGenerateKey_NBD(b *testing.B) { for i := 0; i < b.N; i++ { _ = nbd.GeneratePrivateKey() } } func BenchmarkGenerateKey_Fiat(b *testing.B) { for i := 0; i < b.N; i++ { _ = fiat.Generate() } } // ============================================================================ // Event Signing Benchmarks // ============================================================================ func BenchmarkEventSign_NWIO(b *testing.B) { key, err := nwio.GenerateKey() if err != nil { b.Fatal(err) } b.ResetTimer() for i := 0; i < b.N; i++ { evt := &nwio.Event{ CreatedAt: 1672531200, Kind: 1, Tags: nwio.Tags{}, Content: "Hello Nostr!", } if err := key.Sign(evt); err != nil { b.Fatal(err) } } } func BenchmarkEventSign_NBD(b *testing.B) { sk := nbd.GeneratePrivateKey() b.ResetTimer() for i := 0; i < b.N; i++ { evt := &nbd.Event{ CreatedAt: nbd.Timestamp(1672531200), Kind: 1, Tags: nbd.Tags{}, Content: "Hello Nostr!", } if err := evt.Sign(sk); err != nil { b.Fatal(err) } } } func BenchmarkEventSign_Fiat(b *testing.B) { sk := fiat.Generate() b.ResetTimer() for i := 0; i < b.N; i++ { evt := &fiat.Event{ CreatedAt: fiat.Timestamp(1672531200), Kind: 1, Tags: fiat.Tags{}, Content: "Hello Nostr!", } if err := evt.Sign(sk); err != nil { b.Fatal(err) } } } // ============================================================================ // Event Verification Benchmarks // ============================================================================ func BenchmarkEventVerify_NWIO(b *testing.B) { // Create and sign an event key, _ := nwio.GenerateKey() evt := &nwio.Event{ CreatedAt: 1672531200, Kind: 1, Tags: nwio.Tags{}, Content: "Hello Nostr!", } key.Sign(evt) b.ResetTimer() for i := 0; i < b.N; i++ { if !evt.Verify() { b.Fatal("verification failed") } } } func BenchmarkEventVerify_NBD(b *testing.B) { // Create and sign an event sk := nbd.GeneratePrivateKey() evt := &nbd.Event{ CreatedAt: nbd.Timestamp(1672531200), Kind: 1, Tags: nbd.Tags{}, Content: "Hello Nostr!", } evt.Sign(sk) b.ResetTimer() for i := 0; i < b.N; i++ { if ok, _ := evt.CheckSignature(); !ok { b.Fatal("verification failed") } } } func BenchmarkEventVerify_Fiat(b *testing.B) { // Create and sign an event sk := fiat.Generate() evt := &fiat.Event{ CreatedAt: fiat.Timestamp(1672531200), Kind: 1, Tags: fiat.Tags{}, Content: "Hello Nostr!", } evt.Sign(sk) b.ResetTimer() for i := 0; i < b.N; i++ { if !evt.VerifySignature() { b.Fatal("verification failed") } } } // ============================================================================ // Filter Matching Benchmarks // ============================================================================ func BenchmarkFilterMatch_NWIO(b *testing.B) { filter := &nwio.Filter{ Kinds: []int{1}, Authors: []string{samplePubKey}, } evt := &nwio.Event{ PubKey: samplePubKey, CreatedAt: 1672531200, Kind: 1, Content: "Hello Nostr!", } b.ResetTimer() for i := 0; i < b.N; i++ { if !filter.Matches(evt) { b.Fatal("filter should match") } } } func BenchmarkFilterMatch_NBD(b *testing.B) { filter := nbd.Filter{ Kinds: []int{1}, Authors: []string{samplePubKey}, } evt := &nbd.Event{ PubKey: samplePubKey, CreatedAt: nbd.Timestamp(1672531200), Kind: 1, Content: "Hello Nostr!", } b.ResetTimer() for i := 0; i < b.N; i++ { if !filter.Matches(evt) { b.Fatal("filter should match") } } } func BenchmarkFilterMatch_Fiat(b *testing.B) { pubkey, _ := fiat.PubKeyFromHex(samplePubKey) filter := fiat.Filter{ Kinds: []fiat.Kind{1}, Authors: []fiat.PubKey{pubkey}, } evt := &fiat.Event{ PubKey: pubkey, CreatedAt: fiat.Timestamp(1672531200), Kind: 1, Content: "Hello Nostr!", } b.ResetTimer() for i := 0; i < b.N; i++ { if !filter.Matches(*evt) { b.Fatal("filter should match") } } } // ============================================================================ // Complex Filter Matching Benchmarks (with tags) // ============================================================================ func BenchmarkFilterMatchComplex_NWIO(b *testing.B) { filter := &nwio.Filter{ Kinds: []int{1, 6, 7}, Authors: []string{samplePubKey[:8]}, // Prefix match Tags: map[string][]string{ "e": {"5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}, }, } evt := &nwio.Event{ PubKey: samplePubKey, CreatedAt: 1672531200, Kind: 1, Tags: nwio.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}}, Content: "Hello Nostr!", } b.ResetTimer() for i := 0; i < b.N; i++ { if !filter.Matches(evt) { b.Fatal("filter should match") } } } func BenchmarkFilterMatchComplex_NBD(b *testing.B) { filter := nbd.Filter{ Kinds: []int{1, 6, 7}, Authors: []string{samplePubKey}, // NBD also supports prefix, use full key for simplicity Tags: nbd.TagMap{ "e": []string{"5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}, }, } evt := &nbd.Event{ PubKey: samplePubKey, CreatedAt: nbd.Timestamp(1672531200), Kind: 1, Tags: nbd.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}}, Content: "Hello Nostr!", } b.ResetTimer() for i := 0; i < b.N; i++ { if !filter.Matches(evt) { b.Fatal("filter should match") } } } func BenchmarkFilterMatchComplex_Fiat(b *testing.B) { pubkey, _ := fiat.PubKeyFromHex(samplePubKey) filter := fiat.Filter{ Kinds: []fiat.Kind{1, 6, 7}, Authors: []fiat.PubKey{pubkey}, // Use full pubkey for simplicity Tags: fiat.TagMap{ "e": []string{"5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}, }, } evt := &fiat.Event{ PubKey: pubkey, CreatedAt: fiat.Timestamp(1672531200), Kind: 1, Tags: fiat.Tags{{"e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}}, Content: "Hello Nostr!", } b.ResetTimer() for i := 0; i < b.N; i++ { if !filter.Matches(*evt) { b.Fatal("filter should match") } } }