Skip to content

Commit

Permalink
report: Add -hist option for -type=json
Browse files Browse the repository at this point in the history
  • Loading branch information
fxkr committed Apr 24, 2019
1 parent 7bf09f4 commit 2de0f61
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ Options:
--type Which report type to generate (text | json | hist[buckets]).
[default: text]

--hist Histogram buckets, e.g.: '[0,1ms,10ms]'

--every Write the report to --output at every given interval (e.g 100ms)
The default of 0 means the report will only be written after
all results have been processed. [default: 0]
Expand Down Expand Up @@ -434,6 +436,7 @@ The `Error Set` shows a unique set of errors returned by all issued requests. Th
"99th": 3530000,
"max": 3660505
},
"hist": [0, 0, 10, 46, 44, 0],
"bytes_in": {
"total": 606700,
"mean": 6067
Expand All @@ -457,6 +460,8 @@ The `Error Set` shows a unique set of errors returned by all issued requests. Th
}
```

If the `-hist` parameter is not present, the `hist` field is omitted.

#### `report -type=hist`

Computes and prints a text based histogram for the given buckets.
Expand Down
6 changes: 6 additions & 0 deletions lib/histogram.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vegeta

import (
"encoding/json"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -35,6 +36,11 @@ func (h *Histogram) Add(r *Result) {
h.Counts[i]++
}

// MarshalJSON returns a JSON encoding of the list of counts.
func (h *Histogram) MarshalJSON() ([]byte, error) {
return json.Marshal(h.Counts)
}

// Nth returns the nth bucket represented as a string.
func (bs Buckets) Nth(i int) (left, right string) {
if i >= len(bs)-1 {
Expand Down
6 changes: 6 additions & 0 deletions lib/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
type Metrics struct {
// Latencies holds computed request latency metrics.
Latencies LatencyMetrics `json:"latencies"`
// Histogram, only if requested
Histogram *Histogram `json:"hist,omitempty"`
// BytesIn holds computed incoming byte metrics.
BytesIn ByteMetrics `json:"bytes_in"`
// BytesOut holds computed outgoing byte metrics.
Expand Down Expand Up @@ -75,6 +77,10 @@ func (m *Metrics) Add(r *Result) {
m.Errors = append(m.Errors, r.Error)
}
}

if m.Histogram != nil {
m.Histogram.Add(r)
}
}

// Close implements the Close method of the Report interface by computing
Expand Down
11 changes: 9 additions & 2 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func reportCmd() command {
typ := fs.String("type", "text", "Report type to generate [text, json, hist[buckets]]")
every := fs.Duration("every", 0, "Report interval")
output := fs.String("output", "stdout", "Output file")
hist := fs.String("hist", "", "Histogram buckets, e.g.: \"[0,1ms,10ms]\"")

fs.Usage = func() {
fmt.Fprintln(os.Stderr, reportUsage)
Expand All @@ -51,11 +52,11 @@ func reportCmd() command {
if len(files) == 0 {
files = append(files, "stdin")
}
return report(files, *typ, *output, *every)
return report(files, *typ, *output, *every, *hist)
}}
}

func report(files []string, typ, output string, every time.Duration) error {
func report(files []string, typ, output string, every time.Duration, histStr string) error {
if len(typ) < 4 {
return fmt.Errorf("invalid report type: %s", typ)
}
Expand Down Expand Up @@ -85,6 +86,12 @@ func report(files []string, typ, output string, every time.Duration) error {
rep, report = vegeta.NewTextReporter(&m), &m
case "json":
var m vegeta.Metrics
if histStr != "" {
m.Histogram = &vegeta.Histogram{}
if err := m.Histogram.Buckets.UnmarshalText([]byte(histStr)); err != nil {
return err
}
}
rep, report = vegeta.NewJSONReporter(&m), &m
case "hist":
if len(typ) < 6 {
Expand Down

0 comments on commit 2de0f61

Please sign in to comment.