fs/accounting: fix moving average speed for file stats

Before this change the moving average for the individual file stats
would start at 0 and only converge to the correct value over 15-30
seconds.

This change starts the weighting period as 1 and moves it up once per
sample which gets the average to a better value instantly.
This commit is contained in:
Nick Craig-Wood 2018-08-28 22:55:51 +01:00
parent 66fe4a2523
commit de6ec8056f
1 changed files with 6 additions and 1 deletions

View File

@ -129,6 +129,7 @@ func (acc *Account) UpdateReader(in io.ReadCloser) {
// averageLoop calculates averages for the stats in the background
func (acc *Account) averageLoop() {
tick := time.NewTicker(time.Second)
var period float64
defer tick.Stop()
for {
select {
@ -137,7 +138,11 @@ func (acc *Account) averageLoop() {
// Add average of last second.
elapsed := now.Sub(acc.lpTime).Seconds()
avg := float64(acc.lpBytes) / elapsed
acc.avg = (avg + (averagePeriod-1)*acc.avg) / averagePeriod
// Soft start the moving average
if period < averagePeriod {
period++
}
acc.avg = (avg + (period-1)*acc.avg) / period
acc.lpBytes = 0
acc.lpTime = now
// Unlock stats