-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
334 lines (296 loc) · 7.39 KB
/
Copy pathmain.go
File metadata and controls
334 lines (296 loc) · 7.39 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"bufio"
"encoding/csv"
"flag"
"fmt"
"io"
"log"
"os"
"regexp"
"sort"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
)
func check(e error) {
if e != nil {
panic(e)
}
}
type xdebugLine struct {
depth int64
functionNumber int64
entry int64
time float64
memoryUsage int64
name string
internalFunction int64
include string
filename string
lineNumber int64
numOfParams int64
}
type stackEntry struct {
order int
calls int
funcName string
time float64
memory int64
nestedTime float64
nestedMemory int64
}
type functionEntry struct {
name string
order int
calls int
timeInclusive float64
memoryInclusive int64
timeChildren float64
memoryChildren int64
timeOwn float64
memoryOwn int64
}
var functions map[string]stackEntry
var stackFunctions []string
var stack map[string]stackEntry
var sortKey string
var supportedKeys []string
var resultsLimit int
var csvFileName string
var useCSV bool
func main() {
supportedKeys := []string{"calls", "flow", "time", "memory"}
filenamePtr := flag.String("filename", "", "Path to xdebug log")
sortKeyPtr := flag.String("sortKey", "flow", "Sort Key [ flow, calls, time, memory ] ")
resultsLimitPtr := flag.Int("limit", 25, "Number of results returned")
useCSVPtr := flag.Bool("useCSV", false, "Output to csv file")
resultsLimit = *resultsLimitPtr
useCSV = *useCSVPtr
//sortKey = *sortKeyPtr
flag.Parse()
if len(*filenamePtr) == 0 {
panic("Filename is invalid")
}
foundKey := false
for _, supportedKey := range supportedKeys {
if *sortKeyPtr == supportedKey {
foundKey = true
}
}
if foundKey == false {
panic("Invalid Sort Key was used")
}
if fileInfo, err := os.Stat(*filenamePtr); os.IsNotExist(err) {
panic("Filename does not exist")
} else {
csvFileName = fmt.Sprintf("%s.sorted_by_%s.csv", fileInfo.Name(), *sortKeyPtr)
}
f, err := os.Open(*filenamePtr)
check(err)
reader := bufio.NewReader(f)
funcs := parse(reader, *sortKeyPtr)
f.Close()
sort.Sort(FunctionList(funcs))
writeToStdout(funcs)
}
func writeToStdout(funcs []functionEntry) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeader([]string{"Name", "Calls", "Time Inclusive", "Memory", "Nested Time", "Nested Memory", "Order"})
//check(err)
//Convert to multi dimensional array
var multidim [][]string
var dim []string
for _, v := range funcs {
dim = []string{
v.name,
strconv.Itoa(v.calls),
fmt.Sprintf("%f", v.timeInclusive),
fmt.Sprintf("%d", v.memoryInclusive),
fmt.Sprintf("%f", v.timeOwn),
fmt.Sprintf("%d", v.memoryOwn),
fmt.Sprintf("%d", v.order),
}
multidim = append(multidim, dim)
}
table.AppendBulk(multidim[:resultsLimit])
table.Render()
if useCSV {
writeToCSV(multidim)
}
}
func parse(reader *bufio.Reader, sortKey string) []functionEntry {
lineCount := int(0)
newlinebytes := byte('\n')
stack = make(map[string]stackEntry)
functions = make(map[string]stackEntry)
stack["-1"] = stackEntry{0, 0, "", 0, 0, 0, 0}
stack["0"] = stackEntry{0, 0, "", 0, 0, 0, 0}
stackFunctions = []string{}
tabRegexp := regexp.MustCompile("\t")
for {
line, err := reader.ReadString(newlinebytes)
if err == io.EOF {
break
}
check(err)
lineCount++
if lineCount < 4 {
continue
}
parsedSlice := tabRegexp.Split(line, -1)
//fmt.Println(line)
if len(parsedSlice) < 5 {
continue
}
xLine := xdebugLine{}
for i, val := range parsedSlice {
val = strings.Trim(val, "")
re := regexp.MustCompile(`\r?\n`)
val = re.ReplaceAllString(val, "")
switch i {
case 0:
xLine.depth, _ = strconv.ParseInt(val, 0, 64)
case 1:
xLine.functionNumber, _ = strconv.ParseInt(val, 0, 64)
case 2:
xLine.entry, _ = strconv.ParseInt(val, 0, 64)
case 3:
xLine.time, _ = strconv.ParseFloat(val, 64)
case 4:
xLine.memoryUsage, _ = strconv.ParseInt(val, 10, 64)
case 5:
xLine.name = val
case 6:
xLine.internalFunction, _ = strconv.ParseInt(val, 10, 64)
case 7:
xLine.include = val
case 8:
xLine.filename = val
case 9:
xLine.lineNumber, _ = strconv.ParseInt(val, 10, 64)
case 10:
xLine.numOfParams, _ = strconv.ParseInt(val, 10, 64)
}
}
depthKey := strconv.FormatInt(xLine.depth, 10)
if xLine.entry == 0 {
stackentry := stackEntry{
funcName: xLine.name,
time: xLine.time,
memory: xLine.memoryUsage,
nestedMemory: 0,
nestedTime: 0,
order: lineCount,
}
stack[depthKey] = stackentry
stackFunctions = append(stackFunctions, xLine.name)
} else if xLine.entry == 1 {
//get Previous Line
prevXLine := stack[depthKey]
dTimeString := fmt.Sprintf("%f", xLine.time-prevXLine.time)
dTime, _ := strconv.ParseFloat(dTimeString, 64)
dMemory := xLine.memoryUsage - prevXLine.memory
prevDepthKey := strconv.FormatInt(xLine.depth-1, 10)
se2 := stack[prevDepthKey]
se2.nestedTime += dTime
se2.nestedMemory += dMemory
stack[prevDepthKey] = se2
stackFunctions = slicePop(stackFunctions)
addToFunction(prevXLine.funcName,
dTime,
dMemory,
prevXLine.nestedTime,
prevXLine.nestedMemory,
prevXLine.order)
}
}
//sort by sortKey
funcs := getFunctions()
return funcs
}
func writeToCSV(data [][]string) {
file, err := os.Create(csvFileName)
check(err)
defer file.Close()
w := csv.NewWriter(file)
for _, record := range data {
if err := w.Write(record); err != nil {
log.Fatalln("Error writing record to csv:", err)
}
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
}
func getFunctions() []functionEntry {
var f []functionEntry
for funcName, arr := range functions {
fe := functionEntry{
name: funcName,
calls: arr.calls,
timeInclusive: arr.time,
memoryInclusive: arr.memory,
timeChildren: arr.nestedTime,
memoryChildren: arr.nestedMemory,
timeOwn: arr.time - arr.nestedTime,
memoryOwn: arr.memory - arr.nestedMemory,
order: arr.order,
}
f = append(f, fe)
}
return f
}
//FunctionList is an array of function entries
type FunctionList []functionEntry
//Len gets function list length
func (f FunctionList) Len() int {
return len(f)
}
// Swap just swaps elements of slice
func (f FunctionList) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}
func (f FunctionList) Less(i, j int) bool {
var r bool
switch sortKey {
case "flow":
r = f[i].order < f[j].order
case "calls":
r = f[i].calls > f[j].calls
case "time", "time-inclusive":
r = f[i].timeInclusive > f[j].timeInclusive
case "memory-inclusive", "memory":
r = f[i].memoryInclusive > f[j].memoryInclusive
default:
r = false
}
return r
}
func slicePop(slice []string) []string {
sliceLength := len(slice)
return slice[:sliceLength-1]
}
func addToFunction(functionName string, time float64, memory int64, nestedTime float64, nestedMemory int64, order int) {
if _, prs := functions[functionName]; prs == false {
functions[functionName] = stackEntry{}
}
elem := functions[functionName]
elem.calls++
found := false
for _, v := range stackFunctions {
if functionName == v {
found = true
}
}
if found == false {
elem.time += time
elem.memory += memory
elem.nestedTime += nestedTime
elem.nestedMemory += nestedMemory
elem.order = order
}
functions[functionName] = elem
}