Skip to content

Instantly share code, notes, and snippets.

@wallyqs
Last active March 31, 2021 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallyqs/37bf7aff6af658127b8d954ff7996b24 to your computer and use it in GitHub Desktop.
Save wallyqs/37bf7aff6af658127b8d954ff7996b24 to your computer and use it in GitHub Desktop.
Custom Inbox concat
package main
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/nats-io/nats.go"
"github.com/nats-io/nuid"
)
const (
InboxPrefix = "_INBOX."
nuidSize = 22
inboxPrefixLen = 7
)
func BenchmarkInbox(b *testing.B) {
for n := 0; n < b.N; n++ {
nats.NewInbox()
}
b.StopTimer()
}
func BenchmarkInbox_Concat(b *testing.B) {
customInbox := "_my_inbox."
for n := 0; n < b.N; n++ {
inbox := nats.NewInbox()
inbox = customInbox + inbox[inboxPrefixLen:]
}
b.StopTimer()
}
func BenchmarkInbox_Builder(b *testing.B) {
customInbox := "_my_inbox."
customInboxLen := len(customInbox)
for n := 0; n < b.N; n++ {
inbox := nats.NewInbox()
suffix := inbox[inboxPrefixLen:]
var b strings.Builder
b.Grow(customInboxLen + nuidSize)
b.WriteString(customInbox)
b.WriteString(suffix)
inbox = b.String()
}
b.StopTimer()
}
func BenchmarkInbox_ByteBuffer(b *testing.B) {
customInbox := "_my_inbox."
for n := 0; n < b.N; n++ {
inbox := nats.NewInbox()
suffix := inbox[inboxPrefixLen:]
var buf bytes.Buffer
buf.WriteString(customInbox)
buf.WriteString(suffix)
_ = buf.String()
}
b.StopTimer()
}
func BenchmarkInbox_BuilderByte(b *testing.B) {
customInbox := []byte("_my_inbox.")
customInboxLen := len(customInbox)
for n := 0; n < b.N; n++ {
inbox := nats.NewInbox()
suffix := inbox[inboxPrefixLen:]
var b strings.Builder
b.Grow(customInboxLen + nuidSize)
b.Write(customInbox)
b.WriteString(suffix)
inbox = b.String()
}
b.StopTimer()
}
func BenchmarkInbox_ConcatCopy(b *testing.B) {
customInbox := "_my_inbox."
customInboxLen := len(customInbox)
for n := 0; n < b.N; n++ {
cinbox := make([]byte, len(customInbox)+inboxPrefixLen+nuidSize)
copy(cinbox[:customInboxLen], customInbox)
copy(cinbox[customInboxLen:], nuid.Next())
_ = string(cinbox[:])
}
b.StopTimer()
}
func BenchmarkInbox_Copy(b *testing.B) {
customInbox := "_my_inbox."
cinbox := make([]byte, len(customInbox)+inboxPrefixLen+nuidSize)
for n := 0; n < b.N; n++ {
inbox := nats.NewInbox()
copy(cinbox[:len(customInbox)], customInbox)
copy(cinbox[len(customInbox):], inbox[inboxPrefixLen:])
inbox = string(cinbox[:])
}
b.StopTimer()
}
func BenchmarkInbox_Append(b *testing.B) {
customInbox := "_my_inbox."
var cinbox []byte
for n := 0; n < b.N; n++ {
inbox := nats.NewInbox()
cinbox = append(cinbox, customInbox...)
cinbox = append(cinbox, inbox[inboxPrefixLen:]...)
// Slowest
_ = string(cinbox)
}
b.StopTimer()
}
func BenchmarkInbox_Sprintf(b *testing.B) {
customInbox := "_my_inbox."
for n := 0; n < b.N; n++ {
inbox := nats.NewInbox()
inbox = fmt.Sprintf("%s%s", customInbox, inbox[inboxPrefixLen:])
}
b.StopTimer()
}
go test -bench=. -benchmem
goos: darwin
goarch: amd64
pkg: github.com/nats-io/nats.go/_mbench
cpu: Intel(R) Core(TM) i5-8210Y CPU @ 1.60GHz
BenchmarkInbox-4 11174296 91.29 ns/op 56 B/op 2 allocs/op
BenchmarkInbox_Concat-4 10445680 113.6 ns/op 56 B/op 2 allocs/op
BenchmarkInbox_Builder-4 9393054 127.8 ns/op 88 B/op 3 allocs/op
BenchmarkInbox_ByteBuffer-4 8086458 146.6 ns/op 120 B/op 3 allocs/op
BenchmarkInbox_BuilderByte-4 9553987 126.5 ns/op 88 B/op 3 allocs/op
BenchmarkInbox_ConcatCopy-4 9236703 127.1 ns/op 120 B/op 3 allocs/op
BenchmarkInbox_Copy-4 8714382 126.1 ns/op 104 B/op 3 allocs/op
BenchmarkInbox_Append-4 49188 86960 ns/op 791286 B/op 3 allocs/op
BenchmarkInbox_Sprintf-4 4244360 276.2 ns/op 120 B/op 5 allocs/op
PASS
ok github.com/nats-io/nats.go/_mbench 15.003s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment