1 /*
2     bench.c - Demo program to benchmark open-source compression algorithm
3     Copyright (C) Yann Collet 2012-2014
4     GPL v2 License
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 
20     You can contact the author at :
21     - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
22     - LZ4 source repository : http://code.google.com/p/lz4/
23 */
24 
25 /**************************************
26 *  Compiler Options
27 ***************************************/
28 /* Disable some Visual warning messages */
29 #define _CRT_SECURE_NO_WARNINGS
30 #define _CRT_SECURE_NO_DEPRECATE     /* VS2005 */
31 
32 /* Unix Large Files support (>4GB) */
33 #define _FILE_OFFSET_BITS 64
34 #if (defined(__sun__) && (!defined(__LP64__)))   /* Sun Solaris 32-bits requires specific definitions */
35 #  define _LARGEFILE_SOURCE
36 #elif ! defined(__LP64__)                        /* No point defining Large file for 64 bit */
37 #  define _LARGEFILE64_SOURCE
38 #endif
39 
40 /* S_ISREG & gettimeofday() are not supported by MSVC */
41 #if defined(_MSC_VER) || defined(_WIN32)
42 #  define BMK_LEGACY_TIMER 1
43 #endif
44 
45 
46 /**************************************
47 *  Includes
48 ***************************************/
49 #include <stdlib.h>      /* malloc */
50 #include <stdio.h>       /* fprintf, fopen, ftello64 */
51 #include <sys/types.h>   /* stat64 */
52 #include <sys/stat.h>    /* stat64 */
53 
54 /* Use ftime() if gettimeofday() is not available on your target */
55 #if defined(BMK_LEGACY_TIMER)
56 #  include <sys/timeb.h>   /* timeb, ftime */
57 #else
58 #  include <sys/time.h>    /* gettimeofday */
59 #endif
60 
61 #include "lz4.h"
62 #define COMPRESSOR0 LZ4_compress_local
LZ4_compress_local(const char * src,char * dst,int size,int clevel)63 static int LZ4_compress_local(const char* src, char* dst, int size, int clevel) { (void)clevel; return LZ4_compress(src, dst, size); }
64 #include "lz4hc.h"
65 #define COMPRESSOR1 LZ4_compressHC2
66 #define DEFAULTCOMPRESSOR COMPRESSOR0
67 
68 #include "xxhash.h"
69 
70 
71 /**************************************
72 *  Compiler specifics
73 ***************************************/
74 #if !defined(S_ISREG)
75 #  define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
76 #endif
77 
78 
79 /**************************************
80 *  Basic Types
81 ***************************************/
82 #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   /* C99 */
83 # include <stdint.h>
84   typedef uint8_t  BYTE;
85   typedef uint16_t U16;
86   typedef uint32_t U32;
87   typedef  int32_t S32;
88   typedef uint64_t U64;
89 #else
90   typedef unsigned char       BYTE;
91   typedef unsigned short      U16;
92   typedef unsigned int        U32;
93   typedef   signed int        S32;
94   typedef unsigned long long  U64;
95 #endif
96 
97 
98 /**************************************
99 *  Constants
100 ***************************************/
101 #define NBLOOPS    3
102 #define TIMELOOP   2000
103 
104 #define KB *(1 <<10)
105 #define MB *(1 <<20)
106 #define GB *(1U<<30)
107 
108 #define MAX_MEM             (2 GB - 64 MB)
109 #define DEFAULT_CHUNKSIZE   (4 MB)
110 
111 
112 /**************************************
113 *  Local structures
114 ***************************************/
115 struct chunkParameters
116 {
117     U32   id;
118     char* origBuffer;
119     char* compressedBuffer;
120     int   origSize;
121     int   compressedSize;
122 };
123 
124 struct compressionParameters
125 {
126     int (*compressionFunction)(const char*, char*, int, int);
127     int (*decompressionFunction)(const char*, char*, int);
128 };
129 
130 
131 /**************************************
132 *  MACRO
133 ***************************************/
134 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
135 
136 
137 /**************************************
138 *  Benchmark Parameters
139 ***************************************/
140 static int chunkSize = DEFAULT_CHUNKSIZE;
141 static int nbIterations = NBLOOPS;
142 static int BMK_pause = 0;
143 
BMK_SetBlocksize(int bsize)144 void BMK_SetBlocksize(int bsize) { chunkSize = bsize; }
145 
BMK_SetNbIterations(int nbLoops)146 void BMK_SetNbIterations(int nbLoops)
147 {
148     nbIterations = nbLoops;
149     DISPLAY("- %i iterations -\n", nbIterations);
150 }
151 
BMK_SetPause(void)152 void BMK_SetPause(void) { BMK_pause = 1; }
153 
154 
155 /*********************************************************
156 *  Private functions
157 **********************************************************/
158 
159 #if defined(BMK_LEGACY_TIMER)
160 
BMK_GetMilliStart(void)161 static int BMK_GetMilliStart(void)
162 {
163   /* Based on Legacy ftime()
164      Rolls over every ~ 12.1 days (0x100000/24/60/60)
165      Use GetMilliSpan to correct for rollover */
166   struct timeb tb;
167   int nCount;
168   ftime( &tb );
169   nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000);
170   return nCount;
171 }
172 
173 #else
174 
BMK_GetMilliStart(void)175 static int BMK_GetMilliStart(void)
176 {
177   /* Based on newer gettimeofday()
178      Use GetMilliSpan to correct for rollover */
179   struct timeval tv;
180   int nCount;
181   gettimeofday(&tv, NULL);
182   nCount = (int) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000);
183   return nCount;
184 }
185 
186 #endif
187 
188 
BMK_GetMilliSpan(int nTimeStart)189 static int BMK_GetMilliSpan( int nTimeStart )
190 {
191   int nSpan = BMK_GetMilliStart() - nTimeStart;
192   if ( nSpan < 0 )
193     nSpan += 0x100000 * 1000;
194   return nSpan;
195 }
196 
197 
BMK_findMaxMem(U64 requiredMem)198 static size_t BMK_findMaxMem(U64 requiredMem)
199 {
200     size_t step = 64 MB;
201     BYTE* testmem=NULL;
202 
203     requiredMem = (((requiredMem >> 26) + 1) << 26);
204     requiredMem += 2*step;
205     if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
206 
207     while (!testmem)
208     {
209         requiredMem -= step;
210         testmem = (BYTE*) malloc ((size_t)requiredMem);
211     }
212 
213     free (testmem);
214     return (size_t) (requiredMem - step);
215 }
216 
217 
BMK_GetFileSize(char * infilename)218 static U64 BMK_GetFileSize(char* infilename)
219 {
220     int r;
221 #if defined(_MSC_VER)
222     struct _stat64 statbuf;
223     r = _stat64(infilename, &statbuf);
224 #else
225     struct stat statbuf;
226     r = stat(infilename, &statbuf);
227 #endif
228     if (r || !S_ISREG(statbuf.st_mode)) return 0;   /* No good... */
229     return (U64)statbuf.st_size;
230 }
231 
232 
233 /*********************************************************
234 *  Public function
235 **********************************************************/
236 
BMK_benchFile(char ** fileNamesTable,int nbFiles,int cLevel)237 int BMK_benchFile(char** fileNamesTable, int nbFiles, int cLevel)
238 {
239   int fileIdx=0;
240   char* orig_buff;
241   struct compressionParameters compP;
242   int cfunctionId;
243 
244   U64 totals = 0;
245   U64 totalz = 0;
246   double totalc = 0.;
247   double totald = 0.;
248 
249 
250   /* Init */
251   if (cLevel <= 3) cfunctionId = 0; else cfunctionId = 1;
252   switch (cfunctionId)
253   {
254 #ifdef COMPRESSOR0
255   case 0 : compP.compressionFunction = COMPRESSOR0; break;
256 #endif
257 #ifdef COMPRESSOR1
258   case 1 : compP.compressionFunction = COMPRESSOR1; break;
259 #endif
260   default : compP.compressionFunction = DEFAULTCOMPRESSOR;
261   }
262   compP.decompressionFunction = LZ4_decompress_fast;
263 
264   /* Loop for each file */
265   while (fileIdx<nbFiles)
266   {
267       FILE*  inFile;
268       char*  inFileName;
269       U64    inFileSize;
270       size_t benchedSize;
271       int nbChunks;
272       int maxCompressedChunkSize;
273       size_t readSize;
274       char* compressedBuffer; int compressedBuffSize;
275       struct chunkParameters* chunkP;
276       U32 crcOrig;
277 
278       /* Check file existence */
279       inFileName = fileNamesTable[fileIdx++];
280       inFile = fopen( inFileName, "rb" );
281       if (inFile==NULL)
282       {
283         DISPLAY( "Pb opening %s\n", inFileName);
284         return 11;
285       }
286 
287       /* Memory allocation & restrictions */
288       inFileSize = BMK_GetFileSize(inFileName);
289       benchedSize = (size_t) BMK_findMaxMem(inFileSize * 2) / 2;
290       if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
291       if (benchedSize < inFileSize)
292       {
293           DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20));
294       }
295 
296       /* Alloc */
297       chunkP = (struct chunkParameters*) malloc(((benchedSize / (size_t)chunkSize)+1) * sizeof(struct chunkParameters));
298       orig_buff = (char*)malloc((size_t )benchedSize);
299       nbChunks = (int) ((int)benchedSize / chunkSize) + 1;
300       maxCompressedChunkSize = LZ4_compressBound(chunkSize);
301       compressedBuffSize = nbChunks * maxCompressedChunkSize;
302       compressedBuffer = (char*)malloc((size_t )compressedBuffSize);
303 
304 
305       if (!orig_buff || !compressedBuffer)
306       {
307         DISPLAY("\nError: not enough memory!\n");
308         free(orig_buff);
309         free(compressedBuffer);
310         free(chunkP);
311         fclose(inFile);
312         return 12;
313       }
314 
315       /* Init chunks data */
316       {
317           int i;
318           size_t remaining = benchedSize;
319           char* in = orig_buff;
320           char* out = compressedBuffer;
321           for (i=0; i<nbChunks; i++)
322           {
323               chunkP[i].id = i;
324               chunkP[i].origBuffer = in; in += chunkSize;
325               if ((int)remaining > chunkSize) { chunkP[i].origSize = chunkSize; remaining -= chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; }
326               chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
327               chunkP[i].compressedSize = 0;
328           }
329       }
330 
331       /* Fill input buffer */
332       DISPLAY("Loading %s...       \r", inFileName);
333       readSize = fread(orig_buff, 1, benchedSize, inFile);
334       fclose(inFile);
335 
336       if (readSize != benchedSize)
337       {
338         DISPLAY("\nError: problem reading file '%s' !!    \n", inFileName);
339         free(orig_buff);
340         free(compressedBuffer);
341         free(chunkP);
342         return 13;
343       }
344 
345       /* Calculating input Checksum */
346       crcOrig = XXH32(orig_buff, (unsigned int)benchedSize,0);
347 
348 
349       /* Bench */
350       {
351         int loopNb, chunkNb;
352         size_t cSize=0;
353         double fastestC = 100000000., fastestD = 100000000.;
354         double ratio=0.;
355         U32 crcCheck=0;
356 
357         DISPLAY("\r%79s\r", "");
358         for (loopNb = 1; loopNb <= nbIterations; loopNb++)
359         {
360           int nbLoops;
361           int milliTime;
362 
363           /* Compression */
364           DISPLAY("%1i-%-14.14s : %9i ->\r", loopNb, inFileName, (int)benchedSize);
365           { size_t i; for (i=0; i<benchedSize; i++) compressedBuffer[i]=(char)i; }     /* warmimg up memory */
366 
367           nbLoops = 0;
368           milliTime = BMK_GetMilliStart();
369           while(BMK_GetMilliStart() == milliTime);
370           milliTime = BMK_GetMilliStart();
371           while(BMK_GetMilliSpan(milliTime) < TIMELOOP)
372           {
373             for (chunkNb=0; chunkNb<nbChunks; chunkNb++)
374                 chunkP[chunkNb].compressedSize = compP.compressionFunction(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize, cLevel);
375             nbLoops++;
376           }
377           milliTime = BMK_GetMilliSpan(milliTime);
378 
379           if ((double)milliTime < fastestC*nbLoops) fastestC = (double)milliTime/nbLoops;
380           cSize=0; for (chunkNb=0; chunkNb<nbChunks; chunkNb++) cSize += chunkP[chunkNb].compressedSize;
381           ratio = (double)cSize/(double)benchedSize*100.;
382 
383           DISPLAY("%1i-%-14.14s : %9i -> %9i (%5.2f%%),%7.1f MB/s\r", loopNb, inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000.);
384 
385           /* Decompression */
386           { size_t i; for (i=0; i<benchedSize; i++) orig_buff[i]=0; }     /* zeroing area, for CRC checking */
387 
388           nbLoops = 0;
389           milliTime = BMK_GetMilliStart();
390           while(BMK_GetMilliStart() == milliTime);
391           milliTime = BMK_GetMilliStart();
392           while(BMK_GetMilliSpan(milliTime) < TIMELOOP)
393           {
394             for (chunkNb=0; chunkNb<nbChunks; chunkNb++)
395                 chunkP[chunkNb].compressedSize = LZ4_decompress_fast(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer, chunkP[chunkNb].origSize);
396             nbLoops++;
397           }
398           milliTime = BMK_GetMilliSpan(milliTime);
399 
400           if ((double)milliTime < fastestD*nbLoops) fastestD = (double)milliTime/nbLoops;
401           DISPLAY("%1i-%-14.14s : %9i -> %9i (%5.2f%%),%7.1f MB/s ,%7.1f MB/s\r", loopNb, inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000., (double)benchedSize / fastestD / 1000.);
402 
403           /* CRC Checking */
404           crcCheck = XXH32(orig_buff, (unsigned int)benchedSize,0);
405           if (crcOrig!=crcCheck) { DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", inFileName, (unsigned)crcOrig, (unsigned)crcCheck); break; }
406         }
407 
408         if (crcOrig==crcCheck)
409         {
410             if (ratio<100.)
411                 DISPLAY("%-16.16s : %9i -> %9i (%5.2f%%),%7.1f MB/s ,%7.1f MB/s\n", inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000., (double)benchedSize / fastestD / 1000.);
412             else
413                 DISPLAY("%-16.16s : %9i -> %9i (%5.1f%%),%7.1f MB/s ,%7.1f MB/s \n", inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000., (double)benchedSize / fastestD / 1000.);
414         }
415         totals += benchedSize;
416         totalz += cSize;
417         totalc += fastestC;
418         totald += fastestD;
419       }
420 
421       free(orig_buff);
422       free(compressedBuffer);
423       free(chunkP);
424   }
425 
426   if (nbFiles > 1)
427         DISPLAY("%-16.16s :%10llu ->%10llu (%5.2f%%), %6.1f MB/s , %6.1f MB/s\n", "  TOTAL", (long long unsigned int)totals, (long long unsigned int)totalz, (double)totalz/(double)totals*100., (double)totals/totalc/1000., (double)totals/totald/1000.);
428 
429   if (BMK_pause) { DISPLAY("\npress enter...\n"); getchar(); }
430 
431   return 0;
432 }
433 
434 
435 
436