github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/stats/avg.go (about) 1 // Copyright 2024 syzkaller project authors. All rights reserved. 2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. 3 4 package stats 5 6 import ( 7 "sync" 8 "time" 9 ) 10 11 type AverageParameter interface { 12 time.Duration 13 } 14 15 type AverageValue[T AverageParameter] struct { 16 mu sync.Mutex 17 total int64 18 avg T 19 } 20 21 func (av *AverageValue[T]) Value() T { 22 av.mu.Lock() 23 defer av.mu.Unlock() 24 return av.avg 25 } 26 27 func (av *AverageValue[T]) Save(val T) { 28 av.mu.Lock() 29 defer av.mu.Unlock() 30 av.total++ 31 av.avg += (val - av.avg) / T(av.total) 32 }