-
Notifications
You must be signed in to change notification settings - Fork 0
/
PerfTest_c-compiler.py
147 lines (106 loc) · 3.39 KB
/
PerfTest_c-compiler.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import os
import commands
import time
# In some of the cpp/c files, add/remove some simple
# flags make the compilation successes.
# e.g. -std=c++11
# e.g. -fpermissive
# configurations
options = ['-O0', '-O1', '-O2', '-O3', '-Os', '-Ofast']
extensions = ['.cpp', '.cc', '.c', '.C', '.CC', '.CPP', '.Cpp']
# Global variables
class Glob:
c_compiler = ''
REPEATS = 1
benchmark_dir = '' # Root directory of benchmark files.
benchmarks = [] # Absolute path of each benchmark file.
def extract_time(output):
compile_time_inMSMs = output.split('zbug_start')[1].split('zbug_end')[0].strip()
m,s = compile_time_inMSMs.split(":")
compile_time_in_sec = float(m)*60 + float(s)
return compile_time_in_sec
def prepare_benchmarks(root):
items = os.listdir(root)
for item in items:
path = os.path.join(root, item)
if os.path.isdir(path):
prepare_benchmarks(path)
elif os.path.splitext('%s'%path)[1] in extensions:
Glob.benchmarks.append(path)
def parse_args():
# Error handling
if not len(sys.argv) == 4:
print('Argument Error. '
'Usage:\n\tpython %s <c_compiler_name> <benchmark_dir> <repeats>'
'\t\t<c_compiler_name> Only Support gcc, g++, clang or clang++'
'\t\t<benchmark_dir> The root directory of benchmark files.'
'\t\t<repeats> How many times do you want to run repeatedly for a case.'
%(sys.argv[0]))
exit(1)
elif sys.argv[1] not in {'gcc', 'g++', 'clang', 'clang++'}:
print('Argument 1 Error. Only gcc, g++, clang, clang++ is supported.')
exit(1)
elif not os.path.exists(sys.argv[2]) or os.path.isfile(sys.argv[2]):
print('Argument 2 Error. Please give a valid directory.')
exit(1)
# write user parameters
Glob.c_compiler = sys.argv[1]
Glob.benchmark_dir = sys.argv[2]
Glob.REPEATS = int(sys.argv[3])
def main():
parse_args()
prepare_benchmarks(Glob.benchmark_dir)
# the error log. When compilation fails, write messages to it.
logfile = open('log.err', 'w')
# statistic
suc = 0
fail = 0
total = len(options) * len(Glob.benchmarks)
cur = 0
# start performance testing.
for j in range(0, len(Glob.benchmarks)):
result_file = open('[Result]%s.txt'%Glob.benchmarks[j].split('/')[-1].split('.')[0], 'w')
for i in range(0, len(options)):
cur += 1
compile_times = []
cmd = '/usr/bin/time -f "zbug_start%%Ezbug_end" %s -std=c++11 %s %s -o %s.out'%(
Glob.c_compiler,
options[i],
Glob.benchmarks[j],
Glob.benchmarks[j].split('/')[-1].split('.')[0]
)
(status, output) = commands.getstatusoutput(cmd)
if status:
fail += 1
logfile.write('%s\n%s\n%s\n%s\n\n'%(40*'-', cmd, 40*'-', output))
print('[%d/%d]Uncompilable: %s %s'%(
cur,
total,
Glob.benchmarks[j].split('/')[-1],
options[i]
))
else:
suc += 1
compile_times.append(extract_time(output))
for k in range(1, Glob.REPEATS):
output = commands.getoutput(cmd)
compile_times.append(extract_time(output))
result_file.write('%s\t%s\n'%
(
options[i],
'\t'.join(str(aa) for aa in compile_times)
))
print('[%d/%d]OK, Finshed: %s %s'%(
cur,
total,
Glob.benchmarks[j].split('/')[-1],
options[i]
) )
result_file.close()
logfile.close()
print('Final Result: OK %d/%d NOT-OK %d/%d'%(
suc, total, fail, total))
main()