Skip to content

Commit

Permalink
Merge pull request #13 from bojand/json_output
Browse files Browse the repository at this point in the history
Add JSON output support. Actually implement -o output flag.
  • Loading branch information
bojand committed May 4, 2018
2 parents ed29faf + 470cc01 commit 4fb9a24
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 36 deletions.
13 changes: 12 additions & 1 deletion cmd/grpcannon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,20 @@ func main() {
errAndExit(err.Error())
}

output := os.Stdout
outputPath := strings.TrimSpace(cfg.Output)
if outputPath != "" {
f, err := os.Create(outputPath)
if err != nil {
errAndExit(err.Error())
}
defer f.Close()
output = f
}

p := printer.ReportPrinter{
Report: report,
Out: os.Stdout}
Out: output}

p.Print(cfg.Format)
}
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func New(proto, protoset, call, cert, cName string, n, c, qps int, z time.Durati
Timeout: timeout,
DataPath: dataPath,
MetadataPath: mdPath,
Output: output,
Format: format,
Host: host,
ImportPaths: importPaths,
Expand Down
37 changes: 22 additions & 15 deletions printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,30 @@ type ReportPrinter struct {
// If format is "csv" detailed listing is printer in csv format.
// Otherwise the summary of results is printed.
func (rp *ReportPrinter) Print(format string) {
outputTmpl := format
switch outputTmpl {
case "":
outputTmpl = defaultTmpl
case "csv":
outputTmpl = csvTmpl
}
buf := &bytes.Buffer{}
templ := template.Must(template.New("tmpl").Funcs(tmplFuncMap).Parse(outputTmpl))
if err := templ.Execute(buf, *rp.Report); err != nil {
log.Println("error:", err.Error())
return
}
switch format {
case "", "csv":
outputTmpl := defaultTmpl
if format == "csv" {
outputTmpl = csvTmpl
}
buf := &bytes.Buffer{}
templ := template.Must(template.New("tmpl").Funcs(tmplFuncMap).Parse(outputTmpl))
if err := templ.Execute(buf, *rp.Report); err != nil {
log.Println("error:", err.Error())
return
}

rp.printf(buf.String())
rp.printf(buf.String())

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

func (rp *ReportPrinter) printf(s string, v ...interface{}) {
Expand Down
40 changes: 20 additions & 20 deletions reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ type Report struct {
Details []ResultDetail
}

// LatencyDistribution holds latency distribution data
type LatencyDistribution struct {
Percentage int
Latency time.Duration
}

// 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
}

// ResultDetail data for each result
type ResultDetail struct {
Latency time.Duration
Error string
Status string
}

func newReporter(results chan *callResult, n int) *Reporter {
cap := min(n, maxResult)
return &Reporter{
Expand Down Expand Up @@ -168,23 +188,3 @@ func histogram(latencies *[]float64, slowest, fastest float64) []Bucket {
}
return res
}

// LatencyDistribution holds latency distribution data
type LatencyDistribution struct {
Percentage int
Latency time.Duration
}

// Bucket holds histogram data
type Bucket struct {
Mark float64
Count int
Frequency float64
}

// ResultDetail data for each result
type ResultDetail struct {
Latency time.Duration
Error string
Status string
}

0 comments on commit 4fb9a24

Please sign in to comment.