2015-10-26 23:11:09 +00:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
2015-10-26 23:54:00 +00:00
|
|
|
"fmt"
|
2015-10-26 23:11:09 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rcrowley/go-metrics"
|
|
|
|
)
|
|
|
|
|
2015-10-26 23:54:00 +00:00
|
|
|
type Manager interface {
|
|
|
|
Publisher
|
2015-10-27 00:33:04 +00:00
|
|
|
Fetch(name string) Stats
|
|
|
|
Reset()
|
2015-10-26 23:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Publisher interface {
|
|
|
|
Add(name string, dur time.Duration)
|
|
|
|
Error(name string)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Stats interface {
|
|
|
|
Processed() int64
|
|
|
|
Errors() int64
|
|
|
|
Min() int64
|
|
|
|
Max() int64
|
|
|
|
P95() float64
|
|
|
|
Mean() float64
|
|
|
|
StdDev() float64
|
|
|
|
}
|
|
|
|
|
2015-10-26 23:11:09 +00:00
|
|
|
type base struct {
|
|
|
|
sync.Mutex
|
2015-10-27 00:03:21 +00:00
|
|
|
stats map[string]*baseStats
|
|
|
|
sampleSize int
|
2015-10-26 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type baseStats struct {
|
2015-10-26 23:54:00 +00:00
|
|
|
name string
|
2015-10-26 23:11:09 +00:00
|
|
|
time metrics.Histogram
|
|
|
|
errors metrics.Counter
|
|
|
|
}
|
|
|
|
|
2015-10-26 23:54:00 +00:00
|
|
|
const (
|
2015-10-27 00:03:21 +00:00
|
|
|
DefaultSampleSize = 1000
|
|
|
|
Latency = "Latency"
|
|
|
|
TaskWait = "TaskWait"
|
2015-10-26 23:54:00 +00:00
|
|
|
)
|
|
|
|
|
2015-10-26 23:11:09 +00:00
|
|
|
func (b *base) Add(name string, dur time.Duration) {
|
|
|
|
b.metrics(name).time.Update(int64(dur))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *base) Error(name string) {
|
|
|
|
b.metrics(name).errors.Inc(1)
|
|
|
|
}
|
|
|
|
|
2015-10-26 23:54:00 +00:00
|
|
|
func (b *base) Fetch(name string) Stats {
|
|
|
|
return b.metrics(name)
|
|
|
|
}
|
|
|
|
|
2015-10-27 00:33:04 +00:00
|
|
|
func (b *base) Reset() {
|
|
|
|
for _, s := range b.stats {
|
|
|
|
s.time.Clear()
|
|
|
|
s.errors.Clear()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 23:54:00 +00:00
|
|
|
func (s *baseStats) Processed() int64 {
|
|
|
|
return s.time.Count()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *baseStats) Errors() int64 {
|
|
|
|
return s.errors.Count()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *baseStats) Min() int64 {
|
|
|
|
return s.time.Min()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *baseStats) Max() int64 {
|
|
|
|
return s.time.Max()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *baseStats) P95() float64 {
|
|
|
|
return s.time.Percentile(0.95)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *baseStats) Mean() float64 {
|
|
|
|
return s.time.Mean()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *baseStats) StdDev() float64 {
|
|
|
|
return s.time.StdDev()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *baseStats) String() string {
|
|
|
|
return fmt.Sprintf("%s statistics:\n"+
|
|
|
|
"Processed: %10d\n"+
|
|
|
|
"Errors: %10d\n"+
|
|
|
|
"Min: %10s\n"+
|
|
|
|
"Mean: %10s\n"+
|
|
|
|
"95%%: %10s\n"+
|
|
|
|
"Max: %10s\n"+
|
|
|
|
"StdDev: %10s",
|
|
|
|
s.name,
|
|
|
|
s.time.Count(),
|
|
|
|
s.errors.Count(),
|
|
|
|
formatDuration(float64(s.time.Min())),
|
|
|
|
formatDuration(s.time.Mean()),
|
|
|
|
formatDuration(s.time.Percentile(0.95)),
|
|
|
|
formatDuration(float64(s.time.Max())),
|
|
|
|
formatDuration(s.time.StdDev()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-10-26 23:11:09 +00:00
|
|
|
func (b *base) init() {
|
|
|
|
b.stats = make(map[string]*baseStats)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *base) metrics(name string) *baseStats {
|
|
|
|
if _, ok := b.stats[name]; !ok {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
|
|
|
|
// Double checking being protected by mutex
|
|
|
|
if s, ok := b.stats[name]; ok {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-10-27 00:03:21 +00:00
|
|
|
if b.sampleSize == 0 {
|
|
|
|
b.sampleSize = DefaultSampleSize
|
|
|
|
}
|
2015-10-26 23:11:09 +00:00
|
|
|
b.stats[name] = &baseStats{
|
2015-10-26 23:54:00 +00:00
|
|
|
name: name,
|
2015-10-27 00:03:21 +00:00
|
|
|
time: metrics.NewHistogram(metrics.NewUniformSample(b.sampleSize)),
|
2015-10-26 23:11:09 +00:00
|
|
|
errors: metrics.NewCounter(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.stats[name]
|
|
|
|
}
|
2015-10-26 23:54:00 +00:00
|
|
|
|
|
|
|
func formatDuration(dur float64) string {
|
|
|
|
switch {
|
|
|
|
case dur < 1000:
|
|
|
|
return fmt.Sprintf("%10.0fns", dur)
|
|
|
|
case dur < 1000000:
|
|
|
|
return fmt.Sprintf("%10.3fμs", dur/1000)
|
|
|
|
case dur < 1000000000:
|
|
|
|
return fmt.Sprintf("%10.3fms", dur/1000000)
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%10.3fs", dur/1000000000)
|
|
|
|
}
|
|
|
|
}
|