-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathto_csv.py
executable file
·133 lines (102 loc) · 3.39 KB
/
to_csv.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2012-12-09
@author: Sergey <[email protected]>
Convert ./run.sh output to CSV file
This one:
===============
pypy
===============
******************************
parser:bsoup3_parser.py file:../page_google.html
1.15545797348 s
real:1.18 user:1.17 sys:0.01 max RSS:56532
Becomes CSV like:
platform,parser,file,parser s,real s,user s,sys s,maximum RSS
pypy,bsoup3_parser.py,page_google.html,1.15545797348,1.18,1.17,0.01,56532
'''
import sys
import csv
import re
import argparse
argparser = argparse.ArgumentParser(
description='Convert benchmark output to CSV')
argparser.add_argument(
'--skip-header', dest='skip_header', action='store_true',
help="Don't output column names (useful for concatenation)")
argparser.add_argument(
'--skip-errors', dest='skip_errors', action='store_true',
help="Silently drop benchmarks, that we can't parse (eg, with tracebacks)")
l1_re = re.compile(r"^parser:([0-9a-zA-Z_.-]+)\tfile:(.*)")
def parse_line1(line):
match = l1_re.search(line)
parser = match.group(1).split("/")[-1]
filepath = match.group(2).split("/")[-1]
return parser, filepath
l2_re = re.compile(r"^([0-9.]+)( s)?")
def parse_line2(line):
match = l2_re.search(line)
return match.group(1)
l3_re = re.compile(
r"^real:([0-9.]+)\tuser:([0-9.]+)\tsys:([0-9.]+)\tmax RSS:([0-9]+)")
def parse_line3(line):
match = l3_re.search(line)
return match.group(1), match.group(2), match.group(3), match.group(4)
columns = ["platform", "parser", "file", "parser s",
"real s", "user s", "sys s", "maximum RSS"]
PLATFORM_HDR, PLATFORM, TEST_HDR, TEST_1, TEST_2, TEST_3 = range(6)
class Parser(object):
def __init__(self, skip_errors=False):
self.skip_errors = skip_errors
self.state = None
self.r = {}
def reset(self):
self.r = {"platform": self.r["platform"]}
def parse_line(self, line):
try:
return self.do_parse_line(line)
except:
if self.skip_errors:
self.reset()
self.state = None
else:
raise
def do_parse_line(self, line):
c = self
if c.state == PLATFORM_HDR:
c.r["platform"] = line.strip()
c.state = PLATFORM
elif c.state == PLATFORM:
c.state = None
elif c.state == TEST_HDR:
c.r["parser"], c.r["file"] = parse_line1(line)
c.state = TEST_1
elif c.state == TEST_1:
c.r["parser s"] = parse_line2(line)
c.state = TEST_2
elif c.state == TEST_2:
(c.r["real s"], c.r["user s"],
c.r["sys s"], c.r["maximum RSS"]) = parse_line3(line)
r = self.r
c.reset()
c.state = TEST_3
return r
elif line.startswith("==="):
c.state = PLATFORM_HDR
elif line.startswith("***"):
c.state = TEST_HDR
else:
raise ValueError('Invalid input: "{0}"'.format(line))
def main(in_file=sys.stdin, out_file=sys.stdout):
args = argparser.parse_args()
csvwriter = csv.DictWriter(out_file, columns)
if not args.skip_header:
csvwriter.writeheader()
c = Parser(skip_errors=args.skip_errors)
for line in in_file:
row = c.parse_line(line)
if row:
csvwriter.writerow(row)
if __name__ == '__main__':
main()