Fix broadcast benchmarks (#542)

* do not use cached PreparedMessage in broadcast benchmarks

* pick better name for benchmark method
This commit is contained in:
Alexander Emelin 2022-01-02 18:35:34 +03:00 committed by GitHub
parent bcef8431c9
commit beca1d3940
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 15 deletions

View File

@ -18,7 +18,6 @@ import (
// scenarios with many subscribers in one channel. // scenarios with many subscribers in one channel.
type broadcastBench struct { type broadcastBench struct {
w io.Writer w io.Writer
message *broadcastMessage
closeCh chan struct{} closeCh chan struct{}
doneCh chan struct{} doneCh chan struct{}
count int32 count int32
@ -52,14 +51,6 @@ func newBroadcastBench(usePrepared, compression bool) *broadcastBench {
usePrepared: usePrepared, usePrepared: usePrepared,
compression: compression, compression: compression,
} }
msg := &broadcastMessage{
payload: textMessages(1)[0],
}
if usePrepared {
pm, _ := NewPreparedMessage(TextMessage, msg.payload)
msg.prepared = pm
}
bench.message = msg
bench.makeConns(10000) bench.makeConns(10000)
return bench return bench
} }
@ -78,7 +69,7 @@ func (b *broadcastBench) makeConns(numConns int) {
for { for {
select { select {
case msg := <-c.msgCh: case msg := <-c.msgCh:
if b.usePrepared { if msg.prepared != nil {
c.conn.WritePreparedMessage(msg.prepared) c.conn.WritePreparedMessage(msg.prepared)
} else { } else {
c.conn.WriteMessage(TextMessage, msg.payload) c.conn.WriteMessage(TextMessage, msg.payload)
@ -100,9 +91,9 @@ func (b *broadcastBench) close() {
close(b.closeCh) close(b.closeCh)
} }
func (b *broadcastBench) runOnce() { func (b *broadcastBench) broadcastOnce(msg *broadcastMessage) {
for _, c := range b.conns { for _, c := range b.conns {
c.msgCh <- b.message c.msgCh <- msg
} }
<-b.doneCh <-b.doneCh
} }
@ -114,17 +105,25 @@ func BenchmarkBroadcast(b *testing.B) {
compression bool compression bool
}{ }{
{"NoCompression", false, false}, {"NoCompression", false, false},
{"WithCompression", false, true}, {"Compression", false, true},
{"NoCompressionPrepared", true, false}, {"NoCompressionPrepared", true, false},
{"WithCompressionPrepared", true, true}, {"CompressionPrepared", true, true},
} }
payload := textMessages(1)[0]
for _, bm := range benchmarks { for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) { b.Run(bm.name, func(b *testing.B) {
bench := newBroadcastBench(bm.usePrepared, bm.compression) bench := newBroadcastBench(bm.usePrepared, bm.compression)
defer bench.close() defer bench.close()
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { 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() b.ReportAllocs()
}) })