-
Notifications
You must be signed in to change notification settings - Fork 0
/
timings.go
61 lines (51 loc) · 1.45 KB
/
timings.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
package timings
import (
"log"
"os"
"time"
)
// Tracker is a simple device to track timings
type Tracker struct {
start time.Time
end time.Time
}
// Start records the start time
func (t *Tracker) Start() {
t.start = time.Now()
}
// Stop records the end time
func (t *Tracker) Stop() {
t.end = time.Now()
}
// Since returns a Duration since the start time
func (t *Tracker) Since() time.Duration {
return time.Since(t.start)
}
// Duration returns the Duration of time between Start() and Stop()
func (t *Tracker) Duration() time.Duration {
return t.end.Sub(t.start)
}
// Track tracks how long a function takes, to a log.Logger
// e.g. make the first line
// defer Track("func_name", time.Now(), logger).
func Track(name string, start time.Time, l *log.Logger) {
elapsed := time.Since(start)
l.Printf("%s took %s", name, elapsed)
}
// TrackOut tracks how long a function takes, to Stdout
// e.g. make the first line:
// defer TrackOut("func_name", time.Now()).
func TrackOut(name string, start time.Time) {
Track(name, start, log.New(os.Stdout, "[TIMING]", log.Lshortfile))
}
// TrackErr tracks how long a function takes, to Stderr
// e.g. make the first line:
// defer Trackerr("func_name", time.Now()).
func TrackErr(name string, start time.Time) {
Track(name, start, log.New(os.Stderr, "[TIMING]", log.Lshortfile))
}
// Now is equivalent to time.Now(), but saves the import of "time" if
// it's not otherwise needed
func Now() time.Time {
return time.Now()
}