Extract Metrics out of ReportText
Metrics will be reused with other Reporters
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Reporter represents any function which takes a slice of Results and
|
||||
@@ -17,51 +16,25 @@ import (
|
||||
// in case of failure
|
||||
type Reporter func([]Result, io.Writer) error
|
||||
|
||||
// ReportText computes and prints some metrics out of results
|
||||
// as formatted text. Metrics include avg time per request, success ratio,
|
||||
// total number of request, avg bytes in and avg bytes out.
|
||||
// ReportText writes a computed Metrics struct to out as aligned, formatted text
|
||||
func ReportText(results []Result, out io.Writer) error {
|
||||
totalRequests := float64(len(results))
|
||||
totalTime := time.Duration(0)
|
||||
totalBytesOut := uint64(0)
|
||||
totalBytesIn := uint64(0)
|
||||
totalSuccess := uint64(0)
|
||||
histogram := map[uint16]uint64{}
|
||||
errors := map[string]struct{}{}
|
||||
|
||||
for _, res := range results {
|
||||
histogram[res.Code]++
|
||||
totalTime += res.Timing
|
||||
totalBytesOut += res.BytesOut
|
||||
totalBytesIn += res.BytesIn
|
||||
if res.Code >= 200 && res.Code < 300 {
|
||||
totalSuccess++
|
||||
}
|
||||
if res.Error != nil {
|
||||
errors[res.Error.Error()] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
avgTime := time.Duration(float64(totalTime) / totalRequests)
|
||||
avgBytesOut := float64(totalBytesOut) / totalRequests
|
||||
avgBytesIn := float64(totalBytesIn) / totalRequests
|
||||
avgSuccess := float64(totalSuccess) / totalRequests
|
||||
m := NewMetrics(results)
|
||||
|
||||
w := tabwriter.NewWriter(out, 0, 8, 2, '\t', tabwriter.StripEscape)
|
||||
fmt.Fprintf(w, "Time(avg)\tRequests\tSuccess\tBytes(rx/tx)\n")
|
||||
fmt.Fprintf(w, "%s\t%d\t%.2f%%\t%.2f/%.2f\n", avgTime, int(totalRequests), avgSuccess*100, avgBytesIn, avgBytesOut)
|
||||
fmt.Fprintf(w, "%s\t%d\t%.2f%%\t%.2f/%.2f\n", m.MeanTiming, m.TotalRequests, m.MeanSuccess*100, m.MeanBytesIn, m.MeanBytesOut)
|
||||
|
||||
fmt.Fprintf(w, "\nCount:\t")
|
||||
for _, count := range histogram {
|
||||
for _, count := range m.StatusCodes {
|
||||
fmt.Fprintf(w, "%d\t", count)
|
||||
}
|
||||
fmt.Fprintf(w, "\nStatus:\t")
|
||||
for code := range histogram {
|
||||
for code := range m.StatusCodes {
|
||||
fmt.Fprintf(w, "%d\t", code)
|
||||
}
|
||||
|
||||
fmt.Fprintln(w, "\n\nError Set:")
|
||||
for err := range errors {
|
||||
for err := range m.Errors {
|
||||
fmt.Fprintln(w, err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user