Skip to content

Commit

Permalink
Write perf results to a CSV file
Browse files Browse the repository at this point in the history
This is something people coming from Cosbench expect.
Currently there's only a handful of metrics, but they can be extended if needed.
  • Loading branch information
mulbc committed Jun 29, 2020
1 parent b741b2f commit d944a2c
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package main

import (
"encoding/csv"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"time"

"github.com/mulbc/gosbench/common"
Expand Down Expand Up @@ -152,6 +155,7 @@ func scheduleTests(config common.Testconf) {
WithField("Average latency in ms", benchResult.LatencyAvg).
WithField("Test runtime on server", benchResult.Duration).
Infof("PERF RESULTS")
writeResultToCSV(benchResult)
}
log.Info("All performance tests finished")
for {
Expand Down Expand Up @@ -209,3 +213,68 @@ func sumBenchmarkResults(results []common.BenchmarkResult) common.BenchmarkResul
sum.Bandwidth = bandwidthAverages
return sum
}

func writeResultToCSV(benchResult common.BenchmarkResult) {
file, created, err := getCSVFileHandle()
if err != nil {
log.WithError(err).Error("Could not get a file handle for the CSV results")
return
}
defer file.Close()

csvwriter := csv.NewWriter(file)

if created {
err = csvwriter.Write([]string{
"testName",
"Total Operations",
"Total Bytes",
"Average Bandwidth in Bytes/s",
"Average Latency in ms",
"Test duration seen by server in seconds",
})
if err != nil {
log.WithError(err).Error("Failed writing line to results csv")
return
}
}

err = csvwriter.Write([]string{
benchResult.TestName,
fmt.Sprintf("%.0f", benchResult.Operations),
fmt.Sprintf("%.0f", benchResult.Bytes),
fmt.Sprintf("%f", benchResult.Bandwidth),
fmt.Sprintf("%f", benchResult.LatencyAvg),
fmt.Sprintf("%f", benchResult.Duration.Seconds()),
})
if err != nil {
log.WithError(err).Error("Failed writing line to results csv")
return
}

csvwriter.Flush()

}

func getCSVFileHandle() (*os.File, bool, error) {
file, err := os.OpenFile("gosbench_results.csv", os.O_APPEND|os.O_WRONLY, 0755)
if err == nil {
return file, false, nil
}
file, err = os.OpenFile("/tmp/gosbench_results.csv", os.O_APPEND|os.O_WRONLY, 0755)
if err == nil {
return file, false, nil
}

file, err = os.OpenFile("gosbench_results.csv", os.O_WRONLY|os.O_CREATE, 0755)
if err == nil {
return file, true, nil
}
file, err = os.OpenFile("/tmp/gosbench_results.csv", os.O_WRONLY|os.O_CREATE, 0755)
if err == nil {
return file, true, nil
}

return nil, false, errors.New("Could not find previous CSV for appending and could not write new CSV file to current dir and /tmp/ giving up")

}

0 comments on commit d944a2c

Please sign in to comment.