-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·344 lines (291 loc) · 11.1 KB
/
test.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
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
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python3
from collections import namedtuple
import multiprocessing
import os
import re
import psycopg2
import subprocess
import time
import random
import string
import colorama
import glob
CompressionParams = namedtuple(
'CompressionParams',
'arg1_type, arg2_type, arg3_type, arg4_type, min_level, max_level, other_1, other_2, other_3, other_4')
DecompressionParams = namedtuple('DecompressionParams',
'arg1_type, arg2_type, arg3_type, arg4_type, other_1, other_2, other_3, other_4')
query = '''
INSERT INTO compression_result (
command, compression_program_id, input_file, input_file_hash,
input_file_size, output_file, output_file_hash, output_file_size,
time_started, time_finished, duration, compression_ratio, kb_per_second, bits_per_byte,
decompression_command, decompression_duration, decompression_kb_per_second, level
)
VALUES (
'{}', {}, '{}', '{}', {} :: BIGINT,
'{}', '{}', {} :: BIGINT,
to_timestamp({} / 1000), to_timestamp({} / 1000), {} :: DOUBLE PRECISION, {} :: DOUBLE PRECISION, {} :: DOUBLE PRECISION, {} :: DOUBLE PRECISION,
'{}', {}, {}, {}
);
'''
colorama.init()
def get_connection():
conn = psycopg2.connect("dbname=eppie user=eppie host=localhost port=5432")
conn.set_session(readonly=False, autocommit=True)
return conn
def get_test_files():
parent = 'testfiles'
testfiles = []
for folder, _, files in os.walk(parent):
for filename in files:
testfiles.append(os.path.join(folder, filename))
return sorted(testfiles)
def get_executables():
parent = 'bin'
executables = []
for folder, _, files in os.walk(parent):
for filename in files:
executables.append(os.path.join(folder, filename))
return sorted(executables)
def get_size(filename):
return os.path.getsize(filename)
def get_hash(filename):
p = subprocess.run('shasum {}'.format(filename), stdout=subprocess.PIPE, shell=True)
return p.stdout.split()[0].decode('ascii')
def get_milli():
return time.time() * 1000
def make_outfilename():
return 'out/' + ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
def calculate_timeout(original_size):
BYTES_PER_KILOBYTE = 1024
MINIMUM_KB_PER_SECOND = 1
return (original_size / BYTES_PER_KILOBYTE * (1 / MINIMUM_KB_PER_SECOND)) + 1
def remove_file(f):
try:
os.remove(f)
except BaseException:
pass
def is_already_done(program_id, original_hash, level):
query = """
SELECT * FROM compression_result WHERE compression_program_id = {} AND input_file_hash = '{}' AND level = {};
"""
conn = get_connection()
cursor = conn.cursor()
cursor.execute(query.format(program_id, original_hash, level))
return cursor.rowcount == 1
def evaluate_one_command_one_file(command, outfile, infile, program_id, decompression_command, level):
original_size = get_size(infile)
original_hash = get_hash(infile)
if(is_already_done(program_id, original_hash, level)):
print(colorama.Fore.BLUE + '{} # Already done!'.format(command) + colorama.Fore.RESET)
return True
start_time = get_milli()
try:
p = subprocess.run(
command,
stdout=subprocess.PIPE,
shell=True,
check=True,
timeout=calculate_timeout(original_size))
except subprocess.CalledProcessError as e:
print(colorama.Fore.RED + '{} # exited with nonzero error code'.format(command) + colorama.Fore.RESET)
with open('nonzero.log', 'a') as f:
f.write(command + '\n')
remove_file(outfile)
return False
except subprocess.TimeoutExpired:
print(colorama.Fore.RED + '{} # TIMEOUT'.format(command) + colorama.Fore.RESET)
remove_file(outfile)
return False
end_time = get_milli()
duration = (end_time - start_time) / 1000
try:
outfiles = glob.glob(outfile + '*')
if len(outfiles) != 1:
print(colorama.Fore.RED + command + ' # BAD OUTFILE(S)' + colorama.Fore.RESET)
return False
outfile = outfiles[0]
new_size = get_size(outfile)
new_hash = get_hash(outfile)
ratio = original_size / new_size
bits_per_byte = 8 / ratio
kb_per_sec = original_size / duration / 1024
print(colorama.Fore.GREEN + command + colorama.Fore.RESET)
print('Duration: {:.2f}s, Ratio: {:.2f}, kB/s: {:.2f}, bpb: {:.2f}, ({}/{})'.format(
duration, ratio, kb_per_sec, bits_per_byte, new_size, original_size))
decompression_duration = 0
decompression_kb_per_sec = 0
'''
start_time = get_milli()
try:
p = subprocess.run(decompression_command, stdout=subprocess.PIPE, shell=True,
check=True, timeout=calculate_timeout(original_size))
except subprocess.CalledProcessError as e:
print(
colorama.Fore.RED +
'{} # exited with nonzero error code'.format(decompression_command) +
colorama.Fore.RESET)
with open('nonzero.log', 'a') as f:
f.write(decompression_command + '\n')
remove_file(outfile)
return False
except subprocess.TimeoutExpired:
print(colorama.Fore.RED + '{} # TIMEOUT'.format(decompression_command) + colorama.Fore.RESET)
remove_file(outfile)
return False
end_time = get_milli()
duration = (end_time - start_time) / 1000
decompression_duration = (end_time - start_time) / 1000
decompression_kb_per_sec = original_size / duration / 1024
print(colorama.Fore.BLUE + decompression_command + colorama.Fore.RESET)
'''
remove_file(outfile)
conn = get_connection()
cursor = conn.cursor()
cursor.execute(
query.format(
command,
program_id,
infile,
original_hash,
original_size,
outfile,
new_hash,
new_size,
start_time,
end_time,
duration,
ratio,
kb_per_sec,
bits_per_byte,
decompression_command,
decompression_duration,
decompression_kb_per_sec,
level))
conn.close()
return True
except Exception as e:
print(colorama.Fore.RED + command + colorama.Fore.RESET)
print(e)
return False
def insert_program(executable, program_hash):
""" Create a row in `compression_program` for this executable if one doesn't yet exist.
Returns the id of the row.
"""
select_query = '''
SELECT id
FROM compression_program
WHERE program_hash = '{}';
'''
insert_query = '''
INSERT INTO compression_program
(program_name, program_hash
)
VALUES ('{}', '{}')
RETURNING id;
'''
conn = get_connection()
cursor = conn.cursor()
cursor.execute(select_query.format(program_hash))
if cursor.rowcount == 0:
cursor.execute(insert_query.format(executable, program_hash))
cursor.execute(select_query.format(program_hash))
program_id = cursor.fetchone()[0]
conn.close()
return program_id
else:
program_id = cursor.fetchone()[0]
conn.close()
return program_id
def get_params(executable):
query = '''
SELECT t.arg1_type, t.arg2_type, t.arg3_type, t.arg4_type, min_level, max_level, other_1, other_2, other_3, other_4
FROM (SELECT * FROM compression_params) AS t
WHERE regexp_match('{}', program_name_pattern) IS NOT NULL
ORDER BY length(program_name_pattern) DESC;
'''
conn = get_connection()
cursor = conn.cursor()
cursor.execute(query.format(executable))
try:
result = cursor.fetchone()
conn.close()
return CompressionParams(*result)
except BaseException:
conn.close()
return None
def get_decompression_params(executable):
query = '''
SELECT t.arg1_type, t.arg2_type, t.arg3_type, t.arg4_type, other_1, other_2, other_3, other_4
FROM (SELECT * FROM decompression_params) AS t
WHERE regexp_match('{}', program_name_pattern) IS NOT NULL
ORDER BY length(program_name_pattern) DESC;
'''
conn = get_connection()
cursor = conn.cursor()
cursor.execute(query.format(executable))
try:
result = cursor.fetchone()
conn.close()
return DecompressionParams(*result)
except BaseException:
conn.close()
return None
def substitute_arg(arg_type, testfile, outfile, level, other=None):
if arg_type == 'LEVEL_HYPHEN':
return '-{}'.format(level)
elif arg_type == 'LEVEL':
return str(level)
elif arg_type == 'INFILE':
return testfile
elif arg_type == 'OUTFILE':
return outfile
elif arg_type == 'OTHER':
return other
else:
return ''
def construct_command(executable, params, testfile, outfile, level):
arg1_type = params.arg1_type
arg2_type = params.arg2_type
arg3_type = params.arg3_type
arg4_type = params.arg4_type
other1 = params.other_1
other2 = params.other_2
other3 = params.other_3
other4 = params.other_4
command = '{executable} {arg1} {arg2} {arg3} {arg4}'
result = command.format(
executable=executable,
arg1=substitute_arg(arg1_type, testfile, outfile, level, other1),
arg2=substitute_arg(arg2_type, testfile, outfile, level, other2),
arg3=substitute_arg(arg3_type, testfile, outfile, level, other3),
arg4=substitute_arg(arg4_type, testfile, outfile, level, other4)
)
return result
def worker_main(queue):
while True:
command, outfile, infile, program_id, decompression_command, level = queue.get(True)
evaluate_one_command_one_file(command, outfile, infile, program_id, decompression_command, level)
testfiles = get_test_files()
executables = get_executables()
the_queue = multiprocessing.Queue()
the_pool = multiprocessing.Pool(16, worker_main, (the_queue,))
for executable in executables:
program_hash = get_hash(executable)
program_id = insert_program(executable, program_hash)
compression_params = get_params(executable[4:])
decompression_params = get_decompression_params(executable[4:])
if not compression_params:
print(colorama.Fore.RED + 'No compression params for executable: {}'.format(executable) + colorama.Fore.RESET)
continue
for testfile in testfiles:
min_level = compression_params.min_level if compression_params.min_level else 0
max_level = compression_params.max_level if compression_params.max_level else 0
for level in range(min_level, max_level + 1):
outfile = make_outfilename()
command = construct_command(executable, compression_params, testfile, outfile, level)
decompression_command = construct_command(executable, decompression_params, testfile, outfile, level)
the_queue.put((command, outfile, testfile, program_id, decompression_command, level))
while not the_queue.empty():
time.sleep(100)