mirror of
https://github.com/wwhai/mqtt-benchmark.git
synced 2025-10-05 15:47:19 +08:00
Bugfix #3: empty JSON output if '-clients=1' or '-count=1' (NaN sample std for a single sample)
This commit is contained in:
@@ -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
|
||||
|
19
main.go
19
main.go
@@ -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)
|
||||
// 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")
|
||||
|
||||
|
Reference in New Issue
Block a user