bpo-32713: Fix tarfile.itn for large/negative float values. (GH-5434)
[python-delta-tar] / testing / test_performance.py
CommitLineData
ea6edc93
CH
1#!/usr/bin/env python3
2
3""" Test performance of compressed write
4
5Creates a single big volume with random files added
6
a7660b1c
CH
7Results on my devel box:
8~0.055s/MB --> ~1 minute per GB
9compress ratio ~1.15
10adding encryption had no noticeable effect
11switching mode between w|gz or w#gz had no noticeable effect
12switching to bzip2 reduced speed to ~0.174s/MB, compression ratio approx. equal
13
ea6edc93
CH
14.. codeauthor:: Intra2net <info@intra2net.com>
15"""
16
a7660b1c 17from tempfile import mkstemp
ea6edc93
CH
18from time import perf_counter
19import os
20
21if __name__ == '__main__':
22 from os.path import dirname, abspath
23 import sys
24 parent_dir = dirname(dirname(abspath(__file__)))
25 sys.path.insert(0, parent_dir)
26 print('pre-prended {} to sys path'.format(parent_dir))
27import deltatar
28from deltatar.tarfile import TarFile
29
30from test_multivol_compression_sizes import find_random_files
31
a7660b1c 32
ea6edc93
CH
33def main():
34 """ Main function, called when running file as script
35
36 see module doc for more info
37 """
38
39 mode = 'w#gz'
40 suffix = '.tgz'
41 #goal_size = 650*1e6 # 1 CD
42 goal_size = 2 * 1e9 # 2 GB (2/3 of space in my /tmp)
43 size_added = 0
44 size_tol = 32 * 1000 # 32k
45 min_size = 5
46 open_time = add_time = close_time = 0.0
47 n_files_added = 0
48
a7660b1c
CH
49 result_file_name = None
50 file_handle = None
51 try:
52 # create temp file
53 file_handle, result_file_name = mkstemp(
54 prefix='deltatar_multivol_cmp_tst_', suffix=suffix)
55 os.close(file_handle)
56 file_handle = None
57
ea6edc93
CH
58 print('opening temp file ' + result_file_name)
59
60 start = perf_counter()
a7660b1c 61 tarobj = TarFile.open(result_file_name, mode=mode, password='test1234')
ea6edc93
CH
62 end = perf_counter()
63 open_time = end - start
64
a7660b1c 65 for add_file_name in find_random_files(min_size):
ea6edc93
CH
66 # check file
67 if add_file_name.startswith(result_file_name[:-6]):
68 continue # do not accidentally add self
69 file_size = os.lstat(add_file_name).st_size
70 if file_size < min_size:
71 continue
72 if file_size + size_added > goal_size + size_tol:
73 continue # new file is too big
74
75 # do add
76 start = perf_counter()
77 tarobj.add(add_file_name)
78 end = perf_counter()
79 add_time += (end - start)
80
81 # update sizes and counts
82 size_added += file_size
83 n_files_added += 1
84 #print('added file of size {:9d}, {:9d} left (file name: {})'
85 # .format(file_size, goal_size-size_added, add_file_name))
86 if n_files_added % 100 == 0:
87 print('added {:4d} files of overall size {:6.1f}MB, {:6.1f}MB '
88 'left ({:4.1f}%); avg time to add per MB: {:.3f}s'
89 .format(n_files_added, size_added/1.e6,
90 (goal_size-size_added)/1.e6,
91 size_added / goal_size * 100.,
92 add_time/size_added*1.e6))
93
94 if size_added > goal_size - size_tol:
95 break
96
97 print('closing file')
98 start = perf_counter()
99 tarobj.close()
100 end = perf_counter()
101 close_time = end - start
102
103 result_size = os.stat(result_file_name).st_size
104
a7660b1c
CH
105 # summarize
106 print('time to open/close the tar file: {:.3f} / {:.3f}ms'
107 .format(open_time*1000., close_time*1000.))
108 print('time to add {} files: {:.3f}s (avg {:.3f}ms per file)'
109 .format(n_files_added, add_time, add_time / n_files_added * 1000.))
110 print('average added file size: {:.3f}KB'
111 .format(size_added/n_files_added/1000.))
112 print('time to add per MB: {:.3f}s'.format(add_time/size_added*1.0e6))
113 print('size of result file: {} --> compression ratio {:.2f}'
114 .format(result_size, size_added/result_size))
115
116 finally:
117 if file_handle:
118 os.close(file_handle)
119 if result_file_name:
120 # del temp file
121 os.unlink(result_file_name)
ea6edc93
CH
122
123
124if __name__ == '__main__':
125 main()