1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
16
duration time.Duration
20
func (t *timer) String() string {
21
this := fmt.Sprintf("%.3fs %*s%s\n", t.duration.Seconds(), t.depth, "", t.action)
22
for _, sub := range t.subActions {
30
// Start a timer, used for tracking time spent.
31
// Generally used with either defer, as in:
32
// defer utils.Timeit("my func")()
33
// Which will track how much time is spent in your function. Or
34
// if you want to track the time spent in a function you are calling
35
// then you would use:
36
// toc := utils.Timeit("anotherFunc()")
39
// This tracks nested calls by indenting the output, and will print out the
40
// full stack of timing when we reach the top of the stack.
41
func Timeit(action string) func() {
42
cur := &timer{action: action, start: time.Now(), depth: len(stack)}
44
tip := stack[len(stack)-1]
45
tip.subActions = append(tip.subActions, cur)
47
stack = append(stack, cur)
49
cur.duration = time.Since(cur.start)
50
if len(stack) == 0 || cur == stack[0] {
51
fmt.Fprint(os.Stderr, cur)
54
stack = stack[0 : len(stack)-1]