bench.c 13 KB

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