-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathprofiler_parser.py
More file actions
113 lines (96 loc) · 3.92 KB
/
Copy pathprofiler_parser.py
File metadata and controls
113 lines (96 loc) · 3.92 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
#!/usr/bin/env python
"""
Utility script to parse output from Profiler framework and print statistics.
More information about the benchmarking framework can be found in the
Polycube documentation, section 'developers/profiler.rst'.
This is a script with the aim of printing in a more human-readable format
information previously gathered by the framework.
Usage: profiler_parse.py <filename> [<FLAGS>]
-h/--help: show the usage menu
-s/--statistics: show only general statistics
-e/--export: export in csv format
"""
import sys
import re
import csv
#Support variable
data = []
previous = 0
padding = 40
#Pattern to match. The framework's output file is:
#<filename>,<line_of_code>:<timestamp>
pattern = '(\\S+),(\\d+),(.*),(\\d+)'
#Function to parse a matched line
def parseMatch(match, number):
global data, padding, previous
#Used last string from '/' since the __FILE__ macro in C
#could contain the entire path
file = match.group(1)[match.group(1).rfind('/')+1:]
line = int(match.group(2))
name = match.group(3)
loopId = len([1 for x in data if x[0] == file and x[1] == line])
timestamp = int(match.group(4))
duration = 0 if number == 0 else timestamp - previous
previous = timestamp
padding = padding if len(file)+len(str(line))+1 < padding else len(file)+len(str(line))+1
padding = padding if len(name) < padding else len(name)
data.append((file,line,name,loopId,timestamp,duration))
#Function to read the specified file
def readFile(filename):
try:
lines = open(filename, 'r').readlines()
if len(lines) == 0:
raise IOError()
except IOError:
print('Error: no such file \'' + sys.argv[1] + '\' or empty')
exit()
#Foreach line read, parse it and update data structures
for line_number, line in enumerate(lines[1:]):
match = re.match(pattern, line, flags=0)
if match:
parseMatch(match, line_number)
else:
print("Error while parsing line " + str(line_number) + ": " + line)
print("Is it compliant with the format <file>,<line_of_code>,<checkpoint_name>,<timestamp> ?\n")
showUsage()
exit()
#Function to print script usage
def showUsage():
print("profiler_parser.py <filename> [<FLAGS>]")
print("\t-h/--help: show the usage menu")
print("\t-s/--statistics: show only general statistics")
print("\t-e/--export: export in csv format")
def main():
global data
#Checking arguments
if len(sys.argv) == 1 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
showUsage()
exit()
readFile(sys.argv[1])
#Checking export flag
if len(sys.argv) == 3 and (sys.argv[2] == "--export" or sys.argv[2] == "-e"):
filename = sys.argv[1][:sys.argv[1].rfind('.')]+".csv"
with open(filename, 'w') as out:
csv_out=csv.writer(out)
csv_out.writerow(["File", "Line", "Name", "LoopId", "Timestamp(ns)", "Duration(ns)"])
for row in data:
csv_out.writerow(row)
print("CSV exported at `" + filename + "`")
exit()
#Checking whether to display all point-to-point distances or not
if len(sys.argv) == 2 or (len(sys.argv) == 3 and not (sys.argv[2] == "--statistics" or sys.argv[2] == "-s")):
print("="*padding*3 + "="*6)
print("||" + "FROM".ljust(padding) + "|" + "TO".ljust(padding) + "|" + "TIME(ns)".ljust(padding) + "||")
print("="*padding*3 + "="*6)
for count, item in enumerate(data[:-1]):
name1 = item[2] if item[2] != "" else item[0] + "," +str(item[1])
name2 = data[count+1][2] if data[count+1][2] != "" else data[count+1][0] + "," + str(data[count+1][1])
print("||" + name1.ljust(padding) + "|" + name2.ljust(padding) + "|" + str(data[count+1][5]).ljust(padding) + "||")
print("="*padding*3 + "="*6)
#Printing statistics
duration = [item[5] for item in data[1:]]
print("Max execution time: " + str(max(duration)) + " ns")
print("Min execution time: " + str(min(duration)) + " ns")
print("Avg execution time: " + str(sum(duration)/len(duration)) + " ns")
if __name__== '__main__':
main()