-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotchkiss_test.go
228 lines (208 loc) · 5.76 KB
/
otchkiss_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
package otchkiss
import (
"context"
"errors"
"os"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/ryo-yamaoka/otchkiss/result"
"github.com/ryo-yamaoka/otchkiss/setting"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testRequesterImpl struct{}
func (tr *testRequesterImpl) Init() error {
return nil
}
func (tr *testRequesterImpl) RequestOne(_ context.Context) error {
return nil
}
func (tr *testRequesterImpl) Terminate() error {
return nil
}
func TestNew(t *testing.T) {
testCases := map[string]struct {
requester Requester
wantOtchkiss *Otchkiss
wantError assert.ErrorAssertionFunc
}{
"ok": {
requester: &testRequesterImpl{},
wantOtchkiss: &Otchkiss{
Requester: &testRequesterImpl{},
Setting: &setting.Setting{
MaxConcurrent: 1,
MaxRPS: 1,
RunDuration: 5 * time.Second,
WarmUpTime: 5 * time.Second,
},
},
wantError: assert.NoError,
},
"ng: nil": {
requester: nil,
wantOtchkiss: nil,
wantError: assert.Error,
},
}
for tn, tc := range testCases {
tc := tc
t.Run(tn, func(t *testing.T) {
os.Args = []string{"dummy"} // Avoid flag parse error
ot, err := New(tc.requester)
tc.wantError(t, err)
if tc.wantOtchkiss != nil {
assert.Equal(t, tc.wantOtchkiss.Requester, ot.Requester)
assert.Equal(t, tc.wantOtchkiss.Setting, ot.Setting)
assert.NotNil(t, ot.Result)
return
}
assert.Nil(t, ot)
})
}
}
func TestFromConfig(t *testing.T) {
t.Parallel()
type args struct {
requester Requester
setting *setting.Setting
cap int
}
testCases := map[string]struct {
args args
wantOtchkiss *Otchkiss
wantError assert.ErrorAssertionFunc
}{
"ok": {
args: args{
requester: &testRequesterImpl{},
setting: &setting.Setting{
MaxConcurrent: 2,
RunDuration: 2 * time.Second,
WarmUpTime: 10 * time.Second,
},
cap: 1,
},
wantOtchkiss: &Otchkiss{
Requester: &testRequesterImpl{},
Setting: &setting.Setting{
MaxConcurrent: 2,
RunDuration: 2 * time.Second,
WarmUpTime: 10 * time.Second,
},
},
wantError: assert.NoError,
},
"ng: requester nil": {
args: args{
requester: nil,
setting: &setting.Setting{
MaxConcurrent: 2,
RunDuration: 2 * time.Second,
WarmUpTime: 10 * time.Second,
},
cap: 1,
},
wantOtchkiss: nil,
wantError: assert.Error,
},
"ng: setting nil": {
args: args{
requester: &testRequesterImpl{},
setting: nil,
cap: 1,
},
wantOtchkiss: nil,
wantError: assert.Error,
},
"ng: invalid capacity": {
args: args{
requester: &testRequesterImpl{},
setting: &setting.Setting{
MaxConcurrent: 2,
RunDuration: 2 * time.Second,
WarmUpTime: 10 * time.Second,
},
cap: -1,
},
wantOtchkiss: nil,
wantError: assert.Error,
},
}
for tn, tc := range testCases {
tc := tc
t.Run(tn, func(t *testing.T) {
t.Parallel()
ot, err := FromConfig(tc.args.requester, tc.args.setting, tc.args.cap)
tc.wantError(t, err)
if tc.wantOtchkiss != nil {
assert.Equal(t, tc.wantOtchkiss.Requester, ot.Requester)
assert.Equal(t, tc.wantOtchkiss.Setting, ot.Setting)
require.NotNil(t, ot.Result)
return
}
assert.Nil(t, ot)
})
}
}
func TestReport(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
setting *setting.Setting
templ string
wantReport string
wantError assert.ErrorAssertionFunc
}{
"default": {
setting: &setting.Setting{
MaxConcurrent: 1,
MaxRPS: 1,
RunDuration: 2 * time.Second,
WarmUpTime: 3 * time.Second,
},
templ: defaultReportTemplate,
wantReport: "\n[Setting]\n* warm up time: 3s\n* duration: 2s\n* max concurrent: 1\n* max RPS: 1\n\n[Request]\n* total: 3\n* succeeded: 2\n* failed: 1\n* error rate: 33.3 %\n* RPS: 1.5\n\n[Latency]\n* max: 3,000 ms\n* min: 1,000 ms\n* avg: 2,000 ms\n* med: 1,000 ms\n* 99th percentile: 2,000 ms\n* 90th percentile: 2,000 ms\n\n[Histogram]\n1s-1.222222222s 33.3% █████████████████████████▏ 1\n1.222222222s-1.444444444s 0% ▏ \n1.444444444s-1.666666666s 0% ▏ \n1.666666666s-1.888888888s 0% ▏ \n1.888888888s-2.111111111s 33.3% █████████████████████████▏ 1\n2.111111111s-2.333333333s 0% ▏ \n2.333333333s-2.555555555s 0% ▏ \n2.555555555s-2.777777777s 0% ▏ \n2.777777777s-3s 33.3% █████████████████████████▏ 1\n\n",
wantError: assert.NoError,
},
"user format": {
setting: &setting.Setting{
WarmUpTime: 3 * time.Second,
},
templ: "{{.WarmUpTime}}",
wantReport: "3s",
wantError: assert.NoError,
},
"empty": {
setting: &setting.Setting{},
templ: "",
wantReport: "",
wantError: assert.Error,
},
}
for tn, tc := range testCases {
tn, tc := tn, tc
t.Run(tn, func(t *testing.T) {
t.Parallel()
r, err := result.WithCapacity(3)
require.NoError(t, err)
ot := Otchkiss{
Result: r,
Setting: tc.setting,
}
ot.Result.AppendSuccess(1)
ot.Result.AppendSuccess(2)
ot.Result.AppendFail(3, errors.New("err1"))
report, err := ot.TemplateReport(tc.templ)
tc.wantError(t, err)
diff := cmp.Diff(tc.wantReport, report)
assert.Empty(t, diff)
if tn == "default" {
report, err := ot.Report()
tc.wantError(t, err)
diff := cmp.Diff(tc.wantReport, report)
assert.Empty(t, diff)
}
})
}
}