github.com/macb/etcd@v0.3.1-0.20140227003422-a60481c6b1a0/server/raft_follower_stats.go (about)

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