-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
50 lines (42 loc) · 1.45 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package fixedts
import (
"fmt"
"math"
"time"
)
type Stats struct {
From time.Time
Steps uint64
TimeAccum float64
StepRate float64
StepAverage float64
GapAccum float64
GapAbsAccum float64
GapAbsAverage float64
GapMax float64
GapMin float64
}
func (ts *FixedTimestep) newStats() *Stats {
return &Stats{From: ts.Now, GapMax: math.Inf(-1), GapMin: math.Inf(1)}
}
func (ts *FixedTimestep) updateStats(stats *Stats) {
stats.Steps++
stats.TimeAccum += ts.Delta
stats.GapAccum += ts.Gap
stats.GapAbsAccum += math.Abs(ts.Gap)
stats.StepRate = float64(stats.Steps) / stats.TimeAccum
stats.StepAverage = stats.TimeAccum / float64(stats.Steps)
if ts.Gap > stats.GapMax {
stats.GapMax = ts.Gap
}
if ts.Gap < stats.GapMin {
stats.GapMin = ts.Gap
}
stats.GapAbsAverage = stats.GapAbsAccum / float64(stats.Steps)
}
func (stats *Stats) String() string {
return fmt.Sprintf("elapsed: %s from: %s\nsteps: %d avg: %s rate: %0.3f/s\ngap_accum: %s gap_abs_accum: %s gap_abs_avg: %s gap_min: %s gap_max: %s", secondsToDuration(stats.TimeAccum), stats.From, stats.Steps, secondsToDuration(stats.StepAverage), stats.StepRate, secondsToDuration(stats.GapAccum), secondsToDuration(stats.GapAbsAccum), secondsToDuration(stats.GapAbsAverage), secondsToDuration(stats.GapMin), secondsToDuration(stats.GapMax))
}
func secondsToDuration(seconds float64) time.Duration {
return time.Duration(seconds * float64(time.Second))
}