2 bench.c - Demo program to benchmark open-source compression algorithm
\r
3 Copyright (C) Yann Collet 2012-2013
\r
6 This program is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 This program is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License along
\r
17 with this program; if not, write to the Free Software Foundation, Inc.,
\r
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
20 You can contact the author at :
\r
21 - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
\r
22 - LZ4 source repository : http://code.google.com/p/lz4/
\r
25 //**************************************
\r
27 //**************************************
\r
28 // Disable some Visual warning messages
\r
29 #define _CRT_SECURE_NO_WARNINGS
\r
30 #define _CRT_SECURE_NO_DEPRECATE // VS2005
\r
32 // Unix Large Files support (>4GB)
\r
33 #if (defined(__sun__) && (!defined(__LP64__))) // Sun Solaris 32-bits requires specific definitions
\r
34 # define _LARGEFILE_SOURCE
\r
35 # define _FILE_OFFSET_BITS 64
\r
36 #elif ! defined(__LP64__) // No point defining Large file for 64 bit
\r
37 # define _LARGEFILE64_SOURCE
\r
40 // S_ISREG & gettimeofday() are not supported by MSVC
\r
41 #if defined(_MSC_VER)
\r
42 # define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
\r
43 # define BMK_LEGACY_TIMER 1
\r
46 // GCC does not support _rotl outside of Windows
\r
47 #if !defined(_WIN32)
\r
48 # define _rotl(x,r) ((x << r) | (x >> (32 - r)))
\r
52 //**************************************
\r
54 //**************************************
\r
55 #include <stdlib.h> // malloc
\r
56 #include <stdio.h> // fprintf, fopen, ftello64
\r
57 #include <sys/types.h> // stat64
\r
58 #include <sys/stat.h> // stat64
\r
60 // Use ftime() if gettimeofday() is not available on your target
\r
61 #if defined(BMK_LEGACY_TIMER)
\r
62 # include <sys/timeb.h> // timeb, ftime
\r
64 # include <sys/time.h> // gettimeofday
\r
68 #define COMPRESSOR0 LZ4_compress
\r
70 #define COMPRESSOR1 LZ4_compressHC
\r
71 #define DEFAULTCOMPRESSOR COMPRESSOR0
\r
76 //**************************************
\r
78 //**************************************
\r
79 #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99
\r
80 # include <stdint.h>
\r
81 typedef uint8_t BYTE;
\r
82 typedef uint16_t U16;
\r
83 typedef uint32_t U32;
\r
84 typedef int32_t S32;
\r
85 typedef uint64_t U64;
\r
87 typedef unsigned char BYTE;
\r
88 typedef unsigned short U16;
\r
89 typedef unsigned int U32;
\r
90 typedef signed int S32;
\r
91 typedef unsigned long long U64;
\r
95 //**************************************
\r
97 //**************************************
\r
99 #define TIMELOOP 2000
\r
101 #define KNUTH 2654435761U
\r
102 #define MAX_MEM (1984<<20)
\r
103 #define DEFAULT_CHUNKSIZE (4<<20)
\r
106 //**************************************
\r
107 // Local structures
\r
108 //**************************************
\r
109 struct chunkParameters
\r
113 char* compressedBuffer;
\r
115 int compressedSize;
\r
118 struct compressionParameters
\r
120 int (*compressionFunction)(const char*, char*, int);
\r
121 int (*decompressionFunction)(const char*, char*, int);
\r
125 //**************************************
\r
127 //**************************************
\r
128 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
\r
132 //**************************************
\r
133 // Benchmark Parameters
\r
134 //**************************************
\r
135 static int chunkSize = DEFAULT_CHUNKSIZE;
\r
136 static int nbIterations = NBLOOPS;
\r
137 static int BMK_pause = 0;
\r
139 void BMK_SetBlocksize(int bsize)
\r
142 DISPLAY("-Using Block Size of %i KB-\n", chunkSize>>10);
\r
145 void BMK_SetNbIterations(int nbLoops)
\r
147 nbIterations = nbLoops;
\r
148 DISPLAY("- %i iterations -\n", nbIterations);
\r
151 void BMK_SetPause()
\r
156 //*********************************************************
\r
157 // Private functions
\r
158 //*********************************************************
\r
160 #if defined(BMK_LEGACY_TIMER)
\r
162 static int BMK_GetMilliStart()
\r
164 // Based on Legacy ftime()
\r
165 // Rolls over every ~ 12.1 days (0x100000/24/60/60)
\r
166 // Use GetMilliSpan to correct for rollover
\r
170 nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000);
\r
176 static int BMK_GetMilliStart()
\r
178 // Based on newer gettimeofday()
\r
179 // Use GetMilliSpan to correct for rollover
\r
182 gettimeofday(&tv, NULL);
\r
183 nCount = (int) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000);
\r
190 static int BMK_GetMilliSpan( int nTimeStart )
\r
192 int nSpan = BMK_GetMilliStart() - nTimeStart;
\r
194 nSpan += 0x100000 * 1000;
\r
199 static size_t BMK_findMaxMem(U64 requiredMem)
\r
201 size_t step = (64U<<20); // 64 MB
\r
202 BYTE* testmem=NULL;
\r
204 requiredMem = (((requiredMem >> 25) + 1) << 26);
\r
205 if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
\r
207 requiredMem += 2*step;
\r
210 requiredMem -= step;
\r
211 testmem = (BYTE*) malloc ((size_t)requiredMem);
\r
215 return (size_t) (requiredMem - step);
\r
219 static U64 BMK_GetFileSize(char* infilename)
\r
222 #if defined(_MSC_VER)
\r
223 struct _stat64 statbuf;
\r
224 r = _stat64(infilename, &statbuf);
\r
226 struct stat statbuf;
\r
227 r = stat(infilename, &statbuf);
\r
229 if (r || !S_ISREG(statbuf.st_mode)) return 0; // No good...
\r
230 return (U64)statbuf.st_size;
\r
234 //*********************************************************
\r
236 //*********************************************************
\r
238 int BMK_benchFile(char** fileNamesTable, int nbFiles, int cLevel)
\r
244 size_t benchedSize;
\r
249 char* compressed_buff; int compressed_buff_size;
\r
250 struct chunkParameters* chunkP;
\r
252 struct compressionParameters compP;
\r
256 double totalc = 0.;
\r
257 double totald = 0.;
\r
264 case 0 : compP.compressionFunction = COMPRESSOR0; break;
\r
267 case 1 : compP.compressionFunction = COMPRESSOR1; break;
\r
269 default : compP.compressionFunction = DEFAULTCOMPRESSOR;
\r
271 compP.decompressionFunction = LZ4_decompress_fast;
\r
273 // Loop for each file
\r
274 while (fileIdx<nbFiles)
\r
276 // Check file existence
\r
277 infilename = fileNamesTable[fileIdx++];
\r
278 fileIn = fopen( infilename, "rb" );
\r
281 DISPLAY( "Pb opening %s\n", infilename);
\r
285 // Memory allocation & restrictions
\r
286 largefilesize = BMK_GetFileSize(infilename);
\r
287 benchedSize = (size_t) BMK_findMaxMem(largefilesize) / 2;
\r
288 if ((U64)benchedSize > largefilesize) benchedSize = (size_t)largefilesize;
\r
289 if (benchedSize < largefilesize)
\r
291 DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", infilename, (int)(benchedSize>>20));
\r
295 chunkP = (struct chunkParameters*) malloc(((benchedSize / chunkSize)+1) * sizeof(struct chunkParameters));
\r
296 orig_buff = (char*)malloc((size_t )benchedSize);
\r
297 nbChunks = (int) (benchedSize / chunkSize) + 1;
\r
298 maxCChunkSize = LZ4_compressBound(chunkSize);
\r
299 compressed_buff_size = nbChunks * maxCChunkSize;
\r
300 compressed_buff = (char*)malloc((size_t )compressed_buff_size);
\r
303 if(!orig_buff || !compressed_buff)
\r
305 DISPLAY("\nError: not enough memory!\n");
\r
307 free(compressed_buff);
\r
312 // Init chunks data
\r
315 size_t remaining = benchedSize;
\r
316 char* in = orig_buff;
\r
317 char* out = compressed_buff;
\r
318 for (i=0; i<nbChunks; i++)
\r
321 chunkP[i].origBuffer = in; in += chunkSize;
\r
322 if ((int)remaining > chunkSize) { chunkP[i].origSize = chunkSize; remaining -= chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; }
\r
323 chunkP[i].compressedBuffer = out; out += maxCChunkSize;
\r
324 chunkP[i].compressedSize = 0;
\r
328 // Fill input buffer
\r
329 DISPLAY("Loading %s... \r", infilename);
\r
330 readSize = fread(orig_buff, 1, benchedSize, fileIn);
\r
333 if(readSize != benchedSize)
\r
335 DISPLAY("\nError: problem reading file '%s' !! \n", infilename);
\r
337 free(compressed_buff);
\r
341 // Calculating input Checksum
\r
342 crcc = XXH32(orig_buff, (unsigned int)benchedSize,0);
\r
347 int loopNb, nb_loops, chunkNb;
\r
350 double fastestC = 100000000., fastestD = 100000000.;
\r
353 DISPLAY("\r%79s\r", "");
\r
354 for (loopNb = 1; loopNb <= nbIterations; loopNb++)
\r
357 DISPLAY("%1i-%-14.14s : %9i ->\r", loopNb, infilename, (int)benchedSize);
\r
358 { size_t i; for (i=0; i<benchedSize; i++) compressed_buff[i]=(char)i; } // warmimg up memory
\r
361 milliTime = BMK_GetMilliStart();
\r
362 while(BMK_GetMilliStart() == milliTime);
\r
363 milliTime = BMK_GetMilliStart();
\r
364 while(BMK_GetMilliSpan(milliTime) < TIMELOOP)
\r
366 for (chunkNb=0; chunkNb<nbChunks; chunkNb++)
\r
367 chunkP[chunkNb].compressedSize = compP.compressionFunction(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize);
\r
370 milliTime = BMK_GetMilliSpan(milliTime);
\r
372 if ((double)milliTime < fastestC*nb_loops) fastestC = (double)milliTime/nb_loops;
\r
373 cSize=0; for (chunkNb=0; chunkNb<nbChunks; chunkNb++) cSize += chunkP[chunkNb].compressedSize;
\r
374 ratio = (double)cSize/(double)benchedSize*100.;
\r
376 DISPLAY("%1i-%-14.14s : %9i -> %9i (%5.2f%%),%7.1f MB/s\r", loopNb, infilename, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000.);
\r
379 { size_t i; for (i=0; i<benchedSize; i++) orig_buff[i]=0; } // zeroing area, for CRC checking
\r
382 milliTime = BMK_GetMilliStart();
\r
383 while(BMK_GetMilliStart() == milliTime);
\r
384 milliTime = BMK_GetMilliStart();
\r
385 while(BMK_GetMilliSpan(milliTime) < TIMELOOP)
\r
387 for (chunkNb=0; chunkNb<nbChunks; chunkNb++)
\r
388 //chunkP[chunkNb].origSize = LZ4_decompress_safe(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedSize, chunkSize);
\r
389 chunkP[chunkNb].compressedSize = LZ4_decompress_fast(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer, chunkP[chunkNb].origSize);
\r
390 //chunkP[chunkNb].compressedSize = LZ4_decompress_fast_withPrefix64k(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer, chunkP[chunkNb].origSize);
\r
391 //chunkP[chunkNb].origSize = LZ4_decompress_safe_withPrefix64k(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedSize, chunkSize);
\r
392 //chunkP[chunkNb].origSize = LZ4_decompress_safe_partial(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedSize, chunkSize-5, chunkSize);
\r
393 //chunkP[chunkNb].compressedSize = LZ4_uncompress(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer, chunkP[chunkNb].origSize);
\r
394 //chunkP[chunkNb].origSize = LZ4_uncompress_unknownOutputSize(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedSize, chunkSize);
\r
397 milliTime = BMK_GetMilliSpan(milliTime);
\r
399 if ((double)milliTime < fastestD*nb_loops) fastestD = (double)milliTime/nb_loops;
\r
400 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.);
\r
403 crcd = XXH32(orig_buff, (unsigned int)benchedSize,0);
\r
404 if (crcc!=crcd) { DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", infilename, (unsigned)crcc, (unsigned)crcd); break; }
\r
410 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.);
\r
412 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.);
\r
414 totals += benchedSize;
\r
416 totalc += fastestC;
\r
417 totald += fastestD;
\r
421 free(compressed_buff);
\r
426 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.);
\r
428 if (BMK_pause) { DISPLAY("press enter...\n"); getchar(); }
\r