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