forked from iotaledger/wasp-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.go
More file actions
60 lines (51 loc) · 1.07 KB
/
Copy pathtimer.go
File metadata and controls
60 lines (51 loc) · 1.07 KB
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
51
52
53
54
55
56
57
58
59
60
package util
import (
"fmt"
"time"
)
type timerStep struct {
name string
start time.Time
duration time.Duration
}
type Timer struct {
steps []*timerStep
}
func NewTimer() *Timer {
return &Timer{
steps: []*timerStep{
{name: "pending", start: time.Now()},
},
}
}
func (t *Timer) Duration() time.Duration {
return time.Since(t.steps[0].start)
}
func (t *Timer) Step(name string) {
t.Done(name)
t.steps = append(t.steps, t.newStep())
}
func (t *Timer) lastStep() *timerStep {
return t.steps[len(t.steps)-1]
}
func (t *Timer) newStep() *timerStep {
return &timerStep{name: "pending", start: time.Now()}
}
func (t *Timer) Done(name string) {
lastStep := t.lastStep()
if lastStep.duration == 0 {
lastStep.name = name
lastStep.duration = time.Since(lastStep.start)
}
}
func (t *Timer) String() string {
t.Done("last")
if len(t.steps) == 1 {
return fmt.Sprintf("Total: %v", t.Duration())
}
stepsStr := ""
for _, st := range t.steps {
stepsStr += fmt.Sprintf(", %v=%v", st.name, st.duration)
}
return fmt.Sprintf("Total: %v%s", t.Duration(), stepsStr)
}