From c740cb91ee9d7b45e1645a3e62acc37f6c02bebf Mon Sep 17 00:00:00 2001 From: Bojan Djurkovic Date: Mon, 14 May 2018 09:12:43 -0300 Subject: [PATCH] add prety_json support. add json formatting. update test cert and key --- README.md | 3 ++- cmd/grpcannon/main.go | 3 ++- printer/printer.go | 13 +++++++++- reporter.go | 47 ++++++++++++++++++++---------------- testdata/localhost.crt | 28 ++++++++++----------- testdata/localhost.key | 55 +++++++++++++++++++++--------------------- 6 files changed, 84 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index ed978533..8af9496b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/cmd/grpcannon/main.go b/cmd/grpcannon/main.go index 25dd5391..9a0c0dd8 100644 --- a/cmd/grpcannon/main.go +++ b/cmd/grpcannon/main.go @@ -78,8 +78,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. diff --git a/printer/printer.go b/printer/printer.go index cb6e3ee0..e9d58056 100644 --- a/printer/printer.go +++ b/printer/printer.go @@ -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)) } } diff --git a/reporter.go b/reporter.go index 6fa673bb..f45fd5a4 100644 --- a/reporter.go +++ b/reporter.go @@ -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 { diff --git a/testdata/localhost.crt b/testdata/localhost.crt index 27eff570..f339bf96 100644 --- a/testdata/localhost.crt +++ b/testdata/localhost.crt @@ -1,18 +1,18 @@ -----BEGIN CERTIFICATE----- -MIIC5TCCAc2gAwIBAgIJALLvqFlCiW3VMA0GCSqGSIb3DQEBCwUAMBQxEjAQBgNV -BAMMCWxvY2FsaG9zdDAeFw0xODA0MDcxNjU0MjRaFw0xODA1MDcxNjU0MjRaMBQx +MIIC5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xOshZMFvLlgDzJxMF2 -UwLgNjJHaK8kkEtGSQy8Qg02vF+iuZMYGknGHyjCSOyriDhsynh7085cRPq91d+9 -Rj2egomZYCQlOaWyqB7jA6pZM5JHBQOGPJttuASFZC8IdhNrI25Ki8JBibpT3DqK -BJP4PccK8gGT5yxtv7fYf3Tp4Spsiqk0yyRawAIKXCbIpSTY7MrjYJ9iqzTT7WiY -NXMRASmS7yTP4NK8Um1X6GP4+gAuh5UeNnYAGFCqqYznxrohoNbqEnp3E4Oqy/EQ -6qe8HePlE+oho7TecaZRvuLXcHMCVatJMA== +AQsFAAOCAQEAi06brp5sUGSN7EIz8isZ2wRrBXcMIrKOmzbTSNYNBb4B57/97RFY +Z4OA0WdCJHUhWJ89owPtikpVnS9NxJLdiZQrpsz8O6wWPS3+lbc5mVyfWLHMQhIB +V3MMhsI3qWpSXoN/xpjdzYsP4EzHbK5G1y5XX64TvfArogtCUIl+bIUt2N1jyH5+ +IcAAmppW85HMpyMflYNUMKTmzOyOg/ZCJDbq/24QLzdjQPFoFyww+1PI9R+hNduk +rBmbUEsDlUlamwICW+o2hbm97fDxE9AyQOUSu2/+xu1m2rMXzwtzEy56qx0COuZv +peEgi2sajoxeie3HuziU4tUbRYqQWeriHA== -----END CERTIFICATE----- diff --git a/testdata/localhost.key b/testdata/localhost.key index e024c723..2d9e0eb8 100644 --- a/testdata/localhost.key +++ b/testdata/localhost.key @@ -1,27 +1,28 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEpQIBAAKCAQEA3OZyIWGK2rOawcscoLH/IwcVg4BccEWdatqz41xb/mIPKbU5 -7VCBefTLVxfVv1wg1wvnEeI2MjeIVbb8U9DxNnY6kXHcwD8dbSto7Jmqh+5t9KLJ -BclXamL7mmyM//3+iA+Ve5aO2YCyf3bdeF7Eo4vEOTefmSGAPvi3Gtw61fxUY8ac -wcsO1RYJ2JnBIqhxY2pTb6szDd+K+7LM8pXHm4wHcUOOogdNPKstFJHxa1FlBoj5 -ewheJgLfW1W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