1
0
Fork 0

Make stats snapshot a server entity

This commit is contained in:
Gregory Eremin 2015-10-29 02:21:34 +03:00
parent 06f9023ed3
commit d0a9829002
2 changed files with 43 additions and 40 deletions

View File

@ -150,41 +150,6 @@ func (s *baseStats) String() string {
) )
} }
func (s *baseStats) snapshot() *baseSnapshot {
return &baseSnapshot{
timestamp: time.Now().UTC().Unix(),
processed: s.time.Count(),
errors: s.errors.Count(),
min: round(float64(s.time.Min())/1000000, 6),
mean: round(s.time.Mean()/1000000, 6),
p95: round(s.time.Percentile(0.95)/1000000, 6),
max: round(float64(s.time.Max())/1000000, 6),
stddev: round(s.time.StdDev()/1000000, 6),
}
}
//
// baseSnapshot
//
type baseSnapshot struct {
timestamp int64
processed int64
errors int64
min float64
mean float64
p95 float64
max float64
stddev float64
}
// Implements json.Marshaler
func (s *baseSnapshot) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("[%d,%d,%d,%.6f,%.6f,%.6f,%.6f,%.6f]",
s.timestamp, s.processed, s.errors, s.min,
s.mean, s.p95, s.max, s.stddev)), nil
}
// //
// Helpers // Helpers
// //

View File

@ -10,18 +10,18 @@ import (
type Server struct { type Server struct {
base base
history map[string][]*baseSnapshot history map[string][]*serverStatsSnapshot
} }
const ( const (
serverSnapshotIntervl = 5 * time.Second serverSnapshotIntervl = 3 * time.Second
serverHistorySize = 360 // 30 minutes of 5 second snapshots serverHistorySize = 30
) )
func NewServer() *Server { func NewServer() *Server {
s := &Server{} s := &Server{}
s.init() s.init()
s.history = make(map[string][]*baseSnapshot) s.history = make(map[string][]*serverStatsSnapshot)
go s.takeSnapshots() go s.takeSnapshots()
return s return s
@ -45,9 +45,47 @@ func (s *Server) takeSnapshots() {
if len(s.history[name]) >= serverHistorySize { if len(s.history[name]) >= serverHistorySize {
s.history[name] = s.history[name][1:] s.history[name] = s.history[name][1:]
} }
s.history[name] = append(s.history[name], stat.snapshot()) s.history[name] = append(s.history[name], makeServerStatsSnapshot(stat))
} }
s.Reset() s.Reset()
s.Unlock() s.Unlock()
} }
} }
//
// Stats
//
func makeServerStatsSnapshot(s *baseStats) *serverStatsSnapshot {
ps := s.time.Percentiles([]float64{0.25, 0.5, 0.75})
return &serverStatsSnapshot{
timestamp: time.Now().UTC().Unix(),
processed: s.time.Count(),
errors: s.errors.Count(),
min: round(float64(s.time.Min())/1000000, 6),
p25: round(ps[0]/1000000, 6),
mean: round(s.time.Mean()/1000000, 6),
median: round(ps[1]/1000000, 6),
p75: round(ps[2]/1000000, 6),
max: round(float64(s.time.Max())/1000000, 6),
}
}
type serverStatsSnapshot struct {
timestamp int64
processed int64
errors int64
min float64
p25 float64
mean float64
median float64
p75 float64
max float64
}
// Implements json.Marshaler
func (s *serverStatsSnapshot) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("[%d,%d,%d,%.6f,%.6f,%.6f,%.6f,%.6f,%.6f]",
s.timestamp, s.processed, s.errors, s.min, s.p25,
s.mean, s.median, s.p75, s.max)), nil
}