1 /*
2 bench.c - Demo program to benchmark open-source compression algorithm
3 Copyright (C) Yann Collet 2012-2015
4 Copyright (C) Przemyslaw Skibinski 2016-2017
5
6 GPL v2 License
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 You can contact the author at :
23 - Lizard source repository : https://github.com/inikep/lizard
24 */
25
26 /**************************************
27 * Compiler Options
28 **************************************/
29 /* Disable some Visual warning messages */
30 #define _CRT_SECURE_NO_WARNINGS
31 #define _CRT_SECURE_NO_DEPRECATE /* VS2005 */
32
33 /* Unix Large Files support (>4GB) */
34 #if (defined(__sun__) && (!defined(__LP64__))) // Sun Solaris 32-bits requires specific definitions
35 # define _LARGEFILE_SOURCE
36 # define _FILE_OFFSET_BITS 64
37 #elif ! defined(__LP64__) // No point defining Large file for 64 bit
38 # define _LARGEFILE64_SOURCE
39 #endif
40
41 // S_ISREG & gettimeofday() are not supported by MSVC
42 #if defined(_MSC_VER) || defined(_WIN32)
43 # define BMK_LEGACY_TIMER 1
44 #endif
45
46
47 /**************************************
48 * Includes
49 **************************************/
50 #include <stdlib.h> /* malloc, free */
51 #include <stdio.h> /* fprintf, fopen, ftello64 */
52 #include <sys/types.h> /* stat64 */
53 #include <sys/stat.h> /* stat64 */
54 #include <string.h> /* strcmp */
55 #include <time.h> /* clock_t, clock(), CLOCKS_PER_SEC */
56
57 #include "lizard_compress.h"
58 #include "lizard_decompress.h"
59 #include "lizard_common.h" /* Lizard_compress_MinLevel, Lizard_createStream_MinLevel */
60 #include "lizard_frame.h"
61
62 #include "xxhash/xxhash.h"
63
64
65 /**************************************
66 * Compiler Options
67 **************************************/
68 /* S_ISREG & gettimeofday() are not supported by MSVC */
69 #if !defined(S_ISREG)
70 # define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
71 #endif
72
73
74
75 /**************************************
76 * Constants
77 **************************************/
78 #define PROGRAM_DESCRIPTION "Lizard speed analyzer"
79 #define AUTHOR "Yann Collet"
80 #define WELCOME_MESSAGE "*** %s v%s %i-bits, by %s ***\n", PROGRAM_DESCRIPTION, LIZARD_VERSION_STRING, (int)(sizeof(void*)*8), AUTHOR
81
82 #define NBLOOPS 6
83 #define TIMELOOP (CLOCKS_PER_SEC * 25 / 10)
84
85 #define KNUTH 2654435761U
86 #define MAX_MEM (1920 MB)
87 #define DEFAULT_CHUNKSIZE (4 MB)
88
89 #define ALL_COMPRESSORS 0
90 #define ALL_DECOMPRESSORS 0
91
92
93 /**************************************
94 * Local structures
95 **************************************/
96 struct chunkParameters
97 {
98 U32 id;
99 char* origBuffer;
100 char* compressedBuffer;
101 int origSize;
102 int compressedSize;
103 };
104
105
106 /**************************************
107 * Macros
108 **************************************/
109 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
110 #define PROGRESS(...) g_noPrompt ? 0 : DISPLAY(__VA_ARGS__)
111
112
113 /**************************************
114 * Benchmark Parameters
115 **************************************/
116 static int g_chunkSize = DEFAULT_CHUNKSIZE;
117 static int g_nbIterations = NBLOOPS;
118 static int g_pause = 0;
119 static int g_compressionTest = 1;
120 static int g_compressionAlgo = ALL_COMPRESSORS;
121 static int g_decompressionTest = 1;
122 static int g_decompressionAlgo = ALL_DECOMPRESSORS;
123 static int g_noPrompt = 0;
124
BMK_setBlocksize(int bsize)125 static void BMK_setBlocksize(int bsize)
126 {
127 g_chunkSize = bsize;
128 DISPLAY("-Using Block Size of %i KB-\n", g_chunkSize>>10);
129 }
130
BMK_setNbIterations(int nbLoops)131 static void BMK_setNbIterations(int nbLoops)
132 {
133 g_nbIterations = nbLoops;
134 DISPLAY("- %i iterations -\n", g_nbIterations);
135 }
136
BMK_setPause(void)137 static void BMK_setPause(void)
138 {
139 g_pause = 1;
140 }
141
142
143 /*********************************************************
144 * Private functions
145 *********************************************************/
BMK_GetClockSpan(clock_t clockStart)146 static clock_t BMK_GetClockSpan( clock_t clockStart )
147 {
148 return clock() - clockStart; /* works even if overflow; max span ~30 mn */
149 }
150
151
BMK_findMaxMem(U64 requiredMem)152 static size_t BMK_findMaxMem(U64 requiredMem)
153 {
154 size_t step = 64 MB;
155 BYTE* testmem=NULL;
156
157 requiredMem = (((requiredMem >> 26) + 1) << 26);
158 requiredMem += 2*step;
159 if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
160
161 while (!testmem) {
162 if (requiredMem > step) requiredMem -= step;
163 else requiredMem >>= 1;
164 testmem = (BYTE*) malloc ((size_t)requiredMem);
165 }
166 free (testmem);
167
168 /* keep some space available */
169 if (requiredMem > step) requiredMem -= step;
170 else requiredMem >>= 1;
171
172 return (size_t)requiredMem;
173 }
174
175
BMK_GetFileSize(const char * infilename)176 static U64 BMK_GetFileSize(const char* infilename)
177 {
178 int r;
179 #if defined(_MSC_VER)
180 struct _stat64 statbuf;
181 r = _stat64(infilename, &statbuf);
182 #else
183 struct stat statbuf;
184 r = stat(infilename, &statbuf);
185 #endif
186 if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
187 return (U64)statbuf.st_size;
188 }
189
190
191 /*********************************************************
192 * Benchmark function
193 *********************************************************/
194 Lizard_stream_t *Lizard_stream;
local_Lizard_createStream(void)195 static void local_Lizard_createStream(void)
196 {
197 Lizard_stream = Lizard_resetStream_MinLevel(Lizard_stream);
198 }
199
local_Lizard_saveDict(const char * in,char * out,int inSize)200 static int local_Lizard_saveDict(const char* in, char* out, int inSize)
201 {
202 (void)in;
203 return Lizard_saveDict(Lizard_stream, out, inSize);
204 }
205
local_Lizard_compress_default_large(const char * in,char * out,int inSize)206 static int local_Lizard_compress_default_large(const char* in, char* out, int inSize)
207 {
208 return Lizard_compress_MinLevel(in, out, inSize, Lizard_compressBound(inSize));
209 }
210
local_Lizard_compress_default_small(const char * in,char * out,int inSize)211 static int local_Lizard_compress_default_small(const char* in, char* out, int inSize)
212 {
213 return Lizard_compress_MinLevel(in, out, inSize, Lizard_compressBound(inSize)-1);
214 }
215
local_Lizard_compress_withState(const char * in,char * out,int inSize)216 static int local_Lizard_compress_withState(const char* in, char* out, int inSize)
217 {
218 return Lizard_compress_extState_MinLevel(Lizard_stream, in, out, inSize, Lizard_compressBound(inSize));
219 }
220
local_Lizard_compress_limitedOutput_withState(const char * in,char * out,int inSize)221 static int local_Lizard_compress_limitedOutput_withState(const char* in, char* out, int inSize)
222 {
223 return Lizard_compress_extState_MinLevel(Lizard_stream, in, out, inSize, Lizard_compressBound(inSize)-1);
224 }
225
local_Lizard_compress_continue(const char * in,char * out,int inSize)226 static int local_Lizard_compress_continue(const char* in, char* out, int inSize)
227 {
228 return Lizard_compress_continue(Lizard_stream, in, out, inSize, Lizard_compressBound(inSize));
229 }
230
local_Lizard_compress_limitedOutput_continue(const char * in,char * out,int inSize)231 static int local_Lizard_compress_limitedOutput_continue(const char* in, char* out, int inSize)
232 {
233 return Lizard_compress_continue(Lizard_stream, in, out, inSize, Lizard_compressBound(inSize)-1);
234 }
235
236
237 /* HC compression functions */
238 Lizard_stream_t* Lizard_streamPtr;
local_Lizard_resetStream(void)239 static void local_Lizard_resetStream(void)
240 {
241 Lizard_streamPtr = Lizard_resetStream(Lizard_streamPtr, 0);
242 }
243
local_Lizard_saveDictHC(const char * in,char * out,int inSize)244 static int local_Lizard_saveDictHC(const char* in, char* out, int inSize)
245 {
246 (void)in;
247 return Lizard_saveDict(Lizard_streamPtr, out, inSize);
248 }
249
local_Lizard_compress_extState(const char * in,char * out,int inSize)250 static int local_Lizard_compress_extState(const char* in, char* out, int inSize)
251 {
252 return Lizard_compress_extState(Lizard_streamPtr, in, out, inSize, Lizard_compressBound(inSize), 0);
253 }
254
local_Lizard_compress_extState_limitedOutput(const char * in,char * out,int inSize)255 static int local_Lizard_compress_extState_limitedOutput(const char* in, char* out, int inSize)
256 {
257 return Lizard_compress_extState(Lizard_streamPtr, in, out, inSize, Lizard_compressBound(inSize)-1, 0);
258 }
259
local_Lizard_compress(const char * in,char * out,int inSize)260 static int local_Lizard_compress(const char* in, char* out, int inSize)
261 {
262 return Lizard_compress(in, out, inSize, Lizard_compressBound(inSize), 0);
263 }
264
local_Lizard_compress_limitedOutput(const char * in,char * out,int inSize)265 static int local_Lizard_compress_limitedOutput(const char* in, char* out, int inSize)
266 {
267 return Lizard_compress(in, out, inSize, Lizard_compressBound(inSize)-1, 0);
268 }
269
local_Lizard_compressHC_continue(const char * in,char * out,int inSize)270 static int local_Lizard_compressHC_continue(const char* in, char* out, int inSize)
271 {
272 return Lizard_compress_continue(Lizard_streamPtr, in, out, inSize, Lizard_compressBound(inSize));
273 }
274
local_Lizard_compress_continue_limitedOutput(const char * in,char * out,int inSize)275 static int local_Lizard_compress_continue_limitedOutput(const char* in, char* out, int inSize)
276 {
277 return Lizard_compress_continue(Lizard_streamPtr, in, out, inSize, Lizard_compressBound(inSize)-1);
278 }
279
280
281 /* decompression functions */
local_Lizard_decompress_safe_usingDict(const char * in,char * out,int inSize,int outSize)282 static int local_Lizard_decompress_safe_usingDict(const char* in, char* out, int inSize, int outSize)
283 {
284 (void)inSize;
285 Lizard_decompress_safe_usingDict(in, out, inSize, outSize, out - 65536, 65536);
286 return outSize;
287 }
288
289 extern int Lizard_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize, const char* dict, int dictSize);
290
local_Lizard_decompress_safe_forceExtDict(const char * in,char * out,int inSize,int outSize)291 static int local_Lizard_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize)
292 {
293 (void)inSize;
294 Lizard_decompress_safe_forceExtDict(in, out, inSize, outSize, out - 65536, 65536);
295 return outSize;
296 }
297
local_Lizard_decompress_safe_partial(const char * in,char * out,int inSize,int outSize)298 static int local_Lizard_decompress_safe_partial(const char* in, char* out, int inSize, int outSize)
299 {
300 return Lizard_decompress_safe_partial(in, out, inSize, outSize - 5, outSize);
301 }
302
303
304 /* frame functions */
local_LizardF_compressFrame(const char * in,char * out,int inSize)305 static int local_LizardF_compressFrame(const char* in, char* out, int inSize)
306 {
307 return (int)LizardF_compressFrame(out, 2*inSize + 16, in, inSize, NULL);
308 }
309
310 static LizardF_decompressionContext_t g_dCtx;
311
local_LizardF_decompress(const char * in,char * out,int inSize,int outSize)312 static int local_LizardF_decompress(const char* in, char* out, int inSize, int outSize)
313 {
314 size_t srcSize = inSize;
315 size_t dstSize = outSize;
316 size_t result;
317 result = LizardF_decompress(g_dCtx, out, &dstSize, in, &srcSize, NULL);
318 if (result!=0) { DISPLAY("Error decompressing frame : unfinished frame (%d)\n", (int)result); exit(8); }
319 if (srcSize != (size_t)inSize) { DISPLAY("Error decompressing frame : read size incorrect\n"); exit(9); }
320 return (int)dstSize;
321 }
322
323
324 #define NB_COMPRESSION_ALGORITHMS 100
325 #define NB_DECOMPRESSION_ALGORITHMS 100
fullSpeedBench(const char ** fileNamesTable,int nbFiles)326 int fullSpeedBench(const char** fileNamesTable, int nbFiles)
327 {
328 int fileIdx=0;
329
330 /* Init */
331 { size_t const errorCode = LizardF_createDecompressionContext(&g_dCtx, LIZARDF_VERSION);
332 if (LizardF_isError(errorCode)) { DISPLAY("dctx allocation issue \n"); return 10; } }
333
334 Lizard_streamPtr = Lizard_createStream(0);
335 if (!Lizard_streamPtr) { DISPLAY("Lizard_streamPtr allocation issue \n"); return 10; }
336
337 Lizard_stream = Lizard_createStream_MinLevel();
338 if (!Lizard_stream) { DISPLAY("Lizard_stream allocation issue \n"); return 10; }
339
340 /* Loop for each fileName */
341 while (fileIdx<nbFiles) {
342 char* orig_buff = NULL;
343 struct chunkParameters* chunkP = NULL;
344 char* compressed_buff=NULL;
345 const char* const inFileName = fileNamesTable[fileIdx++];
346 FILE* const inFile = fopen( inFileName, "rb" );
347 U64 inFileSize;
348 size_t benchedSize;
349 int nbChunks;
350 int maxCompressedChunkSize;
351 size_t readSize;
352 int compressedBuffSize;
353 U32 crcOriginal;
354 size_t errorCode;
355
356 /* Check file existence */
357 if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
358
359 /* Memory size adjustments */
360 inFileSize = BMK_GetFileSize(inFileName);
361 if (inFileSize==0) { DISPLAY( "file is empty\n"); fclose(inFile); return 11; }
362 benchedSize = BMK_findMaxMem(inFileSize*2) / 2; /* because 2 buffers */
363 if (benchedSize==0) { DISPLAY( "not enough memory\n"); fclose(inFile); return 11; }
364 if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
365 if (benchedSize < inFileSize)
366 DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20));
367
368 /* Allocation */
369 chunkP = (struct chunkParameters*) malloc(((benchedSize / (size_t)g_chunkSize)+1) * sizeof(struct chunkParameters));
370 orig_buff = (char*) malloc(benchedSize);
371 nbChunks = (int) ((benchedSize + (g_chunkSize-1)) / g_chunkSize);
372 maxCompressedChunkSize = Lizard_compressBound(g_chunkSize);
373 compressedBuffSize = nbChunks * maxCompressedChunkSize;
374 compressed_buff = (char*)malloc((size_t)compressedBuffSize);
375 if(!chunkP || !orig_buff || !compressed_buff) {
376 DISPLAY("\nError: not enough memory!\n");
377 fclose(inFile);
378 free(orig_buff);
379 free(compressed_buff);
380 free(chunkP);
381 return(12);
382 }
383
384 /* Fill in src buffer */
385 DISPLAY("Loading %s... \r", inFileName);
386 readSize = fread(orig_buff, 1, benchedSize, inFile);
387 fclose(inFile);
388
389 if (readSize != benchedSize) {
390 DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
391 free(orig_buff);
392 free(compressed_buff);
393 free(chunkP);
394 return 13;
395 }
396
397 /* Calculating input Checksum */
398 crcOriginal = XXH32(orig_buff, benchedSize,0);
399
400
401 /* Bench */
402 { int loopNb, nb_loops, chunkNb, cAlgNb, dAlgNb;
403 size_t cSize=0;
404 double ratio=0.;
405
406 DISPLAY("\r%79s\r", "");
407 DISPLAY(" %s : \n", inFileName);
408
409 /* Bench Compression Algorithms */
410 for (cAlgNb=0; (cAlgNb <= NB_COMPRESSION_ALGORITHMS) && (g_compressionTest); cAlgNb++) {
411 const char* compressorName;
412 int (*compressionFunction)(const char*, char*, int);
413 void (*initFunction)(void) = NULL;
414 double bestTime = 100000000.;
415
416 /* filter compressionAlgo only */
417 if ((g_compressionAlgo != ALL_COMPRESSORS) && (g_compressionAlgo != cAlgNb)) continue;
418
419 /* Init data chunks */
420 { int i;
421 size_t remaining = benchedSize;
422 char* in = orig_buff;
423 char* out = compressed_buff;
424 nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize);
425 for (i=0; i<nbChunks; i++) {
426 chunkP[i].id = i;
427 chunkP[i].origBuffer = in; in += g_chunkSize;
428 if ((int)remaining > g_chunkSize) { chunkP[i].origSize = g_chunkSize; remaining -= g_chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; }
429 chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
430 chunkP[i].compressedSize = 0;
431 }
432 }
433
434 switch(cAlgNb)
435 {
436 case 0 : DISPLAY("Compression functions : \n"); continue;
437 case 1 : compressionFunction = local_Lizard_compress_default_large; compressorName = "Lizard_compress_MinLevel"; break;
438 case 2 : compressionFunction = local_Lizard_compress_default_small; compressorName = "Lizard_compress_MinLevel(small dst)"; break;
439
440 case 10: compressionFunction = local_Lizard_compress; compressorName = "Lizard_compress"; break;
441 case 11: compressionFunction = local_Lizard_compress_limitedOutput; compressorName = "Lizard_compress limitedOutput"; break;
442 case 12 : compressionFunction = local_Lizard_compress_extState; compressorName = "Lizard_compress_extState"; break;
443 case 13: compressionFunction = local_Lizard_compress_extState_limitedOutput; compressorName = "Lizard_compress_extState limitedOutput"; break;
444 case 14: compressionFunction = local_Lizard_compressHC_continue; initFunction = local_Lizard_resetStream; compressorName = "Lizard_compress_continue"; break;
445 case 15: compressionFunction = local_Lizard_compress_continue_limitedOutput; initFunction = local_Lizard_resetStream; compressorName = "Lizard_compress_continue limitedOutput"; break;
446 case 30: compressionFunction = local_LizardF_compressFrame; compressorName = "LizardF_compressFrame";
447 chunkP[0].origSize = (int)benchedSize; nbChunks=1;
448 break;
449 case 40: compressionFunction = local_Lizard_saveDict; compressorName = "Lizard_saveDict";
450 Lizard_loadDict(Lizard_stream, chunkP[0].origBuffer, chunkP[0].origSize);
451 break;
452 case 41: compressionFunction = local_Lizard_saveDictHC; compressorName = "Lizard_saveDict";
453 Lizard_loadDict(Lizard_streamPtr, chunkP[0].origBuffer, chunkP[0].origSize);
454 break;
455 case 16: compressionFunction = local_Lizard_compress_withState; compressorName = "Lizard_compress_extState_MinLevel(1)"; break;
456 case 17: compressionFunction = local_Lizard_compress_limitedOutput_withState; compressorName = "Lizard_compress_extState_MinLevel(1) limitedOutput"; break;
457 case 18: compressionFunction = local_Lizard_compress_continue; initFunction = local_Lizard_createStream; compressorName = "Lizard_compress_continue(1)"; break;
458 case 19: compressionFunction = local_Lizard_compress_limitedOutput_continue; initFunction = local_Lizard_createStream; compressorName = "Lizard_compress_continue(1) limitedOutput"; break;
459 case 60: DISPLAY("Obsolete compression functions : \n"); continue;
460 default :
461 continue; /* unknown ID : just skip */
462 }
463
464 for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
465 double averageTime;
466 clock_t clockTime;
467
468 PROGRESS("%1i- %-28.28s :%9i ->\r", loopNb, compressorName, (int)benchedSize);
469 { size_t i; for (i=0; i<benchedSize; i++) compressed_buff[i]=(char)i; } /* warming up memory */
470
471 nb_loops = 0;
472 clockTime = clock();
473 while(clock() == clockTime);
474 clockTime = clock();
475 while(BMK_GetClockSpan(clockTime) < TIMELOOP) {
476 if (initFunction!=NULL) initFunction();
477 for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
478 chunkP[chunkNb].compressedSize = compressionFunction(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize);
479 if (chunkP[chunkNb].compressedSize==0) DISPLAY("ERROR ! %s() = 0 !! \n", compressorName), exit(1);
480 }
481 nb_loops++;
482 }
483 clockTime = BMK_GetClockSpan(clockTime);
484
485 nb_loops += !nb_loops; /* avoid division by zero */
486 averageTime = ((double)clockTime) / nb_loops / CLOCKS_PER_SEC;
487 if (averageTime < bestTime) bestTime = averageTime;
488 cSize=0; for (chunkNb=0; chunkNb<nbChunks; chunkNb++) cSize += chunkP[chunkNb].compressedSize;
489 ratio = (double)cSize/(double)benchedSize*100.;
490 PROGRESS("%1i- %-28.28s :%9i ->%9i (%5.2f%%),%7.1f MB/s\r", loopNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000000);
491 }
492
493 if (ratio<100.)
494 DISPLAY("%2i-%-28.28s :%9i ->%9i (%5.2f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000000);
495 else
496 DISPLAY("%2i-%-28.28s :%9i ->%9i (%5.1f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 100000);
497 }
498
499 /* Prepare layout for decompression */
500 /* Init data chunks */
501 { int i;
502 size_t remaining = benchedSize;
503 char* in = orig_buff;
504 char* out = compressed_buff;
505
506 nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize);
507 for (i=0; i<nbChunks; i++) {
508 chunkP[i].id = i;
509 chunkP[i].origBuffer = in; in += g_chunkSize;
510 if ((int)remaining > g_chunkSize) { chunkP[i].origSize = g_chunkSize; remaining -= g_chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; }
511 chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
512 chunkP[i].compressedSize = 0;
513 }
514 }
515 for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
516 chunkP[chunkNb].compressedSize = Lizard_compress_MinLevel(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize, Lizard_compressBound(chunkP[chunkNb].origSize));
517 if (chunkP[chunkNb].compressedSize==0) DISPLAY("ERROR ! %s() = 0 !! \n", "Lizard_compress_MinLevel"), exit(1);
518 }
519
520 /* Decompression Algorithms */
521 for (dAlgNb=0; (dAlgNb <= NB_DECOMPRESSION_ALGORITHMS) && (g_decompressionTest); dAlgNb++) {
522 const char* dName;
523 int (*decompressionFunction)(const char*, char*, int, int);
524 double bestTime = 100000000.;
525
526 if ((g_decompressionAlgo != ALL_DECOMPRESSORS) && (g_decompressionAlgo != dAlgNb)) continue;
527
528 switch(dAlgNb)
529 {
530 case 0: DISPLAY("Decompression functions : \n"); continue;
531 case 4: decompressionFunction = Lizard_decompress_safe; dName = "Lizard_decompress_safe"; break;
532 case 6: decompressionFunction = local_Lizard_decompress_safe_usingDict; dName = "Lizard_decompress_safe_usingDict"; break;
533 case 7: decompressionFunction = local_Lizard_decompress_safe_partial; dName = "Lizard_decompress_safe_partial"; break;
534 case 8: decompressionFunction = local_Lizard_decompress_safe_forceExtDict; dName = "Lizard_decompress_safe_forceExtDict"; break;
535 case 9: decompressionFunction = local_LizardF_decompress; dName = "LizardF_decompress";
536 errorCode = LizardF_compressFrame(compressed_buff, compressedBuffSize, orig_buff, benchedSize, NULL);
537 if (LizardF_isError(errorCode)) {
538 DISPLAY("Error while preparing compressed frame\n");
539 free(orig_buff);
540 free(compressed_buff);
541 free(chunkP);
542 return 1;
543 }
544 chunkP[0].origSize = (int)benchedSize;
545 chunkP[0].compressedSize = (int)errorCode;
546 nbChunks = 1;
547 break;
548 default :
549 continue; /* skip if unknown ID */
550 }
551
552 { size_t i; for (i=0; i<benchedSize; i++) orig_buff[i]=0; } /* zeroing source area, for CRC checking */
553
554 for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
555 double averageTime;
556 clock_t clockTime;
557 U32 crcDecoded;
558
559 PROGRESS("%1i- %-29.29s :%10i ->\r", loopNb, dName, (int)benchedSize);
560
561 nb_loops = 0;
562 clockTime = clock();
563 while(clock() == clockTime);
564 clockTime = clock();
565 while(BMK_GetClockSpan(clockTime) < TIMELOOP) {
566 for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
567 int decodedSize = decompressionFunction(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedSize, chunkP[chunkNb].origSize);
568 if (chunkP[chunkNb].origSize != decodedSize) DISPLAY("ERROR ! %s() == %i != %i !! \n", dName, decodedSize, chunkP[chunkNb].origSize), exit(1);
569 }
570 nb_loops++;
571 }
572 clockTime = BMK_GetClockSpan(clockTime);
573
574 nb_loops += !nb_loops; /* Avoid division by zero */
575 averageTime = (double)clockTime / nb_loops / CLOCKS_PER_SEC;
576 if (averageTime < bestTime) bestTime = averageTime;
577
578 PROGRESS("%1i- %-29.29s :%10i -> %7.1f MB/s\r", loopNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000000);
579
580 /* CRC Checking */
581 crcDecoded = XXH32(orig_buff, (int)benchedSize, 0);
582 if (crcOriginal!=crcDecoded) { DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", inFileName, (unsigned)crcOriginal, (unsigned)crcDecoded); exit(1); }
583 }
584
585 DISPLAY("%2i-%-29.29s :%10i -> %7.1f MB/s\n", dAlgNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000000);
586 }
587 }
588 free(orig_buff);
589 free(compressed_buff);
590 free(chunkP);
591 }
592
593 Lizard_freeStream(Lizard_stream);
594 Lizard_freeStream(Lizard_streamPtr);
595 LizardF_freeDecompressionContext(g_dCtx);
596 if (g_pause) { printf("press enter...\n"); (void)getchar(); }
597
598 return 0;
599 }
600
601
usage(const char * exename)602 static int usage(const char* exename)
603 {
604 DISPLAY( "Usage :\n");
605 DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename);
606 DISPLAY( "Arguments :\n");
607 DISPLAY( " -c : compression tests only\n");
608 DISPLAY( " -d : decompression tests only\n");
609 DISPLAY( " -H/-h : Help (this text + advanced options)\n");
610 return 0;
611 }
612
usage_advanced(void)613 static int usage_advanced(void)
614 {
615 DISPLAY( "\nAdvanced options :\n");
616 DISPLAY( " -c# : test only compression function # [1-%i]\n", NB_COMPRESSION_ALGORITHMS);
617 DISPLAY( " -d# : test only decompression function # [1-%i]\n", NB_DECOMPRESSION_ALGORITHMS);
618 DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS);
619 DISPLAY( " -B# : Block size [4-7](default : 7)\n");
620 return 0;
621 }
622
badusage(const char * exename)623 static int badusage(const char* exename)
624 {
625 DISPLAY("Wrong parameters\n");
626 usage(exename);
627 return 0;
628 }
629
main(int argc,const char ** argv)630 int main(int argc, const char** argv)
631 {
632 int i,
633 filenamesStart=2;
634 const char* exename = argv[0];
635 const char* input_filename=0;
636
637 // Welcome message
638 DISPLAY(WELCOME_MESSAGE);
639
640 if (argc<2) { badusage(exename); return 1; }
641
642 for(i=1; i<argc; i++) {
643 const char* argument = argv[i];
644
645 if(!argument) continue; // Protection if argument empty
646 if (!strcmp(argument, "--no-prompt")) {
647 g_noPrompt = 1;
648 continue;
649 }
650
651 // Decode command (note : aggregated commands are allowed)
652 if (argument[0]=='-') {
653 while (argument[1]!=0) {
654 argument ++;
655
656 switch(argument[0])
657 {
658 // Select compression algorithm only
659 case 'c':
660 g_decompressionTest = 0;
661 while ((argument[1]>= '0') && (argument[1]<= '9')) {
662 g_compressionAlgo *= 10;
663 g_compressionAlgo += argument[1] - '0';
664 argument++;
665 }
666 break;
667
668 // Select decompression algorithm only
669 case 'd':
670 g_compressionTest = 0;
671 while ((argument[1]>= '0') && (argument[1]<= '9')) {
672 g_decompressionAlgo *= 10;
673 g_decompressionAlgo += argument[1] - '0';
674 argument++;
675 }
676 break;
677
678 // Display help on usage
679 case 'h' :
680 case 'H': usage(exename); usage_advanced(); return 0;
681
682 // Modify Block Properties
683 case 'B':
684 while (argument[1]!=0)
685 switch(argument[1])
686 {
687 case '4':
688 case '5':
689 case '6':
690 case '7':
691 { int B = argument[1] - '0';
692 int S = 1 << (8 + 2*B);
693 BMK_setBlocksize(S);
694 argument++;
695 break;
696 }
697 case 'D': argument++; break;
698 default : goto _exit_blockProperties;
699 }
700 _exit_blockProperties:
701 break;
702
703 // Modify Nb Iterations
704 case 'i':
705 if ((argument[1] >='0') && (argument[1] <='9')) {
706 int iters = argument[1] - '0';
707 BMK_setNbIterations(iters);
708 argument++;
709 }
710 break;
711
712 // Pause at the end (hidden option)
713 case 'p': BMK_setPause(); break;
714
715 // Unknown command
716 default : badusage(exename); return 1;
717 }
718 }
719 continue;
720 }
721
722 // first provided filename is input
723 if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
724
725 }
726
727 // No input filename ==> Error
728 if(!input_filename) { badusage(exename); return 1; }
729
730 return fullSpeedBench(argv+filenamesStart, argc-filenamesStart);
731
732 }
733