Skip to content

Commit

Permalink
Added raw-data flag, added indent formatting to .json output (#47)
Browse files Browse the repository at this point in the history
Added raw-data flag (bool), if set, we write the RunnerMetricReport in
.json to file specified by the --result flag. If not. Latencies is set
to null and rendered

---------

Co-authored-by: manasachinta <manasachinta@microsoft.com>
  • Loading branch information
manasachi and mchinta7 authored Jan 17, 2024
1 parent 0be56d2 commit 39b3e2e
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions cmd/kperf/commands/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package runner

import (
"context"
"encoding/json"

"flag"
"fmt"
"os"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
},
}
Expand Down Expand Up @@ -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

}

0 comments on commit 39b3e2e

Please sign in to comment.