Skip to content

Commit

Permalink
feat(utils): add time statistics tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
xendarboh committed Sep 30, 2023
1 parent 61b119d commit 052f0b5
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions cmd/xtrellis/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,43 @@ func DebugLog(format string, args ...interface{}) {
s2 := fmt.Sprintf(format, args...)
log.Printf("%s %s", s1, s2)
}

// Time statistics tracker
type TimeStats struct {
Avg float64
Last float64
Max float64
Min float64
Total float64

times []float64
}

func NewTimeStats() *TimeStats {
return &TimeStats{}
}

func (ts *TimeStats) RecordTime(time float64) {
init := len(ts.times) == 0

if init || time < ts.Min {
ts.Min = time
}

if init || time > ts.Max {
ts.Max = time
}

ts.Last = time

ts.Total += time

ts.times = append(ts.times, time)

ts.Avg = ts.Total / float64(len(ts.times))
}

func (ts *TimeStats) GetStatsString() string {
var ms float64 = 1000000
return fmt.Sprintf("last/min/avg/max = %.2f / %.2f / %.2f / %.2f ms", ts.Last/ms, ts.Min/ms, ts.Avg/ms, ts.Max/ms)
}

0 comments on commit 052f0b5

Please sign in to comment.