3 # Copyright (C) 2014 Intra2net AG
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published
7 # by the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see
17 # <http://www.gnu.org/licenses/lgpl-3.0.html>
19 # Authors: Victor Ramirez de la Corte <victor@wadobo.com>
20 # Eduardo Robles Elvira <edulix@wadobo.com>
31 def main(args = None):
33 Main function, parses the command line arguments and launches the appropiate
36 parser = argparse.ArgumentParser(description='Profiling test options. ')
37 parser.add_argument('-t', '--test',
38 choices=['delta-tarfile', 'tarfile', 'zlib',
40 help='Select option for testing. ')
41 parser.add_argument('-l', '--compression-level', type=int, default=9,
42 choices=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
43 help='Select level of compression for zlib. ')
44 parser.add_argument('-p', '--path',
45 help='path to the file/dir to compress.')
46 parser.add_argument('-P', '--profile', action='store_true',
47 help='Enable profile')
48 parser.add_argument('-m', '--tarmode', default="w:gz",
49 help='Specify tarfile mode [w:gz]')
50 parser.add_argument('-st', '--strategy', default='default',
51 choices=['default', 'filtered', 'huffman_only'],
52 help='Select the deflate strategy in zlib related '
56 help='Sort output profile',
57 choices=[ 'calls', 'cumulative', 'cumtime', 'file', 'path', 'module',
58 'ncalls', 'pcalls', 'line', 'name', 'nfl', 'stdname', 'time',
63 pargs = parser.parse_args()
65 pargs = parser.parse_args(args)
67 compression_level = pargs.compression_level
72 pr = cProfile.Profile()
75 if pargs.test in ['delta-tarfile', 'tarfile']:
76 test_tarfile(pargs.test, compression_level, path, pargs.tarmode)
77 elif pargs.test == 'zlib':
78 test_zlib(compression_level, path, pargs.strategy)
79 elif pargs.test == 'zlib-blocks':
80 test_zlib_blocks(compression_level, path, pargs.strategy)
87 print_profile(pr, pargs.sort)
89 def test_tarfile(res, compression_level=9, path='source_dir', tarmode="w:gz"):
91 Test that creates a tarfile called test_tarfile.tar.gz
93 if res == 'delta-tarfile':
94 deltatar = os.path.abspath("../deltatar")
97 from deltatar import tarfile as TarFile
98 elif res == 'tarfile':
99 import tarfile as TarFile
101 print("creating a tarfile with mode = '%s'" % tarmode)
102 with open('test_tarfile.tar.gz', 'wb') as fo:
103 tar = TarFile.open(mode=tarmode, fileobj=fo,
104 compresslevel=compression_level)
107 def test_zlib(compression_level=9, path='source_dir', strategy='default',
108 BUFSIZE = 16 * 1024):
110 Test that compresses a directory with zlib
112 if strategy == 'default':
113 strategy = zlib.Z_DEFAULT_STRATEGY
114 elif strategy == 'filtered':
115 strategy = zlib.Z_FILTERED
117 strategy = zlib.Z_HUFFMAN_ONLY
119 c = zlib.compressobj(compression_level,
128 if os.path.isdir(path):
129 for f in os.listdir(path):
130 l2, l_orig2 = add(os.path.join(path, f))
133 elif os.path.isfile(path):
134 with open(path, 'rb') as fo:
136 buf = fo.read(BUFSIZE)
140 buf = c.compress(buf)
145 l, l_orig = add(path)
147 print("total size: %d MiB compressed, %d MiB uncompressed" % (
149 (l_orig / (1024 ** 2))))
152 def test_zlib_blocks(compression_level=9, path='source_dir', strategy='default',
153 BUFSIZE = 16 * 1024):
155 Test that compresses a directory with zlib
157 if strategy == 'default':
158 strategy = zlib.Z_DEFAULT_STRATEGY
159 elif strategy == 'filtered':
160 strategy = zlib.Z_FILTERED
162 strategy = zlib.Z_HUFFMAN_ONLY
167 if os.path.isdir(path):
168 for f in os.listdir(path):
169 l2, l_orig2 = add(os.path.join(path, f))
172 elif os.path.isfile(path):
173 c = zlib.compressobj(compression_level,
178 with open(path, 'rb') as fo:
180 buf = fo.read(BUFSIZE)
185 buf = c.compress(buf)
190 l, l_orig = add(path)
191 print("total size: %d MiB compressed, %d MiB uncompressed" % (
193 (l_orig / (1024 ** 2))))
195 def print_profile(pr, sort):
199 ps = pstats.Stats(pr, stream=s).sort_stats(sort)
204 if __name__ == '__main__':