Skip to content

Commit

Permalink
feat: add support for outputting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
daulet committed Nov 21, 2024
1 parent 2957a32 commit c49565c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"net"
"net/http"
_ "net/http/pprof"
Expand Down Expand Up @@ -47,6 +48,7 @@ var (

autoOpenBrowser = kingpin.Flag("auto-open-browser", "Specify whether auto open browser to show web charts").Bool()
clean = kingpin.Flag("clean", "Clean the histogram bar once its finished. Default is true").Default("true").NegatableBool()
outputErrors = kingpin.Flag("output-errors", "Output errors to file").String()
summary = kingpin.Flag("summary", "Only print the summary without realtime reports").Default("false").Bool()
pprofAddr = kingpin.Flag("pprof", "Enable pprof at special address").Hidden().String()
url = kingpin.Arg("url", "Request url").Required().String()
Expand Down Expand Up @@ -226,6 +228,15 @@ func main() {
}
}

errWriter := io.Discard
if *outputErrors != "" {
errWriter, err = os.Create(*outputErrors)
if err != nil {
errAndExit(err.Error())
return
}
}

clientOpt := ClientOpt{
url: *url,
method: *method,
Expand All @@ -248,7 +259,7 @@ func main() {
host: *host,
}

requester, err := NewRequester(*concurrency, *requests, *duration, reqRate.Limit(), &clientOpt)
requester, err := NewRequester(*concurrency, *requests, *duration, reqRate.Limit(), errWriter, &clientOpt)
if err != nil {
errAndExit(err.Error())
return
Expand Down
5 changes: 3 additions & 2 deletions report.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"github.com/beorn7/perks/histogram"
"github.com/beorn7/perks/quantile"
"math"
"sync"
"time"

"github.com/beorn7/perks/histogram"
"github.com/beorn7/perks/quantile"
)

var quantiles = []float64{0.50, 0.75, 0.90, 0.95, 0.99, 0.999, 0.9999}
Expand Down
12 changes: 10 additions & 2 deletions requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type Requester struct {
clientOpt *ClientOpt
httpClient *fasthttp.HostClient
httpHeader *fasthttp.RequestHeader
errWriter io.Writer

recordChan chan *ReportRecord
closeOnce sync.Once
Expand Down Expand Up @@ -131,7 +132,7 @@ type ClientOpt struct {
host string
}

func NewRequester(concurrency int, requests int64, duration time.Duration, reqRate *rate.Limit, clientOpt *ClientOpt) (*Requester, error) {
func NewRequester(concurrency int, requests int64, duration time.Duration, reqRate *rate.Limit, errWriter io.Writer, clientOpt *ClientOpt) (*Requester, error) {
maxResult := concurrency * 100
if maxResult > 8192 {
maxResult = 8192
Expand All @@ -141,6 +142,7 @@ func NewRequester(concurrency int, requests int64, duration time.Duration, reqRa
reqRate: reqRate,
requests: requests,
duration: duration,
errWriter: errWriter,
clientOpt: clientOpt,
recordChan: make(chan *ReportRecord, maxResult),
}
Expand Down Expand Up @@ -260,7 +262,13 @@ func (r *Requester) DoRequest(req *fasthttp.Request, resp *fasthttp.Response, rr
rr.error = err.Error()
return
}
err = resp.BodyWriteTo(io.Discard)

writeTo := io.Discard
if resp.StatusCode() >= 500 {
writeTo = r.errWriter
_, _ = r.errWriter.Write([]byte(fmt.Sprintf("\n%d %s\n", resp.StatusCode(), rr.cost)))
}
err = resp.BodyWriteTo(writeTo)
if err != nil {
rr.cost = time.Since(startTime) - t1
rr.error = err.Error()
Expand Down

0 comments on commit c49565c

Please sign in to comment.