raft_follower_stats.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package server
  2. import (
  3. "math"
  4. "time"
  5. )
  6. type raftFollowersStats struct {
  7. Leader string `json:"leader"`
  8. Followers map[string]*raftFollowerStats `json:"followers"`
  9. }
  10. func NewRaftFollowersStats(name string) *raftFollowersStats {
  11. return &raftFollowersStats{
  12. Leader: name,
  13. Followers: make(map[string]*raftFollowerStats),
  14. }
  15. }
  16. type raftFollowerStats struct {
  17. Latency struct {
  18. Current float64 `json:"current"`
  19. Average float64 `json:"average"`
  20. averageSquare float64
  21. StandardDeviation float64 `json:"standardDeviation"`
  22. Minimum float64 `json:"minimum"`
  23. Maximum float64 `json:"maximum"`
  24. } `json:"latency"`
  25. Counts struct {
  26. Fail uint64 `json:"fail"`
  27. Success uint64 `json:"success"`
  28. } `json:"counts"`
  29. }
  30. // Succ function update the raftFollowerStats with a successful send
  31. func (ps *raftFollowerStats) Succ(d time.Duration) {
  32. total := float64(ps.Counts.Success) * ps.Latency.Average
  33. totalSquare := float64(ps.Counts.Success) * ps.Latency.averageSquare
  34. ps.Counts.Success++
  35. ps.Latency.Current = float64(d) / (1000000.0)
  36. if ps.Latency.Current > ps.Latency.Maximum {
  37. ps.Latency.Maximum = ps.Latency.Current
  38. }
  39. if ps.Latency.Current < ps.Latency.Minimum {
  40. ps.Latency.Minimum = ps.Latency.Current
  41. }
  42. ps.Latency.Average = (total + ps.Latency.Current) / float64(ps.Counts.Success)
  43. ps.Latency.averageSquare = (totalSquare + ps.Latency.Current*ps.Latency.Current) / float64(ps.Counts.Success)
  44. // sdv = sqrt(avg(x^2) - avg(x)^2)
  45. ps.Latency.StandardDeviation = math.Sqrt(ps.Latency.averageSquare - ps.Latency.Average*ps.Latency.Average)
  46. }
  47. // Fail function update the raftFollowerStats with a unsuccessful send
  48. func (ps *raftFollowerStats) Fail() {
  49. ps.Counts.Fail++
  50. }