From a8d9c40e782f8c3067d2e21aa733586f95fc9a24 Mon Sep 17 00:00:00 2001 From: manasachinta Date: Tue, 9 Jan 2024 18:28:36 -0500 Subject: [PATCH] different approach to writing raw data as .json --- cmd/kperf/commands/runner/runner.go | 51 ++++++++++++++++++----------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/cmd/kperf/commands/runner/runner.go b/cmd/kperf/commands/runner/runner.go index 57acd6f..bc127ab 100644 --- a/cmd/kperf/commands/runner/runner.go +++ b/cmd/kperf/commands/runner/runner.go @@ -8,7 +8,6 @@ import ( "path/filepath" "sort" "strconv" - "time" "github.com/Azure/kperf/api/types" "github.com/Azure/kperf/request" @@ -160,26 +159,46 @@ func loadConfig(cliCtx *cli.Context) (*types.LoadProfile, error) { } func printResponseStats(f *os.File, stats *types.ResponseStats, rawData bool) { - - copy := ResponseStatsCopy{} - copy.Total = stats.Total - copy.FailureList = stats.FailureList - copy.TotalReceivedBytes = stats.TotalReceivedBytes - copy.Duration = stats.Duration - - copy.PercentileLatencies = make(map[string]string) + percentileLatencies := make(map[string]string) for i, v := range stats.PercentileLatencies { iCopy := strconv.FormatFloat(i, 'f', 2, 64) - copy.PercentileLatencies[iCopy] = strconv.FormatFloat(v, 'f', 3, 64) + percentileLatencies[iCopy] = strconv.FormatFloat(v, 'f', 3, 64) } if rawData { - b, err := json.MarshalIndent(copy, "", " ") + total, err := json.MarshalIndent(stats.Total, "", " ") + if err != nil { + fmt.Println(err) + return + } + fList, err := json.MarshalIndent(stats.FailureList, "", " ") if err != nil { fmt.Println(err) return } - fmt.Fprint(f, string(b)) + bytes, err := json.MarshalIndent(stats.TotalReceivedBytes, "", " ") + if err != nil { + fmt.Println(err) + return + } + duration, err := json.MarshalIndent(stats.Duration, "", " ") + if err != nil { + fmt.Println(err) + return + } + pLatencies, err := json.MarshalIndent(percentileLatencies, "", " ") + if err != nil { + fmt.Println(err) + return + } + fmt.Fprint(f, "{\n") + fmt.Fprintf(f, " \"Total\": %s,\n", string(total)) + fmt.Fprintf(f, " \"Failure List\": %s,\n", string(fList)) + fmt.Fprintf(f, " \"Total Received Bytes\":%s,\n", string(bytes)) + fmt.Fprintf(f, " \"Duration\":%s,\n", string(duration)) + fmt.Fprintf(f, " \"Percentile Latencies\":%s,\n", pLatencies) + fmt.Fprint(f, "}") + } else { fmt.Fprint(f, "Response Stat: \n") fmt.Fprintf(f, " Total: %v\n", stats.Total) @@ -212,11 +231,3 @@ func printResponseStats(f *os.File, stats *types.ResponseStats, rawData bool) { } } - -type ResponseStatsCopy struct { - Total int - FailureList []error - TotalReceivedBytes int64 - Duration time.Duration - PercentileLatencies map[string]string -}