raft_follower_stats.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. type raftFollowerStats struct {
  11. Latency struct {
  12. Current float64 `json:"current"`
  13. Average float64 `json:"average"`
  14. averageSquare float64
  15. StandardDeviation float64 `json:"standardDeviation"`
  16. Minimum float64 `json:"minimum"`
  17. Maximum float64 `json:"maximum"`
  18. } `json:"latency"`
  19. Counts struct {
  20. Fail uint64 `json:"fail"`
  21. Success uint64 `json:"success"`
  22. } `json:"counts"`
  23. }
  24. // Succ function update the raftFollowerStats with a successful send
  25. func (ps *raftFollowerStats) Succ(d time.Duration) {
  26. total := float64(ps.Counts.Success) * ps.Latency.Average
  27. totalSquare := float64(ps.Counts.Success) * ps.Latency.averageSquare
  28. ps.Counts.Success++
  29. ps.Latency.Current = float64(d) / (1000000.0)
  30. if ps.Latency.Current > ps.Latency.Maximum {
  31. ps.Latency.Maximum = ps.Latency.Current
  32. }
  33. if ps.Latency.Current < ps.Latency.Minimum {
  34. ps.Latency.Minimum = ps.Latency.Current
  35. }
  36. ps.Latency.Average = (total + ps.Latency.Current) / float64(ps.Counts.Success)
  37. ps.Latency.averageSquare = (totalSquare + ps.Latency.Current*ps.Latency.Current) / float64(ps.Counts.Success)
  38. // sdv = sqrt(avg(x^2) - avg(x)^2)
  39. ps.Latency.StandardDeviation = math.Sqrt(ps.Latency.averageSquare - ps.Latency.Average*ps.Latency.Average)
  40. }
  41. // Fail function update the raftFollowerStats with a unsuccessful send
  42. func (ps *raftFollowerStats) Fail() {
  43. ps.Counts.Fail++
  44. }