forked from nats-io/nats.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchlib_test.go
226 lines (200 loc) · 5.66 KB
/
benchlib_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
package bench
import (
"fmt"
"strings"
"testing"
"time"
"github.com/nats-io/go-nats"
)
const (
MsgSize = 8
Million = 1000 * 1000
)
var baseTime = time.Now()
func millionMessagesSecondSample(seconds int) *Sample {
messages := Million * seconds
start := baseTime
end := start.Add(time.Second * time.Duration(seconds))
nc := new(nats.Conn)
s := NewSample(messages, MsgSize, start, end, nc)
s.MsgCnt = uint64(messages)
s.MsgBytes = uint64(messages * MsgSize)
s.IOBytes = s.MsgBytes
return s
}
func TestDuration(t *testing.T) {
s := millionMessagesSecondSample(1)
duration := s.End.Sub(s.Start)
if duration != s.Duration() || duration != time.Second {
t.Fatal("Expected sample duration to be 1 second")
}
}
func TestSeconds(t *testing.T) {
s := millionMessagesSecondSample(1)
seconds := s.End.Sub(s.Start).Seconds()
if seconds != s.Seconds() || seconds != 1.0 {
t.Fatal("Expected sample seconds to be 1 second")
}
}
func TestRate(t *testing.T) {
s := millionMessagesSecondSample(60)
if s.Rate() != Million {
t.Fatal("Expected rate at 1 million msgs")
}
}
func TestThoughput(t *testing.T) {
s := millionMessagesSecondSample(60)
if s.Throughput() != Million*MsgSize {
t.Fatalf("Expected throughput at %d million bytes/sec", MsgSize)
}
}
func TestStrings(t *testing.T) {
s := millionMessagesSecondSample(60)
if len(s.String()) == 0 {
t.Fatal("Sample didn't provide a String")
}
}
func TestGroupDuration(t *testing.T) {
sg := NewSampleGroup()
sg.AddSample(millionMessagesSecondSample(1))
sg.AddSample(millionMessagesSecondSample(2))
duration := sg.End.Sub(sg.Start)
if duration != sg.Duration() || duration != time.Duration(2)*time.Second {
t.Fatal("Expected aggregate duration to be 2.0 seconds")
}
}
func TestGroupSeconds(t *testing.T) {
sg := NewSampleGroup()
sg.AddSample(millionMessagesSecondSample(1))
sg.AddSample(millionMessagesSecondSample(2))
sg.AddSample(millionMessagesSecondSample(3))
seconds := sg.End.Sub(sg.Start).Seconds()
if seconds != sg.Seconds() || seconds != 3.0 {
t.Fatal("Expected aggregate seconds to be 3.0 seconds")
}
}
func TestGroupRate(t *testing.T) {
sg := NewSampleGroup()
sg.AddSample(millionMessagesSecondSample(1))
sg.AddSample(millionMessagesSecondSample(2))
sg.AddSample(millionMessagesSecondSample(3))
if sg.Rate() != Million*2 {
t.Fatal("Expected MsgRate at 2 million msg/sec")
}
}
func TestGroupThoughput(t *testing.T) {
sg := NewSampleGroup()
sg.AddSample(millionMessagesSecondSample(1))
sg.AddSample(millionMessagesSecondSample(2))
sg.AddSample(millionMessagesSecondSample(3))
if sg.Throughput() != 2*Million*MsgSize {
t.Fatalf("Expected througput at %d million bytes/sec", 2*MsgSize)
}
}
func TestMinMaxRate(t *testing.T) {
sg := NewSampleGroup()
sg.AddSample(millionMessagesSecondSample(1))
sg.AddSample(millionMessagesSecondSample(2))
sg.AddSample(millionMessagesSecondSample(3))
if sg.MinRate() != sg.MaxRate() {
t.Fatal("Expected MinRate == MaxRate")
}
}
func TestAvgRate(t *testing.T) {
sg := NewSampleGroup()
sg.AddSample(millionMessagesSecondSample(1))
sg.AddSample(millionMessagesSecondSample(2))
sg.AddSample(millionMessagesSecondSample(3))
if sg.MinRate() != sg.AvgRate() {
t.Fatal("Expected MinRate == AvgRate")
}
}
func TestStdDev(t *testing.T) {
sg := NewSampleGroup()
sg.AddSample(millionMessagesSecondSample(1))
sg.AddSample(millionMessagesSecondSample(2))
sg.AddSample(millionMessagesSecondSample(3))
if sg.StdDev() != 0.0 {
t.Fatal("Expected stddev to be zero")
}
}
func TestBenchSetup(t *testing.T) {
bench := NewBenchmark("test", 1, 1)
bench.AddSubSample(millionMessagesSecondSample(1))
bench.AddPubSample(millionMessagesSecondSample(1))
bench.Close()
if len(bench.RunID) == 0 {
t.Fatal("Bench doesn't have a RunID")
}
if len(bench.Pubs.Samples) != 1 {
t.Fatal("Expected one publisher")
}
if len(bench.Subs.Samples) != 1 {
t.Fatal("Expected one subscriber")
}
if bench.MsgCnt != 2*Million {
t.Fatal("Expected 2 million msgs")
}
if bench.IOBytes != 2*Million*MsgSize {
t.Fatalf("Expected %d million bytes", 2*MsgSize)
}
if bench.Duration() != time.Second {
t.Fatal("Expected duration to be 1 second")
}
}
func makeBench(subs, pubs int) *Benchmark {
bench := NewBenchmark("test", subs, pubs)
for i := 0; i < subs; i++ {
bench.AddSubSample(millionMessagesSecondSample(1))
}
for i := 0; i < pubs; i++ {
bench.AddPubSample(millionMessagesSecondSample(1))
}
bench.Close()
return bench
}
func TestCsv(t *testing.T) {
bench := makeBench(1, 1)
csv := bench.CSV()
lines := strings.Split(csv, "\n")
if len(lines) != 4 {
t.Fatal("Expected 4 lines of output from the CSV string")
}
fields := strings.Split(lines[1], ",")
if len(fields) != 7 {
t.Fatal("Expected 7 fields")
}
}
func TestBenchStrings(t *testing.T) {
bench := makeBench(1, 1)
s := bench.Report()
lines := strings.Split(s, "\n")
if len(lines) != 4 {
t.Fatal("Expected 3 lines of output: header, pub, sub, empty")
}
bench = makeBench(2, 2)
s = bench.Report()
lines = strings.Split(s, "\n")
if len(lines) != 10 {
fmt.Printf("%q\n", s)
t.Fatal("Expected 11 lines of output: header, pub header, pub x 2, stats, sub headers, sub x 2, stats, empty")
}
}
func TestMsgsPerClient(t *testing.T) {
zero := MsgsPerClient(0, 0)
if len(zero) != 0 {
t.Fatal("Expected 0 length for 0 clients")
}
onetwo := MsgsPerClient(1, 2)
if len(onetwo) != 2 || onetwo[0] != 1 || onetwo[1] != 0 {
t.Fatal("Expected uneven distribution")
}
twotwo := MsgsPerClient(2, 2)
if len(twotwo) != 2 || twotwo[0] != 1 || twotwo[1] != 1 {
t.Fatal("Expected even distribution")
}
threetwo := MsgsPerClient(3, 2)
if len(threetwo) != 2 || threetwo[0] != 2 || threetwo[1] != 1 {
t.Fatal("Expected uneven distribution")
}
}