13
duration time.Duration
17
func (t *timer) String() string {
18
this := fmt.Sprintf("%.3fs %*s%s\n", t.duration.Seconds(), t.depth, "", t.action)
19
for _, sub := range t.subActions {
27
// Start a timer, used for tracking time spent.
28
// Generally used with either defer, as in:
29
// defer utils.Timeit("my func")()
30
// Which will track how much time is spent in your function. Or
31
// if you want to track the time spent in a function you are calling
32
// then you would use:
33
// toc := utils.Timeit("anotherFunc()")
36
// This tracks nested calls by indenting the output, and will print out the
37
// full stack of timing when we reach the top of the stack.
38
func Timeit(action string) func() {
39
cur := &timer{action: action, start: time.Now(), depth: len(stack)}
41
tip := stack[len(stack)-1]
42
tip.subActions = append(tip.subActions, cur)
44
stack = append(stack, cur)
46
cur.duration = time.Since(cur.start)
48
fmt.Fprint(os.Stderr, cur)
51
stack = stack[0 : len(stack)-1]