-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtimer.go
79 lines (67 loc) · 1.56 KB
/
gtimer.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package gtimer
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/petermattis/goid"
)
type (
ResultWriter func(topic string, steps Steps)
timer struct {
gid int64
topic string
steps []StepInfo
}
)
var timerMap sync.Map
//Start create a timer and begin recording
func Start(topic string) *timer {
t := &timer{
gid: goid.Get(),
topic: topic,
steps: []StepInfo{{At: time.Now(), Name: "begin"}},
}
timerMap.Store(t.gid, t)
return t
}
//End end up the recording, and write the result by the default result writer
func (t *timer) End() {
t.EndWiteWriter(defaultResultWriter)
}
//End end up the recording, and write the result by the given result writer
func (t *timer) EndWiteWriter(w ResultWriter) {
t.tick("end")
if w != nil {
w(t.topic, t.steps)
}
timerMap.Delete(t.gid)
}
func (t *timer) tick(step string) {
t.steps = append(t.steps, StepInfo{At: time.Now(), Name: step})
}
//Tick
func Tick(step string) {
t, found := timerMap.Load(goid.Get())
if !found {
return
}
timer, isTimer := t.(*timer)
if !isTimer {
return
}
timer.tick(step)
}
func defaultWriteResult(topic string, steps Steps) {
rc := rand.Intn(10000)
fmt.Printf("Time cost for topic '%s@%04d' total %s: \n", topic, rc, steps.TotalDuration())
for i := 1; i < len(steps); i++ {
fmt.Printf(" |- [%s@%04d: \"%s\" ~ \"%s\"]: %s\n",
topic, rc, steps[i-1].Name, steps[i].Name, steps.DurationBetween(i-1, i))
}
}
var defaultResultWriter = defaultWriteResult
//SetDefaultWriter set default result writer
func SetDefaultWriter(w ResultWriter) {
defaultResultWriter = w
}