-
Notifications
You must be signed in to change notification settings - Fork 7
/
speed_test.cpp
386 lines (330 loc) · 13.3 KB
/
speed_test.cpp
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
//speed_test.cpp
// for tinyuz
/*
The MIT License (MIT)
Copyright (c) 2012-2022 HouSisong All Rights Reserved.
*/
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include "../HDiffPatch/_clock_for_demo.h" //in HDiffPatch
#include "decompress/tuz_dec.h"
#include "compress/tuz_enc.h"
#include "zlib.h"
#ifdef min
# undef min
#endif
std::string TEST_FILE_DIR;
tuz_BOOL isDictSizeTest=0;
static const tuz_size_t tDictSizes[]={24,79,255,1<<10,5<<10,32<<10,1<<20,32<<20};
static const char* tDictSizes_tag[]={"24","79","255","1k","5k","32k","1m","32m"};
void readFile(std::vector<unsigned char>& data,const char* fileName){
FILE * file=fopen(fileName, "rb");
assert(file);
fseek(file,0,SEEK_END);
int file_length = (int)ftell(file);
fseek(file,0,SEEK_SET);
data.resize(file_length);
if (file_length>0)
fread(&data[0],1,file_length,file);
fclose(file);
}
void writeFile(const std::vector<unsigned char>& data,const char* fileName){
FILE* file=fopen(fileName, "wb");
int dataSize=(int)data.size();
if (dataSize>0)
fwrite(&data[0], 1,dataSize, file);
fclose(file);
}
typedef int (*T_compress)(unsigned char* out_data,unsigned char* out_data_end,const unsigned char* src,const unsigned char* src_end);
typedef bool (*T_decompress)(unsigned char* out_data,unsigned char* out_data_end,const unsigned char* zip_code,const unsigned char* zip_code_end);
struct TTestResult {
std::string procName;
std::string srcFileName;
double compressTime_s;
double decompressTime_s;
int srcSize;
int zipSize;
};
double minEncTestTime=0.5;
double minDecTestTime=0.5;
double testDecodeProc(T_decompress proc_decompress,unsigned char* out_data,unsigned char* out_data_end,const unsigned char* zip_code,const unsigned char* zip_code_end){
int testDecompressCount=0;
double time1=clock_s();
double time2=time1;
do {
for (int i=0; i<10; ++i){
bool ret=proc_decompress(out_data,out_data_end,zip_code,zip_code_end);
++testDecompressCount;
if (!ret) throw "error result!";
if (isDictSizeTest) break;
}
time2=clock_s();
}while ((time2-time1)<minDecTestTime);
double decompressTime_s=(time2-time1)/testDecompressCount;
return decompressTime_s;
}
double testEncodeProc(T_compress proc_compress,std::vector<unsigned char>& compressedCode,const unsigned char* src,const unsigned char* src_end){
int testCompressCount=0;
compressedCode.resize((size_t)((src_end-src)*1.2)+1024);
int dstCodeSize=0;
double time1=clock_s();
do{
dstCodeSize=proc_compress(&compressedCode[0],&compressedCode[0]+compressedCode.size(),src,src_end);
++testCompressCount;
}while ((clock_s()-time1)<minEncTestTime);
double time2=clock_s();
compressedCode.resize(dstCodeSize);
double compressTime_s=(time2-time1)/testCompressCount;
return compressTime_s;
}
TTestResult testProc(const char* srcFileName,T_compress proc_compress,const char* proc_compress_Name,
T_decompress proc_decompress,const char* proc_decompress_Name){
std::string testFilePath=TEST_FILE_DIR; testFilePath.append(srcFileName);
std::vector<unsigned char> oldData; readFile(oldData,testFilePath.c_str());
const unsigned char* src=&oldData[0];
const unsigned char* src_end=src+oldData.size();
std::vector<unsigned char> compressedCode;
double compressTime_s=testEncodeProc(proc_compress,compressedCode,src,src_end);
const unsigned char* unsrc=&compressedCode[0];
std::vector<unsigned char> uncompressedCode(oldData.size(),0);
unsigned char* undst=&uncompressedCode[0];
double decompressTime_s=testDecodeProc(proc_decompress,undst,undst+uncompressedCode.size(),unsrc,unsrc+compressedCode.size());
decompressTime_s=std::min(decompressTime_s,testDecodeProc(proc_decompress,undst,undst+uncompressedCode.size(),unsrc,unsrc+compressedCode.size()));
decompressTime_s=std::min(decompressTime_s,testDecodeProc(proc_decompress,undst,undst+uncompressedCode.size(),unsrc,unsrc+compressedCode.size()));
if (uncompressedCode!=oldData){
throw "error data!";
}
TTestResult result;
result.procName=proc_decompress_Name;
result.srcFileName=srcFileName;
result.compressTime_s=compressTime_s;
result.decompressTime_s=decompressTime_s;
result.srcSize=(int)(src_end-src);
result.zipSize=(int)compressedCode.size();
return result;
}
static void outResult(const TTestResult& rt){
const bool isSimpleView=isDictSizeTest;
if (isSimpleView){
static tuz_BOOL isOutedTag=0;
if (!isOutedTag){
isOutedTag=1;
printf("|zlib -9");
for (int i=(sizeof(tDictSizes)/sizeof(tDictSizes[0])-1);i>=0; --i) {
std::string tag=std::string("tuz -")+tDictSizes_tag[i];
printf("|%s",tag.c_str());
}
}
static std::string srcFileName_back;
if (srcFileName_back!=rt.srcFileName) {
srcFileName_back=rt.srcFileName;
printf("\n%s",rt.srcFileName.c_str());
}
printf("|%.2f%%",rt.zipSize*100.0/rt.srcSize);
}else{//default view
printf("%s|%d|%s|",rt.srcFileName.c_str(),rt.srcSize,rt.procName.c_str());
printf("%.2f|%.0f|",rt.srcSize/rt.compressTime_s/1024/1024,rt.srcSize/rt.decompressTime_s/1024/1024);
printf("%.2f|%.0f|",rt.zipSize/rt.compressTime_s/1024/1024,rt.zipSize/rt.decompressTime_s/1024/1024);
printf("%d|%.2f%%\n",rt.zipSize,rt.zipSize*100.0/rt.srcSize);
}
}
////
int zlib_windowBits = 0;
int zlib_level = 9;
int zlib_compress(unsigned char* out_data,unsigned char* out_data_end,
const unsigned char* src,const unsigned char* src_end){
const unsigned char* _zipSrc=&src[0];
unsigned char* _zipDst=&out_data[0];
z_stream c_stream;
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.opaque = (voidpf)0;
c_stream.next_in = (Bytef*)_zipSrc;
c_stream.avail_in = (int)(src_end-src);
c_stream.next_out = (Bytef*)_zipDst;
c_stream.avail_out = (unsigned int)(out_data_end-out_data);
int ret = deflateInit2(&c_stream,zlib_level,Z_DEFLATED,zlib_windowBits,MAX_MEM_LEVEL,Z_DEFAULT_STRATEGY);
if(ret!=Z_OK)
throw "deflateInit2 error !";
ret = deflate(&c_stream,Z_FINISH);
if (ret!=Z_STREAM_END)
throw "deflate error !";
int zipLen = (int)c_stream.total_out;
ret = deflateEnd(&c_stream);
if (ret!=Z_OK)
throw "deflateEnd error !";
return zipLen;
}
const tuz_size_t kCodeCacheSize=1024*16;
bool zlib_decompress(unsigned char* out_data,unsigned char* out_data_end,
const unsigned char* zip_code,const unsigned char* zip_code_end){
int ret;
unsigned have;
z_stream strm;
unsigned char out[kCodeCacheSize];
int totalsize = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit2(&strm,zlib_windowBits);
if (ret != Z_OK)
return false;
strm.avail_in = (int)(zip_code_end-zip_code);
strm.next_in = (unsigned char*)zip_code;
do {
strm.avail_out = kCodeCacheSize;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
switch (ret)
{
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
inflateEnd(&strm);
return false;
}
have = kCodeCacheSize - strm.avail_out;
memcpy(out_data + totalsize,out,have);
totalsize += have;
assert(out_data+totalsize<=out_data_end);
} while (strm.avail_out == 0);
inflateEnd(&strm);
return ret == Z_STREAM_END;
}
tuz_size_t _tuz_kDictSize=0;
int _test_tuz_compress(unsigned char* out_data,unsigned char* out_data_end,
const unsigned char* src,const unsigned char* src_end){
hpatch_TStreamOutput out_stream;
mem_as_hStreamOutput(&out_stream,out_data,out_data_end);
hpatch_TStreamInput in_stream;
mem_as_hStreamInput(&in_stream,src,src_end);
tuz_TCompressProps props=tuz_kDefaultCompressProps;
props.dictSize=_tuz_kDictSize;
props.threadNum=8;
//props.maxSaveLength=255;
hpatch_StreamPos_t codeSize=tuz_compress(&out_stream,&in_stream,&props);
return (int)codeSize;
}
struct TTuzListener{
const unsigned char* src;
const unsigned char* src_end;
static tuz_BOOL read_code(void* listener,tuz_byte* out_code,tuz_size_t* code_size){
TTuzListener* self=(TTuzListener*)listener;
tuz_size_t r_size=*code_size;
size_t s_size=self->src_end-self->src;
if (r_size>s_size){
r_size=(tuz_size_t)s_size;
*code_size=r_size;
}
memcpy(out_code,self->src,r_size);
self->src+=r_size;
return tuz_TRUE;
}
};
const bool is_decode_step=true;
tuz_TResult tuz_decompress_stream(const tuz_byte* code,const tuz_byte* code_end,
tuz_byte* out_uncompress,size_t* uncompress_size){
TTuzListener listener={code,code_end};
tuz_byte* _dict_buf=0;
tuz_TStream tuz;
tuz_TResult result=tuz_OK;
tuz_size_t dictSize=tuz_TStream_read_dict_size(&listener,listener.read_code);
assert((tuz_size_t)(dictSize-1)<tuz_kMaxOfDictSize);
_dict_buf=(tuz_byte*)malloc(dictSize+kCodeCacheSize);
assert(_dict_buf!=0);
result=tuz_TStream_open(&tuz,&listener,listener.read_code,_dict_buf,dictSize,kCodeCacheSize);
if (is_decode_step){
const size_t buf_size=*uncompress_size;
size_t data_size=0;
while (result==tuz_OK) {
tuz_size_t step_size=kCodeCacheSize; //for test
if (data_size+step_size>buf_size)
step_size=(tuz_size_t)(buf_size-data_size);
result=tuz_TStream_decompress_partial(&tuz,out_uncompress,&step_size);
data_size+=step_size;
out_uncompress+=step_size;
}
*uncompress_size=data_size;
}else if (result==tuz_OK){
tuz_size_t usize=(tuz_size_t)(*uncompress_size);
assert(usize==*uncompress_size);
result=tuz_TStream_decompress_partial(&tuz,out_uncompress,&usize);
*uncompress_size=usize;
}
free(_dict_buf);
return result;
}
bool _test_tuz_decompress_stream(unsigned char* out_data,unsigned char* out_data_end,const unsigned char* zip_code,const unsigned char* zip_code_end){
size_t uncompress_size=out_data_end-out_data;
tuz_TResult ret=tuz_decompress_stream(zip_code,zip_code_end,out_data,&uncompress_size);
return (ret==tuz_STREAM_END)&&(uncompress_size==(out_data_end-out_data));
}
bool _test_tuz_decompress_mem(unsigned char* out_data,unsigned char* out_data_end,const unsigned char* zip_code,const unsigned char* zip_code_end){
tuz_size_t uncompress_size=(tuz_size_t)(out_data_end-out_data);
tuz_TResult ret=tuz_decompress_mem(zip_code,(tuz_size_t)(zip_code_end-zip_code),out_data,&uncompress_size);
return (ret==tuz_STREAM_END)&&(uncompress_size==(out_data_end-out_data));
}
static void testFile(const char* srcFileName){
//zlib_level=6;
//outResult(testProc(srcFileName, zlib_compress, "", zlib_decompress, " zlib -6"));
zlib_level=9;
if (!isDictSizeTest) {
outResult(testProc(srcFileName,zlib_compress ,"",zlib_decompress ," zlib -9"));
outResult(testProc(srcFileName,_test_tuz_compress,"",_test_tuz_decompress_stream,"tinyuz_stream"));
outResult(testProc(srcFileName,_test_tuz_compress,"",_test_tuz_decompress_mem ," tinyuz_mem"));
}else{
zlib_windowBits=-15;
outResult(testProc(srcFileName,zlib_compress ,"",zlib_decompress ,"zlib -9"));
for (int i=(sizeof(tDictSizes)/sizeof(tDictSizes[0])-1);i>=0; --i) {
std::string tag=std::string("tinyuz -c-")+tDictSizes_tag[i];
_tuz_kDictSize=tDictSizes[i];
outResult(testProc(srcFileName,_test_tuz_compress,"",_test_tuz_decompress_stream,tag.c_str()));
}
}
}
int main(int argc, const char * argv[]){
if (argc>2){
std::cout << "speed_test \"testFile\"\n";
return -1;
}
isDictSizeTest=0;
const int testDictBit=15;
zlib_windowBits=-testDictBit;
_tuz_kDictSize = (1 << testDictBit);
if (!isDictSizeTest){
std::cout << " ( dictSize: " << _tuz_kDictSize
<< " codeCacheSize: " << kCodeCacheSize << " )\n";
}
if (argc==2) { testFile(argv[1]); return 0; }
//* //for test
std::cout << "test start> \n";
//testFile("empty1.txt");
//testFile("V0.pat"); testFile("V1.pat"); testFile("V2.pat"); testFile("V3.pat");
//testFile("V0.bin"); testFile("V1.bin"); testFile("V2.bin"); testFile("V3.bin"); testFile("V4.bin");
//*
//testFile("aMCU.bin");
//testFile("aMCU.bin.diff");
testFile("A10.jpg");
testFile("AcroRd32.exe");
testFile("english.dic");
testFile("FlashMX.pdf");
testFile("FP.LOG");
testFile("MSO97.DLL");
testFile("ohs.doc");
testFile("rafale.bmp");
testFile("vcfiu.hlp");
testFile("world95.txt");
testFile("enwik8");
testFile("silesia.tar");
//testFile("enwik9");
//*/
std::cout << "done!\n";
return 0;
}