aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-rw-r--r--pkg/stats/avg.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/pkg/stats/avg.go b/pkg/stats/avg.go
new file mode 100644
index 000000000..430ff335b
--- /dev/null
+++ b/pkg/stats/avg.go
@@ -0,0 +1,32 @@
+// Copyright 2024 syzkaller project authors. All rights reserved.
+// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
+
+package stats
+
+import (
+ "sync"
+ "time"
+)
+
+type AverageParameter interface {
+ time.Duration
+}
+
+type AverageValue[T AverageParameter] struct {
+ mu sync.Mutex
+ total int64
+ avg T
+}
+
+func (av *AverageValue[T]) Value() T {
+ av.mu.Lock()
+ defer av.mu.Unlock()
+ return av.avg
+}
+
+func (av *AverageValue[T]) Save(val T) {
+ av.mu.Lock()
+ defer av.mu.Unlock()
+ av.total++
+ av.avg += (val - av.avg) / T(av.total)
+}