NewStatistics returns a new Statistics object. Statistics object is thread-safe and compute statistics on the fly based on sample recorded. Available statistics are: - min - max - avg - count - stddev - stdvar If a Statistics object with the same name already exists it is returned.
(name string)
| 232 | // - stdvar |
| 233 | // If a Statistics object with the same name already exists it is returned. |
| 234 | func NewStatistics(name string) *Statistics { |
| 235 | s := &Statistics{ |
| 236 | min: atomic.NewFloat64(math.Inf(0)), |
| 237 | max: atomic.NewFloat64(math.Inf(-1)), |
| 238 | count: atomic.NewInt64(0), |
| 239 | avg: atomic.NewFloat64(0), |
| 240 | mean: atomic.NewFloat64(0), |
| 241 | value: atomic.NewFloat64(0), |
| 242 | } |
| 243 | existing := expvar.Get(statsPrefix + name) |
| 244 | if existing != nil { |
| 245 | if s, ok := existing.(*Statistics); ok { |
| 246 | return s |
| 247 | } |
| 248 | panic(fmt.Sprintf("%v is set to a non-Statistics value", name)) |
| 249 | } |
| 250 | expvar.Publish(statsPrefix+name, s) |
| 251 | return s |
| 252 | } |
| 253 | |
| 254 | func (s *Statistics) String() string { |
| 255 | b, _ := json.Marshal(s.Value()) |