func NewStat(data []float64) Stat { return Stat{ Min: stats.StatsMin(data), Max: stats.StatsMax(data), Mean: stats.StatsMean(data), StdDev: stats.StatsPopulationStandardDeviation(data), Total: stats.StatsSum(data), } }
func (c *Client) Run(res chan *RunResults) { newMsgs := make(chan *Message) pubMsgs := make(chan *Message) doneGen := make(chan bool) donePub := make(chan bool) runResults := new(RunResults) started := time.Now() // start generator go c.genMessages(newMsgs, doneGen) // start publisher go c.pubMessages(newMsgs, pubMsgs, doneGen, donePub) runResults.ID = c.ID times := []float64{} for { select { case m := <-pubMsgs: if m.Error { log.Printf("CLIENT %v ERROR publishing message: %v: at %v\n", c.ID, m.Topic, m.Sent.Unix()) runResults.Failures++ } else { // log.Printf("Message published: %v: sent: %v delivered: %v flight time: %v\n", m.Topic, m.Sent, m.Delivered, m.Delivered.Sub(m.Sent)) runResults.Successes++ times = append(times, m.Delivered.Sub(m.Sent).Seconds()*1000) // in milliseconds } case <-donePub: // calculate results duration := time.Now().Sub(started) 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() // report results and exit res <- runResults return } } }