diff --git a/README.md b/README.md index ac3a942..f2f391c 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ The funny part - I did some benchmark to the client tester tool and not to the s ###Siege: - $>siege -b -t10S -c500 http://localhost:80/ + $ siege -b -t10S -c500 http://localhost:80/ ** SIEGE 2.70 ** Preparing 500 concurrent users for battle. @@ -39,7 +39,7 @@ The funny part - I did some benchmark to the client tester tool and not to the s ###GoBench: - $>gobench -k=true -u http://localhost:80 -c 500 -t 10 + $ gobench -k=true -u http://localhost:80 -c 500 -t 10 Dispatching 500 clients Waiting for results... @@ -62,22 +62,22 @@ Usage ================ 1. download gobench - ```go get github.com/cmpxchg16/gobench``` + ```go install github.com/jeroenvermeulen/gobench@latest``` 1. run some http server on port 80 2. run gobench for HTTP GET - ```$>gobench -u http://localhost:80 -k=true -c 500 -t 10``` + ```$ gobench -u http://localhost:80 -k=true -c 500 -t 10``` 3. run gobench for HTTP POST - ```$>gobench -u http://localhost:80 -k=true -c 500 -t 10 -d /tmp/post``` + ```$ gobench -u http://localhost:80 -k=true -c 500 -t 10 -d /tmp/post``` Notes ================ 2. Because it's a test tool, in HTTPS the ceritificate verification is insecure -3. use Go >= 1.5 (fasthttp require it) +3. use Go >= 1.20 (fasthttp requires it) Help ================ diff --git a/gobench.go b/gobench.go index 21d1ca3..726c370 100644 --- a/gobench.go +++ b/gobench.go @@ -15,10 +15,13 @@ import ( "sync" "sync/atomic" "time" + "math/rand" + "crypto/tls" "github.com/valyala/fasthttp" ) +// Global variables var ( requests int64 period int64 @@ -31,8 +34,12 @@ var ( readTimeout int authHeader string userAgent string + acceptEnc string + randomize bool + insecure bool ) +// Benchmark Client Configuration type Configuration struct { urls []string method string @@ -41,7 +48,8 @@ type Configuration struct { period int64 keepAlive bool authHeader string - userAgent string + acceptEnc string + randomize bool myClient fasthttp.Client } @@ -91,7 +99,10 @@ func init() { flag.IntVar(&writeTimeout, "tw", 5000, "Write timeout (in milliseconds)") flag.IntVar(&readTimeout, "tr", 5000, "Read timeout (in milliseconds)") flag.StringVar(&authHeader, "auth", "", "Authorization header") - flag.StringVar(&userAgent, "agent", "", "User-Agent") + flag.StringVar(&userAgent, "agent", "", "User-Agent header") + flag.StringVar(&acceptEnc, "accept", "", "Accept-Encoding header") + flag.BoolVar(&randomize, "random", false, "Randomize URL order") + flag.BoolVar(&insecure, "insecure", false, "Skip verifing SSL certificate") } func printResults(results map[int]*Result, startTime time.Time) { @@ -179,7 +190,8 @@ func NewConfiguration() *Configuration { keepAlive: keepAlive, requests: int64((1 << 63) - 1), authHeader: authHeader, - userAgent: userAgent} + acceptEnc: acceptEnc, + randomize: randomize} if period != -1 { configuration.period = period @@ -232,9 +244,12 @@ func NewConfiguration() *Configuration { configuration.postData = data } + configuration.myClient.ReadBufferSize = 16384 configuration.myClient.ReadTimeout = time.Duration(readTimeout) * time.Millisecond configuration.myClient.WriteTimeout = time.Duration(writeTimeout) * time.Millisecond configuration.myClient.MaxConnsPerHost = clients + configuration.myClient.Name = userAgent + configuration.myClient.TLSConfig = &tls.Config{ InsecureSkipVerify: insecure } configuration.myClient.Dial = MyDialer() @@ -255,8 +270,15 @@ func MyDialer() func(address string) (conn net.Conn, err error) { } func client(configuration *Configuration, result *Result, done *sync.WaitGroup) { + rand := rand.New(rand.NewSource(time.Now().UnixNano())) for result.requests < configuration.requests { - for _, tmpUrl := range configuration.urls { + var tmpUrls []string; + if (configuration.randomize) { + tmpUrls = []string{configuration.urls[rand.Intn(len(configuration.urls))]} + } else { + tmpUrls = configuration.urls + } + for _, tmpUrl := range tmpUrls { req := fasthttp.AcquireRequest() @@ -273,8 +295,8 @@ func client(configuration *Configuration, result *Result, done *sync.WaitGroup) req.Header.Set("Authorization", configuration.authHeader) } - if len(configuration.userAgent) > 0 { - req.Header.Set("User-Agent", configuration.userAgent) + if len(configuration.acceptEnc) > 0 { + req.Header.Set("Accept-Encoding", configuration.acceptEnc) } req.SetBody(configuration.postData) @@ -287,6 +309,7 @@ func client(configuration *Configuration, result *Result, done *sync.WaitGroup) fasthttp.ReleaseResponse(resp) if err != nil { + fmt.Printf("ERROR: %+v\n", err) result.networkFailed++ continue }