Skip to content

Commit

Permalink
Merge pull request #16 from bojand/pretty_json
Browse files Browse the repository at this point in the history
Pretty json
  • Loading branch information
bojand committed Jun 5, 2018
2 parents 16d9ebc + e29ac01 commit 85341d4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -47,8 +47,9 @@ Options:
-o Output path. If none provided stdout is used.
-O Output type. If none provided, a summary is printed.
"json" outputs the metrics report in JSON format.
"csv" outputs the response metrics in comma-separated values format.
"json" outputs the metrics report in JSON format.
"pretty" outputs the metrics report in pretty JSON format.
-i Comma separated list of proto import paths. The current working directory and the directory
of the protocol buffer file are automatically added to the import list.
Expand Down
3 changes: 2 additions & 1 deletion cmd/grpcannon/main.go
Expand Up @@ -82,8 +82,9 @@ Options:
-o Output path. If none provided stdout is used.
-O Output type. If none provided, a summary is printed.
"json" outputs the metrics report in JSON format.
"csv" outputs the response metrics in comma-separated values format.
"json" outputs the metrics report in JSON format.
"pretty" outputs the metrics report in pretty JSON format.
-i Comma separated list of proto import paths. The current working directory and the directory
of the protocol buffer file are automatically added to the import list.
Expand Down
13 changes: 12 additions & 1 deletion printer/printer.go
Expand Up @@ -42,12 +42,23 @@ func (rp *ReportPrinter) Print(format string) {
rp.printf(buf.String())

rp.printf("\n")
case "json":
case "json", "pretty":
rep, err := json.Marshal(*rp.Report)
if err != nil {
log.Println("error:", err.Error())
return
}

if format == "pretty" {
var out bytes.Buffer
err = json.Indent(&out, rep, "", " ")
if err != nil {
log.Println("error:", err.Error())
return
}
rep = out.Bytes()
}

rp.printf(string(rep))
}
}
Expand Down
47 changes: 26 additions & 21 deletions reporter.go
Expand Up @@ -23,39 +23,44 @@ type Reporter struct {

// Report holds the data for the full test
type Report struct {
Count uint64
Total time.Duration
Average time.Duration
Fastest time.Duration
Slowest time.Duration
Rps float64

ErrorDist map[string]int
StatusCodeDist map[string]int

LatencyDistribution []LatencyDistribution
Histogram []Bucket
Details []ResultDetail
Count uint64 `json:"count"`
Total time.Duration `json:"total"`
Average time.Duration `json:"average"`
Fastest time.Duration `json:"fastest"`
Slowest time.Duration `json:"slowest"`
Rps float64 `json:"rps"`

ErrorDist map[string]int `json:"errorDistribution"`
StatusCodeDist map[string]int `json:"statusCodeDistribution"`

LatencyDistribution []LatencyDistribution `json:"latencyDistribution"`
Histogram []Bucket `json:"histogram"`
Details []ResultDetail `json:"details"`
}

// LatencyDistribution holds latency distribution data
type LatencyDistribution struct {
Percentage int
Latency time.Duration
Percentage int `json:"percentage"`
Latency time.Duration `json:"latency"`
}

// Bucket holds histogram data
type Bucket struct {
Mark float64 // The Mark for histogram bucket in seconds
Count int // The count in the bucket
Frequency float64 // The frequency of results in the bucket as a decimal percentage
// The Mark for histogram bucket in seconds
Mark float64 `json:"mark"`

// The count in the bucket
Count int `json:"count"`

// The frequency of results in the bucket as a decimal percentage
Frequency float64 `json:"frequency"`
}

// ResultDetail data for each result
type ResultDetail struct {
Latency time.Duration
Error string
Status string
Latency time.Duration `json:"latency"`
Error string `json:"error"`
Status string `json:"status"`
}

func newReporter(results chan *callResult, n int) *Reporter {
Expand Down

0 comments on commit 85341d4

Please sign in to comment.