fix: go back to the old averaging
All checks were successful
Release Tag / release (push) Successful in 12s

This commit is contained in:
2025-01-22 19:35:17 -06:00
parent 7401c040dc
commit fed9bbe218

View File

@@ -7,11 +7,9 @@ import (
// AvgCacheState is a cache state that averages the last N cache states.
type AvgCacheState struct {
size int
hits int
misses int
avgs []cachestate.CacheState
mu sync.Mutex
size int
avgs []cachestate.CacheState
mu sync.Mutex
}
// New creates a new average cache state with the given size.
@@ -44,21 +42,8 @@ func (a *AvgCacheState) Add(cs cachestate.CacheState) {
a.avgs = append(a.avgs, cs)
if len(a.avgs) > a.size {
switch a.avgs[0] {
case cachestate.CacheStateHit:
a.hits--
case cachestate.CacheStateMiss:
a.misses--
}
a.avgs = a.avgs[1:]
}
switch cs {
case cachestate.CacheStateHit:
a.hits++
case cachestate.CacheStateMiss:
a.misses++
}
}
// Avg returns the average cache state.
@@ -66,5 +51,13 @@ func (a *AvgCacheState) Avg() float64 {
a.mu.Lock()
defer a.mu.Unlock()
return float64(a.hits) / (float64(a.misses) + 0.0000001)
var hits int
for _, cs := range a.avgs {
if cs == cachestate.CacheStateHit {
hits++
}
}
return float64(hits) / float64(len(a.avgs))
}