-
Notifications
You must be signed in to change notification settings - Fork 5
/
run_test.go
322 lines (282 loc) · 7.5 KB
/
run_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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package main
import (
"fmt"
cfg "github.com/mrlsd/go-benchmark-app/config"
"os"
"os/exec"
"strings"
"testing"
)
const AB_RESULT = `
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Finished 300 requests
Server Software:
Server Hostname: localhost
Server Port: 3000
Document Path: /123
Document Length: 19 bytes
Concurrency Level: 100
Time taken for tests: 0.027 seconds
Complete requests: 300
Failed requests: 0
Non-2xx responses: 300
Total transferred: 38400 bytes
HTML transferred: 5700 bytes
Requests per second: 11038.75 [#/sec] (mean)
Time per request: 9.059 [ms] (mean)
Time per request: 0.091 [ms] (mean, across all concurrent requests)
Transfer rate: 1379.84 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 3 0.9 3 5
Processing: 1 5 1.7 5 8
Waiting: 1 3 1.5 4 8
Total: 5 8 1.7 7 13
Percentage of the requests served within a certain time (ms)
50% 7
66% 8
75% 8
80% 9
90% 10
95% 11
98% 12
99% 13
100% 13 (longest request)
==>> SIEGE
Transactions: 146229 hits
Availability: 99.63 %
Elapsed time: 9.11 secs
Data transferred: 2.65 MB
Response time: 0.01 secs
Transaction rate: 16051.48 trans/sec
Throughput: 0.29 MB/sec
Concurrency: 91.92
Successful transactions: 0
Failed transactions: 546
Longest transaction: 0.31
Shortest transaction: 0.00
`
const WRK_RESULT = `
Running 10s test @ http://localhost:3000/123
100 threads and 5000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 443.73us 701.77us 24.24ms 93.93%
Req/Sec 29.50k 3.64k 41.13k 73.04%
Latency Distribution
50% 269.00us
75% 483.00us
90% 0.86ms
99% 4.21ms
599207 requests in 10.10s, 73.15MB read
Non-2xx or 3xx responses: 599207
Requests/sec: 59353.58
Transfer/sec: 7.25MB
`
const SIEGE_RESULT = `
Transactions: 146229 hits
Availability: 99.63 %
Elapsed time: 9.11 secs
Data transferred: 2.65 MB
Response time: 0.01 secs
Transaction rate: 16051.48 trans/sec
Throughput: 0.29 MB/sec
Concurrency: 91.92
Successful transactions: 0
Failed transactions: 546
Longest transaction: 0.31
Shortest transaction: 0.00
`
// Alias for success runned command
var runCommandSuccess = func(c string, args ...string) ([]byte, error) {
println("\n => runCommandSuccess")
switch c {
case cfg.AB_BENCH:
return []byte(AB_RESULT), nil
case cfg.WRK_BENCH:
return []byte(WRK_RESULT), nil
case cfg.SIEGE_BENCH:
return []byte(SIEGE_RESULT), nil
}
return []byte(""), nil
}
// Alias for success comand execution
// but wrong output results
var runCommandSuccessButWronOutput = func(c string, args ...string) ([]byte, error) {
println(" => runCommandSuccessButWronOutput")
return []byte("test"), nil
}
// Alias for failed runned command
var runCommandFailed = func(c string, args ...string) ([]byte, error) {
println(" => runCommandFailed")
return []byte("test"), fmt.Errorf("test %s", "test")
}
// TestRunBenchmarks - with basic cinfig
func TestRunBenchmarks(t *testing.T) {
RunBenchmarks = runBenchmarks
initConfig := &cfg.Config{}
config, err := cfg.LoadConfig(cfg.ConfigFile, initConfig)
if err != nil {
t.Fatal(err)
}
// Simple check for runCommand
_, _ = runCommand("/bin/bash")
RunCommand = runCommandSuccess
config.Try = 1
config.WaitToRun = 0
config.Delay = 0
_, err = RunBenchmarks(config)
if err != nil {
if !strings.Contains(err.Error(), "no such file or directory") {
t.Fatal(err)
}
}
}
// TestRunBenchmarksWithWrongAppPath - test with basic config
// and wrong App Path
func TestRunBenchmarksWithWrongAppPath(t *testing.T) {
RunBenchmarks = runBenchmarks
initConfig := &cfg.Config{}
config, err := cfg.LoadConfig(cfg.ConfigFile, initConfig)
if err != nil {
t.Fatal(err)
}
config.Try = 1
config.WaitToRun = 0
config.Delay = 0
if len(config.App) > 0 {
config.App[0].Path = "test/test"
} else {
t.Fatal("You should have at least one App")
}
_, err = RunBenchmarks(config)
if err == nil {
t.Fatal("Unexpected exec start result")
}
}
// TestRunBenchmarksWithWrongParams - with basic config
// but some wrong params
func TestRunBenchmarksWithWrongParams(t *testing.T) {
RunBenchmarks = runBenchmarks
initConfig := &cfg.Config{}
config, err := cfg.LoadConfig(cfg.ConfigFile, initConfig)
if err != nil {
t.Fatal(err)
}
// All parameters correct
config.Ab.Requests = 1
config.Ab.Concurency = 1
config.Wrk.Threads = 1
config.Wrk.Connections = 1
config.Wrk.Duration = 1
config.Siege.Concurrent = 1
config.Siege.Time = 1
config.Try = 1
config.WaitToRun = 0
config.Delay = 0
// Re-init app
config.App = []cfg.AppConfig{{
Title: "Test Bash",
Path: "/bin/bash",
}}
// Success benchmarks
RunCommand = runCommandSuccess
_, err = RunBenchmarks(config)
if err != nil {
_, ok := err.(*os.PathError)
if !ok {
t.Fatal(err)
}
}
// Failed benchmarks
RunCommand = runCommandFailed
_, err = RunBenchmarks(config)
if err == nil {
_, ok := err.(*os.PathError)
if !ok {
t.Fatal("Unexpected exec for runCommandFailed")
}
}
// Return to Success Run command
RunCommand = runCommandSuccess
// Wrong AB Concurency parameter
abConfig := *config
abConfig.Ab.Concurency = 0
_, err = RunBenchmarks(&abConfig)
if err == nil {
_, ok := err.(*os.PathError)
if !ok {
t.Fatal("Unexpected exec for abConfig")
}
}
// Wrong WRK Connections parameter
wrkConfig := *config
wrkConfig.Wrk.Connections = 0
_, err = RunBenchmarks(&wrkConfig)
if err == nil {
_, ok := err.(*os.PathError)
if !ok {
t.Fatal("Unexpected exec for wrkConfig")
}
}
// Run Verbose
cfg.Cfg.Verbose = true
// Wrong Siege Concurrent parameter
siegeConfig := *config
siegeConfig.Siege.Concurrent = 0
_, err = RunBenchmarks(&siegeConfig)
if err == nil {
_, ok := err.(*os.PathError)
if !ok {
t.Fatal("Unexpected exec for siegeConfig")
}
}
//cfg.Cfg.Verbose = false
// Simulate Wrong Kill Process
KillProcess = func(cmd *exec.Cmd) error {
return fmt.Errorf("test %s", "test")
}
_, err = RunBenchmarks(config)
if err == nil {
t.Fatal("Unexpected exec for KillProcess")
}
}
// TestRunBenchmarksWrongParse - test wron output results
// for results parsing test
func TestRunBenchmarksWrongParse(t *testing.T) {
RunBenchmarks = runBenchmarks
initConfig := &cfg.Config{}
config, err := cfg.LoadConfig(cfg.ConfigFile, initConfig)
if err != nil {
t.Fatal(err)
}
// All parameters correct
config.Ab.Requests = 1
config.Ab.Concurency = 1
config.Wrk.Threads = 1
config.Wrk.Connections = 1
config.Wrk.Duration = 1
config.Siege.Concurrent = 1
config.Siege.Time = 1
config.Try = 1
config.WaitToRun = 0
config.Delay = 0
// Re-init app
config.App = []cfg.AppConfig{{
Title: "Test Bash",
Path: "/bin/bash",
}}
RunCommand = runCommandSuccessButWronOutput
config.Try = 1
config.WaitToRun = 0
config.Delay = 0
_, err = RunBenchmarks(config)
if err == nil {
t.Fatal(fmt.Errorf("Unexpected result for: %s", "runCommandSuccessButWronOutput"))
}
}