-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
summary.go
121 lines (99 loc) · 2.42 KB
/
summary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"io"
"sort"
"time"
"github.com/aquasecurity/table"
"github.com/liamg/grace/tracer"
)
func configureSummary(t *tracer.Tracer, w io.Writer, sortKey string) {
tracker := &tracker{
counts: make(map[string]int),
errors: make(map[string]int),
durations: make(map[string]time.Duration),
starts: make(map[string]time.Time),
}
t.SetSyscallEnterHandler(tracker.recordEnter)
t.SetSyscallExitHandler(tracker.recordExit)
t.SetDetachHandler(func(i int) {
tracker.print(w, sortKey)
})
}
type tracker struct {
counts map[string]int
errors map[string]int
durations map[string]time.Duration
starts map[string]time.Time
}
func (t *tracker) recordEnter(s *tracer.Syscall) {
t.starts[s.Name()] = time.Now()
}
func (t *tracker) recordExit(s *tracer.Syscall) {
stop := time.Now()
if start, ok := t.starts[s.Name()]; ok {
t.durations[s.Name()] += stop.Sub(start)
delete(t.starts, s.Name())
}
if s.Return().Int() < 0 {
t.errors[s.Name()]++
}
t.counts[s.Name()]++
}
func (t *tracker) print(w io.Writer, sortKey string) {
tab := table.New(w)
tab.SetRowLines(false)
tab.AddHeaders("time %", "seconds", "usecs/call", "count", "errors", "syscall")
tab.SetAlignment(table.AlignRight, table.AlignRight, table.AlignRight, table.AlignRight, table.AlignRight, table.AlignLeft)
tab.SetLineStyle(table.StyleBlue)
var total time.Duration
for _, duration := range t.durations {
total += duration
}
type row struct {
sortKey int
cols []string
name string
}
var rows []row
for name, count := range t.counts {
duration := t.durations[name]
percent := float64(duration) * 100 / float64(total)
var key int
switch sortKey {
case "count":
key = count
case "time":
key = int(percent * 100000)
case "seconds":
key = int(duration)
case "errors":
key = t.errors[name]
}
rows = append(rows, row{
name: name,
sortKey: key,
cols: []string{
fmt.Sprintf("%.2f", percent),
fmt.Sprintf("%.6f", duration.Seconds()),
fmt.Sprintf("%d", duration.Microseconds()/int64(count)),
fmt.Sprintf("%d", count),
fmt.Sprintf("%d", t.errors[name]),
name,
},
})
}
if sortKey == "" {
sort.Slice(rows, func(i, j int) bool {
return rows[i].name < rows[j].name
})
} else {
sort.Slice(rows, func(i, j int) bool {
return rows[i].sortKey > rows[j].sortKey
})
}
for _, row := range rows {
tab.AddRow(row.cols...)
}
tab.Render()
}