| |
| |
| |
|
|
| package base |
|
|
| import ( |
| "fmt" |
| "io" |
| "strings" |
| "time" |
| ) |
|
|
| var Timer Timings |
|
|
| |
| |
| |
| type Timings struct { |
| list []timestamp |
| events map[int][]*event |
| } |
|
|
| type timestamp struct { |
| time time.Time |
| label string |
| start bool |
| } |
|
|
| type event struct { |
| size int64 |
| unit string |
| } |
|
|
| func (t *Timings) append(labels []string, start bool) { |
| t.list = append(t.list, timestamp{time.Now(), strings.Join(labels, ":"), start}) |
| } |
|
|
| |
| |
| func (t *Timings) Start(labels ...string) { |
| t.append(labels, true) |
| } |
|
|
| |
| |
| func (t *Timings) Stop(labels ...string) { |
| t.append(labels, false) |
| } |
|
|
| |
| |
| |
| |
| func (t *Timings) AddEvent(size int64, unit string) { |
| m := t.events |
| if m == nil { |
| m = make(map[int][]*event) |
| t.events = m |
| } |
| i := len(t.list) |
| if i > 0 { |
| i-- |
| } |
| m[i] = append(m[i], &event{size, unit}) |
| } |
|
|
| |
| |
| func (t *Timings) Write(w io.Writer, prefix string) { |
| if len(t.list) > 0 { |
| var lines lines |
|
|
| |
| var group struct { |
| label string |
| tot time.Duration |
| size int |
| } |
|
|
| |
| var unaccounted time.Duration |
|
|
| |
| pt := &t.list[0] |
| tot := t.list[len(t.list)-1].time.Sub(pt.time) |
| for i := 1; i < len(t.list); i++ { |
| qt := &t.list[i] |
| dt := qt.time.Sub(pt.time) |
|
|
| var label string |
| var events []*event |
| if pt.start { |
| |
| label = pt.label |
| events = t.events[i-1] |
| if qt.start { |
| |
| } else { |
| |
| if qt.label != "" { |
| label += ":" + qt.label |
| } |
| |
| if e := t.events[i]; e != nil { |
| events = e |
| } |
| } |
| } else { |
| |
| if qt.start { |
| |
| unaccounted += dt |
| } else { |
| |
| label = qt.label |
| events = t.events[i] |
| } |
| } |
| if label != "" { |
| |
| l := commonPrefix(group.label, label) |
| if group.size == 1 && l != "" || group.size > 1 && l == group.label { |
| |
| group.label = l |
| group.tot += dt |
| group.size++ |
| } else { |
| |
| if group.size > 1 { |
| lines.add(prefix+group.label+"subtotal", 1, group.tot, tot, nil) |
| } |
| group.label = label |
| group.tot = dt |
| group.size = 1 |
| } |
|
|
| |
| lines.add(prefix+label, 1, dt, tot, events) |
| } |
|
|
| pt = qt |
| } |
|
|
| if group.size > 1 { |
| lines.add(prefix+group.label+"subtotal", 1, group.tot, tot, nil) |
| } |
|
|
| if unaccounted != 0 { |
| lines.add(prefix+"unaccounted", 1, unaccounted, tot, nil) |
| } |
|
|
| lines.add(prefix+"total", 1, tot, tot, nil) |
|
|
| lines.write(w) |
| } |
| } |
|
|
| func commonPrefix(a, b string) string { |
| i := 0 |
| for i < len(a) && i < len(b) && a[i] == b[i] { |
| i++ |
| } |
| return a[:i] |
| } |
|
|
| type lines [][]string |
|
|
| func (lines *lines) add(label string, n int, dt, tot time.Duration, events []*event) { |
| var line []string |
| add := func(format string, args ...any) { |
| line = append(line, fmt.Sprintf(format, args...)) |
| } |
|
|
| add("%s", label) |
| add(" %d", n) |
| add(" %d ns/op", dt) |
| add(" %.2f %%", float64(dt)/float64(tot)*100) |
|
|
| for _, e := range events { |
| add(" %d", e.size) |
| add(" %s", e.unit) |
| add(" %d", int64(float64(e.size)/dt.Seconds()+0.5)) |
| add(" %s/s", e.unit) |
| } |
|
|
| *lines = append(*lines, line) |
| } |
|
|
| func (lines lines) write(w io.Writer) { |
| |
| var widths []int |
| var number []bool |
| for _, line := range lines { |
| for i, col := range line { |
| if i < len(widths) { |
| if len(col) > widths[i] { |
| widths[i] = len(col) |
| } |
| } else { |
| widths = append(widths, len(col)) |
| number = append(number, isnumber(col)) |
| } |
| } |
| } |
|
|
| |
| const align = 1 |
| if align > 1 { |
| for i, w := range widths { |
| w += align - 1 |
| widths[i] = w - w%align |
| } |
| } |
|
|
| |
| for _, line := range lines { |
| for i, col := range line { |
| format := "%-*s" |
| if number[i] { |
| format = "%*s" |
| } |
| fmt.Fprintf(w, format, widths[i], col) |
| } |
| fmt.Fprintln(w) |
| } |
| } |
|
|
| func isnumber(s string) bool { |
| for _, ch := range s { |
| if ch <= ' ' { |
| continue |
| } |
| return '0' <= ch && ch <= '9' || ch == '.' || ch == '-' || ch == '+' |
| } |
| return false |
| } |
|
|