Skip to content

Instantly share code, notes, and snippets.

@vearutop
Created July 11, 2019 16:45
Show Gist options
  • Save vearutop/484eb795c15c4119508fe0b18c34e482 to your computer and use it in GitHub Desktop.
Save vearutop/484eb795c15c4119508fe0b18c34e482 to your computer and use it in GitHub Desktop.
pocket load tester
package main
import (
"github.com/VividCortex/gohistogram"
"net/http"
"os"
"time"
)
func main() {
h := gohistogram.NewHistogram(10)
println(os.Args[1])
concurrencyLimit := 50 // Number of simultaneous jobs.
limiter := make(chan struct{}, concurrencyLimit)
start := time.Now()
slow := int64(0)
results := make(chan float64, 100)
go func() {
for f := range results {
if f > 1000 {
slow++
}
h.Add(f)
}
}()
for i := 0; i < 10000; i++ {
limiter <- struct{}{} // Reserve limiter slot.
go func() {
defer func() {
<-limiter // Free limiter slot.
}()
start := time.Now()
req, err := http.NewRequest(http.MethodGet, os.Args[1], nil)
if err != nil {
println(err.Error())
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
println(err.Error())
} else {
err = resp.Body.Close()
if err != nil {
println(err.Error())
}
}
results <- time.Since(start).Seconds() * 1000
}()
if time.Since(start) > 30 * time.Second {
break
}
}
// Wait for goroutines to finish by filling full channel.
for i := 0; i < cap(limiter); i++ {
limiter <- struct{}{}
}
close(results)
println("Request latency distribution in ms:")
println(h.String())
println("Requests with latency more than 1000ms:", slow)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment