Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broadcast benchmarks #542

Merged
merged 2 commits into from Jan 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 14 additions & 15 deletions conn_broadcast_test.go
Expand Up @@ -18,7 +18,6 @@ import (
// scenarios with many subscribers in one channel.
type broadcastBench struct {
w io.Writer
message *broadcastMessage
closeCh chan struct{}
doneCh chan struct{}
count int32
Expand Down Expand Up @@ -52,14 +51,6 @@ func newBroadcastBench(usePrepared, compression bool) *broadcastBench {
usePrepared: usePrepared,
compression: compression,
}
msg := &broadcastMessage{
payload: textMessages(1)[0],
}
if usePrepared {
pm, _ := NewPreparedMessage(TextMessage, msg.payload)
msg.prepared = pm
}
bench.message = msg
bench.makeConns(10000)
return bench
}
Expand All @@ -78,7 +69,7 @@ func (b *broadcastBench) makeConns(numConns int) {
for {
select {
case msg := <-c.msgCh:
if b.usePrepared {
if msg.prepared != nil {
c.conn.WritePreparedMessage(msg.prepared)
} else {
c.conn.WriteMessage(TextMessage, msg.payload)
Expand All @@ -100,9 +91,9 @@ func (b *broadcastBench) close() {
close(b.closeCh)
}

func (b *broadcastBench) runOnce() {
func (b *broadcastBench) broadcastOnce(msg *broadcastMessage) {
for _, c := range b.conns {
c.msgCh <- b.message
c.msgCh <- msg
}
<-b.doneCh
}
Expand All @@ -114,17 +105,25 @@ func BenchmarkBroadcast(b *testing.B) {
compression bool
}{
{"NoCompression", false, false},
{"WithCompression", false, true},
{"Compression", false, true},
{"NoCompressionPrepared", true, false},
{"WithCompressionPrepared", true, true},
{"CompressionPrepared", true, true},
}
payload := textMessages(1)[0]
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
bench := newBroadcastBench(bm.usePrepared, bm.compression)
defer bench.close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
bench.runOnce()
message := &broadcastMessage{
payload: payload,
}
if bench.usePrepared {
pm, _ := NewPreparedMessage(TextMessage, message.payload)
message.prepared = pm
}
bench.broadcastOnce(message)
}
b.ReportAllocs()
})
Expand Down