@@ -4,10 +4,8 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"log"
7
- "math"
8
7
"os"
9
8
"sort"
10
- "strconv"
11
9
12
10
"github.com/Azure/kperf/api/types"
13
11
"github.com/Azure/kperf/request"
@@ -69,7 +67,7 @@ var runCommand = cli.Command{
69
67
70
68
kubeCfgPath := cliCtx .String ("kubeconfig" )
71
69
userAgent := cliCtx .String ("user-agent" )
72
- outputFile := cliCtx .String ("result" )
70
+ outputFilePath := cliCtx .String ("result" )
73
71
74
72
conns := profileCfg .Spec .Conns
75
73
rate := profileCfg .Spec .Rate
@@ -83,18 +81,15 @@ var runCommand = cli.Command{
83
81
return err
84
82
}
85
83
86
- var fileDir string = "result" //change directory to store response stats if needed
87
-
88
84
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 )
91
87
if err != nil {
92
- log .Fatal ( err )
88
+ log .Default (). Printf ( "failed to create directory %s: %v" , outputFilePath , err )
93
89
}
94
- filePath := fmt .Sprintf ("%s/%s" , fileDir , outputFile )
95
- f , err = os .Create (filePath )
90
+ f , err = os .Create (outputFilePath )
96
91
if err != nil {
97
- log . Fatal ( err )
92
+ return err
98
93
}
99
94
defer f .Close ()
100
95
}
@@ -136,22 +131,20 @@ func loadConfig(cliCtx *cli.Context) (*types.LoadProfile, error) {
136
131
}
137
132
138
133
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 )
142
136
143
- fmt .Fprintf (f , " Total Failures: " + strconv . Itoa ( len (stats .FailureList )) + " \n " )
137
+ fmt .Fprintf (f , " Total Failures: %d \n " , len (stats .FailureList ))
144
138
145
- fmt .Fprintf (f , " Observed Bytes: " + strconv . FormatInt ( stats .TotalReceivedBytes , 10 ) + " \n " )
139
+ fmt .Fprintf (f , " Observed Bytes: %v \n " , stats .TotalReceivedBytes )
146
140
147
- fmt .Fprintf (f , " Duration: " + stats .Duration .String ()+ " \n " )
141
+ fmt .Fprintf (f , " Duration: %v \n " , stats .Duration .String ())
148
142
149
143
requestsPerSec := float64 (stats .Total ) / stats .Duration .Seconds ()
150
144
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 )
153
146
154
- fmt .Fprintf (f , " Latency Distribution:\n " )
147
+ fmt .Fprint (f , " Latency Distribution:\n " )
155
148
keys := make ([]float64 , 0 , len (stats .PercentileLatencies ))
156
149
for q := range stats .PercentileLatencies {
157
150
keys = append (keys , q )
@@ -160,7 +153,6 @@ func printResponseStats(f *os.File, stats *types.ResponseStats) {
160
153
sort .Float64s (keys )
161
154
162
155
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 ])
165
157
}
166
158
}
0 commit comments