event verification benchmark.

This commit is contained in:
fiatjaf
2026-04-23 22:15:34 -03:00
parent e144b33fa2
commit 696f377109
3 changed files with 549 additions and 19 deletions
+48 -18
View File
@@ -1,8 +1,12 @@
package nostr package nostr
import ( import (
"bufio"
"bytes"
"fmt" "fmt"
"io"
"math/rand/v2" "math/rand/v2"
"os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -102,23 +106,49 @@ func TestIDCheck(t *testing.T) {
} }
} }
func BenchmarkIDCheck(b *testing.B) { func BenchmarkEventVerifySignatureJSONL(b *testing.B) {
evt := Event{ events := loadBenchmarkEvents(b)
CreatedAt: Timestamp(rand.Int64N(9999999)), b.ReportAllocs()
Content: fmt.Sprintf("hello"), b.ResetTimer()
Tags: Tags{},
for i := 0; i < b.N; i++ {
for _, evt := range events {
if !evt.VerifySignature() {
b.Fatal("signature verification failed")
}
}
} }
evt.Sign(Generate()) }
b.Run("naïve", func(b *testing.B) { func loadBenchmarkEvents(b *testing.B) []Event {
for b.Loop() { b.Helper()
_ = evt.GetID() == evt.ID
} f, err := os.Open("testdata/events.jsonl")
}) require.NoError(b, err)
b.Cleanup(func() { _ = f.Close() })
b.Run("big brain", func(b *testing.B) {
for b.Loop() { r := bufio.NewReader(f)
_ = evt.CheckID() events := make([]Event, 0, 1024)
}
}) for {
line, err := r.ReadBytes('\n')
if err != nil && err != io.EOF {
require.NoError(b, err)
}
line = bytes.TrimSpace(line)
if len(line) != 0 {
var evt Event
require.NoError(b, json.Unmarshal(line, &evt))
require.True(b, evt.VerifySignature(), "fixture contains invalid signature")
events = append(events, evt)
}
if err == io.EOF {
break
}
}
require.NotEmpty(b, events)
return events
} }
+1 -1
View File
@@ -52,7 +52,7 @@ start:
wc.mutex.Lock() wc.mutex.Lock()
if wc.id == id { if wc.id == id {
// there is already a call for this exact pubkey ongoing, so we just wait // there is already a call for this exact pubkey ongoing, so we just wait and copy the results
resch := make(chan WotXorFilter) resch := make(chan WotXorFilter)
wc.resultbacks = append(wc.resultbacks, resch) wc.resultbacks = append(wc.resultbacks, resch)
wc.mutex.Unlock() wc.mutex.Unlock()
+500
View File
File diff suppressed because one or more lines are too long