mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-04-01 00:18:12 +02:00
add benchmarks.
This commit is contained in:
parent
7ba3844a6f
commit
cfdfba3bdd
@ -105,7 +105,7 @@ func Unmarshal(data string, evt *nostr.Event) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func Marshal(evt nostr.Event) (string, error) {
|
||||
func Marshal(evt *nostr.Event) (string, error) {
|
||||
// start building the nson descriptors (without the first byte that represents the nson size)
|
||||
nsonBuf := make([]byte, 256)
|
||||
|
||||
|
@ -53,7 +53,7 @@ func TestNsonEncode(t *testing.T) {
|
||||
if err := json.Unmarshal([]byte(jevt), pevt); err != nil {
|
||||
t.Fatalf("failed to decode normal json: %s", err)
|
||||
}
|
||||
nevt, err := Marshal(*pevt)
|
||||
nevt, err := Marshal(pevt)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to encode nson: %s", err)
|
||||
}
|
||||
@ -145,3 +145,88 @@ var normalEvents = []string{
|
||||
`{"id":"e2aec1b7e297329203f67b61f214c2b745a3bc1590f299ca250a1633714c829c","pubkey":"b6ac413652c8431478cb6177722f822f0f7af774a274fc5574872407834c3253","created_at":1688553478,"kind":1,"tags":[],"content":"やー今日も疲れたなー!\n大将!お勧めでイソシアネートとポリオールね!\nあ、6:4でよろしく!","sig":"12ba5dc9ff18f4ce995941f6de3bfaf8e3636afde37a06a4d3478c930ae22e2f79690e6f0682d532541222746aeb5f6dda29251cd7c31e71d7e206199b04bab4"}`,
|
||||
`{"id":"e4e86256ed64514bcb3350cf8b631ef84b4aeafcdb164cea5096c893ead6a0a1","pubkey":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798","created_at":1688574304,"kind":1,"tags":[],"content":"\b\f\ueeee","sig":"c61a4971facc4899109e1a28b73cbd27f8807fedcff87cfa1d8f5e9b709feab75e3a62a96fc75b5d2a2f42443d5ca35daa6c3d724cd6e6133b9c4a1ef072c1e9"}`,
|
||||
}
|
||||
|
||||
func BenchmarkNSONEncoding(b *testing.B) {
|
||||
events := make([]*nostr.Event, len(normalEvents))
|
||||
for i, jevt := range normalEvents {
|
||||
evt := &nostr.Event{}
|
||||
json.Unmarshal([]byte(jevt), evt)
|
||||
events[i] = evt
|
||||
}
|
||||
|
||||
b.Run("json.Marshal", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, evt := range events {
|
||||
json.Marshal(evt)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("nson.Marshal", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, evt := range events {
|
||||
Marshal(evt)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkNSONDecoding(b *testing.B) {
|
||||
events := make([]string, len(normalEvents))
|
||||
for i, jevt := range normalEvents {
|
||||
evt := &nostr.Event{}
|
||||
json.Unmarshal([]byte(jevt), evt)
|
||||
nevt, _ := Marshal(evt)
|
||||
events[i] = nevt
|
||||
}
|
||||
|
||||
b.Run("json.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)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to unmarshal: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("nson.Unmarshal", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, nevt := range events {
|
||||
evt := &nostr.Event{}
|
||||
err := Unmarshal(nevt, evt)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to unmarshal: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("json.Unmarshal + sig verification", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, nevt := range events {
|
||||
evt := &nostr.Event{}
|
||||
err := json.Unmarshal([]byte(nevt), evt)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to unmarshal: %s", err)
|
||||
}
|
||||
evt.CheckSignature()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("nson.Unmarshal + sig verification", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, nevt := range events {
|
||||
evt := &nostr.Event{}
|
||||
err := Unmarshal(nevt, evt)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to unmarshal: %s", err)
|
||||
}
|
||||
evt.CheckSignature()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user