Make stats snapshot a server entity
This commit is contained in:
		
							parent
							
								
									06f9023ed3
								
							
						
					
					
						commit
						d0a9829002
					
				@ -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
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
@ -10,18 +10,18 @@ import (
 | 
			
		||||
type Server struct {
 | 
			
		||||
	base
 | 
			
		||||
 | 
			
		||||
	history map[string][]*baseSnapshot
 | 
			
		||||
	history map[string][]*serverStatsSnapshot
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	serverSnapshotIntervl = 5 * time.Second
 | 
			
		||||
	serverHistorySize     = 360 // 30 minutes of 5 second snapshots
 | 
			
		||||
	serverSnapshotIntervl = 3 * time.Second
 | 
			
		||||
	serverHistorySize     = 30
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func NewServer() *Server {
 | 
			
		||||
	s := &Server{}
 | 
			
		||||
	s.init()
 | 
			
		||||
	s.history = make(map[string][]*baseSnapshot)
 | 
			
		||||
	s.history = make(map[string][]*serverStatsSnapshot)
 | 
			
		||||
	go s.takeSnapshots()
 | 
			
		||||
 | 
			
		||||
	return s
 | 
			
		||||
@ -45,9 +45,47 @@ func (s *Server) takeSnapshots() {
 | 
			
		||||
			if len(s.history[name]) >= serverHistorySize {
 | 
			
		||||
				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.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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user