• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_destSize(const char * in,char * out,int inSize)187 static int local_LZ4_compress_destSize(const char* in, char* out, int inSize)
188 {
189     return LZ4_compress_destSize(in, out, &inSize, LZ4_compressBound(inSize)-1);
190 }
191 
local_LZ4_compress_fast0(const char * in,char * out,int inSize)192 static int local_LZ4_compress_fast0(const char* in, char* out, int inSize)
193 {
194     return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 0);
195 }
196 
local_LZ4_compress_fast1(const char * in,char * out,int inSize)197 static int local_LZ4_compress_fast1(const char* in, char* out, int inSize)
198 {
199     return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 1);
200 }
201 
local_LZ4_compress_fast2(const char * in,char * out,int inSize)202 static int local_LZ4_compress_fast2(const char* in, char* out, int inSize)
203 {
204     return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 2);
205 }
206 
local_LZ4_compress_fast17(const char * in,char * out,int inSize)207 static int local_LZ4_compress_fast17(const char* in, char* out, int inSize)
208 {
209     return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 17);
210 }
211 
local_LZ4_compress_fast_extState0(const char * in,char * out,int inSize)212 static int local_LZ4_compress_fast_extState0(const char* in, char* out, int inSize)
213 {
214     return LZ4_compress_fast_extState(&LZ4_stream, in, out, inSize, LZ4_compressBound(inSize), 0);
215 }
216 
local_LZ4_compress_fast_continue0(const char * in,char * out,int inSize)217 static int local_LZ4_compress_fast_continue0(const char* in, char* out, int inSize)
218 {
219     return LZ4_compress_fast_continue(&LZ4_stream, in, out, inSize, LZ4_compressBound(inSize), 0);
220 }
221 
222 #ifndef LZ4_DLL_IMPORT
223 #if defined (__cplusplus)
224 extern "C" {
225 #endif
226 
227 /* declare hidden function */
228 extern int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize);
229 
230 #if defined (__cplusplus)
231 }
232 #endif
233 
local_LZ4_compress_forceDict(const char * in,char * out,int inSize)234 static int local_LZ4_compress_forceDict(const char* in, char* out, int inSize)
235 {
236     return LZ4_compress_forceExtDict(&LZ4_stream, in, out, inSize);
237 }
238 #endif
239 
240 
241 /* HC compression functions */
242 LZ4_streamHC_t LZ4_streamHC;
local_LZ4_resetStreamHC(void)243 static void local_LZ4_resetStreamHC(void)
244 {
245     LZ4_resetStreamHC(&LZ4_streamHC, 0);
246 }
247 
local_LZ4_saveDictHC(const char * in,char * out,int inSize)248 static int local_LZ4_saveDictHC(const char* in, char* out, int inSize)
249 {
250     (void)in;
251     return LZ4_saveDictHC(&LZ4_streamHC, out, inSize);
252 }
253 
local_LZ4_compress_HC(const char * in,char * out,int inSize)254 static int local_LZ4_compress_HC(const char* in, char* out, int inSize)
255 {
256     return LZ4_compress_HC(in, out, inSize, LZ4_compressBound(inSize), 9);
257 }
258 
local_LZ4_compress_HC_extStateHC(const char * in,char * out,int inSize)259 static int local_LZ4_compress_HC_extStateHC(const char* in, char* out, int inSize)
260 {
261     return LZ4_compress_HC_extStateHC(&LZ4_streamHC, in, out, inSize, LZ4_compressBound(inSize), 9);
262 }
263 
local_LZ4_compress_HC_continue(const char * in,char * out,int inSize)264 static int local_LZ4_compress_HC_continue(const char* in, char* out, int inSize)
265 {
266     return LZ4_compress_HC_continue(&LZ4_streamHC, in, out, inSize, LZ4_compressBound(inSize));
267 }
268 
269 
270 /* decompression functions */
local_LZ4_decompress_fast(const char * in,char * out,int inSize,int outSize)271 static int local_LZ4_decompress_fast(const char* in, char* out, int inSize, int outSize)
272 {
273     (void)inSize;
274     LZ4_decompress_fast(in, out, outSize);
275     return outSize;
276 }
277 
local_LZ4_decompress_fast_usingDict_prefix(const char * in,char * out,int inSize,int outSize)278 static int local_LZ4_decompress_fast_usingDict_prefix(const char* in, char* out, int inSize, int outSize)
279 {
280     (void)inSize;
281     LZ4_decompress_fast_usingDict(in, out, outSize, out - 65536, 65536);
282     return outSize;
283 }
284 
local_LZ4_decompress_fast_usingExtDict(const char * in,char * out,int inSize,int outSize)285 static int local_LZ4_decompress_fast_usingExtDict(const char* in, char* out, int inSize, int outSize)
286 {
287     (void)inSize;
288     LZ4_decompress_fast_usingDict(in, out, outSize, out - 65536, 65535);
289     return outSize;
290 }
291 
local_LZ4_decompress_safe_usingDict(const char * in,char * out,int inSize,int outSize)292 static int local_LZ4_decompress_safe_usingDict(const char* in, char* out, int inSize, int outSize)
293 {
294     (void)inSize;
295     LZ4_decompress_safe_usingDict(in, out, inSize, outSize, out - 65536, 65536);
296     return outSize;
297 }
298 
299 #ifndef LZ4_DLL_IMPORT
300 #if defined (__cplusplus)
301 extern "C" {
302 #endif
303 
304 extern int LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize, const void* dict, size_t dictSize);
305 
306 #if defined (__cplusplus)
307 }
308 #endif
309 
local_LZ4_decompress_safe_forceExtDict(const char * in,char * out,int inSize,int outSize)310 static int local_LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize)
311 {
312     (void)inSize;
313     LZ4_decompress_safe_forceExtDict(in, out, inSize, outSize, out - 65536, 65536);
314     return outSize;
315 }
316 #endif
317 
local_LZ4_decompress_safe_partial(const char * in,char * out,int inSize,int outSize)318 static int local_LZ4_decompress_safe_partial(const char* in, char* out, int inSize, int outSize)
319 {
320     int result = LZ4_decompress_safe_partial(in, out, inSize, outSize - 5, outSize);
321     if (result < 0) return result;
322     return outSize;
323 }
324 
325 
326 /* frame functions */
local_LZ4F_compressFrame(const char * in,char * out,int inSize)327 static int local_LZ4F_compressFrame(const char* in, char* out, int inSize)
328 {
329     return (int)LZ4F_compressFrame(out, LZ4F_compressFrameBound(inSize, NULL), in, inSize, NULL);
330 }
331 
332 static LZ4F_decompressionContext_t g_dCtx;
333 
local_LZ4F_decompress(const char * in,char * out,int inSize,int outSize)334 static int local_LZ4F_decompress(const char* in, char* out, int inSize, int outSize)
335 {
336     size_t srcSize = inSize;
337     size_t dstSize = outSize;
338     size_t result;
339     result = LZ4F_decompress(g_dCtx, out, &dstSize, in, &srcSize, NULL);
340     if (result!=0) { DISPLAY("Error decompressing frame : unfinished frame\n"); exit(8); }
341     if (srcSize != (size_t)inSize) { DISPLAY("Error decompressing frame : read size incorrect\n"); exit(9); }
342     return (int)dstSize;
343 }
344 
345 
346 #define NB_COMPRESSION_ALGORITHMS 100
347 #define NB_DECOMPRESSION_ALGORITHMS 100
fullSpeedBench(const char ** fileNamesTable,int nbFiles)348 int fullSpeedBench(const char** fileNamesTable, int nbFiles)
349 {
350     int fileIdx=0;
351 
352     /* Init */
353     { size_t const errorCode = LZ4F_createDecompressionContext(&g_dCtx, LZ4F_VERSION);
354       if (LZ4F_isError(errorCode)) { DISPLAY("dctx allocation issue \n"); return 10; } }
355 
356     /* Loop for each fileName */
357     while (fileIdx<nbFiles) {
358       char* orig_buff = NULL;
359       struct chunkParameters* chunkP = NULL;
360       char* compressed_buff=NULL;
361       const char* const inFileName = fileNamesTable[fileIdx++];
362       FILE* const inFile = fopen( inFileName, "rb" );
363       U64   inFileSize;
364       size_t benchedSize;
365       int nbChunks;
366       int maxCompressedChunkSize;
367       size_t readSize;
368       int compressedBuffSize;
369       U32 crcOriginal;
370       size_t errorCode;
371 
372       /* Check file existence */
373       if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
374 
375       /* Memory size adjustments */
376       inFileSize = UTIL_getFileSize(inFileName);
377       if (inFileSize==0) { DISPLAY( "file is empty\n"); fclose(inFile); return 11; }
378       benchedSize = BMK_findMaxMem(inFileSize*2) / 2;   /* because 2 buffers */
379       if (benchedSize==0) { DISPLAY( "not enough memory\n"); fclose(inFile); return 11; }
380       if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
381       if (benchedSize < inFileSize)
382           DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20));
383 
384       /* Allocation */
385       chunkP = (struct chunkParameters*) malloc(((benchedSize / (size_t)g_chunkSize)+1) * sizeof(struct chunkParameters));
386       orig_buff = (char*) malloc(benchedSize);
387       nbChunks = (int) ((benchedSize + (g_chunkSize-1)) / g_chunkSize);
388       maxCompressedChunkSize = LZ4_compressBound(g_chunkSize);
389       compressedBuffSize = nbChunks * maxCompressedChunkSize;
390       compressed_buff = (char*)malloc((size_t)compressedBuffSize);
391       if(!chunkP || !orig_buff || !compressed_buff) {
392           DISPLAY("\nError: not enough memory!\n");
393           fclose(inFile);
394           free(orig_buff);
395           free(compressed_buff);
396           free(chunkP);
397           return(12);
398       }
399 
400       /* Fill in src buffer */
401       DISPLAY("Loading %s...       \r", inFileName);
402       readSize = fread(orig_buff, 1, benchedSize, inFile);
403       fclose(inFile);
404 
405       if (readSize != benchedSize) {
406         DISPLAY("\nError: problem reading file '%s' !!    \n", inFileName);
407         free(orig_buff);
408         free(compressed_buff);
409         free(chunkP);
410         return 13;
411       }
412 
413       /* Calculating input Checksum */
414       crcOriginal = XXH32(orig_buff, benchedSize,0);
415 
416 
417       /* Bench */
418       { int loopNb, nb_loops, chunkNb, cAlgNb, dAlgNb;
419         size_t cSize=0;
420         double ratio=0.;
421 
422         DISPLAY("\r%79s\r", "");
423         DISPLAY(" %s : \n", inFileName);
424 
425         /* Bench Compression Algorithms */
426         for (cAlgNb=0; (cAlgNb <= NB_COMPRESSION_ALGORITHMS) && (g_compressionTest); cAlgNb++) {
427             const char* compressorName;
428             int (*compressionFunction)(const char*, char*, int);
429             void (*initFunction)(void) = NULL;
430             double bestTime = 100000000.;
431 
432             /* filter compressionAlgo only */
433             if ((g_compressionAlgo != ALL_COMPRESSORS) && (g_compressionAlgo != cAlgNb)) continue;
434 
435             /* Init data chunks */
436             {   int i;
437                 size_t remaining = benchedSize;
438                 char* in = orig_buff;
439                 char* out = compressed_buff;
440                 nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize);
441                 for (i=0; i<nbChunks; i++) {
442                     chunkP[i].id = i;
443                     chunkP[i].origBuffer = in; in += g_chunkSize;
444                     if ((int)remaining > g_chunkSize) { chunkP[i].origSize = g_chunkSize; remaining -= g_chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; }
445                     chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
446                     chunkP[i].compressedSize = 0;
447                 }
448             }
449 
450             switch(cAlgNb)
451             {
452             case 0 : DISPLAY("Compression functions : \n"); continue;
453             case 1 : compressionFunction = local_LZ4_compress_default_large; compressorName = "LZ4_compress_default"; break;
454             case 2 : compressionFunction = local_LZ4_compress_default_small; compressorName = "LZ4_compress_default(small dst)"; break;
455             case 3 : compressionFunction = local_LZ4_compress_destSize; compressorName = "LZ4_compress_destSize"; break;
456             case 4 : compressionFunction = local_LZ4_compress_fast0; compressorName = "LZ4_compress_fast(0)"; break;
457             case 5 : compressionFunction = local_LZ4_compress_fast1; compressorName = "LZ4_compress_fast(1)"; break;
458             case 6 : compressionFunction = local_LZ4_compress_fast2; compressorName = "LZ4_compress_fast(2)"; break;
459             case 7 : compressionFunction = local_LZ4_compress_fast17; compressorName = "LZ4_compress_fast(17)"; break;
460             case 8 : compressionFunction = local_LZ4_compress_fast_extState0; compressorName = "LZ4_compress_fast_extState(0)"; break;
461             case 9 : compressionFunction = local_LZ4_compress_fast_continue0; initFunction = local_LZ4_createStream; compressorName = "LZ4_compress_fast_continue(0)"; break;
462 
463             case 10: compressionFunction = local_LZ4_compress_HC; compressorName = "LZ4_compress_HC"; break;
464             case 12: compressionFunction = local_LZ4_compress_HC_extStateHC; compressorName = "LZ4_compress_HC_extStateHC"; break;
465             case 14: compressionFunction = local_LZ4_compress_HC_continue; initFunction = local_LZ4_resetStreamHC; compressorName = "LZ4_compress_HC_continue"; break;
466 #ifndef LZ4_DLL_IMPORT
467             case 20: compressionFunction = local_LZ4_compress_forceDict; initFunction = local_LZ4_resetDictT; compressorName = "LZ4_compress_forceDict"; break;
468 #endif
469             case 30: compressionFunction = local_LZ4F_compressFrame; compressorName = "LZ4F_compressFrame";
470                         chunkP[0].origSize = (int)benchedSize; nbChunks=1;
471                         break;
472             case 40: compressionFunction = local_LZ4_saveDict; compressorName = "LZ4_saveDict";
473                         if (chunkP[0].origSize < 8) { DISPLAY(" cannot bench %s with less then 8 bytes \n", compressorName); continue; }
474                         LZ4_loadDict(&LZ4_stream, chunkP[0].origBuffer, chunkP[0].origSize);
475                         break;
476             case 41: compressionFunction = local_LZ4_saveDictHC; compressorName = "LZ4_saveDictHC";
477                         if (chunkP[0].origSize < 8) { DISPLAY(" cannot bench %s with less then 8 bytes \n", compressorName); continue; }
478                         LZ4_loadDictHC(&LZ4_streamHC, chunkP[0].origBuffer, chunkP[0].origSize);
479                         break;
480             default :
481                 continue;   /* unknown ID : just skip */
482             }
483 
484             for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
485                 double averageTime;
486                 clock_t clockTime;
487 
488                 PROGRESS("%2i-%-34.34s :%10i ->\r", loopNb, compressorName, (int)benchedSize);
489                 { size_t i; for (i=0; i<benchedSize; i++) compressed_buff[i]=(char)i; }     /* warming up memory */
490 
491                 nb_loops = 0;
492                 clockTime = clock();
493                 while(clock() == clockTime);
494                 clockTime = clock();
495                 while(BMK_GetClockSpan(clockTime) < TIMELOOP) {
496                     if (initFunction!=NULL) initFunction();
497                     for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
498                         chunkP[chunkNb].compressedSize = compressionFunction(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize);
499                         if (chunkP[chunkNb].compressedSize==0)
500                             DISPLAY("ERROR ! %s() = 0 !! \n", compressorName), exit(1);
501                     }
502                     nb_loops++;
503                 }
504                 clockTime = BMK_GetClockSpan(clockTime);
505 
506                 nb_loops += !nb_loops;   /* avoid division by zero */
507                 averageTime = ((double)clockTime) / nb_loops / CLOCKS_PER_SEC;
508                 if (averageTime < bestTime) bestTime = averageTime;
509                 cSize=0; for (chunkNb=0; chunkNb<nbChunks; chunkNb++) cSize += chunkP[chunkNb].compressedSize;
510                 ratio = (double)cSize/(double)benchedSize*100.;
511                 PROGRESS("%2i-%-34.34s :%10i ->%9i (%5.2f%%),%7.1f MB/s\r", loopNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000000);
512             }
513 
514             if (ratio<100.)
515                 DISPLAY("%2i-%-34.34s :%10i ->%9i (%5.2f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000000);
516             else
517                 DISPLAY("%2i-%-34.34s :%10i ->%9i (%5.1f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 100000);
518         }
519 
520         /* Prepare layout for decompression */
521         /* Init data chunks */
522         { int i;
523           size_t remaining = benchedSize;
524           char* in = orig_buff;
525           char* out = compressed_buff;
526 
527           nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize);
528           for (i=0; i<nbChunks; i++) {
529               chunkP[i].id = i;
530               chunkP[i].origBuffer = in; in += g_chunkSize;
531               if ((int)remaining > g_chunkSize) { chunkP[i].origSize = g_chunkSize; remaining -= g_chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; }
532               chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
533               chunkP[i].compressedSize = 0;
534           }
535         }
536         for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
537             chunkP[chunkNb].compressedSize = LZ4_compress_default(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize, maxCompressedChunkSize);
538             if (chunkP[chunkNb].compressedSize==0)
539                 DISPLAY("ERROR ! %s() = 0 !! \n", "LZ4_compress"), exit(1);
540         }
541 
542         /* Decompression Algorithms */
543         for (dAlgNb=0; (dAlgNb <= NB_DECOMPRESSION_ALGORITHMS) && g_decompressionTest; dAlgNb++) {
544             const char* dName;
545             int (*decompressionFunction)(const char*, char*, int, int);
546             double bestTime = 100000000.;
547             int checkResult = 1;
548 
549             if ((g_decompressionAlgo != ALL_DECOMPRESSORS) && (g_decompressionAlgo != dAlgNb)) continue;
550 
551             switch(dAlgNb)
552             {
553             case 0: DISPLAY("Decompression functions : \n"); continue;
554             case 1: decompressionFunction = local_LZ4_decompress_fast; dName = "LZ4_decompress_fast"; break;
555             case 2: decompressionFunction = local_LZ4_decompress_fast_usingDict_prefix; dName = "LZ4_decompress_fast_usingDict(prefix)"; break;
556             case 3: decompressionFunction = local_LZ4_decompress_fast_usingExtDict; dName = "LZ4_decompress_fast_using(Ext)Dict"; break;
557             case 4: decompressionFunction = LZ4_decompress_safe; dName = "LZ4_decompress_safe"; break;
558             case 6: decompressionFunction = local_LZ4_decompress_safe_usingDict; dName = "LZ4_decompress_safe_usingDict"; break;
559             case 7: decompressionFunction = local_LZ4_decompress_safe_partial; dName = "LZ4_decompress_safe_partial"; checkResult = 0; break;
560 #ifndef LZ4_DLL_IMPORT
561             case 8: decompressionFunction = local_LZ4_decompress_safe_forceExtDict; dName = "LZ4_decompress_safe_forceExtDict"; break;
562 #endif
563             case 9: decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress";
564                     errorCode = LZ4F_compressFrame(compressed_buff, compressedBuffSize, orig_buff, benchedSize, NULL);
565                     if (LZ4F_isError(errorCode)) {
566                         DISPLAY("Error while preparing compressed frame\n");
567                         free(orig_buff);
568                         free(compressed_buff);
569                         free(chunkP);
570                         return 1;
571                     }
572                     chunkP[0].origSize = (int)benchedSize;
573                     chunkP[0].compressedSize = (int)errorCode;
574                     nbChunks = 1;
575                     break;
576             default :
577                 continue;   /* skip if unknown ID */
578             }
579 
580             { size_t i; for (i=0; i<benchedSize; i++) orig_buff[i]=0; }     /* zeroing source area, for CRC checking */
581 
582             for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
583                 double averageTime;
584                 clock_t clockTime;
585                 U32 crcDecoded;
586 
587                 PROGRESS("%2i-%-34.34s :%10i ->\r", loopNb, dName, (int)benchedSize);
588 
589                 nb_loops = 0;
590                 clockTime = clock();
591                 while(clock() == clockTime);
592                 clockTime = clock();
593                 while(BMK_GetClockSpan(clockTime) < TIMELOOP) {
594                     for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
595                         int const decodedSize = decompressionFunction(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer,
596                                                                       chunkP[chunkNb].compressedSize, chunkP[chunkNb].origSize);
597                         if (chunkP[chunkNb].origSize != decodedSize) {
598                             DISPLAY("ERROR ! %s() == %i != %i !! \n",
599                                     dName, decodedSize, chunkP[chunkNb].origSize);
600                             exit(1);
601                     }   }
602                     nb_loops++;
603                 }
604                 clockTime = BMK_GetClockSpan(clockTime);
605 
606                 nb_loops += !nb_loops;   /* Avoid division by zero */
607                 averageTime = (double)clockTime / nb_loops / CLOCKS_PER_SEC;
608                 if (averageTime < bestTime) bestTime = averageTime;
609 
610                 PROGRESS("%2i-%-34.34s :%10i -> %7.1f MB/s\r", loopNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000000);
611 
612                 /* CRC Checking */
613                 crcDecoded = XXH32(orig_buff, (int)benchedSize, 0);
614                 if (checkResult && (crcOriginal!=crcDecoded)) {
615                     DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n",
616                             inFileName, (unsigned)crcOriginal, (unsigned)crcDecoded);
617                     exit(1);
618             }   }
619 
620             DISPLAY("%2i-%-34.34s :%10i -> %7.1f MB/s\n", dAlgNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000000);
621         }
622       }
623       free(orig_buff);
624       free(compressed_buff);
625       free(chunkP);
626     }
627 
628     LZ4F_freeDecompressionContext(g_dCtx);
629     if (g_pause) { printf("press enter...\n"); (void)getchar(); }
630 
631     return 0;
632 }
633 
634 
usage(const char * exename)635 static int usage(const char* exename)
636 {
637     DISPLAY( "Usage :\n");
638     DISPLAY( "      %s [arg] file1 file2 ... fileX\n", exename);
639     DISPLAY( "Arguments :\n");
640     DISPLAY( " -c     : compression tests only\n");
641     DISPLAY( " -d     : decompression tests only\n");
642     DISPLAY( " -H/-h  : Help (this text + advanced options)\n");
643     return 0;
644 }
645 
usage_advanced(void)646 static int usage_advanced(void)
647 {
648     DISPLAY( "\nAdvanced options :\n");
649     DISPLAY( " -c#    : test only compression function # [1-%i]\n", NB_COMPRESSION_ALGORITHMS);
650     DISPLAY( " -d#    : test only decompression function # [1-%i]\n", NB_DECOMPRESSION_ALGORITHMS);
651     DISPLAY( " -i#    : iteration loops [1-9](default : %i)\n", NBLOOPS);
652     DISPLAY( " -B#    : Block size [4-7](default : 7)\n");
653     return 0;
654 }
655 
badusage(const char * exename)656 static int badusage(const char* exename)
657 {
658     DISPLAY("Wrong parameters\n");
659     usage(exename);
660     return 0;
661 }
662 
main(int argc,const char ** argv)663 int main(int argc, const char** argv)
664 {
665     int i,
666         filenamesStart=2;
667     const char* exename = argv[0];
668     const char* input_filename=0;
669 
670     // Welcome message
671     DISPLAY(WELCOME_MESSAGE);
672 
673     if (argc<2) { badusage(exename); return 1; }
674 
675     for(i=1; i<argc; i++) {
676         const char* argument = argv[i];
677 
678         if(!argument) continue;   // Protection if argument empty
679         if (!strcmp(argument, "--no-prompt")) {
680             g_noPrompt = 1;
681             continue;
682         }
683 
684         // Decode command (note : aggregated commands are allowed)
685         if (argument[0]=='-') {
686             while (argument[1]!=0) {
687                 argument ++;
688 
689                 switch(argument[0])
690                 {
691                     // Select compression algorithm only
692                 case 'c':
693                     g_decompressionTest = 0;
694                     while ((argument[1]>= '0') && (argument[1]<= '9')) {
695                         g_compressionAlgo *= 10;
696                         g_compressionAlgo += argument[1] - '0';
697                         argument++;
698                     }
699                     break;
700 
701                     // Select decompression algorithm only
702                 case 'd':
703                     g_compressionTest = 0;
704                     while ((argument[1]>= '0') && (argument[1]<= '9')) {
705                         g_decompressionAlgo *= 10;
706                         g_decompressionAlgo += argument[1] - '0';
707                         argument++;
708                     }
709                     break;
710 
711                     // Display help on usage
712                 case 'h' :
713                 case 'H': usage(exename); usage_advanced(); return 0;
714 
715                     // Modify Block Properties
716                 case 'B':
717                     while (argument[1]!=0)
718                     switch(argument[1])
719                     {
720                     case '4':
721                     case '5':
722                     case '6':
723                     case '7':
724                     {   int B = argument[1] - '0';
725                         int S = 1 << (8 + 2*B);
726                         BMK_setBlocksize(S);
727                         argument++;
728                         break;
729                     }
730                     case 'D': argument++; break;
731                     default : goto _exit_blockProperties;
732                     }
733 _exit_blockProperties:
734                     break;
735 
736                     // Modify Nb Iterations
737                 case 'i':
738                     if ((argument[1] >='0') && (argument[1] <='9')) {
739                         int iters = argument[1] - '0';
740                         BMK_setNbIterations(iters);
741                         argument++;
742                     }
743                     break;
744 
745                     // Pause at the end (hidden option)
746                 case 'p': BMK_setPause(); break;
747 
748                     // Unknown command
749                 default : badusage(exename); return 1;
750                 }
751             }
752             continue;
753         }
754 
755         // first provided filename is input
756         if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
757 
758     }
759 
760     // No input filename ==> Error
761     if(!input_filename) { badusage(exename); return 1; }
762 
763     return fullSpeedBench(argv+filenamesStart, argc-filenamesStart);
764 
765 }
766