Move all stats related code to stats package

This commit is contained in:
2015-10-27 02:54:00 +03:00
parent 1cace592e6
commit 6f56486566
6 changed files with 131 additions and 53 deletions
+97
View File
@@ -1,22 +1,53 @@
package stats
import (
"fmt"
"sync"
"time"
"github.com/rcrowley/go-metrics"
)
type Manager interface {
Publisher
Fetcher
}
type Publisher interface {
Add(name string, dur time.Duration)
Error(name string)
}
type Fetcher interface {
Fetch(name string) Stats
}
type Stats interface {
Processed() int64
Errors() int64
Min() int64
Max() int64
P95() float64
Mean() float64
StdDev() float64
}
type base struct {
sync.Mutex
stats map[string]*baseStats
}
type baseStats struct {
name string
time metrics.Histogram
errors metrics.Counter
}
const (
Latency = "Latency"
TaskWait = "TaskWait"
)
func (b *base) Add(name string, dur time.Duration) {
b.metrics(name).time.Update(int64(dur))
}
@@ -25,6 +56,58 @@ func (b *base) Error(name string) {
b.metrics(name).errors.Inc(1)
}
func (b *base) Fetch(name string) Stats {
return b.metrics(name)
}
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()),
)
}
func (b *base) init() {
b.stats = make(map[string]*baseStats)
}
@@ -40,6 +123,7 @@ func (b *base) metrics(name string) *baseStats {
}
b.stats[name] = &baseStats{
name: name,
time: metrics.NewHistogram(metrics.NewUniformSample(1000)),
errors: metrics.NewCounter(),
}
@@ -47,3 +131,16 @@ func (b *base) metrics(name string) *baseStats {
return b.stats[name]
}
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)
}
}
+12
View File
@@ -0,0 +1,12 @@
package stats
type Basic struct {
base
}
func NewBasicStats() *Basic {
b := &Basic{}
b.init()
return b
}
+2 -4
View File
@@ -2,15 +2,13 @@ package stats
import (
"time"
"github.com/localhots/satan"
)
type Group struct {
backends []satan.StatsPublisher
backends []Publisher
}
func NewGroup(backends ...satan.StatsPublisher) *Group {
func NewGroup(backends ...Publisher) *Group {
return &Group{
backends: backends,
}
+3 -19
View File
@@ -1,7 +1,6 @@
package stats
import (
"fmt"
"io"
"os"
"time"
@@ -30,24 +29,9 @@ func NewStdoutLogger(interval time.Duration) *Logger {
}
func (l *Logger) Print() {
for name, s := range l.stats {
fmt.Fprintf(l.out, "%s statistics:\n"+
"Processed: %d\n"+
"Errors: %d\n"+
"Min: %.8fms\n"+
"Max: %.8fms\n"+
"95%%: %.8fms\n"+
"Mean: %.8fms\n"+
"StdDev: %.8fms\n",
name,
s.time.Count(),
s.errors.Count(),
float64(s.time.Min())/1000000,
float64(s.time.Max())/1000000,
s.time.Percentile(0.95)/1000000,
s.time.Mean()/1000000,
s.time.StdDev()/1000000,
)
for _, s := range l.stats {
l.out.Write([]byte(s.String()))
l.out.Write([]byte{'\n'})
s.time.Clear()
s.errors.Clear()
}