Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print the percentile of time spent on each request #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions ab-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/jessevdk/go-flags"
statslib "github.com/montanaflynn/stats"
"github.com/schollz/progressbar/v2"
)

Expand Down Expand Up @@ -74,6 +75,17 @@ func exitWithErrorMsg(exitCode int, message string, replacements ...interface{})
os.Exit(exitCode)
}

type RequestCostTimeList struct {
statslib.Float64Data
locker *sync.Mutex
}

func (l *RequestCostTimeList) Insert(v float64) {
l.locker.Lock()
l.Float64Data = append(l.Float64Data, v)
l.locker.Unlock()
}

func main() {
var bar *progressbar.ProgressBar

Expand Down Expand Up @@ -174,7 +186,7 @@ func main() {

}

var stats struct {
var stats = struct {
Requests int64
RequestsCompleted int64
RequestsCompletedCode [1000]int64
Expand All @@ -184,7 +196,8 @@ func main() {
BytesTransferred int64
StartTime time.Time
EndTime time.Time
}
RequestsCostData *RequestCostTimeList
}{}

totalRequests := mainOpts.Bursts * mainOpts.Requests

Expand Down Expand Up @@ -232,6 +245,7 @@ func main() {
// start benchmarking

stats.StartTime = time.Now()
stats.RequestsCostData = &RequestCostTimeList{locker: new(sync.Mutex), Float64Data: statslib.Float64Data{}}

for b := 0; b < mainOpts.Bursts && stopChan != nil; b++ {
var requestsLeft int64
Expand Down Expand Up @@ -261,6 +275,7 @@ func main() {

var err error
var resp *http.Response
var startTheReq = time.Now()
req, err := http.NewRequest("GET", mainOpts.Args.Url, nil)
if err == nil {
req.Header = http.Header(headers)
Expand Down Expand Up @@ -318,19 +333,18 @@ func main() {

resp.Body.Close()


if errWhileReading {
continue
}

atomic.AddInt64(&stats.RequestsCompleted, 1)
stats.RequestsCostData.Insert(float64(time.Since(startTheReq) / time.Millisecond))
if resp.StatusCode >= 0 && resp.StatusCode <= 999 {
atomic.AddInt64(&stats.RequestsCompletedCode[resp.StatusCode], 1)
} else {
atomic.AddInt64(&stats.RequestsCompletedCode[0], 1)
}


}
wg.Done()
}()
Expand Down Expand Up @@ -381,6 +395,12 @@ func main() {
fmt.Printf("Time per request: %s\n", timePerReq)
}

fmt.Println("Percentage of the requests served within a certain time (ms)")
for _, p := range []int{50, 75, 90, 95, 99, 100} {
v, _ := stats.RequestsCostData.Percentile(float64(p))
fmt.Printf("%3d%%\t%g\n", p, v)
}

if mainOpts.ShowErrors {
close(errChan)
errWg.Wait() // wait until all errors from the errChan have been set to the errMap
Expand Down