-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpzip_sim.py
191 lines (160 loc) · 6.74 KB
/
dpzip_sim.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
import argparse
import os, sys
from time import gmtime, strftime
import timeit
import time
import gzip
import zstd
import lz4
import lz4.frame
import numpy as np
compressed_size = []
chunk_kb = 0
def getfilesize(filename):
try:
ret = os.path.getsize(filename)
except:
print("except in getfilesize {}".format(filename))
sys.exit(1)
return ret
def chunk_file_in_bytes(filename, chunk_kB):
ret = []
# Open original file in read only mode
if not os.path.isfile(filename):
print("No such file as: \"%s\"" % filename)
return
filesize=getfilesize(filename)
with open(filename,"rb") as fr:
file_bytes = fr.read()
if not chunk_kB:
return [file_bytes]
splitsize = chunk_kB * 1024
n_splits = (filesize + splitsize - 1)//splitsize
return [file_bytes[i * splitsize:(i + 1) * splitsize] for i in range(n_splits)]
def chunk_mem_in_bytes(data_content, chunk_kB):
ret = []
datasize=len(data_content)
if not chunk_kB:
return [data_content]
splitsize = chunk_kB * 1024
n_splits = (datasize + splitsize - 1)//splitsize
return [data_content[i * splitsize:(i + 1) * splitsize] for i in range(n_splits)]
def zstd2dpzip_size_ratio(s):
if s <= 380:
return int(s * 1.33)
if s <= 633:
return int(s * (1.33+1.21) / 2)
if s <= 738:
return int(s * (1.21+1.21) / 2)
if s <= 1202:
return int(s * (1.21+1.05) / 2)
if s <= 1624:
return int(s * (1.05+1.03) / 2)
if s <= 1734:
return int(s * (1.03+1.03) / 2)
if s <= 1883:
return int(s * (1.00+1.03) / 2)
if s <= 2048:
return int(s * (1.02+1.00) / 2)
if s <= 2539:
return int(s * (1.04+1.02) / 2)
if s <= 3151:
return int(s * (1.00+1.04) / 2)
return s
def compress_bytes(compressor, direction, cmp_level, byte_content):
start_t = time.time()
end_t = time.time()
start_t = time.time()
if compressor == 'gzip':
if not direction:
if cmp_level:
ret = gzip.compress(byte_content, compresslevel=cmp_level)
else:
ret = gzip.compress(byte_content, compresslevel=6)
compressed_size.append(len(ret))
if direction == 1:
ret = gzip.decompress(byte_content)
if compressor in ['zstd', 'dpzip']:
if not direction:
if cmp_level:
ret = zstd.compress(byte_content, cmp_level, 1)
else:
ret = zstd.compress(byte_content, 3, 1)
if compressor == 'zstd':
compressed_size.append(len(ret))
if compressor == 'dpzip':
compressed_size.append(zstd2dpzip_size_ratio(len(ret)))
if direction == 1:
ret = zstd.decompress(byte_content)
if compressor == 'lz4':
if not direction:
ret = lz4.frame.compress(byte_content)
if direction == 1:
ret = lz4.frame.decompress(byte_content)
compressed_size.append(len(ret))
end_t = time.time()
comp_time = end_t - start_t
return ret, len(ret)/len(byte_content), len(byte_content)/1024/1024/(comp_time), 0
def print_compress_metrics(compressed_size_list):
print("Total compressed bytes: {}".format(sum(compressed_size_list)))
print("Percetile compressed file size:\n\t1,\t10,\t20,\t30,\t40,\t50,\t60,\t70,\t80,\t90,\t99")
#print(" ,\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f}".format(np.percentile(compressed_size_list, 1), np.percentile(compressed_size_list, 10),
print("\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f},\t{:.0f}".format(np.percentile(compressed_size_list, 1), np.percentile(compressed_size_list, 10),
np.percentile(compressed_size_list, 20), np.percentile(compressed_size_list, 30), np.percentile(compressed_size_list, 40), np.percentile(compressed_size_list, 50),
np.percentile(compressed_size_list, 60), np.percentile(compressed_size_list, 70), np.percentile(compressed_size_list, 80), np.percentile(compressed_size_list, 90),
#np.percentile(compressed_size_list, 95), np.percentile(compressed_size_list, 99),
np.percentile(compressed_size_list, 99)
))
print("Average compression ratio: {:.2f}".format(len(compressed_size_list)*chunk_kb*1024 / sum(compressed_size_list)))
def read_in_chunks(file_object, chunk_size=1024*1024*1024):
"""Lazy function (generator) to read a file piece by piece.
Default chunk size: 1GB."""
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
def compress_in_mem_chunks(compressor, direction, cmp_level, f_name, chunk_kB, hw_granularity, m_distance):
chunk_cmp_ratio_list = []
chunk_cmp_time_list = []
compressed_chunk_size_list = []
input_num_bytes = 0
output_num_bytes = 0
wasted_num_bytes = 0
# if f_name is smaller than 1GB
if getfilesize(f_name) <= 1024 * 1024 * 1024:
byte_chunk_list = chunk_file_in_bytes(f_name, chunk_kB)
for f in byte_chunk_list:
output, ratio, speed, ent = compress_bytes(compressor, direction, cmp_level, f)
input_num_bytes += len(f)
output_num_bytes += len(output)
if hw_granularity and len(output) % hw_granularity:
wasted_num_bytes += (hw_granularity - len(output) % hw_granularity)
chunk_cmp_time_list.append(speed)
chunk_cmp_ratio_list.append(ratio)
compressed_chunk_size_list.append(len(output))
else:
with open(f_name, 'rb') as f:
for piece in read_in_chunks(f):
byte_chunk_list = chunk_mem_in_bytes(piece, chunk_kB)
for f in byte_chunk_list:
output, ratio, speed, ent = compress_bytes(compressor, direction, cmp_level, f)
chunk_cmp_time_list.append(speed)
chunk_cmp_ratio_list.append(ratio)
# compress
if not direction:
print_compress_metrics(compressed_size)
def do_compress():
global chunk_kb
parser = argparse.ArgumentParser()
parser.add_argument("compressor", type=str, choices=['gzip', 'zstd', 'lz4', 'dpzip'],
help="choose the compressor")
parser.add_argument("input_file_path", type=str,
help="the input file path must be specified")
args = parser.parse_args()
answer = args.input_file_path
chunk_kb = 4
print("Original dataset bytes: {}".format(getfilesize(args.input_file_path)))
compress_in_mem_chunks(args.compressor, 0, 1, args.input_file_path, 4, 0, 0)
if __name__ == '__main__':
do_compress()