• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     bench.c - Demo program to benchmark open-source compression algorithms
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 homepage : http://www.lz4.org
23     - LZ4 source repository : https://github.com/lz4/lz4
24 */
25 
26 
27 /*-************************************
28 *  Compiler options
29 **************************************/
30 #ifdef _MSC_VER    /* Visual Studio */
31 #  pragma warning(disable : 4127)    /* disable: C4127: conditional expression is constant */
32 #endif
33 
34 
35 /* *************************************
36 *  Includes
37 ***************************************/
38 #include "platform.h"    /* Compiler options */
39 #include "util.h"        /* UTIL_GetFileSize, UTIL_sleep */
40 #include <stdlib.h>      /* malloc, free */
41 #include <string.h>      /* memset */
42 #include <stdio.h>       /* fprintf, fopen, ftello */
43 #include <time.h>        /* clock_t, clock, CLOCKS_PER_SEC */
44 #include <assert.h>      /* assert */
45 
46 #include "datagen.h"     /* RDG_genBuffer */
47 #include "xxhash.h"
48 
49 
50 #include "lz4.h"
51 #define COMPRESSOR0 LZ4_compress_local
LZ4_compress_local(const char * src,char * dst,int srcSize,int dstSize,int clevel)52 static int LZ4_compress_local(const char* src, char* dst, int srcSize, int dstSize, int clevel) {
53   int const acceleration = (clevel < 0) ? -clevel + 1 : 1;
54   return LZ4_compress_fast(src, dst, srcSize, dstSize, acceleration);
55 }
56 #include "lz4hc.h"
57 #define COMPRESSOR1 LZ4_compress_HC
58 #define DEFAULTCOMPRESSOR COMPRESSOR0
59 #define LZ4_isError(errcode) (errcode==0)
60 
61 
62 /* *************************************
63 *  Constants
64 ***************************************/
65 #ifndef LZ4_GIT_COMMIT_STRING
66 #  define LZ4_GIT_COMMIT_STRING ""
67 #else
68 #  define LZ4_GIT_COMMIT_STRING LZ4_EXPAND_AND_QUOTE(LZ4_GIT_COMMIT)
69 #endif
70 
71 #define NBSECONDS             3
72 #define TIMELOOP_MICROSEC     1*1000000ULL /* 1 second */
73 #define TIMELOOP_NANOSEC      1*1000000000ULL /* 1 second */
74 #define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */
75 #define COOLPERIOD_SEC        10
76 #define DECOMP_MULT           1 /* test decompression DECOMP_MULT times longer than compression */
77 
78 #define KB *(1 <<10)
79 #define MB *(1 <<20)
80 #define GB *(1U<<30)
81 
82 static const size_t maxMemory = (sizeof(size_t)==4)  ?  (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31));
83 
84 static U32 g_compressibilityDefault = 50;
85 
86 
87 /* *************************************
88 *  console display
89 ***************************************/
90 #define DISPLAY(...)         fprintf(stderr, __VA_ARGS__)
91 #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
92 static U32 g_displayLevel = 2;   /* 0 : no display;   1: errors;   2 : + result + interaction + warnings;   3 : + progression;   4 : + information */
93 
94 #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
95             if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \
96             { g_time = clock(); DISPLAY(__VA_ARGS__); \
97             if (g_displayLevel>=4) fflush(stdout); } }
98 static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100;
99 static clock_t g_time = 0;
100 
101 
102 /* *************************************
103 *  Exceptions
104 ***************************************/
105 #ifndef DEBUG
106 #  define DEBUG 0
107 #endif
108 #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
109 #define EXM_THROW(error, ...)                                             \
110 {                                                                         \
111     DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
112     DISPLAYLEVEL(1, "Error %i : ", error);                                \
113     DISPLAYLEVEL(1, __VA_ARGS__);                                         \
114     DISPLAYLEVEL(1, "\n");                                                \
115     exit(error);                                                          \
116 }
117 
118 
119 /* *************************************
120 *  Benchmark Parameters
121 ***************************************/
122 static U32 g_nbSeconds = NBSECONDS;
123 static size_t g_blockSize = 0;
124 int g_additionalParam = 0;
125 int g_benchSeparately = 0;
126 
BMK_setNotificationLevel(unsigned level)127 void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
128 
BMK_setAdditionalParam(int additionalParam)129 void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
130 
BMK_setNbSeconds(unsigned nbSeconds)131 void BMK_setNbSeconds(unsigned nbSeconds)
132 {
133     g_nbSeconds = nbSeconds;
134     DISPLAYLEVEL(3, "- test >= %u seconds per compression / decompression -\n", g_nbSeconds);
135 }
136 
BMK_setBlockSize(size_t blockSize)137 void BMK_setBlockSize(size_t blockSize) { g_blockSize = blockSize; }
138 
BMK_setBenchSeparately(int separate)139 void BMK_setBenchSeparately(int separate) { g_benchSeparately = (separate!=0); }
140 
141 
142 /* ********************************************************
143 *  Bench functions
144 **********************************************************/
145 typedef struct {
146     const char* srcPtr;
147     size_t srcSize;
148     char*  cPtr;
149     size_t cRoom;
150     size_t cSize;
151     char*  resPtr;
152     size_t resSize;
153 } blockParam_t;
154 
155 struct compressionParameters
156 {
157     int (*compressionFunction)(const char* src, char* dst, int srcSize, int dstSize, int cLevel);
158 };
159 
160 #define MIN(a,b) ((a)<(b) ? (a) : (b))
161 #define MAX(a,b) ((a)>(b) ? (a) : (b))
162 
BMK_benchMem(const void * srcBuffer,size_t srcSize,const char * displayName,int cLevel,const size_t * fileSizes,U32 nbFiles)163 static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
164                         const char* displayName, int cLevel,
165                         const size_t* fileSizes, U32 nbFiles)
166 {
167     size_t const blockSize = (g_blockSize>=32 ? g_blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ;
168     U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
169     blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
170     size_t const maxCompressedSize = LZ4_compressBound((int)srcSize) + (maxNbBlocks * 1024);   /* add some room for safety */
171     void* const compressedBuffer = malloc(maxCompressedSize);
172     void* const resultBuffer = malloc(srcSize);
173     U32 nbBlocks;
174     struct compressionParameters compP;
175     int cfunctionId;
176 
177     /* checks */
178     if (!compressedBuffer || !resultBuffer || !blockTable)
179         EXM_THROW(31, "allocation error : not enough memory");
180 
181     /* init */
182     if (strlen(displayName)>17) displayName += strlen(displayName)-17;   /* can only display 17 characters */
183 
184     /* Init */
185     if (cLevel < LZ4HC_CLEVEL_MIN) cfunctionId = 0; else cfunctionId = 1;
186     switch (cfunctionId)
187     {
188 #ifdef COMPRESSOR0
189     case 0 : compP.compressionFunction = COMPRESSOR0; break;
190 #endif
191 #ifdef COMPRESSOR1
192     case 1 : compP.compressionFunction = COMPRESSOR1; break;
193 #endif
194     default : compP.compressionFunction = DEFAULTCOMPRESSOR;
195     }
196 
197     /* Init blockTable data */
198     {   const char* srcPtr = (const char*)srcBuffer;
199         char* cPtr = (char*)compressedBuffer;
200         char* resPtr = (char*)resultBuffer;
201         U32 fileNb;
202         for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
203             size_t remaining = fileSizes[fileNb];
204             U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
205             U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
206             for ( ; nbBlocks<blockEnd; nbBlocks++) {
207                 size_t const thisBlockSize = MIN(remaining, blockSize);
208                 blockTable[nbBlocks].srcPtr = srcPtr;
209                 blockTable[nbBlocks].cPtr = cPtr;
210                 blockTable[nbBlocks].resPtr = resPtr;
211                 blockTable[nbBlocks].srcSize = thisBlockSize;
212                 blockTable[nbBlocks].cRoom = (size_t)LZ4_compressBound((int)thisBlockSize);
213                 srcPtr += thisBlockSize;
214                 cPtr += blockTable[nbBlocks].cRoom;
215                 resPtr += thisBlockSize;
216                 remaining -= thisBlockSize;
217     }   }   }
218 
219     /* warmimg up memory */
220     RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
221 
222     /* Bench */
223     {   U64 fastestC = (U64)(-1LL), fastestD = (U64)(-1LL);
224         U64 const crcOrig = XXH64(srcBuffer, srcSize, 0);
225         UTIL_time_t coolTime;
226         U64 const maxTime = (g_nbSeconds * TIMELOOP_NANOSEC) + 100;
227         U32 nbCompressionLoops = (U32)((5 MB) / (srcSize+1)) + 1;  /* conservative initial compression speed estimate */
228         U32 nbDecodeLoops = (U32)((200 MB) / (srcSize+1)) + 1;  /* conservative initial decode speed estimate */
229         U64 totalCTime=0, totalDTime=0;
230         U32 cCompleted=0, dCompleted=0;
231 #       define NB_MARKS 4
232         const char* const marks[NB_MARKS] = { " |", " /", " =",  "\\" };
233         U32 markNb = 0;
234         size_t cSize = 0;
235         double ratio = 0.;
236 
237         coolTime = UTIL_getTime();
238         DISPLAYLEVEL(2, "\r%79s\r", "");
239         while (!cCompleted || !dCompleted) {
240             /* overheat protection */
241             if (UTIL_clockSpanMicro(coolTime) > ACTIVEPERIOD_MICROSEC) {
242                 DISPLAYLEVEL(2, "\rcooling down ...    \r");
243                 UTIL_sleep(COOLPERIOD_SEC);
244                 coolTime = UTIL_getTime();
245             }
246 
247             /* Compression */
248             DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (U32)srcSize);
249             if (!cCompleted) memset(compressedBuffer, 0xE5, maxCompressedSize);  /* warm up and erase result buffer */
250 
251             UTIL_sleepMilli(1);  /* give processor time to other processes */
252             UTIL_waitForNextTick();
253 
254             if (!cCompleted) {   /* still some time to do compression tests */
255                 UTIL_time_t const clockStart = UTIL_getTime();
256                 U32 nbLoops;
257                 for (nbLoops=0; nbLoops < nbCompressionLoops; nbLoops++) {
258                     U32 blockNb;
259                     for (blockNb=0; blockNb<nbBlocks; blockNb++) {
260                         size_t const rSize = (size_t)compP.compressionFunction(blockTable[blockNb].srcPtr, blockTable[blockNb].cPtr, (int)blockTable[blockNb].srcSize, (int)blockTable[blockNb].cRoom, cLevel);
261                         if (LZ4_isError(rSize)) EXM_THROW(1, "LZ4 compression failed");
262                         blockTable[blockNb].cSize = rSize;
263                 }   }
264                 {   U64 const clockSpan = UTIL_clockSpanNano(clockStart);
265                     if (clockSpan > 0) {
266                         if (clockSpan < fastestC * nbCompressionLoops)
267                             fastestC = clockSpan / nbCompressionLoops;
268                         assert(fastestC > 0);
269                         nbCompressionLoops = (U32)(TIMELOOP_NANOSEC / fastestC) + 1;  /* aim for ~1sec */
270                     } else {
271                         assert(nbCompressionLoops < 40000000);   /* avoid overflow */
272                         nbCompressionLoops *= 100;
273                     }
274                     totalCTime += clockSpan;
275                     cCompleted = totalCTime>maxTime;
276             }   }
277 
278             cSize = 0;
279             { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; }
280             cSize += !cSize;  /* avoid div by 0 */
281             ratio = (double)srcSize / (double)cSize;
282             markNb = (markNb+1) % NB_MARKS;
283             DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
284                     marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio,
285                     ((double)srcSize / fastestC) * 1000 );
286 
287             (void)fastestD; (void)crcOrig;   /*  unused when decompression disabled */
288 #if 1
289             /* Decompression */
290             if (!dCompleted) memset(resultBuffer, 0xD6, srcSize);  /* warm result buffer */
291 
292             UTIL_sleepMilli(5); /* give processor time to other processes */
293             UTIL_waitForNextTick();
294 
295             if (!dCompleted) {
296                 UTIL_time_t const clockStart = UTIL_getTime();
297                 U32 nbLoops;
298                 for (nbLoops=0; nbLoops < nbDecodeLoops; nbLoops++) {
299                     U32 blockNb;
300                     for (blockNb=0; blockNb<nbBlocks; blockNb++) {
301                         int const regenSize = LZ4_decompress_safe(blockTable[blockNb].cPtr, blockTable[blockNb].resPtr, (int)blockTable[blockNb].cSize, (int)blockTable[blockNb].srcSize);
302                         if (regenSize < 0) {
303                             DISPLAY("LZ4_decompress_safe() failed on block %u \n", blockNb);
304                             break;
305                         }
306                         blockTable[blockNb].resSize = (size_t)regenSize;
307                 }   }
308                 {   U64 const clockSpan = UTIL_clockSpanNano(clockStart);
309                     if (clockSpan > 0) {
310                         if (clockSpan < fastestD * nbDecodeLoops)
311                             fastestD = clockSpan / nbDecodeLoops;
312                         assert(fastestD > 0);
313                         nbDecodeLoops = (U32)(TIMELOOP_NANOSEC / fastestD) + 1;  /* aim for ~1sec */
314                     } else {
315                         assert(nbDecodeLoops < 40000000);   /* avoid overflow */
316                         nbDecodeLoops *= 100;
317                     }
318                     totalDTime += clockSpan;
319                     dCompleted = totalDTime > (DECOMP_MULT*maxTime);
320             }   }
321 
322             markNb = (markNb+1) % NB_MARKS;
323             DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r",
324                     marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio,
325                     ((double)srcSize / fastestC) * 1000,
326                     ((double)srcSize / fastestD) * 1000);
327 
328             /* CRC Checking */
329             {   U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
330                 if (crcOrig!=crcCheck) {
331                     size_t u;
332                     DISPLAY("\n!!! WARNING !!! %17s : Invalid Checksum : %x != %x   \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
333                     for (u=0; u<srcSize; u++) {
334                         if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) {
335                             U32 segNb, bNb, pos;
336                             size_t bacc = 0;
337                             DISPLAY("Decoding error at pos %u ", (U32)u);
338                             for (segNb = 0; segNb < nbBlocks; segNb++) {
339                                 if (bacc + blockTable[segNb].srcSize > u) break;
340                                 bacc += blockTable[segNb].srcSize;
341                             }
342                             pos = (U32)(u - bacc);
343                             bNb = pos / (128 KB);
344                             DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos);
345                             break;
346                         }
347                         if (u==srcSize-1) {  /* should never happen */
348                             DISPLAY("no difference detected\n");
349                     }   }
350                     break;
351             }   }   /* CRC Checking */
352 #endif
353         }   /* for (testNb = 1; testNb <= (g_nbSeconds + !g_nbSeconds); testNb++) */
354 
355         if (g_displayLevel == 1) {
356             double const cSpeed = ((double)srcSize / fastestC) * 1000;
357             double const dSpeed = ((double)srcSize / fastestD) * 1000;
358             if (g_additionalParam)
359                 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s  %s (param=%d)\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName, g_additionalParam);
360             else
361                 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s  %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName);
362         }
363         DISPLAYLEVEL(2, "%2i#\n", cLevel);
364     }   /* Bench */
365 
366     /* clean up */
367     free(blockTable);
368     free(compressedBuffer);
369     free(resultBuffer);
370     return 0;
371 }
372 
373 
BMK_findMaxMem(U64 requiredMem)374 static size_t BMK_findMaxMem(U64 requiredMem)
375 {
376     size_t step = 64 MB;
377     BYTE* testmem=NULL;
378 
379     requiredMem = (((requiredMem >> 26) + 1) << 26);
380     requiredMem += 2*step;
381     if (requiredMem > maxMemory) requiredMem = maxMemory;
382 
383     while (!testmem) {
384         if (requiredMem > step) requiredMem -= step;
385         else requiredMem >>= 1;
386         testmem = (BYTE*) malloc ((size_t)requiredMem);
387     }
388     free (testmem);
389 
390     /* keep some space available */
391     if (requiredMem > step) requiredMem -= step;
392     else requiredMem >>= 1;
393 
394     return (size_t)requiredMem;
395 }
396 
397 
BMK_benchCLevel(void * srcBuffer,size_t benchedSize,const char * displayName,int cLevel,int cLevelLast,const size_t * fileSizes,unsigned nbFiles)398 static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
399                             const char* displayName, int cLevel, int cLevelLast,
400                             const size_t* fileSizes, unsigned nbFiles)
401 {
402     int l;
403 
404     const char* pch = strrchr(displayName, '\\'); /* Windows */
405     if (!pch) pch = strrchr(displayName, '/'); /* Linux */
406     if (pch) displayName = pch+1;
407 
408     SET_REALTIME_PRIORITY;
409 
410     if (g_displayLevel == 1 && !g_additionalParam)
411         DISPLAY("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n", LZ4_VERSION_STRING, LZ4_GIT_COMMIT_STRING, (U32)benchedSize, g_nbSeconds, (U32)(g_blockSize>>10));
412 
413     if (cLevelLast < cLevel) cLevelLast = cLevel;
414 
415     for (l=cLevel; l <= cLevelLast; l++) {
416         BMK_benchMem(srcBuffer, benchedSize,
417                      displayName, l,
418                      fileSizes, nbFiles);
419     }
420 }
421 
422 
423 /*! BMK_loadFiles() :
424     Loads `buffer` with content of files listed within `fileNamesTable`.
425     At most, fills `buffer` entirely */
BMK_loadFiles(void * buffer,size_t bufferSize,size_t * fileSizes,const char ** fileNamesTable,unsigned nbFiles)426 static void BMK_loadFiles(void* buffer, size_t bufferSize,
427                           size_t* fileSizes,
428                           const char** fileNamesTable, unsigned nbFiles)
429 {
430     size_t pos = 0, totalSize = 0;
431     unsigned n;
432     for (n=0; n<nbFiles; n++) {
433         FILE* f;
434         U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
435         if (UTIL_isDirectory(fileNamesTable[n])) {
436             DISPLAYLEVEL(2, "Ignoring %s directory...       \n", fileNamesTable[n]);
437             fileSizes[n] = 0;
438             continue;
439         }
440         f = fopen(fileNamesTable[n], "rb");
441         if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
442         DISPLAYUPDATE(2, "Loading %s...       \r", fileNamesTable[n]);
443         if (fileSize > bufferSize-pos) { /* buffer too small - stop after this file */
444             fileSize = bufferSize-pos;
445             nbFiles=n;
446         }
447         { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
448           if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
449           pos += readSize; }
450         fileSizes[n] = (size_t)fileSize;
451         totalSize += (size_t)fileSize;
452         fclose(f);
453     }
454 
455     if (totalSize == 0) EXM_THROW(12, "no data to bench");
456 }
457 
BMK_benchFileTable(const char ** fileNamesTable,unsigned nbFiles,int cLevel,int cLevelLast)458 static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
459                                int cLevel, int cLevelLast)
460 {
461     void* srcBuffer;
462     size_t benchedSize;
463     size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
464     U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
465     char mfName[20] = {0};
466 
467     if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes");
468 
469     /* Memory allocation & restrictions */
470     benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
471     if (benchedSize==0) EXM_THROW(12, "not enough memory");
472     if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
473     if (benchedSize > LZ4_MAX_INPUT_SIZE) {
474         benchedSize = LZ4_MAX_INPUT_SIZE;
475         DISPLAY("File(s) bigger than LZ4's max input size; testing %u MB only...\n", (U32)(benchedSize >> 20));
476     } else {
477         if (benchedSize < totalSizeToLoad)
478             DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
479     }
480     srcBuffer = malloc(benchedSize + !benchedSize);   /* avoid alloc of zero */
481     if (!srcBuffer) EXM_THROW(12, "not enough memory");
482 
483     /* Load input buffer */
484     BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
485 
486     /* Bench */
487     snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
488     {   const char* displayName = (nbFiles > 1) ? mfName : fileNamesTable[0];
489         BMK_benchCLevel(srcBuffer, benchedSize,
490                         displayName, cLevel, cLevelLast,
491                         fileSizes, nbFiles);
492     }
493 
494     /* clean up */
495     free(srcBuffer);
496     free(fileSizes);
497 }
498 
499 
BMK_syntheticTest(int cLevel,int cLevelLast,double compressibility)500 static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility)
501 {
502     char name[20] = {0};
503     size_t benchedSize = 10000000;
504     void* const srcBuffer = malloc(benchedSize);
505 
506     /* Memory allocation */
507     if (!srcBuffer) EXM_THROW(21, "not enough memory");
508 
509     /* Fill input buffer */
510     RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
511 
512     /* Bench */
513     snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
514     BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1);
515 
516     /* clean up */
517     free(srcBuffer);
518 }
519 
520 
BMK_benchFilesSeparately(const char ** fileNamesTable,unsigned nbFiles,int cLevel,int cLevelLast)521 int BMK_benchFilesSeparately(const char** fileNamesTable, unsigned nbFiles,
522                    int cLevel, int cLevelLast)
523 {
524     unsigned fileNb;
525     if (cLevel > LZ4HC_CLEVEL_MAX) cLevel = LZ4HC_CLEVEL_MAX;
526     if (cLevelLast > LZ4HC_CLEVEL_MAX) cLevelLast = LZ4HC_CLEVEL_MAX;
527     if (cLevelLast < cLevel) cLevelLast = cLevel;
528     if (cLevelLast > cLevel) DISPLAYLEVEL(2, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast);
529 
530     for (fileNb=0; fileNb<nbFiles; fileNb++)
531         BMK_benchFileTable(fileNamesTable+fileNb, 1, cLevel, cLevelLast);
532 
533     return 0;
534 }
535 
536 
BMK_benchFiles(const char ** fileNamesTable,unsigned nbFiles,int cLevel,int cLevelLast)537 int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
538                    int cLevel, int cLevelLast)
539 {
540     double const compressibility = (double)g_compressibilityDefault / 100;
541 
542     if (cLevel > LZ4HC_CLEVEL_MAX) cLevel = LZ4HC_CLEVEL_MAX;
543     if (cLevelLast > LZ4HC_CLEVEL_MAX) cLevelLast = LZ4HC_CLEVEL_MAX;
544     if (cLevelLast < cLevel) cLevelLast = cLevel;
545     if (cLevelLast > cLevel) DISPLAYLEVEL(2, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast);
546 
547     if (nbFiles == 0)
548         BMK_syntheticTest(cLevel, cLevelLast, compressibility);
549     else {
550         if (g_benchSeparately)
551             BMK_benchFilesSeparately(fileNamesTable, nbFiles, cLevel, cLevelLast);
552         else
553             BMK_benchFileTable(fileNamesTable, nbFiles, cLevel, cLevelLast);
554     }
555     return 0;
556 }
557