Skip to content

Commit 96b3dbf

Browse files
committed
addressing final comments, still need review on open file method
1 parent db63e24 commit 96b3dbf

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

cmd/kperf/commands/runner/runner.go

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import (
44
"context"
55
"fmt"
66
"log"
7-
"math"
87
"os"
98
"sort"
10-
"strconv"
119

1210
"github.com/Azure/kperf/api/types"
1311
"github.com/Azure/kperf/request"
@@ -69,7 +67,7 @@ var runCommand = cli.Command{
6967

7068
kubeCfgPath := cliCtx.String("kubeconfig")
7169
userAgent := cliCtx.String("user-agent")
72-
outputFile := cliCtx.String("result")
70+
outputFilePath := cliCtx.String("result")
7371

7472
conns := profileCfg.Spec.Conns
7573
rate := profileCfg.Spec.Rate
@@ -83,18 +81,15 @@ var runCommand = cli.Command{
8381
return err
8482
}
8583

86-
var fileDir string = "result" //change directory to store response stats if needed
87-
8884
var f *os.File = os.Stdout
89-
if outputFile != "" {
90-
err = os.MkdirAll(fileDir, 0750)
85+
if outputFilePath != "" {
86+
err := os.MkdirAll(outputFilePath, os.ModePerm)
9187
if err != nil {
92-
log.Fatal(err)
88+
log.Default().Printf("failed to create directory %s: %v", outputFilePath, err)
9389
}
94-
filePath := fmt.Sprintf("%s/%s", fileDir, outputFile)
95-
f, err = os.Create(filePath)
90+
f, err = os.Create(outputFilePath)
9691
if err != nil {
97-
log.Fatal(err)
92+
return err
9893
}
9994
defer f.Close()
10095
}
@@ -136,22 +131,20 @@ func loadConfig(cliCtx *cli.Context) (*types.LoadProfile, error) {
136131
}
137132

138133
func printResponseStats(f *os.File, stats *types.ResponseStats) {
139-
fmt.Fprintf(f, "Response Stat: \n")
140-
141-
fmt.Fprintf(f, " Total: "+strconv.Itoa(stats.Total)+"\n")
134+
fmt.Fprint(f, "Response Stat: \n")
135+
fmt.Fprintf(f, " Total: %v\n", stats.Total)
142136

143-
fmt.Fprintf(f, " Total Failures: "+strconv.Itoa(len(stats.FailureList))+"\n")
137+
fmt.Fprintf(f, " Total Failures: %d\n", len(stats.FailureList))
144138

145-
fmt.Fprintf(f, " Observed Bytes: "+strconv.FormatInt(stats.TotalReceivedBytes, 10)+"\n")
139+
fmt.Fprintf(f, " Observed Bytes: %v\n", stats.TotalReceivedBytes)
146140

147-
fmt.Fprintf(f, " Duration: "+stats.Duration.String()+"\n")
141+
fmt.Fprintf(f, " Duration: %v\n", stats.Duration.String())
148142

149143
requestsPerSec := float64(stats.Total) / stats.Duration.Seconds()
150144

151-
roundedNumber := math.Round(requestsPerSec*100) / 100
152-
fmt.Fprintf(f, " Requests/sec: "+strconv.FormatFloat(roundedNumber, 'f', -1, 64)+"\n")
145+
fmt.Fprintf(f, " Requests/sec: %.2f\n", requestsPerSec)
153146

154-
fmt.Fprintf(f, " Latency Distribution:\n")
147+
fmt.Fprint(f, " Latency Distribution:\n")
155148
keys := make([]float64, 0, len(stats.PercentileLatencies))
156149
for q := range stats.PercentileLatencies {
157150
keys = append(keys, q)
@@ -160,7 +153,6 @@ func printResponseStats(f *os.File, stats *types.ResponseStats) {
160153
sort.Float64s(keys)
161154

162155
for _, q := range keys {
163-
str := fmt.Sprintf(" [%.2f] %.3fs\n", q/100.0, stats.PercentileLatencies[q])
164-
fmt.Fprint(f, str)
156+
fmt.Fprintf(f, " [%.2f] %.3fs\n", q/100.0, stats.PercentileLatencies[q])
165157
}
166158
}

0 commit comments

Comments
 (0)