forked from zeromq/goczmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sock_draft_test.go
92 lines (75 loc) · 1.83 KB
/
sock_draft_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
// +build draft
package goczmq
import (
"fmt"
"testing"
)
func TestScatterGather(t *testing.T) {
bogusScatter, err := NewScatter("bogus://bogus")
if err == nil {
t.Error(err)
}
defer bogusScatter.Destroy()
bogusGather, err := NewGather("bogus://bogus")
if err == nil {
t.Error(err)
}
defer bogusGather.Destroy()
scatter, err := NewScatter("inproc://scatter1,inproc://scatter2")
if err != nil {
t.Error(err)
}
defer scatter.Destroy()
gather, err := NewGather("inproc://scatter1,inproc://scatter2")
if err != nil {
t.Error(err)
}
defer gather.Destroy()
err = scatter.SendFrame([]byte("Hello World"), FlagNone)
if err != nil {
t.Error(err)
}
frame, _, err := gather.RecvFrame()
if err != nil {
t.Error(err)
}
if want, have := "Hello World", string(frame); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
}
func benchmarkScatterGather(size int, b *testing.B) {
gatherSock := NewSock(Gather)
defer gatherSock.Destroy()
_, err := gatherSock.Bind(fmt.Sprintf("inproc://benchScatterGather%#v", size))
if err != nil {
panic(err)
}
scatterSock := NewSock(Scatter)
defer scatterSock.Destroy()
err = scatterSock.Connect(fmt.Sprintf("inproc://benchScatterGather%#v", size))
if err != nil {
panic(err)
}
go func() {
payload := make([]byte, size)
for i := 0; i < b.N; i++ {
err = scatterSock.SendFrame(payload, FlagNone)
if err != nil {
panic(err)
}
}
}()
for i := 0; i < b.N; i++ {
msg, _, err := gatherSock.RecvFrame()
if err != nil {
panic(err)
}
if len(msg) != size {
panic("msg too small")
}
b.SetBytes(int64(size))
}
}
func BenchmarkScatterGather1k(b *testing.B) { benchmarkScatterGather(1024, b) }
func BenchmarkScatterGather4k(b *testing.B) { benchmarkScatterGather(4096, b) }
func BenchmarkScatterGather16k(b *testing.B) { benchmarkScatterGather(16384, b) }