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