-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.go
More file actions
36 lines (26 loc) · 673 Bytes
/
timer.go
File metadata and controls
36 lines (26 loc) · 673 Bytes
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
package DCP
import "time"
type TimerEntry struct {
Avg int64 `json:"avg"`
Counter int `json:"counter"`
}
type Timer struct {
Timers map[string]TimerEntry `json:"timers"`
}
func (t *Timer) Time(timerName string, from time.Time) {
endTime := time.Now()
timerEntry := TimerEntry{
Avg: 0,
Counter: 1,
}
if te, exist := t.Timers[timerName]; exist {
timerEntry = te
}
runTime := endTime.Sub(from)
timerEntry.Avg = (timerEntry.Avg + int64(runTime)) / int64(timerEntry.Counter)
timerEntry.Counter = timerEntry.Counter + 1
t.Timers[timerName] = timerEntry
}
func NewTimer(timerName string) (string, time.Time) {
return timerName, time.Now()
}