| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package state |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"fmt" |
| 20 |
"sort" |
| 21 |
"time" |
| 22 |
) |
| 23 |
|
| 24 |
type statEntry struct { |
| 25 |
count uint |
| 26 |
total time.Duration |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
type Stats struct { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
byType []statEntry |
| 40 |
|
| 41 |
|
| 42 |
stack []typeID |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
names []string |
| 50 |
|
| 51 |
|
| 52 |
last time.Time |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
func (s *Stats) init() { |
| 57 |
s.last = time.Now() |
| 58 |
s.stack = append(s.stack, 0) |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
func (s *Stats) fini(resolve func(id typeID) string) { |
| 63 |
s.done() |
| 64 |
|
| 65 |
|
| 66 |
s.names = make([]string, len(s.byType)) |
| 67 |
s.names[0] = "state.default" |
| 68 |
for id := typeID(1); int(id) < len(s.names); id++ { |
| 69 |
s.names[id] = resolve(id) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func (s *Stats) sample(id typeID) { |
| 75 |
now := time.Now() |
| 76 |
if len(s.byType) <= int(id) { |
| 77 |
|
| 78 |
s.byType = append(s.byType, make([]statEntry, 1+int(id)-len(s.byType))...) |
| 79 |
} |
| 80 |
s.byType[id].total += now.Sub(s.last) |
| 81 |
s.last = now |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
func (s *Stats) start(id typeID) { |
| 86 |
last := s.stack[len(s.stack)-1] |
| 87 |
s.sample(last) |
| 88 |
s.stack = append(s.stack, id) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func (s *Stats) done() { |
| 93 |
last := s.stack[len(s.stack)-1] |
| 94 |
s.sample(last) |
| 95 |
s.byType[last].count++ |
| 96 |
s.stack = s.stack[:len(s.stack)-1] |
| 97 |
} |
| 98 |
|
| 99 |
type sliceEntry struct { |
| 100 |
name string |
| 101 |
entry *statEntry |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func (s *Stats) String() string { |
| 106 |
|
| 107 |
ss := make([]sliceEntry, 0, len(s.byType)) |
| 108 |
for id := 0; id < len(s.names); id++ { |
| 109 |
ss = append(ss, sliceEntry{ |
| 110 |
name: s.names[id], |
| 111 |
entry: &s.byType[id], |
| 112 |
}) |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
sort.Slice(ss, func(i, j int) bool { |
| 117 |
return ss[i].entry.total > ss[j].entry.total |
| 118 |
}) |
| 119 |
|
| 120 |
|
| 121 |
var ( |
| 122 |
buf bytes.Buffer |
| 123 |
count uint |
| 124 |
total time.Duration |
| 125 |
) |
| 126 |
buf.WriteString("\n") |
| 127 |
buf.WriteString(fmt.Sprintf("% 16s | % 8s | % 16s | %s\n", "total", "count", "per", "type")) |
| 128 |
buf.WriteString("-----------------+----------+------------------+----------------\n") |
| 129 |
for _, se := range ss { |
| 130 |
if se.entry.count == 0 { |
| 131 |
|
| 132 |
|
| 133 |
continue |
| 134 |
} |
| 135 |
count += se.entry.count |
| 136 |
total += se.entry.total |
| 137 |
per := se.entry.total / time.Duration(se.entry.count) |
| 138 |
buf.WriteString(fmt.Sprintf("% 16s | %8d | % 16s | %s\n", |
| 139 |
se.entry.total, se.entry.count, per, se.name)) |
| 140 |
} |
| 141 |
buf.WriteString("-----------------+----------+------------------+----------------\n") |
| 142 |
buf.WriteString(fmt.Sprintf("% 16s | % 8d | % 16s | [all]", |
| 143 |
total, count, total/time.Duration(count))) |
| 144 |
return string(buf.Bytes()) |
| 145 |
} |