-
Notifications
You must be signed in to change notification settings - Fork 42
/
traditionalCompressAndGetRate.py
203 lines (183 loc) · 7.02 KB
/
traditionalCompressAndGetRate.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
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
# Get compress rate of algorithms.
import os
import csv
import sys
import lzma
import gzip
import bz2
import time
import threading
basePath = "./"
dataPath=basePath+"data/"
resultPath=basePath+"results/logTIM_results/"
results = []
# ans = []
tempans=[]
# ------ compress
def deal_fileName(fileName):
length=len(fileName)
for i in range(length):
if fileName[length-1-i]==".":
return fileName[:length-1-i]
def time_func(function):
def inner(sourceFile,destFile,*args,**kwargs):
global tempans
sourceSize = os.path.getsize(sourceFile)
t0=time.time()
function(sourceFile,destFile)
t1=time.time()
destSize = os.path.getsize(destFile)
# ans.append([round(t1-t0,5),round(destSize/sourceSize,5),function.__name__,sourceFile])
tempans=[sourceSize,destSize]
return inner
@time_func
def compress_bzip2(sourceFile,destFile):
with bz2.BZ2File(destFile, 'wb') as des:
with open(sourceFile, 'rb') as sou:
des.writelines(sou)
@time_func
def compress_7zip(sourceFile,destFile):
os.system("7z a -t7z {destfile} {sourcefile}".format(destfile=destFile,sourcefile=sourceFile))
@time_func
def compress_zip(sourceFile,destFile):
os.system("7z a -tzip {destfile} {sourcefile}".format(destfile=destFile,sourcefile=sourceFile))
def deal_argv(argv):
global tempans
global results
try:
with open(argv[1],mode="r") as f:
pass
except:
print("Source file does not exist.")
return
if argv[0] == "bz2":
if len(argv)==2:
argv.append(deal_fileName(argv[1])+".bz2")
elif argv[2][-4:]!=".bz2":
argv[2]=argv[2]+".bz2"
if os.path.exists(argv[2])==True:
tempans = [os.path.getsize(argv[1]),os.path.getsize(argv[2])]
else:
compress_bzip2(argv[1],argv[2])
results[-1].extend([tempans[1],round(tempans[1]/tempans[0],5)])
elif argv[0] == "7z":
if len(argv)==2:
argv.append(deal_fileName(argv[1])+".7z")
elif argv[2][-3:]!=".7z":
argv[2]=argv[2]+".7z"
if os.path.exists(argv[2])==True:
tempans = [os.path.getsize(argv[1]),os.path.getsize(argv[2])]
else:
compress_7zip(argv[1],argv[2])
results[-1].extend([tempans[1],round(tempans[1]/tempans[0],5)])
elif argv[0] == "zip":
if len(argv)==2:
argv.append(deal_fileName(argv[1])+".zip")
elif argv[2][-4:]!=".zip":
argv[2]=argv[2]+".zip"
if os.path.exists(argv[2])==True:
tempans = [os.path.getsize(argv[1]),os.path.getsize(argv[2])]
else:
compress_zip(argv[1],argv[2])
results[-1].extend([tempans[1],round(tempans[1]/tempans[0],5)])
else:
# print("Usage: compressWay source_file_name dest_file_name.compressWay")
print("error")
def deal_argv_match(argv,sourceSize):
global tempans
global results
try:
with open(argv[1],mode="r") as f:
pass
except:
print("Source file does not exist.")
return
if argv[0] == "bz2":
if len(argv)==2:
argv.append(deal_fileName(argv[1])+".bz2")
elif argv[2][-4:]!=".bz2":
argv[2]=argv[2]+".bz2"
if os.path.exists(argv[2])==True:
tempans = [os.path.getsize(argv[1]),os.path.getsize(argv[2])]
else:
compress_bzip2(argv[1],argv[2])
results[-1].extend([tempans[1],round(tempans[1]/sourceSize,5)])
elif argv[0] == "7z":
if len(argv)==2:
argv.append(deal_fileName(argv[1])+".7z")
elif argv[2][-3:]!=".7z":
argv[2]=argv[2]+".7z"
if os.path.exists(argv[2])==True:
tempans = [os.path.getsize(argv[1]),os.path.getsize(argv[2])]
else:
compress_7zip(argv[1],argv[2])
results[-1].extend([tempans[1],round(tempans[1]/sourceSize,5)])
elif argv[0] == "zip":
if len(argv)==2:
argv.append(deal_fileName(argv[1])+".zip")
elif argv[2][-4:]!=".zip":
argv[2]=argv[2]+".zip"
if os.path.exists(argv[2])==True:
tempans = [os.path.getsize(argv[1]),os.path.getsize(argv[2])]
else:
compress_zip(argv[1],argv[2])
results[-1].extend([tempans[1],round(tempans[1]/sourceSize,5)])
else:
# print("Usage: compressWay source_file_name dest_file_name.compressWay")
print("error")
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python compressAndGetRate.py [all] [match] [first] [second] [[1-9]|all]")
else:
tempSysargvs = sys.argv
if tempSysargvs[1] == "all":
wayargvs = ["match","first","second"]
else:
wayargvs = tempSysargvs[1:-1]
if tempSysargvs[-1]=="all":
tempDir = "_tail"
else:
tempDir = "_tail_0."+tempSysargvs[-1]+"0"
tempPaths = os.listdir(dataPath)
rawlogPaths = [x for x in tempPaths if tempDir in x]
rawlogInfo = {}
for i in rawlogPaths:
temp = i.split("_tail")
tempPath = temp[0]+temp[1]
rawlogInfo[tempPath] = {"size":os.path.getsize(dataPath+i+"/rawlog.log"),"path":dataPath+i+"/rawlog.log","judge":0}
algPaths = os.listdir(resultPath)
for path in algPaths:
tempList = os.listdir(resultPath+path)
for subPath in rawlogInfo.keys():
tempPath = resultPath+path+"/"+subPath+"/matchResults.txt"
sourceSize = rawlogInfo[subPath]["size"]
results.append([path,subPath,sourceSize])
if "match" in wayargvs:
try:
matchResultsSize = os.path.getsize(tempPath)
results[-1].extend([matchResultsSize,round(matchResultsSize/sourceSize,5)])
except:
results[-1].extend(["--","--"])
ways = ["bz2","7z","zip"]
if "first" in wayargvs:
for way in ways:
tempargv=[way,rawlogInfo[subPath]["path"]]
deal_argv(tempargv)
if "second" in wayargvs:
for way in ways:
tempargv=[way,tempPath]
deal_argv_match(tempargv,rawlogInfo[subPath]["size"])
csvPath = "result_"+tempSysargvs[1]+"_"+tempSysargvs[2]+".csv"
with open(csvPath,"w") as f:
f_csv = csv.writer(f)
row = ["algorithm","dataset","sourceSize"]
if "match" in wayargvs:
row.extend(["matchResultsSize","matchResults_rate"])
if "first" in wayargvs:
row.extend(["bz2","bz2_rate","7z","7z_rate","zip","zip_rate"])
if "second" in wayargvs:
row.extend(["double_bz2","double_bz2_rate","double_7z","double_7z_rate","double_zip","double_zip_rate"])
f_csv.writerow(row)
f_csv.writerows(results)