binary encoding and some changes to nson benchmarks.

This commit is contained in:
fiatjaf
2023-11-02 15:28:01 -03:00
parent 1789d43d51
commit 4c72e16f3e
7 changed files with 484 additions and 29 deletions
+8 -29
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"testing"
"github.com/mailru/easyjson"
"github.com/nbd-wtf/go-nostr"
)
@@ -153,10 +154,10 @@ func BenchmarkNSONEncoding(b *testing.B) {
events[i] = evt
}
b.Run("json.Marshal", func(b *testing.B) {
b.Run("easyjson.Marshal", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, evt := range events {
json.Marshal(evt)
easyjson.Marshal(evt)
}
}
})
@@ -168,32 +169,22 @@ func BenchmarkNSONEncoding(b *testing.B) {
}
}
})
b.Run("nson.Marshal to bytes", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, evt := range events {
MarshalBytes(evt)
}
}
})
}
func BenchmarkNSONDecoding(b *testing.B) {
events := make([]string, len(normalEvents))
eventsB := make([][]byte, len(normalEvents))
for i, jevt := range normalEvents {
evt := &nostr.Event{}
json.Unmarshal([]byte(jevt), evt)
nevt, _ := Marshal(evt)
events[i] = nevt
eventsB[i] = []byte(nevt)
}
b.Run("json.Unmarshal", func(b *testing.B) {
b.Run("easyjson.Unmarshal", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, nevt := range events {
evt := &nostr.Event{}
err := json.Unmarshal([]byte(nevt), evt)
err := easyjson.Unmarshal([]byte(nevt), evt)
if err != nil {
b.Fatalf("failed to unmarshal: %s", err)
}
@@ -213,23 +204,11 @@ func BenchmarkNSONDecoding(b *testing.B) {
}
})
b.Run("nson.Unmarshal from bytes", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, nevt := range eventsB {
evt := &nostr.Event{}
err := UnmarshalBytes(nevt, evt)
if err != nil {
b.Fatalf("failed to unmarshal: %s", err)
}
}
}
})
b.Run("json.Unmarshal + sig verification", func(b *testing.B) {
b.Run("easyjson.Unmarshal+sig", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, nevt := range events {
evt := &nostr.Event{}
err := json.Unmarshal([]byte(nevt), evt)
err := easyjson.Unmarshal([]byte(nevt), evt)
if err != nil {
b.Fatalf("failed to unmarshal: %s", err)
}
@@ -238,7 +217,7 @@ func BenchmarkNSONDecoding(b *testing.B) {
}
})
b.Run("nson.Unmarshal + sig verification", func(b *testing.B) {
b.Run("nson.Unmarshal+sig", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, nevt := range events {
evt := &nostr.Event{}