This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
broadcast_test.go
342 lines (306 loc) · 9.68 KB
/
broadcast_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright (c) 2013 Project Iris. All rights reserved.
//
// The current language binding is an official support library of the Iris
// cloud messaging framework, and as such, the same licensing terms apply.
// For details please see http://iris.karalabe.com/downloads#License
package iris
import (
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/project-iris/iris/pool"
)
// Service handler for the broadcast tests.
type broadcastTestHandler struct {
conn *Connection
delivers chan []byte
}
func (b *broadcastTestHandler) Init(conn *Connection) error { b.conn = conn; return nil }
func (b *broadcastTestHandler) HandleBroadcast(msg []byte) { b.delivers <- msg }
func (b *broadcastTestHandler) HandleRequest(req []byte) ([]byte, error) { panic("not implemented") }
func (b *broadcastTestHandler) HandleTunnel(tun *Tunnel) { panic("not implemented") }
func (b *broadcastTestHandler) HandleDrop(reason error) { panic("not implemented") }
// Tests multiple concurrent client and service broadcasts.
func TestBroadcast(t *testing.T) {
// Test specific configurations
conf := struct {
clients int
servers int
messages int
}{25, 25, 25}
barrier := newBarrier(conf.clients + conf.servers)
shutdown := new(sync.WaitGroup)
// Start up the concurrent broadcasting clients
for i := 0; i < conf.clients; i++ {
shutdown.Add(1)
go func(client int) {
defer shutdown.Done()
// Connect to the local relay
conn, err := Connect(config.relay)
if err != nil {
barrier.Exit(fmt.Errorf("connection failed: %v", err))
return
}
defer conn.Close()
barrier.Sync()
// Broadcast to the whole service cluster
for i := 0; i < conf.messages; i++ {
message := fmt.Sprintf("client #%d, broadcast %d", client, i)
if err := conn.Broadcast(config.cluster, []byte(message)); err != nil {
barrier.Exit(fmt.Errorf("client broadcast failed: %v", err))
return
}
}
barrier.Exit(nil)
}(i)
}
// Start up the concurrent broadcast services
for i := 0; i < conf.servers; i++ {
shutdown.Add(1)
go func(server int) {
defer shutdown.Done()
// Create the service handler
handler := &broadcastTestHandler{
delivers: make(chan []byte, (conf.clients+conf.servers)*conf.messages),
}
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, nil)
if err != nil {
barrier.Exit(fmt.Errorf("registration failed: %v", err))
return
}
defer serv.Unregister()
barrier.Sync()
// Broadcast to the whole service cluster
for i := 0; i < conf.messages; i++ {
message := fmt.Sprintf("server #%d, broadcast %d", server, i)
if err := handler.conn.Broadcast(config.cluster, []byte(message)); err != nil {
barrier.Exit(fmt.Errorf("server broadcast failed: %v", err))
return
}
}
barrier.Sync()
// Retrieve all the arrived broadcasts
messages := make(map[string]struct{})
for i := 0; i < (conf.clients+conf.servers)*conf.messages; i++ {
select {
case msg := <-handler.delivers:
messages[string(msg)] = struct{}{}
case <-time.After(time.Second):
barrier.Exit(errors.New("broadcast receive timeout"))
return
}
}
// Verify all the individual broadcasts
for i := 0; i < conf.clients; i++ {
for j := 0; j < conf.messages; j++ {
msg := fmt.Sprintf("client #%d, broadcast %d", i, j)
if _, ok := messages[msg]; !ok {
barrier.Exit(fmt.Errorf("broadcast not found: %s", msg))
return
}
delete(messages, msg)
}
}
for i := 0; i < conf.servers; i++ {
for j := 0; j < conf.messages; j++ {
msg := fmt.Sprintf("server #%d, broadcast %d", i, j)
if _, ok := messages[msg]; !ok {
barrier.Exit(fmt.Errorf("broadcast not found: %s", msg))
return
}
delete(messages, msg)
}
}
barrier.Exit(nil)
}(i)
}
// Schedule the parallel operations
if errs := barrier.Wait(); len(errs) != 0 {
t.Fatalf("startup phase failed: %v.", errs)
}
if errs := barrier.Wait(); len(errs) != 0 {
t.Fatalf("broadcasting phase failed: %v.", errs)
}
if errs := barrier.Wait(); len(errs) != 0 {
t.Fatalf("verification phase failed: %v.", errs)
}
// Make sure all children terminated
shutdown.Wait()
}
// Service handler for the broadcast limit tests.
type broadcastLimitTestHandler struct {
conn *Connection
delivers chan []byte
sleep time.Duration
}
func (b *broadcastLimitTestHandler) Init(conn *Connection) error { b.conn = conn; return nil }
func (b *broadcastLimitTestHandler) HandleRequest([]byte) ([]byte, error) { panic("not implemented") }
func (b *broadcastLimitTestHandler) HandleTunnel(tun *Tunnel) { panic("not implemented") }
func (b *broadcastLimitTestHandler) HandleDrop(reason error) { panic("not implemented") }
func (b *broadcastLimitTestHandler) HandleBroadcast(msg []byte) {
time.Sleep(b.sleep)
b.delivers <- msg
}
// Tests the broadcast thread limitation.
func TestBroadcastThreadLimit(t *testing.T) {
// Test specific configurations
conf := struct {
messages int
sleep time.Duration
}{4, 100 * time.Millisecond}
// Create the service handler and limiter
handler := &broadcastLimitTestHandler{
delivers: make(chan []byte, conf.messages),
sleep: conf.sleep,
}
limits := &ServiceLimits{BroadcastThreads: 1}
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, limits)
if err != nil {
t.Fatalf("registration failed: %v.", err)
}
defer serv.Unregister()
// Send a few broadcasts
for i := 0; i < conf.messages; i++ {
if err := handler.conn.Broadcast(config.cluster, []byte{byte(i)}); err != nil {
t.Fatalf("broadcast failed: %v.", err)
}
}
// Wait for half time and verify that only half was processed
time.Sleep(time.Duration(conf.messages/2)*conf.sleep + conf.sleep/2)
for i := 0; i < conf.messages/2; i++ {
select {
case <-handler.delivers:
default:
t.Errorf("broadcast #%d not received", i)
}
}
select {
case <-handler.delivers:
t.Errorf("additional broadcast received")
default:
}
}
// Tests the broadcast memory limitation.
func TestBroadcastMemoryLimit(t *testing.T) {
// Create the service handler and limiter
handler := &broadcastTestHandler{
delivers: make(chan []byte, 1),
}
limits := &ServiceLimits{BroadcastMemory: 1}
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, limits)
if err != nil {
t.Fatalf("registration failed: %v.", err)
}
defer serv.Unregister()
// Check that a 1 byte broadcast passes
if err := handler.conn.Broadcast(config.cluster, []byte{0x00}); err != nil {
t.Fatalf("small broadcast failed: %v.", err)
}
time.Sleep(time.Millisecond)
select {
case <-handler.delivers:
default:
t.Fatalf("small broadcast not received.")
}
// Check that a 2 byte broadcast is dropped
if err := handler.conn.Broadcast(config.cluster, []byte{0x00, 0x00}); err != nil {
t.Fatalf("large broadcast failed: %v.", err)
}
time.Sleep(time.Millisecond)
select {
case <-handler.delivers:
t.Fatalf("large broadcast received.")
default:
}
// Check that space freed gets replenished
if err := handler.conn.Broadcast(config.cluster, []byte{0x00}); err != nil {
t.Fatalf("second small broadcast failed: %v.", err)
}
time.Sleep(time.Millisecond)
select {
case <-handler.delivers:
default:
t.Fatalf("second small broadcast not received.")
}
}
// Benchmarks broadcasting a single message.
func BenchmarkBroadcastLatency(b *testing.B) {
// Create the service handler
handler := &broadcastTestHandler{
delivers: make(chan []byte, b.N),
}
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, nil)
if err != nil {
b.Fatalf("registration failed: %v.", err)
}
defer serv.Unregister()
// Reset timer and benchmark the message transfer
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.conn.Broadcast(config.cluster, []byte{byte(i)})
<-handler.delivers
}
// Stop the timer (don't measure deferred cleanup)
b.StopTimer()
}
// Benchmarks broadcasting a stream of messages.
func BenchmarkBroadcastThroughput1Threads(b *testing.B) {
benchmarkBroadcastThroughput(1, b)
}
func BenchmarkBroadcastThroughput2Threads(b *testing.B) {
benchmarkBroadcastThroughput(2, b)
}
func BenchmarkBroadcastThroughput4Threads(b *testing.B) {
benchmarkBroadcastThroughput(4, b)
}
func BenchmarkBroadcastThroughput8Threads(b *testing.B) {
benchmarkBroadcastThroughput(8, b)
}
func BenchmarkBroadcastThroughput16Threads(b *testing.B) {
benchmarkBroadcastThroughput(16, b)
}
func BenchmarkBroadcastThroughput32Threads(b *testing.B) {
benchmarkBroadcastThroughput(32, b)
}
func BenchmarkBroadcastThroughput64Threads(b *testing.B) {
benchmarkBroadcastThroughput(64, b)
}
func BenchmarkBroadcastThroughput128Threads(b *testing.B) {
benchmarkBroadcastThroughput(128, b)
}
func benchmarkBroadcastThroughput(threads int, b *testing.B) {
// Create the service handler
handler := &broadcastTestHandler{
delivers: make(chan []byte, b.N),
}
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, nil)
if err != nil {
b.Fatalf("registration failed: %v.", err)
}
defer serv.Unregister()
// Create the thread pool with the concurrent broadcasts
workers := pool.NewThreadPool(threads)
for i := 0; i < b.N; i++ {
workers.Schedule(func() {
if err := handler.conn.Broadcast(config.cluster, []byte{byte(i)}); err != nil {
b.Fatalf("broadcast failed: %v.", err)
}
})
}
// Reset timer and benchmark the message transfer
b.ResetTimer()
workers.Start()
for i := 0; i < b.N; i++ {
<-handler.delivers
}
workers.Terminate(false)
// Stop the timer (don't measure deferred cleanup)
b.StopTimer()
}