Skip to content

Commit 302b8e4

Browse files
author
poolqa
committed
add sleep and proxy flag
1 parent d67c5d1 commit 302b8e4

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

bot/bot.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import (
1313
)
1414

1515
type OrderBot struct {
16-
ID int64
17-
Client *fasthttp.Client
18-
procCnt int64
19-
doneCnt int64
20-
gId string
16+
ID int64
17+
Client *fasthttp.Client
18+
SleepInterval time.Duration
19+
procCnt int64
20+
doneCnt int64
21+
gId string
2122
}
2223

2324
func (bot *OrderBot) GetRoutineId() string {
@@ -58,6 +59,7 @@ func (bot *OrderBot) StandBy(ctx context.Context, wg *sync.WaitGroup, runCnt int
5859
err := bot.Client.Do(req, resp)
5960
tps.ETime = time.Now()
6061
if err != nil {
62+
fmt.Printf("error:%v\n", err)
6163
tps.RespStatus = fasthttp.StatusBadRequest
6264
} else {
6365
tps.RespStatus = resp.StatusCode()
@@ -67,6 +69,9 @@ func (bot *OrderBot) StandBy(ctx context.Context, wg *sync.WaitGroup, runCnt int
6769

6870
performance.Push(tps)
6971
bot.doneCnt++
72+
if bot.SleepInterval > 0 {
73+
time.Sleep(bot.SleepInterval)
74+
}
7075
}
7176
wg.Done()
7277
}

flagArg/flag.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func InitFlag(f *FlagArg) {
2424
//flag.BoolVar(&f.ReportDetail, "r", false, "show those routine's requests detail at report log,\nbut it wall use more and more memory.")
2525
flag.BoolVar(&f.PreHeat, "w", false, "waiting all routines stand-by.")
2626
flag.Int64Var(&f.PrintInterval, "i", 1, "print interval for process request count time.")
27+
flag.Int64Var(&f.SleepInterval, "sleep", 0, "sleep msec at every request is finished.")
28+
flag.StringVar(&f.ProxyAddr, "proxy", "", "proxy address.")
2729
flag.BoolVar(&f.Help, "h", false, "this help")
2830

2931
flag.Usage = f.Usage
@@ -40,6 +42,8 @@ type FlagArg struct {
4042
DebugLog bool
4143
ReportDetail bool
4244
PrintInterval int64
45+
SleepInterval int64
46+
ProxyAddr string
4347
Help bool
4448
}
4549

main.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package main
22

33
import (
44
"context"
5+
"crypto/tls"
56
"flag"
67
"fmt"
78
"github.com/valyala/fasthttp"
9+
"github.com/valyala/fasthttp/fasthttpproxy"
810
"golang.org/x/text/language"
911
"golang.org/x/text/message"
1012
"math/rand"
@@ -62,7 +64,15 @@ func main() {
6264

6365
BotArray = make([]BotInterface, flagArg.FlagArgs.RoutineCount)
6466

65-
httpClient := &fasthttp.Client{MaxConnsPerHost: int(flagArg.FlagArgs.RoutineCount)}
67+
httpClient := &fasthttp.Client{
68+
MaxConnsPerHost: int(flagArg.FlagArgs.RoutineCount),
69+
TLSConfig: &tls.Config{
70+
InsecureSkipVerify: true,
71+
},
72+
}
73+
if flagArg.FlagArgs.ProxyAddr != "" {
74+
httpClient.Dial = fasthttpproxy.FasthttpHTTPDialer(flagArg.FlagArgs.ProxyAddr)
75+
}
6676
wg := &sync.WaitGroup{}
6777
done := make(chan struct{})
6878
ctx, runRoutine := context.WithCancel(context.Background())
@@ -75,8 +85,9 @@ func main() {
7585
// request routine start
7686
for idx := int64(0); idx < flagArg.FlagArgs.RoutineCount; idx++ {
7787
BotArray[idx] = &bot.OrderBot{
78-
ID: idx + 1,
79-
Client: httpClient,
88+
ID: idx + 1,
89+
Client: httpClient,
90+
SleepInterval: time.Duration(flagArg.FlagArgs.SleepInterval) * time.Millisecond,
8091
}
8192
wg.Add(1)
8293
go BotArray[idx].StandBy(ctx, wg, flagArg.FlagArgs.RunCount)

0 commit comments

Comments
 (0)