-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmain.go
208 lines (197 loc) · 5.14 KB
/
main.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
package main
import (
"errors"
"github.com/cheggaaa/pb/v3"
"github.com/crossoverJie/ptg/meta"
"github.com/crossoverJie/ptg/model"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"io/ioutil"
"log"
"os"
"os/signal"
"runtime"
"strings"
)
// init bind variable
var (
target string
//respCh chan *meta.Response
thread int
duration int64
method string
bodyPath string
body string
headerSlice cli.StringSlice
headerMap map[string]string
protocol string // http/grpc
protocolFile string // xx/xx/xx.proto
fqn string // fully-qualified method name:[package.Service.Method]
)
var (
//totalRequestTime time.Duration
//totalResponseSize int
//SlowRequestTime time.Duration
//FastRequestTime = time.Minute
//ErrorCount int32
Bar *pb.ProgressBar
)
const (
PbTmpl = `{{ green "Requesting:" }} {{string . "target" | blue}} {{ bar . "<" "-" (cycle . "↖" "↗" "↘" "↙" ) "." ">"}} {{speed . | rndcolor }} {{percent .}}`
Http = "http"
Grpc = "grpc"
)
func main() {
var count int
app := &cli.App{Name: "ptg", Usage: "Performance testing tool (Go)",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "thread",
Usage: "-t 10",
//Value: 1000,
DefaultText: "1",
Aliases: []string{"t"},
Required: true,
Destination: &thread,
},
&cli.StringFlag{
Name: "Request protocol",
Usage: "-proto http/grpc",
DefaultText: "http",
Aliases: []string{"proto"},
Required: true,
Destination: &protocol,
},
&cli.StringFlag{
Name: "protocol buffer file path",
Usage: "-pf /file/order.proto",
DefaultText: "",
Aliases: []string{"pf"},
Required: false,
Destination: &protocolFile,
},
&cli.StringFlag{
Name: "fully-qualified method name",
Usage: "-fqn package.Service.Method",
DefaultText: "",
Aliases: []string{"fqn"},
Required: false,
Destination: &fqn,
},
&cli.Int64Flag{
Name: "duration",
Usage: "-d 10s",
DefaultText: "Duration of test in seconds, Default 10s",
Aliases: []string{"d"},
Required: false,
Destination: &duration,
},
&cli.IntFlag{
Name: "request count",
Usage: "-c 100",
DefaultText: "100",
Aliases: []string{"c"},
Required: false,
Destination: &count,
},
&cli.StringFlag{
Name: "HTTP method",
Usage: "-m GET",
DefaultText: "GET",
Aliases: []string{"M"},
Required: false,
Destination: &method,
},
&cli.StringFlag{
Name: "bodyPath",
Usage: "-body bodyPath.json",
DefaultText: "",
Aliases: []string{"body"},
Required: false,
Destination: &bodyPath,
},
&cli.StringSliceFlag{
Name: "header",
Aliases: []string{"H"},
Usage: "HTTP header to add to request, e.g. -H \"Content-Type: application/json\"",
Required: false,
DefaultText: "",
Destination: &headerSlice,
},
&cli.StringFlag{
Name: "target",
Usage: "http://gobyexample.com/grpc:127.0.0.1:5000",
DefaultText: "",
Aliases: []string{"tg"},
Required: true,
Destination: &target,
},
},
Action: func(c *cli.Context) error {
color.White("thread: %v, duration: %v, count %v", thread, duration, count)
runtime.GOMAXPROCS(runtime.NumCPU() + thread)
// ##########App init##########
if count == 0 && duration == 0 {
return errors.New("request count and duration must choose one")
}
if count > 0 && duration > 0 {
return errors.New("request count and duration can only choose one")
}
if method == "" {
method = "GET"
}
if bodyPath != "" {
bytes, err := ioutil.ReadFile(bodyPath)
if err != nil {
color.Red("could not read file: %s", bodyPath)
return err
}
body = string(bytes)
}
if headerSlice.Value() != nil {
headerMap = make(map[string]string, len(headerSlice.Value()))
for _, s := range headerSlice.Value() {
splitN := strings.SplitN(s, ":", 2)
headerMap[splitN[0]] = splitN[1]
}
}
meta.NewResult()
newMeta := meta.NewMeta(target, method, bodyPath, body, protocol, protocolFile, fqn, thread, duration, &headerSlice, headerMap)
var model model.Model
if count > 0 {
respCh := make(chan *meta.Response, count)
newMeta.SetRespCh(respCh)
model = NewCountModel(count)
Bar = pb.ProgressBarTemplate(PbTmpl).Start(count)
} else {
// 防止写入 goroutine 阻塞,导致泄露。
respCh := make(chan *meta.Response, 3*thread)
newMeta.SetRespCh(respCh)
model = NewDurationModel(duration)
Bar = pb.ProgressBarTemplate(PbTmpl).Start(int(duration))
}
Bar.Set("target", target).
SetWidth(120)
// ##########App init##########
// shutdown
signCh := make(chan os.Signal, 1)
signal.Notify(signCh, os.Interrupt)
go func() {
select {
case <-signCh:
color.Red("shutdown....")
model.Shutdown()
}
}()
model.Init()
model.Run()
model.Finish()
model.PrintSate()
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}