|
| 1 | +// Copyright 2017 The Gorilla WebSocket Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// +build go1.7 |
| 6 | + |
| 7 | +package websocket |
| 8 | + |
| 9 | +import ( |
| 10 | + "io" |
| 11 | + "io/ioutil" |
| 12 | + "sync/atomic" |
| 13 | + "testing" |
| 14 | +) |
| 15 | + |
| 16 | +// broadcastBench allows to run broadcast benchmarks. |
| 17 | +// In every broadcast benchmark we create many connections, then send the same |
| 18 | +// message into every connection and wait for all writes complete. This emulates |
| 19 | +// an application where many connections listen to the same data - i.e. PUB/SUB |
| 20 | +// scenarios with many subscribers in one channel. |
| 21 | +type broadcastBench struct { |
| 22 | + w io.Writer |
| 23 | + message *broadcastMessage |
| 24 | + closeCh chan struct{} |
| 25 | + doneCh chan struct{} |
| 26 | + count int32 |
| 27 | + conns []*broadcastConn |
| 28 | + compression bool |
| 29 | + usePrepared bool |
| 30 | +} |
| 31 | + |
| 32 | +type broadcastMessage struct { |
| 33 | + payload []byte |
| 34 | + prepared *PreparedMessage |
| 35 | +} |
| 36 | + |
| 37 | +type broadcastConn struct { |
| 38 | + conn *Conn |
| 39 | + msgCh chan *broadcastMessage |
| 40 | +} |
| 41 | + |
| 42 | +func newBroadcastConn(c *Conn) *broadcastConn { |
| 43 | + return &broadcastConn{ |
| 44 | + conn: c, |
| 45 | + msgCh: make(chan *broadcastMessage, 1), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func newBroadcastBench(usePrepared, compression bool) *broadcastBench { |
| 50 | + bench := &broadcastBench{ |
| 51 | + w: ioutil.Discard, |
| 52 | + doneCh: make(chan struct{}), |
| 53 | + closeCh: make(chan struct{}), |
| 54 | + usePrepared: usePrepared, |
| 55 | + compression: compression, |
| 56 | + } |
| 57 | + msg := &broadcastMessage{ |
| 58 | + payload: textMessages(1)[0], |
| 59 | + } |
| 60 | + if usePrepared { |
| 61 | + pm, _ := NewPreparedMessage(TextMessage, msg.payload) |
| 62 | + msg.prepared = pm |
| 63 | + } |
| 64 | + bench.message = msg |
| 65 | + bench.makeConns(10000) |
| 66 | + return bench |
| 67 | +} |
| 68 | + |
| 69 | +func (b *broadcastBench) makeConns(numConns int) { |
| 70 | + conns := make([]*broadcastConn, numConns) |
| 71 | + |
| 72 | + for i := 0; i < numConns; i++ { |
| 73 | + c := newConn(fakeNetConn{Reader: nil, Writer: b.w}, true, 1024, 1024) |
| 74 | + if b.compression { |
| 75 | + c.enableWriteCompression = true |
| 76 | + c.newCompressionWriter = compressNoContextTakeover |
| 77 | + } |
| 78 | + conns[i] = newBroadcastConn(c) |
| 79 | + go func(c *broadcastConn) { |
| 80 | + for { |
| 81 | + select { |
| 82 | + case msg := <-c.msgCh: |
| 83 | + if b.usePrepared { |
| 84 | + c.conn.WritePreparedMessage(msg.prepared) |
| 85 | + } else { |
| 86 | + c.conn.WriteMessage(TextMessage, msg.payload) |
| 87 | + } |
| 88 | + val := atomic.AddInt32(&b.count, 1) |
| 89 | + if val%int32(numConns) == 0 { |
| 90 | + b.doneCh <- struct{}{} |
| 91 | + } |
| 92 | + case <-b.closeCh: |
| 93 | + return |
| 94 | + } |
| 95 | + } |
| 96 | + }(conns[i]) |
| 97 | + } |
| 98 | + b.conns = conns |
| 99 | +} |
| 100 | + |
| 101 | +func (b *broadcastBench) close() { |
| 102 | + close(b.closeCh) |
| 103 | +} |
| 104 | + |
| 105 | +func (b *broadcastBench) runOnce() { |
| 106 | + for _, c := range b.conns { |
| 107 | + c.msgCh <- b.message |
| 108 | + } |
| 109 | + <-b.doneCh |
| 110 | +} |
| 111 | + |
| 112 | +func BenchmarkBroadcast(b *testing.B) { |
| 113 | + benchmarks := []struct { |
| 114 | + name string |
| 115 | + usePrepared bool |
| 116 | + compression bool |
| 117 | + }{ |
| 118 | + {"NoCompression", false, false}, |
| 119 | + {"WithCompression", false, true}, |
| 120 | + {"NoCompressionPrepared", true, false}, |
| 121 | + {"WithCompressionPrepared", true, true}, |
| 122 | + } |
| 123 | + for _, bm := range benchmarks { |
| 124 | + b.Run(bm.name, func(b *testing.B) { |
| 125 | + bench := newBroadcastBench(bm.usePrepared, bm.compression) |
| 126 | + defer bench.close() |
| 127 | + b.ResetTimer() |
| 128 | + for i := 0; i < b.N; i++ { |
| 129 | + bench.runOnce() |
| 130 | + } |
| 131 | + b.ReportAllocs() |
| 132 | + }) |
| 133 | + } |
| 134 | +} |
0 commit comments