queue: add comparative benchmarks for sender

This commit is contained in:
Branden J Brown 2025-04-06 11:01:55 -04:00
parent 0bf52b3fac
commit 91be2bf466

View File

@ -1,9 +1,12 @@
package queue
import (
"io"
"math/rand/v2"
"testing"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
)
func TestSenderUnique(t *testing.T) {
@ -37,3 +40,47 @@ func TestSenderRoundTrip(t *testing.T) {
t.Errorf("round-trip failed:\nwant %v\ngot %v", want, got)
}
}
func BenchmarkSenderEncode(b *testing.B) {
enc := jsontext.NewEncoder(io.Discard)
opts := []*sender{ // pointers to avoid allocations
ref(Sender([]byte("nijika"), "kessoku", "bocchi")),
ref(Sender([]byte("kita"), "kessoku", "bocchi")),
ref(Sender([]byte("nijika"), "sickhack", "bocchi")),
ref(Sender([]byte("nijika"), "kessoku", "ryō")),
ref(Sender([]byte("nijika"), "kessoku", "bocchi2")),
ref(Sender([]byte("kita"), "kessoku", "bocchi2")),
ref(Sender([]byte("nijika"), "sickhack", "bocchi2")),
ref(Sender([]byte("nijika"), "kessoku", "ryō2")),
}
b.ReportAllocs()
for b.Loop() {
json.MarshalEncode(enc, opts[rand.IntN(len(opts))])
}
}
func BenchmarkSenderDecode(b *testing.B) {
opts := [][]byte{
must(json.Marshal(Sender([]byte("nijika"), "kessoku", "bocchi"))),
must(json.Marshal(Sender([]byte("kita"), "kessoku", "bocchi"))),
must(json.Marshal(Sender([]byte("nijika"), "sickhack", "bocchi"))),
must(json.Marshal(Sender([]byte("nijika"), "kessoku", "ryō"))),
must(json.Marshal(Sender([]byte("nijika"), "kessoku", "bocchi2"))),
must(json.Marshal(Sender([]byte("kita"), "kessoku", "bocchi2"))),
must(json.Marshal(Sender([]byte("nijika"), "sickhack", "bocchi2"))),
must(json.Marshal(Sender([]byte("nijika"), "kessoku", "ryō2"))),
}
b.ReportAllocs()
var v sender
for b.Loop() {
json.Unmarshal(opts[rand.IntN(len(opts))], &v)
}
}
func ref[T any](x T) *T { return &x }
func must[T any](x T, err error) T {
if err != nil {
panic(err)
}
return x
}