forked from hamano/lb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lb.go
277 lines (257 loc) · 5.97 KB
/
lb.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
package main
import (
"os"
"fmt"
"log"
"time"
"math"
"errors"
"runtime"
"reflect"
"github.com/urfave/cli"
)
func worker(wid int,
c *cli.Context,
rx chan string,
tx chan Result,
job Job) {
num := c.Int("n")
num_per_worker := int(math.Ceil(float64(num) / float64(c.Int("c"))))
job.Init(wid, c)
job.Prep(c)
tx <- Result{}
<- rx
if job.GetVerbose() >= 2 {
log.Printf("worker[%d]: starting job\n", wid)
}
var result Result
result.startTime = time.Now()
for i := 0; i < num_per_worker; i++ {
res := job.Request()
if res {
job.IncSuccess()
}
job.IncCount()
}
result.endTime = time.Now()
job.Finish()
result.elapsedTime = result.endTime.Sub(result.startTime).Seconds()
result.wid = wid
result.count = job.GetCount()
result.success = job.GetSuccess()
tx <- result
}
func waitReady(ch chan Result, n int){
for i := 0; i < n; i++ {
<- ch
}
}
func waitResult(ch chan Result, n int) []Result {
results := make([]Result, n)
for i := 0; i < n; i++ {
result := <- ch
results[result.wid] = result
}
return results
}
func reportResult(ctx *cli.Context, results []Result) {
var firstTime time.Time
var lastTime time.Time
var totalRequest int
var successRequest int
for i := range results {
rpq := float64(results[i].count) / results[i].elapsedTime
if ctx.Int("v") >= 2 {
log.Printf("worker[%d]: %.2f [#/sec] time=%.3f\n",
results[i].wid, rpq, results[i].elapsedTime)
}
totalRequest += results[i].count
successRequest += results[i].success
if firstTime.IsZero() || firstTime.After(results[i].startTime) {
firstTime = results[i].startTime
}
if lastTime.Before(results[i].endTime) {
lastTime = results[i].endTime
}
}
takenTime := lastTime.Sub(firstTime).Seconds()
rpq := float64(totalRequest) / takenTime
concurrency := ctx.Int("c")
if ctx.Bool("short") {
printShortResult(concurrency,
int(float64(successRequest) / float64(totalRequest) * 100), rpq);
}else{
printResult(concurrency, totalRequest, successRequest,
int(float64(successRequest) / float64(totalRequest) * 100),
takenTime, rpq,
float64(concurrency) * takenTime * 1000 / float64(totalRequest),
takenTime * 1000 / float64(totalRequest))
}
}
func printResult(
concurrency int,
totalRequest int,
successRequest int,
successRate int,
takenTime float64,
rpq float64,
tpr float64,
tpr_all float64) {
fmt.Printf("Concurrency Level: %d\n", concurrency)
fmt.Printf("Total Requests: %d\n", totalRequest)
fmt.Printf("Success Requests: %d\n", successRequest)
fmt.Printf("Success Rate: %d%%\n", successRate)
fmt.Printf("Time taken for tests: %.3f seconds\n", takenTime)
fmt.Printf("Requests per second: %.2f [#/sec] (mean)\n", rpq)
fmt.Printf("Time per request: %.3f [ms] (mean)\n", tpr)
fmt.Printf("Time per request: %.3f [ms] " +
"(mean, across all concurrent requests)\n", tpr_all)
fmt.Printf("CPU Number: %d\n", runtime.NumCPU())
fmt.Printf("GOMAXPROCS: %d\n", runtime.GOMAXPROCS(0))
}
func printShortResult(
concurrency int,
successRate int,
rpq float64) {
fmt.Printf("%d %.2f %d%%\n", concurrency, rpq, successRate)
}
func checkArgs(c *cli.Context) error {
if len(c.Args()) < 1 {
cli.ShowAppHelp(c)
return errors.New("few args")
}
if ! c.Bool("q") {
fmt.Printf("This is LDAPBench, Version %s\n", c.App.Version)
fmt.Printf("Copyright 2015 Open Source Solution Technology Corporation\n")
fmt.Printf("This software is released under the MIT License.\n")
fmt.Printf("\n")
}
return nil
}
func runBenchmark(c *cli.Context, jobType reflect.Type) {
if ! c.Bool("q") {
fmt.Printf("%s Benchmarking: %s\n",
jobType.Name(), c.Args().First())
}
workerNum := c.Int("c");
tx := make(chan string)
rx := make(chan Result)
for i := 0; i < workerNum; i++ {
job := reflect.New(jobType).Interface().(Job)
go worker(i, c, tx, rx, job)
}
waitReady(rx, workerNum)
// all worker are ready
for i := 0; i < workerNum; i++ {
tx <- "start"
}
results := waitResult(rx, workerNum)
reportResult(c, results)
}
var commonFlags = []cli.Flag {
cli.IntFlag {
Name: "verbose, v",
Value: 0,
Usage: "How much troubleshooting info to print",
},
cli.BoolFlag {
Name: "quiet, q",
Usage: "Quiet flag",
},
cli.IntFlag {
Name: "n",
Value: 1,
Usage: "Number of requests to perform",
},
cli.IntFlag {
Name: "c",
Value: 1,
Usage: "Number of multiple requests to make",
},
cli.StringFlag {
Name: "D",
Value: "cn=Manager,dc=example,dc=com",
Usage: "Bind DN",
},
cli.StringFlag {
Name: "w",
Value: "secret",
Usage: "Bind Secret",
},
cli.StringFlag {
Name: "b",
Value: "dc=example,dc=com",
Usage: "BaseDN",
},
cli.BoolFlag {
Name: "short",
Usage: "short result",
},
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
app := cli.NewApp()
app.Name = "lb"
app.Usage = "LDAP Benchmarking Tool"
app.Version = Version
app.Author = "HAMANO Tsukasa"
app.Email = "[email protected]"
app.Commands = []cli.Command{
{
Name: "add",
Usage: "LDAP ADD Benchmarking",
Before: checkArgs,
Action: Add,
Flags: append(commonFlags, addFlags...),
},
{
Name: "bind",
Usage: "LDAP BIND Benchmarking",
Before: checkArgs,
Action: Bind,
Flags: append(commonFlags, bindFlags...),
},
{
Name: "delete",
Usage: "LDAP DELETE Benchmarking",
Before: checkArgs,
Action: Delete,
Flags: commonFlags,
},
{
Name: "modify",
Usage: "LDAP MODIFY Benchmarking",
Before: checkArgs,
Action: Modify,
Flags: append(commonFlags, modifyFlags...),
},
{
Name: "search",
Usage: "LDAP SEARCH Benchmarking",
Before: checkArgs,
Action: Search,
Flags: append(commonFlags, searchFlags...),
},
{
Name: "setup",
Usage: "Setup SubCommands",
Subcommands: []cli.Command{
{
Name: "base",
Usage: "Add Base Entry",
Before: checkArgs,
Action: setupBase,
Flags: commonFlags,
},
{
Name: "person",
Usage: "Add User Entry",
Before: checkArgs,
Action: setupPerson,
Flags: append(commonFlags, setupPersonFlags...),
},
},
},
}
app.Run(os.Args)
}