| 1 |
package centrifuge |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
"sync" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/centrifugal/centrifuge/internal/prepared" |
| 14 |
|
| 15 |
"github.com/centrifugal/protocol" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
) |
| 18 |
|
| 19 |
type testTransport struct { |
| 20 |
mu sync.Mutex |
| 21 |
sink chan []byte |
| 22 |
closed bool |
| 23 |
closeCh chan struct{} |
| 24 |
disconnect *Disconnect |
| 25 |
protoType ProtocolType |
| 26 |
cancelFn func() |
| 27 |
} |
| 28 |
|
| 29 |
func newTestTransport(cancelFn func()) *testTransport { |
| 30 |
return &testTransport{ |
| 31 |
cancelFn: cancelFn, |
| 32 |
protoType: ProtocolTypeJSON, |
| 33 |
closeCh: make(chan struct{}), |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func (t *testTransport) setProtocolType(pType ProtocolType) { |
| 38 |
t.protoType = pType |
| 39 |
} |
| 40 |
|
| 41 |
func (t *testTransport) setSink(sink chan []byte) { |
| 42 |
t.sink = sink |
| 43 |
} |
| 44 |
|
| 45 |
func (t *testTransport) Write(data []byte) error { |
| 46 |
t.mu.Lock() |
| 47 |
defer t.mu.Unlock() |
| 48 |
if t.closed { |
| 49 |
return io.EOF |
| 50 |
} |
| 51 |
dataCopy := make([]byte, len(data)) |
| 52 |
copy(dataCopy, data) |
| 53 |
if t.sink != nil { |
| 54 |
t.sink <- dataCopy |
| 55 |
} |
| 56 |
return nil |
| 57 |
} |
| 58 |
|
| 59 |
func (t *testTransport) Name() string { |
| 60 |
return transportWebsocket |
| 61 |
} |
| 62 |
|
| 63 |
func (t *testTransport) Protocol() ProtocolType { |
| 64 |
return t.protoType |
| 65 |
} |
| 66 |
|
| 67 |
func (t *testTransport) Encoding() EncodingType { |
| 68 |
return EncodingTypeJSON |
| 69 |
} |
| 70 |
|
| 71 |
func (t *testTransport) Close(disconnect *Disconnect) error { |
| 72 |
t.mu.Lock() |
| 73 |
defer t.mu.Unlock() |
| 74 |
if t.closed { |
| 75 |
return nil |
| 76 |
} |
| 77 |
t.disconnect = disconnect |
| 78 |
t.closed = true |
| 79 |
t.cancelFn() |
| 80 |
close(t.closeCh) |
| 81 |
return nil |
| 82 |
} |
| 83 |
|
| 84 |
func TestHub(t *testing.T) { |
| 85 |
h := newHub() |
| 86 |
c, err := newClient(context.Background(), defaultTestNode(), newTestTransport(func() {})) |
| 87 |
require.NoError(t, err) |
| 88 |
c.user = "test" |
| 89 |
err = h.remove(c) |
| 90 |
require.NoError(t, err) |
| 91 |
err = h.add(c) |
| 92 |
require.NoError(t, err) |
| 93 |
conns := h.userConnections("test") |
| 94 |
require.Equal(t, 1, len(conns)) |
| 95 |
require.Equal(t, 1, h.NumClients()) |
| 96 |
require.Equal(t, 1, h.NumUsers()) |
| 97 |
|
| 98 |
validUID := c.uid |
| 99 |
c.uid = "invalid" |
| 100 |
err = h.remove(c) |
| 101 |
require.NoError(t, err) |
| 102 |
require.Len(t, h.userConnections("test"), 1) |
| 103 |
|
| 104 |
c.uid = validUID |
| 105 |
err = h.remove(c) |
| 106 |
require.NoError(t, err) |
| 107 |
require.Len(t, h.userConnections("test"), 0) |
| 108 |
} |
| 109 |
|
| 110 |
func TestHubUnsubscribe(t *testing.T) { |
| 111 |
n := defaultTestNode() |
| 112 |
defer func() { _ = n.Shutdown(context.Background()) }() |
| 113 |
|
| 114 |
client := newTestSubscribedClient(t, n, "42", "test_channel") |
| 115 |
transport := client.transport.(*testTransport) |
| 116 |
transport.sink = make(chan []byte, 100) |
| 117 |
|
| 118 |
// Unsubscribe not existed user. |
| 119 |
err := n.hub.unsubscribe("1", "test_channel") |
| 120 |
require.NoError(t, err) |
| 121 |
|
| 122 |
// Unsubscribe subscribed user. |
| 123 |
err = n.hub.unsubscribe("42", "test_channel") |
| 124 |
require.NoError(t, err) |
| 125 |
select { |
| 126 |
case data := <-transport.sink: |
| 127 |
require.Equal(t, "{\"result\":{\"type\":3,\"channel\":\"test_channel\",\"data\":{}}}\n", string(data)) |
| 128 |
case <-time.After(2 * time.Second): |
| 129 |
t.Fatal("no data in sink") |
| 130 |
} |
| 131 |
require.Zero(t, n.hub.NumSubscribers("test_channel")) |
| 132 |
} |
| 133 |
|
| 134 |
func TestHubDisconnect(t *testing.T) { |
| 135 |
n := defaultNodeNoHandlers() |
| 136 |
defer func() { _ = n.Shutdown(context.Background()) }() |
| 137 |
|
| 138 |
n.OnConnect(func(client *Client) { |
| 139 |
client.OnSubscribe(func(event SubscribeEvent, cb SubscribeCallback) { |
| 140 |
cb(SubscribeReply{}, nil) |
| 141 |
}) |
| 142 |
}) |
| 143 |
|
| 144 |
client := newTestSubscribedClient(t, n, "42", "test_channel") |
| 145 |
clientWithReconnect := newTestSubscribedClient(t, n, "24", "test_channel_reconnect") |
| 146 |
require.Len(t, n.hub.userConnections("42"), 1) |
| 147 |
require.Len(t, n.hub.userConnections("24"), 1) |
| 148 |
require.Equal(t, 1, n.hub.NumSubscribers("test_channel")) |
| 149 |
require.Equal(t, 1, n.hub.NumSubscribers("test_channel_reconnect")) |
| 150 |
|
| 151 |
wg := sync.WaitGroup{} |
| 152 |
wg.Add(2) |
| 153 |
|
| 154 |
client.eventHub.disconnectHandler = func(e DisconnectEvent) { |
| 155 |
defer wg.Done() |
| 156 |
require.False(t, e.Disconnect.Reconnect) |
| 157 |
} |
| 158 |
|
| 159 |
clientWithReconnect.eventHub.disconnectHandler = func(e DisconnectEvent) { |
| 160 |
defer wg.Done() |
| 161 |
require.True(t, e.Disconnect.Reconnect) |
| 162 |
} |
| 163 |
|
| 164 |
// Disconnect not existed user. |
| 165 |
err := n.hub.disconnect("1", DisconnectForceNoReconnect, nil) |
| 166 |
require.NoError(t, err) |
| 167 |
|
| 168 |
// Disconnect subscribed user. |
| 169 |
err = n.hub.disconnect("42", DisconnectForceNoReconnect, nil) |
| 170 |
require.NoError(t, err) |
| 171 |
select { |
| 172 |
case <-client.transport.(*testTransport).closeCh: |
| 173 |
case <-time.After(2 * time.Second): |
| 174 |
t.Fatal("no data in sink") |
| 175 |
} |
| 176 |
require.Len(t, n.hub.userConnections("42"), 0) |
| 177 |
require.Equal(t, 0, n.hub.NumSubscribers("test_channel")) |
| 178 |
|
| 179 |
// Disconnect subscribed user with reconnect. |
| 180 |
err = n.hub.disconnect("24", DisconnectForceReconnect, nil) |
| 181 |
require.NoError(t, err) |
| 182 |
select { |
| 183 |
case <-clientWithReconnect.transport.(*testTransport).closeCh: |
| 184 |
case <-time.After(2 * time.Second): |
| 185 |
t.Fatal("no data in sink") |
| 186 |
} |
| 187 |
require.Len(t, n.hub.userConnections("24"), 0) |
| 188 |
require.Equal(t, 0, n.hub.NumSubscribers("test_channel_reconnect")) |
| 189 |
|
| 190 |
wg.Wait() |
| 191 |
|
| 192 |
require.Len(t, n.hub.userConnections("24"), 0) |
| 193 |
require.Len(t, n.hub.userConnections("42"), 0) |
| 194 |
require.Equal(t, 0, n.hub.NumSubscribers("test_channel")) |
| 195 |
require.Equal(t, 0, n.hub.NumSubscribers("test_channel_reconnect")) |
| 196 |
} |
| 197 |
|
| 198 |
func TestHubDisconnect_ClientWhitelist(t *testing.T) { |
| 199 |
n := defaultNodeNoHandlers() |
| 200 |
defer func() { _ = n.Shutdown(context.Background()) }() |
| 201 |
|
| 202 |
n.OnConnect(func(client *Client) { |
| 203 |
client.OnSubscribe(func(event SubscribeEvent, cb SubscribeCallback) { |
| 204 |
cb(SubscribeReply{}, nil) |
| 205 |
}) |
| 206 |
}) |
| 207 |
|
| 208 |
client := newTestSubscribedClient(t, n, "12", "test_channel") |
| 209 |
clientToKeep := newTestSubscribedClient(t, n, "12", "test_channel") |
| 210 |
|
| 211 |
require.Len(t, n.hub.userConnections("12"), 2) |
| 212 |
require.Equal(t, 2, n.hub.NumSubscribers("test_channel")) |
| 213 |
|
| 214 |
shouldBeClosed := make(chan struct{}) |
| 215 |
shouldNotBeClosed := make(chan struct{}) |
| 216 |
|
| 217 |
client.eventHub.disconnectHandler = func(e DisconnectEvent) { |
| 218 |
close(shouldBeClosed) |
| 219 |
} |
| 220 |
|
| 221 |
clientToKeep.eventHub.disconnectHandler = func(e DisconnectEvent) { |
| 222 |
close(shouldNotBeClosed) |
| 223 |
} |
| 224 |
|
| 225 |
whitelist := []string{clientToKeep.ID()} |
| 226 |
|
| 227 |
// Disconnect not existed user. |
| 228 |
err := n.hub.disconnect("12", DisconnectConnectionLimit, whitelist) |
| 229 |
require.NoError(t, err) |
| 230 |
|
| 231 |
select { |
| 232 |
case <-shouldBeClosed: |
| 233 |
select { |
| 234 |
case <-shouldNotBeClosed: |
| 235 |
require.Fail(t, "client should not be disconnected") |
| 236 |
case <-time.After(time.Second): |
| 237 |
require.Len(t, n.hub.userConnections("12"), 1) |
| 238 |
require.Equal(t, 1, n.hub.NumSubscribers("test_channel")) |
| 239 |
} |
| 240 |
case <-time.After(time.Second): |
| 241 |
require.Fail(t, "timeout waiting for channel close") |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
func TestHubBroadcastPublication(t *testing.T) { |
| 246 |
tcs := []struct { |
| 247 |
name string |
| 248 |
protocolType ProtocolType |
| 249 |
}{ |
| 250 |
{name: "JSON", protocolType: ProtocolTypeJSON}, |
| 251 |
{name: "Protobuf", protocolType: ProtocolTypeProtobuf}, |
| 252 |
} |
| 253 |
|
| 254 |
for _, tc := range tcs { |
| 255 |
t.Run(tc.name, func(t *testing.T) { |
| 256 |
n := defaultTestNode() |
| 257 |
defer func() { _ = n.Shutdown(context.Background()) }() |
| 258 |
|
| 259 |
client := newTestSubscribedClient(t, n, "42", "test_channel") |
| 260 |
transport := client.transport.(*testTransport) |
| 261 |
transport.sink = make(chan []byte, 100) |
| 262 |
transport.protoType = tc.protocolType |
| 263 |
|
| 264 |
// Broadcast to not existed channel. |
| 265 |
err := n.hub.broadcastPublication( |
| 266 |
"not_test_channel", |
| 267 |
&protocol.Publication{Data: []byte(`{"data": "broadcast_data"}`)}, |
| 268 |
StreamPosition{}, |
| 269 |
) |
| 270 |
require.NoError(t, err) |
| 271 |
|
| 272 |
// Broadcast to existed channel. |
| 273 |
err = n.hub.broadcastPublication( |
| 274 |
"test_channel", |
| 275 |
&protocol.Publication{Data: []byte(`{"data": "broadcast_data"}`)}, |
| 276 |
StreamPosition{}, |
| 277 |
) |
| 278 |
require.NoError(t, err) |
| 279 |
select { |
| 280 |
case data := <-transport.sink: |
| 281 |
require.True(t, strings.Contains(string(data), "broadcast_data")) |
| 282 |
case <-time.After(2 * time.Second): |
| 283 |
t.Fatal("no data in sink") |
| 284 |
} |
| 285 |
}) |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
func TestHubBroadcastJoin(t *testing.T) { |
| 290 |
tcs := []struct { |
| 291 |
name string |
| 292 |
protocolType ProtocolType |
| 293 |
}{ |
| 294 |
{name: "JSON", protocolType: ProtocolTypeJSON}, |
| 295 |
{name: "Protobuf", protocolType: ProtocolTypeProtobuf}, |
| 296 |
} |
| 297 |
|
| 298 |
for _, tc := range tcs { |
| 299 |
t.Run(tc.name, func(t *testing.T) { |
| 300 |
n := defaultTestNode() |
| 301 |
defer func() { _ = n.Shutdown(context.Background()) }() |
| 302 |
|
| 303 |
client := newTestSubscribedClient(t, n, "42", "test_channel") |
| 304 |
transport := client.transport.(*testTransport) |
| 305 |
transport.sink = make(chan []byte, 100) |
| 306 |
transport.protoType = tc.protocolType |
| 307 |
|
| 308 |
// Broadcast to not existed channel. |
| 309 |
err := n.hub.broadcastJoin("not_test_channel", &protocol.Join{ |
| 310 |
Info: protocol.ClientInfo{Client: "broadcast_client"}, |
| 311 |
}) |
| 312 |
require.NoError(t, err) |
| 313 |
|
| 314 |
// Broadcast to existed channel. |
| 315 |
err = n.hub.broadcastJoin("test_channel", &protocol.Join{ |
| 316 |
Info: protocol.ClientInfo{Client: "broadcast_client"}, |
| 317 |
}) |
| 318 |
require.NoError(t, err) |
| 319 |
select { |
| 320 |
case data := <-transport.sink: |
| 321 |
require.True(t, strings.Contains(string(data), "broadcast_client")) |
| 322 |
case <-time.After(2 * time.Second): |
| 323 |
t.Fatal("no data in sink") |
| 324 |
} |
| 325 |
}) |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
func TestHubBroadcastLeave(t *testing.T) { |
| 330 |
tcs := []struct { |
| 331 |
name string |
| 332 |
protocolType ProtocolType |
| 333 |
}{ |
| 334 |
{name: "JSON", protocolType: ProtocolTypeJSON}, |
| 335 |
{name: "Protobuf", protocolType: ProtocolTypeProtobuf}, |
| 336 |
} |
| 337 |
|
| 338 |
for _, tc := range tcs { |
| 339 |
t.Run(tc.name, func(t *testing.T) { |
| 340 |
n := defaultTestNode() |
| 341 |
defer func() { _ = n.Shutdown(context.Background()) }() |
| 342 |
|
| 343 |
client := newTestSubscribedClient(t, n, "42", "test_channel") |
| 344 |
transport := client.transport.(*testTransport) |
| 345 |
transport.sink = make(chan []byte, 100) |
| 346 |
transport.protoType = tc.protocolType |
| 347 |
|
| 348 |
// Broadcast to not existed channel. |
| 349 |
err := n.hub.broadcastLeave("not_test_channel", &protocol.Leave{ |
| 350 |
Info: protocol.ClientInfo{Client: "broadcast_client"}, |
| 351 |
}) |
| 352 |
require.NoError(t, err) |
| 353 |
|
| 354 |
// Broadcast to existed channel. |
| 355 |
err = n.hub.broadcastLeave("test_channel", &protocol.Leave{ |
| 356 |
Info: protocol.ClientInfo{Client: "broadcast_client"}, |
| 357 |
}) |
| 358 |
require.NoError(t, err) |
| 359 |
select { |
| 360 |
case data := <-transport.sink: |
| 361 |
require.Contains(t, string(data), "broadcast_client") |
| 362 |
case <-time.After(2 * time.Second): |
| 363 |
t.Fatal("no data in sink") |
| 364 |
} |
| 365 |
}) |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
func TestHubShutdown(t *testing.T) { |
| 370 |
h := newHub() |
| 371 |
err := h.shutdown(context.Background()) |
| 372 |
require.NoError(t, err) |
| 373 |
h = newHub() |
| 374 |
c, err := newClient(context.Background(), defaultTestNode(), newTestTransport(func() {})) |
| 375 |
require.NoError(t, err) |
| 376 |
_ = h.add(c) |
| 377 |
|
| 378 |
err = h.shutdown(context.Background()) |
| 379 |
require.NoError(t, err) |
| 380 |
|
| 381 |
ctxCanceled, cancel := context.WithCancel(context.Background()) |
| 382 |
cancel() |
| 383 |
err = h.shutdown(ctxCanceled) |
| 384 |
require.EqualError(t, err, "context canceled") |
| 385 |
} |
| 386 |
|
| 387 |
func TestHubSubscriptions(t *testing.T) { |
| 388 |
h := newHub() |
| 389 |
c, err := newClient(context.Background(), defaultTestNode(), newTestTransport(func() {})) |
| 390 |
require.NoError(t, err) |
| 391 |
|
| 392 |
_, _ = h.addSub("test1", c) |
| 393 |
_, _ = h.addSub("test2", c) |
| 394 |
require.Equal(t, 2, h.NumChannels()) |
| 395 |
require.Contains(t, h.Channels(), "test1") |
| 396 |
require.Contains(t, h.Channels(), "test2") |
| 397 |
require.NotZero(t, h.NumSubscribers("test1")) |
| 398 |
require.NotZero(t, h.NumSubscribers("test2")) |
| 399 |
|
| 400 |
// Not exited sub. |
| 401 |
removed, err := h.removeSub("not_existed", c) |
| 402 |
require.NoError(t, err) |
| 403 |
require.True(t, removed) |
| 404 |
|
| 405 |
// Exited sub with invalid uid. |
| 406 |
validUID := c.uid |
| 407 |
c.uid = "invalid" |
| 408 |
removed, err = h.removeSub("test1", c) |
| 409 |
require.NoError(t, err) |
| 410 |
require.True(t, removed) |
| 411 |
c.uid = validUID |
| 412 |
|
| 413 |
// Exited sub. |
| 414 |
removed, err = h.removeSub("test1", c) |
| 415 |
require.NoError(t, err) |
| 416 |
require.True(t, removed) |
| 417 |
|
| 418 |
// Exited sub. |
| 419 |
removed, err = h.removeSub("test2", c) |
| 420 |
require.NoError(t, err) |
| 421 |
require.True(t, removed) |
| 422 |
|
| 423 |
require.Equal(t, h.NumChannels(), 0) |
| 424 |
require.Zero(t, h.NumSubscribers("test1")) |
| 425 |
require.Zero(t, h.NumSubscribers("test2")) |
| 426 |
} |
| 427 |
|
| 428 |
func TestPreparedReply(t *testing.T) { |
| 429 |
reply := protocol.Reply{} |
| 430 |
preparedReply := prepared.NewReply(&reply, protocol.TypeJSON) |
| 431 |
data := preparedReply.Data() |
| 432 |
require.NotNil(t, data) |
| 433 |
} |
| 434 |
|
| 435 |
func TestUserConnections(t *testing.T) { |
| 436 |
h := newHub() |
| 437 |
c, err := newClient(context.Background(), defaultTestNode(), newTestTransport(func() {})) |
| 438 |
require.NoError(t, err) |
| 439 |
_ = h.add(c) |
| 440 |
|
| 441 |
connections := h.userConnections(c.UserID()) |
| 442 |
require.Equal(t, h.connShards[index(c.UserID(), numHubShards)].conns, connections) |
| 443 |
} |
| 444 |
|
| 445 |
func TestHubSharding(t *testing.T) { |
| 446 |
numUsers := numHubShards * 10 |
| 447 |
numChannels := numHubShards * 10 |
| 448 |
|
| 449 |
channels := make([]string, 0, numChannels) |
| 450 |
for i := 0; i < numChannels; i++ { |
| 451 |
channels = append(channels, "ch"+strconv.Itoa(i)) |
| 452 |
} |
| 453 |
|
| 454 |
n := defaultTestNode() |
| 455 |
defer func() { _ = n.Shutdown(context.Background()) }() |
| 456 |
|
| 457 |
for j := 0; j < 2; j++ { // two connections from the same user. |
| 458 |
for i := 0; i < numUsers; i++ { |
| 459 |
c, err := newClient(context.Background(), n, newTestTransport(func() {})) |
| 460 |
require.NoError(t, err) |
| 461 |
c.user = strconv.Itoa(i) |
| 462 |
require.NoError(t, err) |
| 463 |
_ = n.hub.add(c) |
| 464 |
for _, ch := range channels { |
| 465 |
_, _ = n.hub.addSub(ch, c) |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
for i := range n.hub.connShards { |
| 471 |
require.NotZero(t, n.hub.connShards[i].NumClients()) |
| 472 |
require.NotZero(t, n.hub.connShards[i].NumUsers()) |
| 473 |
} |
| 474 |
for i := range n.hub.subShards { |
| 475 |
require.True(t, len(n.hub.subShards[i].subs) > 0) |
| 476 |
} |
| 477 |
|
| 478 |
require.Equal(t, numUsers, n.Hub().NumUsers()) |
| 479 |
require.Equal(t, 2*numUsers, n.Hub().NumClients()) |
| 480 |
require.Equal(t, numChannels, n.Hub().NumChannels()) |
| 481 |
} |
| 482 |
|
| 483 |
// This benchmark allows to estimate the benefit from Hub sharding. |
| 484 |
// As we have a broadcasting goroutine here it's not very useful to look at |
| 485 |
// total allocations here - it's better to look at operation time. |
| 486 |
func BenchmarkHub_Contention(b *testing.B) { |
| 487 |
numClients := 100 |
| 488 |
numChannels := 128 |
| 489 |
|
| 490 |
n := defaultTestNodeBenchmark(b) |
| 491 |
|
| 492 |
var clients []*Client |
| 493 |
var channels []string |
| 494 |
|
| 495 |
for i := 0; i < numChannels; i++ { |
| 496 |
channels = append(channels, "ch"+strconv.Itoa(i)) |
| 497 |
} |
| 498 |
|
| 499 |
for i := 0; i < numClients; i++ { |
| 500 |
c, err := newClient(context.Background(), n, newTestTransport(func() {})) |
| 501 |
require.NoError(b, err) |
| 502 |
_ = n.hub.add(c) |
| 503 |
clients = append(clients, c) |
| 504 |
for _, ch := range channels { |
| 505 |
_, _ = n.hub.addSub(ch, c) |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
pub := &protocol.Publication{ |
| 510 |
Data: []byte(`{"input": "test"}`), |
| 511 |
} |
| 512 |
streamPosition := StreamPosition{} |
| 513 |
|
| 514 |
b.ResetTimer() |
| 515 |
b.RunParallel(func(pb *testing.PB) { |
| 516 |
i := 0 |
| 517 |
for pb.Next() { |
| 518 |
i++ |
| 519 |
var wg sync.WaitGroup |
| 520 |
wg.Add(1) |
| 521 |
go func() { |
| 522 |
defer wg.Done() |
| 523 |
_ = n.hub.broadcastPublication(channels[(i+numChannels/2)%numChannels], pub, streamPosition) |
| 524 |
}() |
| 525 |
_, _ = n.hub.addSub(channels[i%numChannels], clients[i%numClients]) |
| 526 |
wg.Wait() |
| 527 |
} |
| 528 |
}) |
| 529 |
} |
| 530 |
|
| 531 |
var broadcastBenches = []struct { |
| 532 |
NumSubscribers int |
| 533 |
}{ |
| 534 |
{1000}, |
| 535 |
{10000}, |
| 536 |
{100000}, |
| 537 |
} |
| 538 |
|
| 539 |
// BenchmarkHub_MassiveBroadcast allows estimating time to broadcast |
| 540 |
// a single message to many subscribers inside one channel. |
| 541 |
func BenchmarkHub_MassiveBroadcast(b *testing.B) { |
| 542 |
pub := &protocol.Publication{Data: []byte(`{"input": "test"}`)} |
| 543 |
streamPosition := StreamPosition{} |
| 544 |
|
| 545 |
for _, tt := range broadcastBenches { |
| 546 |
numSubscribers := tt.NumSubscribers |
| 547 |
b.Run(fmt.Sprintf("%d", numSubscribers), func(b *testing.B) { |
| 548 |
b.ReportAllocs() |
| 549 |
n := defaultTestNodeBenchmark(b) |
| 550 |
|
| 551 |
numChannels := 64 |
| 552 |
channels := make([]string, 0, numChannels) |
| 553 |
|
| 554 |
for i := 0; i < numChannels; i++ { |
| 555 |
channels = append(channels, "broadcast"+strconv.Itoa(i)) |
| 556 |
} |
| 557 |
|
| 558 |
sink := make(chan []byte, 1024) |
| 559 |
|
| 560 |
for i := 0; i < numSubscribers; i++ { |
| 561 |
t := newTestTransport(func() {}) |
| 562 |
t.setSink(sink) |
| 563 |
c, err := newClient(context.Background(), n, t) |
| 564 |
require.NoError(b, err) |
| 565 |
_ = n.hub.add(c) |
| 566 |
for _, ch := range channels { |
| 567 |
_, _ = n.hub.addSub(ch, c) |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
b.ResetTimer() |
| 572 |
for i := 0; i < b.N; i++ { |
| 573 |
var wg sync.WaitGroup |
| 574 |
wg.Add(1) |
| 575 |
go func() { |
| 576 |
defer wg.Done() |
| 577 |
j := 0 |
| 578 |
for { |
| 579 |
<-sink |
| 580 |
j++ |
| 581 |
if j == numSubscribers { |
| 582 |
break |
| 583 |
} |
| 584 |
} |
| 585 |
}() |
| 586 |
_ = n.hub.broadcastPublication(channels[i%numChannels], pub, streamPosition) |
| 587 |
wg.Wait() |
| 588 |
} |
| 589 |
}) |
| 590 |
} |
| 591 |
} |