-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathf_speed_size_decompress.py
executable file
·398 lines (366 loc) · 13.7 KB
/
f_speed_size_decompress.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# python3 d_speed_size.py : test speed/compression for folder 'corpus'
# python3 d_speed_size.py indir : test speed/compression for folder 'indir'
import os
import sys
import stat
import time
import shutil
import ntpath
import filecmp
import subprocess
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
def _cmp(
exe,
fnm,
lvl,
opts=' -f -k -'):
"""
compress file 'fnm' using executable 'exe'
Parameters
----------
exe : str
name of compression executable
fnm : str
name of file to be compressed
lvl : int
compression level
opts : str
command line options for executable (default, ' -f -k -')
"""
env = os.environ
cmd = exe + opts + str(lvl) + ' "' + fnm + '"'
subprocess.call(cmd, shell=True)
def test_cmp(
exe='gzip',
indir='',
repeats=1,
ext='.gz',
opts=' -q -f -k -',
max_level=9,
exts=['.gz', '.zstd']
):
"""
compress all files in folder 'indir' using executable 'exe'
Parameters
----------
exe : str
name of compression executable
indir : str
name of folder with files to compress
repeats : int
how many times is each file compressed. More is slower but better timing accuracy
ext : str
extension for files created by exe (default, '.gz')
opts : str
command line options for executable (default, ' -f -k -')
max_level : int
maximum compression level to test (default 9)
"""
if not os.path.exists(exe) and not shutil.which(exe):
print('Skipping test: Unable to find "' + exe + '"')
return ()
if len(indir) < 1:
indir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'corpus')
if not os.path.isdir(indir):
print('Run a_compile.py first: Unable to find "' + indir +'"')
sys.exit()
meth = ntpath.basename(exe)
print('CompressMethod\tLevel\tms\tmb/s\t%')
for lvl in range(1, max_level + 1):
size = 0
nsize = 0
seconds = float("inf")
for rep in range(repeats):
rep_seconds = time.time()
for f in os.listdir(indir):
if not os.path.isfile(os.path.join(indir, f)):
continue
if f.startswith('.'):
continue
if f.endswith(tuple(exts)):
continue
fnm = os.path.join(indir, f)
_cmp(exe, fnm, lvl, opts)
if rep > 0:
continue
size = size + os.stat(fnm).st_size
nsize = nsize + os.stat(fnm + ext).st_size
rep_seconds = time.time() - rep_seconds
seconds = min(seconds, rep_seconds)
size = size
nsize = nsize
# bytes_per_mb = 1024**2
bytes_per_mb = 1000000
speed = size / bytes_per_mb / seconds
print('{}\t{}\t{:.0f}\t{:.0f}\t{:.2f}'.format(meth, lvl,
seconds * 1000, speed, nsize / size * 100))
row_df = pd.DataFrame([[meth, nsize / size * 100, speed, lvl]])
row_df.columns = ['exe', 'size %', 'speed mb/s', 'level']
results_file = ntpath.basename(indir)+'_speed_size.pkl'
try:
df = pd.read_pickle(results_file)
df = pd.concat([row_df, df], ignore_index=True)
except (OSError, IOError) as e:
df = row_df
df.to_pickle(results_file)
# clean up
for f in os.listdir(indir):
if not os.path.isfile(os.path.join(indir, f)):
continue
if f.endswith(tuple(exts)):
fnm = os.path.join(indir, f)
os.remove(fnm)
def plot(results_file):
"""line-plot showing how compression level impacts file size and conpression speed
Parameters
----------
results_file : str
name of pickle format file to plot
"""
#if os.name == 'posix' and 'DISPLAY' not in os.environ:
# print('Plot the results on a machine with a graphical display')
# exit()
df = pd.read_pickle(results_file)
sns.set()
sns_plot = sns.lineplot(x='speed mb/s', y='size %', hue='exe', data=df, marker='o')
#plt.show()
plt.savefig(results_file.replace('.pkl', '.png'))
def validate_decompress_corpus(exe, indir, tmpdir):
"""
time decompression of all files in folder 'indir'
Parameters
----------
exe : dictionary
'exe' : str, exectuable name, e.g. 'zstd'
'uncompress': str, arguments, e.g. ' -T0 -q -f -k -d '
'compress': str, arguemnt for compression, e.g. ' -T0 -q -f -k -'
'max_level': int, maximum supported compression level, eg 19,
'ext': extension used by this compressor, e.g. '.zst'
indir : str
folder with refence copies of uncompressed files
tmpdir : str
folder with files to decompress
size_mb : float
uncompressed size for all files in indir
repeats : int
number of times each item is decompressed
"""
method = exe['exe']
ext = exe['ext']
opt = exe['uncompress']
meth = ntpath.basename(method)
if not os.path.exists(method) and not shutil.which(method):
print('Skipping test: Unable to find "' + method + '"')
return ()
for f in os.listdir(tmpdir):
if not os.path.isfile(os.path.join(tmpdir, f)):
continue
if f.startswith('.'):
continue
if f.endswith(ext):
fnm = os.path.join(tmpdir, f)
cmd = method + ' ' + opt + ' "' + fnm + '"'
subprocess.call(cmd, shell=True)
fbase = os.path.splitext(f)[0]
decompnm = os.path.join(tmpdir, fbase)
if not os.path.isfile(decompnm):
sys.exit('Unable to find decompressed ' + decompnm)
fbase = fbase.split('_', 1)[1]
orignm = os.path.join(indir, fbase)
if not os.path.isfile(orignm):
sys.exit('Unable to find reference ' + orignm)
if not filecmp.cmp(orignm, decompnm):
sys.exit('Files differ "{}":{}'.format(orignm, decompnm))
def decompress_corpus(exe, indir, size_mb, repeats):
"""
time decompression of all files in folder 'indir'
Parameters
----------
exe : dictionary
'exe' : str, exectuable name, e.g. 'zstd'
'uncompress': str, arguments, e.g. ' -T0 -q -f -k -d '
'compress': str, arguemnt for compression, e.g. ' -T0 -q -f -k -'
'max_level': int, maximum supported compression level, eg 19,
'ext': extension used by this compressor, e.g. '.zst'
indir : str
folder with files to decompress
size_mb : float
uncompressed size for all files in indir
repeats : int
number of times each item is decompressed
"""
method = exe['exe']
ext = exe['ext']
opt = exe['uncompress']
meth = ntpath.basename(method)
if not os.path.exists(method) and not shutil.which(method):
print('Skipping test: Unable to find "' + method + '"')
return ()
seconds = float("inf")
for r in range(repeats):
rep_seconds = time.time()
for f in os.listdir(indir):
if not os.path.isfile(os.path.join(indir, f)):
continue
if f.startswith('.'):
continue
if f.endswith(ext):
fnm = os.path.join(indir, f)
cmd = method + ' ' + opt + ' "' + fnm + '"'
subprocess.call(cmd, shell=True)
rep_seconds = time.time() - rep_seconds
seconds = min(seconds, rep_seconds)
speed = (size_mb) / seconds
print('{}\t{:.0f}\t{:.2f}'.format(meth, seconds * 1000, speed))
def compress_all_levels(exe, indir, tmpdir, exts):
"""
compress all files in folder 'indir' and copy to 'tmpdir'
Parameters
----------
exe : dictionary
'exe' : str, exectuable name, e.g. 'zstd'
'uncompress': str, arguments, e.g. ' -T0 -q -f -k -d '
'compress': str, arguemnt for compression, e.g. ' -T0 -q -f -k -'
'max_level': int, maximum supported compression level, eg 19,
'ext': extension used by this compressor, e.g. '.zst'
indir : str
folder with files to compress
tmpdir : str
temporary folder for storing files compress/decompress
tmpdir : list of str
all possible comrpession extensions ['.zst', '.gz']
"""
size = 0
method = exe['exe']
ext = exe['ext']
opt = exe['compress']
max_level = exe['max_level']
meth = ntpath.basename(method)
if not os.path.exists(method) and not shutil.which(method):
print('Skipping test: Unable to find "' + method + '"')
return 0
for lvl in range(1, max_level+1):
for f in os.listdir(indir):
if not os.path.isfile(os.path.join(indir, f)):
continue
if f.startswith('.'):
continue
if f.endswith(tuple(exts)):
continue
fnm = os.path.join(indir, f)
size = size + os.stat(fnm).st_size
cmd = method + opt + str(lvl) + ' "' + fnm + '"'
subprocess.call(cmd, shell=True)
outnm = ntpath.basename(fnm)
fnm = fnm + ext
if not os.path.isfile(fnm):
sys.exit('Unable to find ' + fnm)
outnm = os.path.join(tmpdir, meth + str(lvl) + '_' + outnm + ext)
shutil.move(fnm, outnm)
bytes_per_mb = 1000000
return size / bytes_per_mb
def test_decomp(exes, indir, exts, repeats):
"""
test decompression speed for all files in folder 'indir' using each exes
Parameters
----------
exes : dictionary where each entry has following properties
'exe' : str, exectuable name, e.g. 'zstd'
'uncompress': str, arguments, e.g. ' -T0 -q -f -k -d '
'compress': str, arguemnt for compression, e.g. ' -T0 -q -f -k -'
'max_level': int, maximum supported compression level, eg 19,
'ext': extension used by this compressor, e.g. '.zst'
indir : str
folder with files to compress/decompress
tmpdir : str
temporary folder for storing files compress/decompress
tmpdir : list of str
all possible comrpession extensions ['.zst', '.gz']
repeats : int
number of times each item is decompressed
performance estimate based on fastest run
"""
tmpdir = './temp'
if os.path.isdir(tmpdir):
shutil.rmtree(tmpdir)
try:
os.mkdir(tmpdir)
except OSError:
sys.exit('Unable to create folder "' + tmpdir + '"')
size_mb = 0;
for i in range(len(exes)) :
size_mb += compress_all_levels(exes[i], indir, tmpdir, exts)
print('DecompressMethod\tms\tmb/s')
for i in range(len(exes)) :
decompress_corpus(exes[i], tmpdir, size_mb, repeats)
for i in range(len(exes)) :
validate_decompress_corpus(exes[i], indir, tmpdir)
if __name__ == '__main__':
"""Compare speed and size for different compression tools
Parameters
----------
indir : str
folder with files to compress (default './corpus')
repeats : int
how many times is each file compressed (default 3)
"""
indir = ''
if len(sys.argv) > 1:
indir = sys.argv[1]
if len(indir) < 1:
indir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'silesia')
if not os.path.isdir(indir):
print('Run a_compile.py first: Unable to find "' + indir +'"')
sys.exit()
repeats = 7
if len(sys.argv) > 2:
repeats = int(sys.argv[2])
results_file = ntpath.basename(indir)+'_speed_size.pkl'
if os.path.exists(results_file):
os.remove(results_file)
exes = []
exes.append({'exe': 'zstd', 'uncompress': ' -T0 -q -f -k -d ', 'compress': ' -T0 -q -f -k -', 'max_level': 19, 'ext': '.zst' })
#exes.append({'exe': 'pbzip2', 'uncompress': ' -q -f -k -d ', 'compress': ' -q -f -k -', 'max_level': 9, 'ext': '.bz2' })
exes.append({'exe': 'lbzip2', 'uncompress': ' -q -f -k -d ', 'compress': ' -q -f -k -', 'max_level': 9, 'ext': '.bz2' })
#exes.append({'exe': 'lz4', 'uncompress': ' -q -f -k -d ', 'compress': ' -q -f -k -', 'max_level': 9, 'ext': '.lz4' })
#exes.append({'exe': 'xz', 'uncompress': ' -T0 -q -f -k -d ', 'compress': ' -T0 -q -f -k -', 'max_level': 9, 'ext': '.xz' })
exes.append({'exe': 'gzip', 'uncompress': ' -q -f -k -d ', 'compress': ' -q -f -k -', 'max_level': 9, 'ext': '.gz' })
executable = stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH
exeDir = './exe'
if not os.path.isdir(exeDir):
print('Run a_compile.py first: Unable to find "' + exeDir +'"')
else:
for exe in os.listdir(exeDir):
exe = os.path.join(exeDir, exe)
if os.path.isfile(exe):
st = os.stat(exe)
mode = st.st_mode
if mode & executable:
exe = os.path.abspath(exe)
exes.append({'exe': exe, 'uncompress': ' -q -f -k -d ', 'compress': ' -q -f -k -', 'max_level': 9, 'ext': '.gz' })
exts = []
for i in range(len(exes)) :
ext = exes[i]['ext']
if ext not in exts:
exts.append(ext)
for i in range(len(exes)) :
test_cmp(
exes[i]['exe'],
indir,
repeats,
exes[i]['ext'],
exes[i]['compress'],
exes[i]['max_level'],
exts)
plot(results_file)
for i in range(len(exts)) :
ext = exts[i]
exes2 = []
for i in range(len(exes)) :
if exes[i]['ext'] == ext :
exes2.append(exes[i])
test_decomp(exes2, indir, exts, repeats)