/
utils.go
190 lines (169 loc) · 4.27 KB
/
utils.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package cmd
import (
"fmt"
"os"
"strings"
"github.com/tideways/toolkit/xhprof"
"github.com/olekukonko/tablewriter"
)
type Unit struct {
Name string
Divisor float32
}
var (
ms Unit = Unit{Name: "ms", Divisor: 1000.0}
kb Unit = Unit{Name: "KB", Divisor: 1024.0}
plain Unit = Unit{Name: "", Divisor: 1.0}
)
type FieldInfo struct {
Name string
Label string
Header string
Unit Unit
}
var fieldsMap map[string]FieldInfo = map[string]FieldInfo{
"wt": FieldInfo{
Name: "WallTime",
Label: "Inclusive Wall-Time",
Header: "Wall-Time",
Unit: ms,
},
"excl_wt": FieldInfo{
Name: "ExclusiveWallTime",
Label: "Exclusive Wall-Time",
Header: "Wall-Time",
Unit: ms,
},
"cpu": FieldInfo{
Name: "CpuTime",
Label: "Inclusive CPU-Time",
Header: "CPU-Time",
Unit: ms,
},
"excl_cpu": FieldInfo{
Name: "ExclusiveCpuTime",
Label: "Exclusive CPU-Time",
Header: "CPU-Time",
Unit: ms,
},
"memory": FieldInfo{
Name: "Memory",
Label: "Inclusive Memory",
Header: "Memory",
Unit: kb,
},
"excl_memory": FieldInfo{
Name: "ExclusiveMemory",
Label: "Exclusive Memory",
Header: "Memory",
Unit: kb,
},
"io": FieldInfo{
Name: "IoTime",
Label: "Inclusive I/O-Time",
Header: "I/O-Time",
Unit: ms,
},
"excl_io": FieldInfo{
Name: "ExclusiveIoTime",
Label: "Exclusive I/O-Time",
Header: "I/O-Time",
Unit: ms,
},
"num_alloc": FieldInfo{
Name: "NumAlloc",
Label: "Number of Allocations",
Header: "Num. Alloc.",
Unit: plain,
},
"excl_num_alloc": FieldInfo{
Name: "ExclusiveNumAlloc",
Label: "Exclusive Number of Allocations",
Header: "Num. Alloc.",
Unit: plain,
},
"alloc_amt": FieldInfo{
Name: "AllocAmount",
Label: "Amount of allocated Memory",
Header: "Alloc. Amount",
Unit: kb,
},
"excl_alloc_amt": FieldInfo{
Name: "ExclusiveAllocAmount",
Label: "Exclusive Amount of allocated Memory",
Header: "Alloc. Amount",
Unit: kb,
},
"num_free": FieldInfo{
Name: "NumFree",
Label: "Number of Frees",
Header: "Num. Frees",
Unit: plain,
},
"excl_num_free": FieldInfo{
Name: "ExclusiveNumFree",
Label: "Exclusive Number of Frees",
Header: "Num. Frees",
Unit: plain,
},
}
func renderProfile(profile *xhprof.Profile, field string, fieldInfo FieldInfo, minValue float32) error {
header := fieldInfo.Header
exclHeader := "Excl. " + fieldInfo.Header
var fields []FieldInfo
var headers []string
if strings.HasPrefix(field, "excl_") {
fields = []FieldInfo{fieldsMap[strings.TrimPrefix(field, "excl_")], fieldInfo}
exclHeader = fmt.Sprintf("%s (>= %2.2f %s)", exclHeader, minValue/fieldInfo.Unit.Divisor, fieldInfo.Unit.Name)
headers = []string{"Function", "Count", header, exclHeader}
} else {
fields = []FieldInfo{fieldInfo}
header = fmt.Sprintf("%s (>= %2.2f %s)", header, minValue/fieldInfo.Unit.Divisor, fieldInfo.Unit.Name)
headers = []string{"Function", "Count", header}
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(headers)
for _, call := range profile.Calls {
table.Append(getRow(call, fields))
}
fmt.Printf("Showing XHProf data by %s\n", fieldInfo.Label)
table.Render()
return nil
}
func getRow(call *xhprof.Call, fields []FieldInfo) []string {
res := []string{
fmt.Sprintf("%.90s", call.Name),
fmt.Sprintf("%d", call.Count),
}
for _, field := range fields {
var col string
if field.Unit == plain {
col = fmt.Sprintf("%2.0f %s", call.GetFloat32Field(field.Name)/field.Unit.Divisor, field.Unit.Name)
} else {
col = fmt.Sprintf("%2.2f %s", call.GetFloat32Field(field.Name)/field.Unit.Divisor, field.Unit.Name)
}
res = append(res, col)
}
return res
}
func renderProfileDiff(diff *xhprof.ProfileDiff, limit int) error {
diff.Sort()
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Function", "Count", "Wall-Time", "Fraction Wall-Time From", "Fraction Wall-Time To"})
for i, call := range diff.Calls {
if i >= limit {
break
}
row := []string{
fmt.Sprintf("%.90s", call.Name),
fmt.Sprintf("%d", call.Count),
fmt.Sprintf("%2.2f ms", call.WallTime/1000),
fmt.Sprintf("%2.2f", call.FractionWtFrom),
fmt.Sprintf("%2.2f", call.FractionWtTo),
}
table.Append(row)
}
fmt.Printf("Showing XHProf data by the difference of fractions\n")
table.Render()
return nil
}