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
import (
"bufio"
"bytes"
"fmt"
"io"
"math/rand/v2"
"os"
"testing"
"github.com/stretchr/testify/assert"
@@ -102,23 +106,49 @@ func TestIDCheck(t *testing.T) {
}
}
func BenchmarkIDCheck(b *testing.B) {
evt := Event{
CreatedAt: Timestamp(rand.Int64N(9999999)),
Content: fmt.Sprintf("hello"),
Tags: Tags{},
func BenchmarkEventVerifySignatureJSONL(b *testing.B) {
events := loadBenchmarkEvents(b)
b.ReportAllocs()
b.ResetTimer()
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) {
for b.Loop() {
_ = evt.GetID() == evt.ID
}
})
b.Run("big brain", func(b *testing.B) {
for b.Loop() {
_ = evt.CheckID()
}
})
}
func loadBenchmarkEvents(b *testing.B) []Event {
b.Helper()
f, err := os.Open("testdata/events.jsonl")
require.NoError(b, err)
b.Cleanup(func() { _ = f.Close() })
r := bufio.NewReader(f)
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()
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)
wc.resultbacks = append(wc.resultbacks, resch)
wc.mutex.Unlock()
+500
View File
File diff suppressed because one or more lines are too long