Merge lp:~jameinel/juju-utils/timeit into lp:juju-utils

Proposed by John A Meinel
Status: Needs review
Proposed branch: lp:~jameinel/juju-utils/timeit
Merge into: lp:juju-utils
Diff against target: 59 lines (+54/-0)
1 file modified
utils/timeit.go (+54/-0)
To merge this branch: bzr merge lp:~jameinel/juju-utils/timeit
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+165294@code.launchpad.net

Description of the change

Pulling the timeit functionality out of juju-core itself.
It still is the only thing that gives wallclock time for what functions you're blocking in.
The nesting can still be wrong with concurrent routines, but it still gives correct *time* just incorrect nesting of the output display.

We talked about moving this out into "juju-core-utils", but since you've already created juju-utils I figured I'd just use it.
The focus is slightly different (your's seems more executable vs library oriented), but it seems reasonable.

To post a comment you must log in.

Unmerged revisions

3. By John A Meinel

Add timeit from juju-core itself.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'utils'
2=== added file 'utils/timeit.go'
3--- utils/timeit.go 1970-01-01 00:00:00 +0000
4+++ utils/timeit.go 2013-05-23 07:22:23 +0000
5@@ -0,0 +1,54 @@
6+package utils
7+
8+import (
9+ "fmt"
10+ "os"
11+ "time"
12+)
13+
14+type timer struct {
15+ action string
16+ start time.Time
17+ depth int
18+ duration time.Duration
19+ subActions []*timer
20+}
21+
22+func (t *timer) String() string {
23+ this := fmt.Sprintf("%.3fs %*s%s\n", t.duration.Seconds(), t.depth, "", t.action)
24+ for _, sub := range t.subActions {
25+ this += sub.String()
26+ }
27+ return this
28+}
29+
30+var stack []*timer
31+
32+// Start a timer, used for tracking time spent.
33+// Generally used with either defer, as in:
34+// defer utils.Timeit("my func")()
35+// Which will track how much time is spent in your function. Or
36+// if you want to track the time spent in a function you are calling
37+// then you would use:
38+// toc := utils.Timeit("anotherFunc()")
39+// anotherFunc()
40+// toc()
41+// This tracks nested calls by indenting the output, and will print out the
42+// full stack of timing when we reach the top of the stack.
43+func Timeit(action string) func() {
44+ cur := &timer{action: action, start: time.Now(), depth: len(stack)}
45+ if len(stack) != 0 {
46+ tip := stack[len(stack)-1]
47+ tip.subActions = append(tip.subActions, cur)
48+ }
49+ stack = append(stack, cur)
50+ return func() {
51+ cur.duration = time.Since(cur.start)
52+ if len(stack) == 0 || cur == stack[0] {
53+ fmt.Fprint(os.Stderr, cur)
54+ stack = nil
55+ } else {
56+ stack = stack[0 : len(stack)-1]
57+ }
58+ }
59+}

Subscribers

People subscribed via source and target branches