-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
215 lines (177 loc) · 6.01 KB
/
run.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
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
# Imports
import os
import sys
import optparse
def run_test(graph, test, diff, mem):
# Command for performance and penazzi test
command = ['make ' + test, 'GRAPHS=', 'SWITCH=', 'RMBC=', 'INPUT=', 'OUTPUT=', 'OFLAG=']
# Arguments for command
gr, sw, rm, inp, out, ans = "", "", "", 'samples/' + test + '/' + graph, 'out/' + test + '/' + graph, 'ans/' + test + '/' + graph
# Important files
ansfile, outfile = None, None
# Check that inputfile exits
if not os.path.isfile(inp):
print("Graph " + graph + " does not exist")
exit(0)
# We open ansfile
try:
ansfile = open(ans, 'r')
except IOError:
print("Answer file for " + graph + " not found.")
exit(0)
# Create the paths out/$(test)/graph
if not os.path.isdir('out/' + test):
os.mkdir('out/' + test)
if not os.path.isfile(out):
os.mknod(out)
if mem and not os.path.isdir('out/memory'):
os.mkdir('out/memory')
# Open output file
outfile = open(out, 'r')
#if memory read graphs too
if test == 'memory':
try:
gr, sw, rm = ansfile.readline().split()
except ValueError:
print("Answer file has wrong format, 'gr sw rm' needed")
exit(0)
else:
try:
sw, rm = ansfile.readline().split()
except ValueError:
print("Answer file has wrong format, 'sw rm' needed")
exit(0)
# Make arguments
args = [gr, sw, rm, inp, out]
# Merge command with arguments
for i in range(1, len(command) - 1):
command[i] += args[i - 1]
if diff:
command[-1] += '-DHARD'
if mem:
command[0] += '.v'
# Convert command to string
command = ' '.join(command)
# Launch command
os.system(command)
# What have we done?
message = 'Runned ' + test + ' test with ' + sw + ' switches and ' + rm + ' RMBCs on ' + graph
print(message)
# Collect data depending on test
if 'penazzi' == test:
ansline = ansfile.readline()
outline = outfile.readline()
checks = ['GREEDY\n', 'WELSH\n', 'SWITCH\n', 'RMBC\n', 'Bipartito\n']
expected = []
given = []
while ansline and outline:
expected.append(ansline)
given.append(outline)
ansline, outline = ansfile.readline(), outfile.readline()
for i in range (0, 5):
checks[i] += 'Expected: ' + expected[i] + 'Given: ' + given[i]
checks = '\n'.join(checks)
print(checks)
if 'performance' == test:
print('Time spent creating graph: ' + outfile.readline())
outfile.readline();
print('Time spent running Natural: ' + outfile.readline())
outfile.readline();
print('Time spent running Welsh: ' + outfile.readline())
outfile.readline();
print('Time spent running Switch: ' + outfile.readline())
outfile.readline();
print('Time spent running RMBC: ' + outfile.readline())
outfile.readline();
print('Time spent running Bipartito: ' + outfile.readline())
print('Time spent destroying graph: ' + outfile.readline())
print('Penazzi time: ' + ansfile.readline())
print('Your time: ' + outfile.readline())
if 'memory' == test or mem:
vfile = open('out/memory/' + graph, 'r')
for line in vfile:
if 'HEAP SUMMARY' in line:
info = line
break
nextl = vfile.readline()
while nextl: # Search the first char
i = 9
while info[i] == ' ':
i += 1
print(info[i:])
info = vfile.readline()
nextl = vfile.readline()
vfile.close()
# Close files
ansfile.close()
outfile.close()
def valid(str):
if str == 'penazzi':
return True
elif str == 'performance':
return True
elif str == 'memory':
return True
def main():
"""
Interfaz simple para correr los tests y obtener los resultados
"""
# Crea los directorios si no existen.
if not os.path.isdir('bin'):
os.mkdir('bin')
if not os.path.isdir('obj'):
os.mkdir('obj')
if not os.path.isdir('out'):
os.mkdir('out')
# Parsear argumentos-
parser = optparse.OptionParser(usage="%prog [options] test graph")
parser.add_option("-t", "--test",
help="Run tests with hard difficulty", action="store_true", default=False, dest="diff")
parser.add_option("-v", "--valgrind",
help="Run tests with valgrind", action="store_true", default=False, dest="mem")
options, args = parser.parse_args()
diff = '.difficulty'
if not os.path.isfile(diff):
os.mknod(diff)
d = open(diff, 'w')
d.write('EASY')
d.close()
# Read difficulty
diffile = open(diff, 'r')
mode = diffile.readline()
diffile.close()
# Change it if necessary, and do make clean
if 'EASY' in mode and options.diff:
os.system('make clean')
diffile = open(diff,'w')
diffile.write('HARD')
diffile.close()
elif 'HARD' in mode and not options.diff:
os.system('make clean')
diffile = open(diff,'w')
diffile.write('EASY')
diffile.close()
# Check if the arguments are valid
# if test == 'memory' and len(args) != 3:
# print("Usage: run.py [options] test graph")
# exit(0)
if len(args) != 2:
print("Usage: run.py [options] test graph")
exit(0)
# Load the arguments in the variables
# if args[1] == 'memory': gr sw rm
test, graph = args
if(graph == '*'):
graph = os.listdir('samples/' + test)
else:
graph = [graph]
if valid(test):
for g in graph:
run_test(g, test, options.diff, options.mem)
else:
print("Wrong test name")
print("Available tests are penazzi, performance and memory")
#os.system('make clean')
exit(0)
if __name__ == '__main__':
main()