-
Notifications
You must be signed in to change notification settings - Fork 4
/
benchkit_test.go
258 lines (229 loc) · 6.01 KB
/
benchkit_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
package chanserv
import (
"fmt"
"log"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/rcrowley/go-metrics"
"github.com/zenhotels/astranet"
)
var chanservCli Client
func init() {
chanservCli, _ = getChanservAstranet()
time.Sleep(time.Second)
}
const GB = 1024 * 1024 * 1024
const MB = 1024 * 1024
const (
heavySourceLimit = 10
heavyFrameLimit = 500
heavyFrameSize = 1024 * 1024 // 1 MB
floodSourceLimit = 1000
floodFrameLimit = 5000
floodFrameSize = 64 // 64 bytes
)
func getMeters() (frameMeter metrics.Meter, frameDataMeter metrics.Meter, stop func()) {
stopC := make(chan struct{})
doneC := make(chan struct{})
stop = func() {
close(stopC)
<-doneC
}
frameMeter = metrics.NewMeter()
frameDataMeter = metrics.NewMeter()
go func() {
t := time.NewTicker(time.Second)
var prevFrames int64
var prevFramesData int64
for range t.C {
var mem runtime.MemStats
snap := frameMeter.Snapshot()
runtime.ReadMemStats(&mem)
fmt.Printf("METRICS> Heap: %.1fM\n", float64(mem.HeapInuse)/MB)
fmt.Printf("METRICS> Goroutines: %.1fK\n", float64(runtime.NumGoroutine())/1000)
fmt.Printf("METRICS> Frames: %.1fK\n", float64(snap.Count())/1000)
fmt.Printf("METRICS> Frames/s: %.1fK\n", float64(snap.Count()-prevFrames)/1000)
prevFrames = snap.Count()
snap = frameDataMeter.Snapshot()
fmt.Printf("METRICS> GB: %.3f\n", float64(snap.Count())/GB)
fmt.Printf("METRICS> Gbit/second: %.3f\n", float64(snap.Count()-prevFramesData)*8/GB)
prevFramesData = snap.Count()
fmt.Println()
select {
case <-stopC:
close(doneC)
t.Stop()
return
default:
}
}
}()
return frameMeter, frameDataMeter, stop
}
func BenchmarkConnectChanserv(b *testing.B) {
b.ReportAllocs()
b.SetParallelism(runtime.NumCPU())
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
sources, err := chanservCli.LookupAndPost("chanserv-astranet-empty", []byte("hello"), nil)
if err != nil {
b.Fatal(err)
return
}
var frames int64
wg := new(sync.WaitGroup)
wg.Add(1)
for src := range sources {
go func(src Source) {
for range src.Out() {
atomic.AddInt64(&frames, 1)
}
wg.Done()
}(src)
}
wg.Wait()
if atomic.LoadInt64(&frames) != 1 {
b.Fatal("BenchmarkConnectChanserv: frames != 1")
}
}
})
}
func BenchmarkHeavyChanserv(b *testing.B) {
frameMeter, frameDataMeter, stopFunc := getMeters()
defer stopFunc()
for i := 0; i < b.N; i++ {
sources, err := chanservCli.LookupAndPost("chanserv-astranet-heavy", nil, nil)
if err != nil {
b.Fatal(err)
return
}
var frames int64
wg := new(sync.WaitGroup)
wg.Add(heavySourceLimit)
for src := range sources {
go func(src Source) {
for frame := range src.Out() {
frameMeter.Mark(1)
frameDataMeter.Mark(int64(len(frame.Bytes())))
atomic.AddInt64(&frames, 1)
}
wg.Done()
// log.Println("source done", atomic.LoadInt64(&frames))
}(src)
}
wg.Wait()
if atomic.LoadInt64(&frames) != heavySourceLimit*heavyFrameLimit {
b.Fatalf("BenchmarkHeavyChanserv: frames != %d", heavySourceLimit*heavyFrameLimit)
}
}
}
func BenchmarkFloodChanserv(b *testing.B) {
frameMeter, frameDataMeter, stopFunc := getMeters()
defer stopFunc()
for i := 0; i < b.N; i++ {
sources, err := chanservCli.LookupAndPost("chanserv-astranet-flood", nil, nil)
if err != nil {
b.Fatal(err)
return
}
var frames int64
wg := new(sync.WaitGroup)
wg.Add(floodSourceLimit)
for src := range sources {
go func(src Source) {
for frame := range src.Out() {
frameMeter.Mark(1)
frameDataMeter.Mark(int64(len(frame.Bytes())))
atomic.AddInt64(&frames, 1)
}
wg.Done()
// log.Println("source done", atomic.LoadInt64(&frames))
}(src)
}
wg.Wait()
if atomic.LoadInt64(&frames) != floodSourceLimit*floodFrameLimit {
b.Fatalf("BenchmarkHeavyChanserv: frames != %d", floodSourceLimit*floodFrameLimit)
}
}
}
func getChanservAstranet() (Client, Server) {
mpx := astranet.New().Server()
if err := mpx.ListenAndServe("tcp4", ":5556"); err != nil {
log.Fatalln("[astranet ERR]", err)
}
srv := NewServer(mpx, ServerOnError(func(err error) {
log.Println("[server WARN]", err)
}))
emptySrcFunc := getBenchSrcFunc(1, 1, 0)
if err := srv.ListenAndServe("chanserv-astranet-empty", emptySrcFunc); err != nil {
log.Fatalln("[server ERR]", err)
}
floodSrcFunc := getBenchSrcFunc(floodSourceLimit, floodFrameLimit, floodFrameSize)
if err := srv.ListenAndServe("chanserv-astranet-flood", floodSrcFunc); err != nil {
log.Fatalln("[server ERR]", err)
}
heavySrcFunc := getBenchSrcFunc(heavySourceLimit, heavyFrameLimit, heavyFrameSize)
if err := srv.ListenAndServe("chanserv-astranet-heavy", heavySrcFunc); err != nil {
log.Fatalln("[server ERR]", err)
}
mpx2 := astranet.New().Client()
if err := mpx2.Join("tcp4", "localhost:5556"); err != nil {
log.Fatalln("[astranet ERR]", err)
}
cli := NewClient(mpx2,
ClientSourceBufferSize(1000),
ClientFrameBufferSize(30000),
ClientDialTimeout(5*time.Second),
ClientOnError(func(err error) {
log.Println("[client WARN]", err)
}),
)
return cli, srv
}
func getBenchSrcFunc(srcLimit, frameLimit, frameSize int) func(req []byte) <-chan Source {
return func(req []byte) <-chan Source {
out := make(chan Source, srcLimit)
go func() {
for i := 0; i < srcLimit; i++ {
out <- newBenchSrc(frameLimit, frameSize)
}
close(out)
}()
return out
}
}
type benchSrc struct {
frames int
size int
buf chan Frame
}
func newBenchSrc(frames, size int) Source {
src := benchSrc{
frames: frames,
size: size,
buf: make(chan Frame, size),
}
go func() {
for i := 0; i < frames; i++ {
var contents []byte
if size > 0 {
contents = make([]byte, size)
}
src.buf <- benchFrame{contents}
}
close(src.buf)
}()
return src
}
func (e benchSrc) Header() []byte { return nil }
func (e benchSrc) Meta() MetaData { return nil }
func (e benchSrc) Out() <-chan Frame { return e.buf }
type benchFrame struct {
contents []byte
}
func (e benchFrame) Bytes() []byte {
return e.contents
}