Skip to content

Commit

Permalink
report, main, client: Tweak concurrency, reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
shazow committed Dec 17, 2019
1 parent 14d5a2a commit b8ea189
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
20 changes: 13 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
)

type clientStats struct {
mu sync.Mutex
NumTotal int // Number of requests
NumErrors int // Number of errors
TimeTotal time.Duration // Total duration of requests
mu sync.Mutex
NumTotal int // Number of requests
NumErrors int // Number of errors
TimeErrors time.Duration // Duration of error responses specifically
TimeTotal time.Duration // Total duration of requests
Errors map[string]int
}

func (stats *clientStats) Count(err error, elapsed time.Duration) {
Expand All @@ -22,17 +24,21 @@ func (stats *clientStats) Count(err error, elapsed time.Duration) {
stats.NumTotal += 1
if err != nil {
stats.NumErrors += 1
stats.TimeErrors += elapsed

if stats.Errors == nil {
stats.Errors = map[string]int{}
}
stats.Errors[err.Error()] += 1
}
stats.TimeTotal += elapsed
}

const chanBuffer = 20

func NewClient(endpoint string, concurrency int) (*Client, error) {
c := Client{
Endpoint: endpoint,
Concurrency: concurrency,
In: make(chan Request, chanBuffer),
In: make(chan Request, 2*concurrency),
}
return &c, nil
}
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,15 @@ func run(ctx context.Context, options Options) error {
timeout = d
}

if options.Concurrency < 1 {
logger.Info().Int("concurrency", options.Concurrency).Msg("concurrency is less than 1, overriding to 1")
options.Concurrency = 1
}

g, ctx := errgroup.WithContext(ctx)

// responses is closed when clients are shut down
responses := make(chan Response, chanBuffer)
responses := make(chan Response, options.Concurrency*2)

// Launch clients
clients, err := NewClients(options.Args.Endpoints, options.Concurrency, timeout)
Expand Down
7 changes: 5 additions & 2 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ type report struct {
}

func (r *report) Render(w io.Writer) error {
elapsedAvg := r.elapsed / time.Duration(r.requests)
errorRate := float64(r.errors*100) / float64(r.requests)

fmt.Fprintf(w, "\n* Report for %d endpoints:\n", len(r.Clients))
fmt.Fprintf(w, " Completed: %d results with %d total requests\n", r.completed, r.requests)
fmt.Fprintf(w, " Elapsed: %s spent on requests, %s total run time\n", r.elapsed, time.Now().Sub(r.started))
fmt.Fprintf(w, " Errors: %d\n", r.errors)
fmt.Fprintf(w, " Elapsed: %s request avg, %s total run time\n", elapsedAvg, time.Now().Sub(r.started))
fmt.Fprintf(w, " Errors: %d (%0.2f%%)\n", r.errors, errorRate)
fmt.Fprintf(w, " Mismatched: %d\n", r.mismatched)

if r.overloaded > 0 {
Expand Down

0 comments on commit b8ea189

Please sign in to comment.