diff --git a/cmd/kperf/commands/runner/runner.go b/cmd/kperf/commands/runner/runner.go index 43d371b..7e72d47 100644 --- a/cmd/kperf/commands/runner/runner.go +++ b/cmd/kperf/commands/runner/runner.go @@ -2,6 +2,8 @@ package runner import ( "context" + "encoding/json" + "flag" "fmt" "os" @@ -70,6 +72,10 @@ var runCommand = cli.Command{ Name: "result", Usage: "Path to the file which stores results", }, + cli.BoolFlag{ + Name: "raw-data", + Usage: "write ResponseStats to file in .json format", + }, cli.StringFlag{ Name: "v", Usage: "log level for V logs", @@ -100,6 +106,7 @@ var runCommand = cli.Command{ kubeCfgPath := cliCtx.String("kubeconfig") userAgent := cliCtx.String("user-agent") outputFilePath := cliCtx.String("result") + rawDataFlagIncluded := cliCtx.Bool("result") conns := profileCfg.Spec.Conns rate := profileCfg.Spec.Rate @@ -132,8 +139,11 @@ var runCommand = cli.Command{ defer f.Close() } - //TODO: add printResponseStats for .json format - printResponseStats(f, stats) + err = printResponseStats(f, rawDataFlagIncluded, stats) + if err != nil { + return fmt.Errorf("error while printing response stats: %w", err) + } + return nil }, } @@ -173,7 +183,27 @@ func loadConfig(cliCtx *cli.Context) (*types.LoadProfile, error) { return &profileCfg, nil } -// TODO: Complete this function -func printResponseStats(f *os.File, stats *request.Result) { - fmt.Fprintf(f, "Response Stat: %v\n", stats) +func printResponseStats(f *os.File, rawDataFlagIncluded bool, stats *request.Result) error { + output := types.RunnerMetricReport{ + Total: stats.Total, + FailureList: stats.FailureList, + Duration: stats.Duration, + Latencies: stats.Latencies, + TotalReceivedBytes: stats.TotalReceivedBytes, + } + + encoder := json.NewEncoder(f) + encoder.SetIndent("", " ") + + if !rawDataFlagIncluded { + output.Latencies = nil + } + + err := encoder.Encode(output) + if err != nil { + return fmt.Errorf("failed to encode json: %w", err) + } + + return nil + }