Skip to content

Commit

Permalink
addressing comments on latest review
Browse files Browse the repository at this point in the history
  • Loading branch information
mchinta7 committed Jan 4, 2024
1 parent 988a785 commit db63e24
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ bin/
# Go workspace file
go.work

#output file
*.txt
#output file directory
result/

#tmp folder which contains .yaml files
tmp/
57 changes: 22 additions & 35 deletions cmd/kperf/commands/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,22 @@ var runCommand = cli.Command{
return err
}

var f *os.File
if outputFile == "" {
f = os.Stdout
} else {
f, err = os.Create(outputFile)
var fileDir string = "result" //change directory to store response stats if needed

var f *os.File = os.Stdout
if outputFile != "" {
err = os.MkdirAll(fileDir, 0750)
if err != nil {
log.Fatal(err)
}
filePath := fmt.Sprintf("%s/%s", fileDir, outputFile)
f, err = os.Create(filePath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
}
writeToFile(f, stats)

printResponseStats(f, stats)
return nil
},
}
Expand Down Expand Up @@ -130,41 +135,23 @@ func loadConfig(cliCtx *cli.Context) (*types.LoadProfile, error) {
return &profileCfg, nil
}

func writeToFile(f *os.File, stats *types.ResponseStats) {
func printResponseStats(f *os.File, stats *types.ResponseStats) {
fmt.Fprintf(f, "Response Stat: \n")

// remember to close the file
defer f.Close()
fmt.Fprintf(f, " Total: "+strconv.Itoa(stats.Total)+"\n")

//write Total requests
f.WriteString("Response Stat: \n")
_, err := f.WriteString(" Total: " + strconv.Itoa(stats.Total) + "\n")
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(f, " Total Failures: "+strconv.Itoa(len(stats.FailureList))+"\n")

_, err = f.WriteString(" Total Failures: " + strconv.Itoa(len(stats.FailureList)) + "\n")
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(f, " Observed Bytes: "+strconv.FormatInt(stats.TotalReceivedBytes, 10)+"\n")

_, err = f.WriteString(" Observed Bytes: " + strconv.FormatInt(stats.TotalReceivedBytes, 10) + "\n")
if err != nil {
log.Fatal(err)
}

_, err = f.WriteString(" Duration: " + stats.Duration.String() + "\n")
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(f, " Duration: "+stats.Duration.String()+"\n")

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

roundedNumber := math.Round(requestsPerSec*100) / 100
_, err = f.WriteString(" Requests/sec: " + strconv.FormatFloat(roundedNumber, 'f', -1, 64) + "\n")
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(f, " Requests/sec: "+strconv.FormatFloat(roundedNumber, 'f', -1, 64)+"\n")

f.WriteString(" Latency Distribution:\n")
fmt.Fprintf(f, " Latency Distribution:\n")
keys := make([]float64, 0, len(stats.PercentileLatencies))
for q := range stats.PercentileLatencies {
keys = append(keys, q)
Expand All @@ -174,6 +161,6 @@ func writeToFile(f *os.File, stats *types.ResponseStats) {

for _, q := range keys {
str := fmt.Sprintf(" [%.2f] %.3fs\n", q/100.0, stats.PercentileLatencies[q])
f.WriteString(str)
fmt.Fprint(f, str)
}
}
4 changes: 1 addition & 3 deletions metrics/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@ func (m *responseMetricImpl) ObserveFailure(err error) {

// ObserveReceivedBytes implements ResponseMetric.
func (m *responseMetricImpl) ObserveReceivedBytes(bytes int64) {
m.mu.Lock()
defer m.mu.Unlock()
atomic.AddInt64(&m.receivedBytes, bytes)
}

// Gather implements ResponseMetric.
func (m *responseMetricImpl) Gather() ([]float64, map[float64]float64, []error, int64) {
latencies := m.dumpLatencies()
return latencies, buildPercentileLatencies(latencies), m.failureList, m.receivedBytes
return latencies, buildPercentileLatencies(latencies), m.failureList, atomic.LoadInt64(&m.receivedBytes)
}

func (m *responseMetricImpl) dumpLatencies() []float64 {
Expand Down

0 comments on commit db63e24

Please sign in to comment.