-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench_test.go
194 lines (174 loc) · 3.85 KB
/
bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package websocket
import (
"io"
"net"
"testing"
"time"
)
func BenchmarkReceive(b *testing.B) {
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
b.Fatal(err)
}
// concatenate all golden frames
var frames []byte
var messageCount, messageSize int
for _, gold := range GoldenFrames {
frames = append(frames, gold.Masked...)
messageCount++
messageSize += len(gold.Message)
}
for _, gold := range GoldenFragments {
for _, s := range gold.Maskeds {
frames = append(frames, s...)
messageCount++
messageSize += len(s)
}
}
// feed testEnd
go func() {
for {
conn, err := ln.Accept()
if err != nil {
return
}
for err == nil {
_, err = conn.Write(frames)
}
}
}()
b.Run("buffer", func(b *testing.B) {
b.SetBytes(int64(messageSize / messageCount))
b.ReportAllocs()
conn := dialListener(b, ln)
buf := make([]byte, 100*1024)
for i := 0; i < b.N; i++ {
_, _, err := conn.Receive(buf, time.Millisecond, time.Millisecond)
if err != nil {
b.Fatal(err)
}
}
})
b.Run("stream", func(b *testing.B) {
b.SetBytes(int64(messageSize / messageCount))
b.ReportAllocs()
conn := dialListener(b, ln)
buf := make([]byte, 1024)
for i := 0; i < b.N; i++ {
_, r, err := conn.ReceiveStream(time.Millisecond, time.Millisecond)
if err != nil {
b.Fatal(err)
}
for {
_, err = r.Read(buf)
if err == io.EOF {
break
}
if err != nil {
b.Fatal(err)
}
}
}
})
b.Run("tcp", func(b *testing.B) {
b.SetBytes(int64(messageSize / messageCount))
b.ReportAllocs()
conn := dialListener(b, ln).Conn
buf := make([]byte, 100*1024)
for i := 0; i < b.N; i++ {
conn.SetReadDeadline(time.Now().Add(time.Millisecond))
size := messageSize / messageCount
for {
n, err := conn.Read(buf[:size])
if err != nil {
b.Fatal(err)
}
if size -= n; size == 0 {
break
}
}
}
})
}
func BenchmarkSend(b *testing.B) {
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
b.Fatal(err)
}
// concatenate all golden messages
var messages [][]byte
var opcodes []uint
var messageCount, messageSize int
for _, gold := range GoldenFrames {
messages = append(messages, []byte(gold.Message))
opcodes = append(opcodes, gold.Opcode)
messageCount++
messageSize += len(gold.Message)
}
for _, gold := range GoldenFragments {
for _, s := range gold.Messages {
messages = append(messages, []byte(s))
opcodes = append(opcodes, gold.Opcode)
messageCount++
messageSize += len(s)
}
}
// drain testEnd
go func() {
buf := make([]byte, 1024)
for {
conn, err := ln.Accept()
if err != nil {
return
}
for err == nil {
_, err = conn.Read(buf)
}
}
}()
b.Run("buffer", func(b *testing.B) {
b.SetBytes(int64(messageSize / messageCount))
b.ReportAllocs()
conn := dialListener(b, ln)
for i := 0; i < b.N; i++ {
err := conn.Send(opcodes[i%len(opcodes)], messages[i%len(messages)], time.Millisecond)
if err != nil {
b.Fatal(err)
}
}
})
b.Run("stream", func(b *testing.B) {
b.SetBytes(int64(messageSize / messageCount))
b.ReportAllocs()
conn := dialListener(b, ln)
for i := 0; i < b.N; i++ {
w := conn.SendStream(opcodes[i%len(opcodes)], time.Millisecond)
_, err := w.Write(messages[i%len(messages)])
if err != nil {
b.Fatal(err)
}
if err := w.Close(); err != nil {
b.Fatal(err)
}
}
})
b.Run("tcp", func(b *testing.B) {
b.SetBytes(int64(messageSize / messageCount))
b.ReportAllocs()
conn := dialListener(b, ln).Conn
for i := 0; i < b.N; i++ {
conn.SetWriteDeadline(time.Now().Add(time.Millisecond))
_, err := conn.Write(messages[i%len(messages)])
if err != nil {
b.Fatal(err)
}
}
})
}
func dialListener(tb testing.TB, ln net.Listener) *Conn {
c, err := net.Dial("tcp", ln.Addr().String())
if err != nil {
tb.Fatal(err)
}
return &Conn{Conn: c}
}