Bugfix #3: empty JSON output if '-clients=1' or '-count=1' (NaN sample std for a single sample)

This commit is contained in:
Alexandr Krylovskiy
2019-10-13 17:39:48 +02:00
parent 774ac6e90a
commit 8fb006ba61
2 changed files with 22 additions and 8 deletions

View File

@@ -55,9 +55,12 @@ func (c *Client) Run(res chan *RunResults) {
runResults.MsgTimeMin = stats.StatsMin(times)
runResults.MsgTimeMax = stats.StatsMax(times)
runResults.MsgTimeMean = stats.StatsMean(times)
runResults.MsgTimeStd = stats.StatsSampleStandardDeviation(times)
runResults.RunTime = duration.Seconds()
runResults.MsgsPerSec = float64(runResults.Successes) / duration.Seconds()
// calculate std if sample is > 1, otherwise leave as 0 (convention)
if c.MsgCount > 1 {
runResults.MsgTimeStd = stats.StatsSampleStandardDeviation(times)
}
// report results and exit
res <- runResults
@@ -123,8 +126,8 @@ func (c *Client) pubMessages(in, out chan *Message, doneGen, donePub chan bool)
SetAutoReconnect(true).
SetOnConnectHandler(onConnected).
SetConnectionLostHandler(func(client mqtt.Client, reason error) {
log.Printf("CLIENT %v lost connection to the broker: %v. Will reconnect...\n", c.ID, reason.Error())
})
log.Printf("CLIENT %v lost connection to the broker: %v. Will reconnect...\n", c.ID, reason.Error())
})
if c.BrokerUser != "" && c.BrokerPass != "" {
opts.SetUsername(c.BrokerUser)
opts.SetPassword(c.BrokerPass)

21
main.go
View File

@@ -72,7 +72,11 @@ func main() {
flag.Parse()
if *clients < 1 {
log.Fatal("Invlalid arguments")
log.Fatalf("Invalid arguments: number of clients should be > 1, given: %v", *clients)
}
if *count < 1 {
log.Fatalf("Invalid arguments: messages count should be > 1, given: %v", *count)
}
resCh := make(chan *RunResults)
@@ -101,12 +105,13 @@ func main() {
results[i] = <-resCh
}
totalTime := time.Now().Sub(start)
totals := calculateTotalResults(results, totalTime)
totals := calculateTotalResults(results, totalTime, *clients)
// print stats
printResults(results, totals, *format)
}
func calculateTotalResults(results []*RunResults, totalTime time.Duration) *TotalResults {
func calculateTotalResults(results []*RunResults, totalTime time.Duration, sampleSize int) *TotalResults {
totals := new(TotalResults)
totals.TotalRunTime = totalTime.Seconds()
@@ -138,7 +143,10 @@ func calculateTotalResults(results []*RunResults, totalTime time.Duration) *Tota
totals.AvgMsgsPerSec = stats.StatsMean(msgsPerSecs)
totals.AvgRunTime = stats.StatsMean(runTimes)
totals.MsgTimeMeanAvg = stats.StatsMean(msgTimeMeans)
totals.MsgTimeMeanStd = stats.StatsSampleStandardDeviation(msgTimeMeans)
// calculate std if sample is > 1, otherwise leave as 0 (convention)
if sampleSize > 1 {
totals.MsgTimeMeanStd = stats.StatsSampleStandardDeviation(msgTimeMeans)
}
return totals
}
@@ -150,7 +158,10 @@ func printResults(results []*RunResults, totals *TotalResults, format string) {
Runs: results,
Totals: totals,
}
data, _ := json.Marshal(jr)
data, err := json.Marshal(jr)
if err != nil {
log.Fatalf("Error marshalling results: %v", err)
}
var out bytes.Buffer
json.Indent(&out, data, "", "\t")