1 /*
2 bench.c - Demo program to benchmark open-source compression algorithm
3 Copyright (C) Yann Collet 2012-2020
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 #if defined(_MSC_VER) || defined(_WIN32)
28 /* S_ISREG & gettimeofday() are not supported by MSVC */
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 * Memory management, to test LZ4_USER_MEMORY_FUNCTIONS
160 *********************************************************/
LZ4_malloc(size_t s)161 void* LZ4_malloc(size_t s) { return malloc(s); }
LZ4_calloc(size_t n,size_t s)162 void* LZ4_calloc(size_t n, size_t s) { return calloc(n,s); }
LZ4_free(void * p)163 void LZ4_free(void* p) { free(p); }
164
165
166 /*********************************************************
167 * Benchmark function
168 *********************************************************/
169 static LZ4_stream_t LZ4_stream;
local_LZ4_resetDictT(void)170 static void local_LZ4_resetDictT(void)
171 {
172 void* const r = LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream));
173 assert(r != NULL); (void)r;
174 }
175
local_LZ4_createStream(void)176 static void local_LZ4_createStream(void)
177 {
178 void* const r = LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream));
179 assert(r != NULL); (void)r;
180 }
181
local_LZ4_saveDict(const char * in,char * out,int inSize)182 static int local_LZ4_saveDict(const char* in, char* out, int inSize)
183 {
184 (void)in;
185 return LZ4_saveDict(&LZ4_stream, out, inSize);
186 }
187
local_LZ4_compress_default_large(const char * in,char * out,int inSize)188 static int local_LZ4_compress_default_large(const char* in, char* out, int inSize)
189 {
190 return LZ4_compress_default(in, out, inSize, LZ4_compressBound(inSize));
191 }
192
local_LZ4_compress_default_small(const char * in,char * out,int inSize)193 static int local_LZ4_compress_default_small(const char* in, char* out, int inSize)
194 {
195 return LZ4_compress_default(in, out, inSize, LZ4_compressBound(inSize)-1);
196 }
197
local_LZ4_compress_destSize(const char * in,char * out,int inSize)198 static int local_LZ4_compress_destSize(const char* in, char* out, int inSize)
199 {
200 return LZ4_compress_destSize(in, out, &inSize, LZ4_compressBound(inSize)-1);
201 }
202
local_LZ4_compress_fast0(const char * in,char * out,int inSize)203 static int local_LZ4_compress_fast0(const char* in, char* out, int inSize)
204 {
205 return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 0);
206 }
207
local_LZ4_compress_fast1(const char * in,char * out,int inSize)208 static int local_LZ4_compress_fast1(const char* in, char* out, int inSize)
209 {
210 return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 1);
211 }
212
local_LZ4_compress_fast2(const char * in,char * out,int inSize)213 static int local_LZ4_compress_fast2(const char* in, char* out, int inSize)
214 {
215 return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 2);
216 }
217
local_LZ4_compress_fast17(const char * in,char * out,int inSize)218 static int local_LZ4_compress_fast17(const char* in, char* out, int inSize)
219 {
220 return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 17);
221 }
222
local_LZ4_compress_fast_extState0(const char * in,char * out,int inSize)223 static int local_LZ4_compress_fast_extState0(const char* in, char* out, int inSize)
224 {
225 return LZ4_compress_fast_extState(&LZ4_stream, in, out, inSize, LZ4_compressBound(inSize), 0);
226 }
227
local_LZ4_compress_fast_continue0(const char * in,char * out,int inSize)228 static int local_LZ4_compress_fast_continue0(const char* in, char* out, int inSize)
229 {
230 return LZ4_compress_fast_continue(&LZ4_stream, in, out, inSize, LZ4_compressBound(inSize), 0);
231 }
232
233 #ifndef LZ4_DLL_IMPORT
234 #if defined (__cplusplus)
235 extern "C" {
236 #endif
237
238 /* declare hidden function */
239 extern int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize);
240
241 #if defined (__cplusplus)
242 }
243 #endif
244
local_LZ4_compress_forceDict(const char * in,char * out,int inSize)245 static int local_LZ4_compress_forceDict(const char* in, char* out, int inSize)
246 {
247 return LZ4_compress_forceExtDict(&LZ4_stream, in, out, inSize);
248 }
249 #endif
250
251
252 /* HC compression functions */
253 LZ4_streamHC_t LZ4_streamHC;
local_LZ4_resetStreamHC(void)254 static void local_LZ4_resetStreamHC(void)
255 {
256 LZ4_initStreamHC(&LZ4_streamHC, sizeof(LZ4_streamHC));
257 }
258
local_LZ4_saveDictHC(const char * in,char * out,int inSize)259 static int local_LZ4_saveDictHC(const char* in, char* out, int inSize)
260 {
261 (void)in;
262 return LZ4_saveDictHC(&LZ4_streamHC, out, inSize);
263 }
264
local_LZ4_compress_HC(const char * in,char * out,int inSize)265 static int local_LZ4_compress_HC(const char* in, char* out, int inSize)
266 {
267 return LZ4_compress_HC(in, out, inSize, LZ4_compressBound(inSize), 9);
268 }
269
local_LZ4_compress_HC_extStateHC(const char * in,char * out,int inSize)270 static int local_LZ4_compress_HC_extStateHC(const char* in, char* out, int inSize)
271 {
272 return LZ4_compress_HC_extStateHC(&LZ4_streamHC, in, out, inSize, LZ4_compressBound(inSize), 9);
273 }
274
local_LZ4_compress_HC_continue(const char * in,char * out,int inSize)275 static int local_LZ4_compress_HC_continue(const char* in, char* out, int inSize)
276 {
277 return LZ4_compress_HC_continue(&LZ4_streamHC, in, out, inSize, LZ4_compressBound(inSize));
278 }
279
280
281 /* decompression functions */
local_LZ4_decompress_fast(const char * in,char * out,int inSize,int outSize)282 static int local_LZ4_decompress_fast(const char* in, char* out, int inSize, int outSize)
283 {
284 (void)inSize;
285 LZ4_decompress_fast(in, out, outSize);
286 return outSize;
287 }
288
local_LZ4_decompress_fast_usingDict_prefix(const char * in,char * out,int inSize,int outSize)289 static int local_LZ4_decompress_fast_usingDict_prefix(const char* in, char* out, int inSize, int outSize)
290 {
291 (void)inSize;
292 LZ4_decompress_fast_usingDict(in, out, outSize, out - 65536, 65536);
293 return outSize;
294 }
295
local_LZ4_decompress_fast_usingExtDict(const char * in,char * out,int inSize,int outSize)296 static int local_LZ4_decompress_fast_usingExtDict(const char* in, char* out, int inSize, int outSize)
297 {
298 (void)inSize;
299 LZ4_decompress_fast_usingDict(in, out, outSize, out - 65536, 65535);
300 return outSize;
301 }
302
local_LZ4_decompress_safe_withPrefix64k(const char * in,char * out,int inSize,int outSize)303 static int local_LZ4_decompress_safe_withPrefix64k(const char* in, char* out, int inSize, int outSize)
304 {
305 LZ4_decompress_safe_withPrefix64k(in, out, inSize, outSize);
306 return outSize;
307 }
308
local_LZ4_decompress_safe_usingDict(const char * in,char * out,int inSize,int outSize)309 static int local_LZ4_decompress_safe_usingDict(const char* in, char* out, int inSize, int outSize)
310 {
311 LZ4_decompress_safe_usingDict(in, out, inSize, outSize, out - 65536, 65536);
312 return outSize;
313 }
314
local_LZ4_decompress_safe_partial_usingDict(const char * in,char * out,int inSize,int outSize)315 static int local_LZ4_decompress_safe_partial_usingDict(const char* in, char* out, int inSize, int outSize)
316 {
317 int result = LZ4_decompress_safe_partial_usingDict(in, out, inSize, outSize - 5, outSize, out - 65536, 65536);
318 if (result < 0) return result;
319 return outSize;
320 }
321
322 #ifndef LZ4_DLL_IMPORT
323 #if defined (__cplusplus)
324 extern "C" {
325 #endif
326
327 extern int LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize, const void* dict, size_t dictSize);
328
329 #if defined (__cplusplus)
330 }
331 #endif
332
local_LZ4_decompress_safe_forceExtDict(const char * in,char * out,int inSize,int outSize)333 static int local_LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize)
334 {
335 LZ4_decompress_safe_forceExtDict(in, out, inSize, outSize, out - 65536, 65536);
336 return outSize;
337 }
338 #endif
339
340 #ifndef LZ4_DLL_IMPORT
341 #if defined (__cplusplus)
342 extern "C" {
343 #endif
344
345 extern int LZ4_decompress_safe_partial_forceExtDict(const char* in, char* out, int inSize, int targetOutputSize, int dstCapacity, const void* dict, size_t dictSize);
346
347 #if defined (__cplusplus)
348 }
349 #endif
350
local_LZ4_decompress_safe_partial_forceExtDict(const char * in,char * out,int inSize,int outSize)351 static int local_LZ4_decompress_safe_partial_forceExtDict(const char* in, char* out, int inSize, int outSize)
352 {
353 int result = LZ4_decompress_safe_partial_forceExtDict(in, out, inSize, outSize - 5, outSize, out - 65536, 65536);
354 if (result < 0) return result;
355 return outSize;
356 }
357 #endif
358
local_LZ4_decompress_safe_partial(const char * in,char * out,int inSize,int outSize)359 static int local_LZ4_decompress_safe_partial(const char* in, char* out, int inSize, int outSize)
360 {
361 int result = LZ4_decompress_safe_partial(in, out, inSize, outSize - 5, outSize);
362 if (result < 0) return result;
363 return outSize;
364 }
365
366
367 /* frame functions */
local_LZ4F_compressFrame(const char * in,char * out,int inSize)368 static int local_LZ4F_compressFrame(const char* in, char* out, int inSize)
369 {
370 assert(inSize >= 0);
371 return (int)LZ4F_compressFrame(out, LZ4F_compressFrameBound((size_t)inSize, NULL), in, (size_t)inSize, NULL);
372 }
373
374 static LZ4F_decompressionContext_t g_dCtx;
375
local_LZ4F_decompress(const char * in,char * out,int inSize,int outSize)376 static int local_LZ4F_decompress(const char* in, char* out, int inSize, int outSize)
377 {
378 size_t srcSize = (size_t)inSize;
379 size_t dstSize = (size_t)outSize;
380 size_t result;
381 assert(inSize >= 0);
382 assert(outSize >= 0);
383 result = LZ4F_decompress(g_dCtx, out, &dstSize, in, &srcSize, NULL);
384 if (result!=0) { DISPLAY("Error decompressing frame : unfinished frame \n"); exit(8); }
385 if (srcSize != (size_t)inSize) { DISPLAY("Error decompressing frame : read size incorrect \n"); exit(9); }
386 return (int)dstSize;
387 }
388
local_LZ4F_decompress_followHint(const char * src,char * dst,int srcSize,int dstSize)389 static int local_LZ4F_decompress_followHint(const char* src, char* dst, int srcSize, int dstSize)
390 {
391 size_t totalInSize = (size_t)srcSize;
392 size_t maxOutSize = (size_t)dstSize;
393
394 size_t inPos = 0;
395 size_t inSize = 0;
396 size_t outPos = 0;
397 size_t outRemaining = maxOutSize - outPos;
398
399 for (;;) {
400 size_t const sizeHint =
401 LZ4F_decompress(g_dCtx, dst+outPos, &outRemaining, src+inPos, &inSize, NULL);
402 assert(!LZ4F_isError(sizeHint));
403
404 inPos += inSize;
405 inSize = sizeHint;
406
407 outPos += outRemaining;
408 outRemaining = maxOutSize - outPos;
409
410 if (!sizeHint) break;
411 }
412
413 /* frame completed */
414 if (inPos != totalInSize) {
415 DISPLAY("Error decompressing frame : must read (%u) full frame (%u) \n",
416 (unsigned)inPos, (unsigned)totalInSize);
417 exit(10);
418 }
419 return (int)outPos;
420
421 }
422
423 /* always provide input by block of 64 KB */
local_LZ4F_decompress_noHint(const char * src,char * dst,int srcSize,int dstSize)424 static int local_LZ4F_decompress_noHint(const char* src, char* dst, int srcSize, int dstSize)
425 {
426 size_t totalInSize = (size_t)srcSize;
427 size_t maxOutSize = (size_t)dstSize;
428
429 size_t inPos = 0;
430 size_t inSize = 64 KB;
431 size_t outPos = 0;
432 size_t outRemaining = maxOutSize - outPos;
433
434 for (;;) {
435 size_t const sizeHint = LZ4F_decompress(g_dCtx, dst+outPos, &outRemaining, src+inPos, &inSize, NULL);
436 assert(!LZ4F_isError(sizeHint));
437
438 inPos += inSize;
439 inSize = (inPos + 64 KB <= totalInSize) ? 64 KB : totalInSize - inPos;
440
441 outPos += outRemaining;
442 outRemaining = maxOutSize - outPos;
443
444 if (!sizeHint) break;
445 }
446
447 /* frame completed */
448 if (inPos != totalInSize) {
449 DISPLAY("Error decompressing frame : must read (%u) full frame (%u) \n",
450 (unsigned)inPos, (unsigned)totalInSize);
451 exit(10);
452 }
453 return (int)outPos;
454
455 }
456
457 #define NB_COMPRESSION_ALGORITHMS 100
458 #define NB_DECOMPRESSION_ALGORITHMS 100
fullSpeedBench(const char ** fileNamesTable,int nbFiles)459 int fullSpeedBench(const char** fileNamesTable, int nbFiles)
460 {
461 int fileIdx=0;
462
463 /* Init */
464 { size_t const errorCode = LZ4F_createDecompressionContext(&g_dCtx, LZ4F_VERSION);
465 if (LZ4F_isError(errorCode)) { DISPLAY("dctx allocation issue \n"); return 10; } }
466
467 /* Loop for each fileName */
468 while (fileIdx<nbFiles) {
469 char* orig_buff = NULL;
470 struct chunkParameters* chunkP = NULL;
471 char* compressed_buff=NULL;
472 const char* const inFileName = fileNamesTable[fileIdx++];
473 FILE* const inFile = fopen( inFileName, "rb" );
474 U64 const inFileSize = UTIL_getFileSize(inFileName);
475 size_t benchedSize = BMK_findMaxMem(inFileSize*2) / 2; /* because 2 buffers */
476 int nbChunks;
477 int maxCompressedChunkSize;
478 size_t readSize;
479 int compressedBuffSize;
480 U32 crcOriginal;
481
482 /* Check infile pre-requisites */
483 if (inFile==NULL) { DISPLAY("Pb opening %s \n", inFileName); return 11; }
484 if (inFileSize==0) { DISPLAY("file is empty \n"); fclose(inFile); return 11; }
485 if (benchedSize==0) { DISPLAY("not enough memory \n"); fclose(inFile); return 11; }
486
487 /* Memory size adjustments */
488 if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
489 if (benchedSize < inFileSize) {
490 DISPLAY("Not enough memory for '%s' full size; testing %i MB only... \n",
491 inFileName, (int)(benchedSize>>20));
492 }
493
494 /* Allocation */
495 chunkP = (struct chunkParameters*) malloc(((benchedSize / (size_t)g_chunkSize)+1) * sizeof(struct chunkParameters));
496 orig_buff = (char*) malloc(benchedSize);
497 nbChunks = (int) ((benchedSize + (size_t)g_chunkSize - 1) / (size_t)g_chunkSize);
498 maxCompressedChunkSize = LZ4_compressBound(g_chunkSize);
499 compressedBuffSize = nbChunks * maxCompressedChunkSize;
500 compressed_buff = (char*)malloc((size_t)compressedBuffSize);
501 if(!chunkP || !orig_buff || !compressed_buff) {
502 DISPLAY("\nError: not enough memory! \n");
503 fclose(inFile);
504 free(orig_buff);
505 free(compressed_buff);
506 free(chunkP);
507 return(12);
508 }
509
510 /* Fill in src buffer */
511 DISPLAY("Loading %s... \r", inFileName);
512 readSize = fread(orig_buff, 1, benchedSize, inFile);
513 fclose(inFile);
514
515 if (readSize != benchedSize) {
516 DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
517 free(orig_buff);
518 free(compressed_buff);
519 free(chunkP);
520 return 13;
521 }
522
523 /* Calculating input Checksum */
524 crcOriginal = XXH32(orig_buff, benchedSize,0);
525
526
527 /* Bench */
528 { int loopNb, nb_loops, chunkNb, cAlgNb, dAlgNb;
529 size_t cSize=0;
530 double ratio=0.;
531
532 DISPLAY("\r%79s\r", "");
533 DISPLAY(" %s : \n", inFileName);
534
535 /* Bench Compression Algorithms */
536 for (cAlgNb=0; (cAlgNb <= NB_COMPRESSION_ALGORITHMS) && (g_compressionTest); cAlgNb++) {
537 const char* compressorName;
538 int (*compressionFunction)(const char*, char*, int);
539 void (*initFunction)(void) = NULL;
540 double bestTime = 100000000.;
541
542 /* filter compressionAlgo only */
543 if ((g_compressionAlgo != ALL_COMPRESSORS) && (g_compressionAlgo != cAlgNb)) continue;
544
545 /* Init data chunks */
546 { int i;
547 size_t remaining = benchedSize;
548 char* in = orig_buff;
549 char* out = compressed_buff;
550 assert(nbChunks >= 1);
551 for (i=0; i<nbChunks; i++) {
552 chunkP[i].id = (U32)i;
553 chunkP[i].origBuffer = in; in += g_chunkSize;
554 assert(g_chunkSize > 0);
555 if (remaining > (size_t)g_chunkSize) {
556 chunkP[i].origSize = g_chunkSize;
557 remaining -= (size_t)g_chunkSize;
558 } else {
559 chunkP[i].origSize = (int)remaining;
560 remaining = 0;
561 }
562 chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
563 chunkP[i].compressedSize = 0;
564 }
565 }
566
567 switch(cAlgNb)
568 {
569 case 0 : DISPLAY("Compression functions : \n"); continue;
570 case 1 : compressionFunction = local_LZ4_compress_default_large; compressorName = "LZ4_compress_default"; break;
571 case 2 : compressionFunction = local_LZ4_compress_default_small; compressorName = "LZ4_compress_default(small dst)"; break;
572 case 3 : compressionFunction = local_LZ4_compress_destSize; compressorName = "LZ4_compress_destSize"; break;
573 case 4 : compressionFunction = local_LZ4_compress_fast0; compressorName = "LZ4_compress_fast(0)"; break;
574 case 5 : compressionFunction = local_LZ4_compress_fast1; compressorName = "LZ4_compress_fast(1)"; break;
575 case 6 : compressionFunction = local_LZ4_compress_fast2; compressorName = "LZ4_compress_fast(2)"; break;
576 case 7 : compressionFunction = local_LZ4_compress_fast17; compressorName = "LZ4_compress_fast(17)"; break;
577 case 8 : compressionFunction = local_LZ4_compress_fast_extState0; compressorName = "LZ4_compress_fast_extState(0)"; break;
578 case 9 : compressionFunction = local_LZ4_compress_fast_continue0; initFunction = local_LZ4_createStream; compressorName = "LZ4_compress_fast_continue(0)"; break;
579
580 case 10: compressionFunction = local_LZ4_compress_HC; compressorName = "LZ4_compress_HC"; break;
581 case 12: compressionFunction = local_LZ4_compress_HC_extStateHC; compressorName = "LZ4_compress_HC_extStateHC"; break;
582 case 14: compressionFunction = local_LZ4_compress_HC_continue; initFunction = local_LZ4_resetStreamHC; compressorName = "LZ4_compress_HC_continue"; break;
583 #ifndef LZ4_DLL_IMPORT
584 case 20: compressionFunction = local_LZ4_compress_forceDict; initFunction = local_LZ4_resetDictT; compressorName = "LZ4_compress_forceDict"; break;
585 #endif
586 case 30: compressionFunction = local_LZ4F_compressFrame; compressorName = "LZ4F_compressFrame";
587 chunkP[0].origSize = (int)benchedSize; nbChunks=1;
588 break;
589 case 40: compressionFunction = local_LZ4_saveDict; compressorName = "LZ4_saveDict";
590 if (chunkP[0].origSize < 8) { DISPLAY(" cannot bench %s with less then 8 bytes \n", compressorName); continue; }
591 LZ4_loadDict(&LZ4_stream, chunkP[0].origBuffer, chunkP[0].origSize);
592 break;
593 case 41: compressionFunction = local_LZ4_saveDictHC; compressorName = "LZ4_saveDictHC";
594 if (chunkP[0].origSize < 8) { DISPLAY(" cannot bench %s with less then 8 bytes \n", compressorName); continue; }
595 LZ4_loadDictHC(&LZ4_streamHC, chunkP[0].origBuffer, chunkP[0].origSize);
596 break;
597 default :
598 continue; /* unknown ID : just skip */
599 }
600
601 for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
602 double averageTime;
603 clock_t clockTime;
604
605 PROGRESS("%2i-%-34.34s :%10i ->\r", loopNb, compressorName, (int)benchedSize);
606 { size_t i; for (i=0; i<benchedSize; i++) compressed_buff[i]=(char)i; } /* warming up memory */
607
608 nb_loops = 0;
609 clockTime = clock();
610 while(clock() == clockTime);
611 clockTime = clock();
612 while(BMK_GetClockSpan(clockTime) < TIMELOOP) {
613 if (initFunction!=NULL) initFunction();
614 for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
615 chunkP[chunkNb].compressedSize = compressionFunction(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize);
616 if (chunkP[chunkNb].compressedSize==0) {
617 DISPLAY("ERROR ! %s() = 0 !! \n", compressorName);
618 exit(1);
619 } }
620 nb_loops++;
621 }
622 clockTime = BMK_GetClockSpan(clockTime);
623
624 nb_loops += !nb_loops; /* avoid division by zero */
625 averageTime = ((double)clockTime) / nb_loops / CLOCKS_PER_SEC;
626 if (averageTime < bestTime) bestTime = averageTime;
627 cSize=0; for (chunkNb=0; chunkNb<nbChunks; chunkNb++) cSize += (size_t)chunkP[chunkNb].compressedSize;
628 ratio = (double)cSize/(double)benchedSize*100.;
629 PROGRESS("%2i-%-34.34s :%10i ->%9i (%5.2f%%),%7.1f MB/s\r", loopNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000000);
630 }
631
632 if (ratio<100.)
633 DISPLAY("%2i-%-34.34s :%10i ->%9i (%5.2f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000000);
634 else
635 DISPLAY("%2i-%-34.34s :%10i ->%9i (%5.1f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 100000);
636 }
637
638 /* Prepare layout for decompression */
639 /* Init data chunks */
640 { int i;
641 size_t remaining = benchedSize;
642 char* in = orig_buff;
643 char* out = compressed_buff;
644
645 nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize);
646 for (i=0; i<nbChunks; i++) {
647 chunkP[i].id = (U32)i;
648 chunkP[i].origBuffer = in; in += g_chunkSize;
649 if ((int)remaining > g_chunkSize) {
650 chunkP[i].origSize = g_chunkSize;
651 remaining -= (size_t)g_chunkSize;
652 } else {
653 chunkP[i].origSize = (int)remaining;
654 remaining = 0;
655 }
656 chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
657 chunkP[i].compressedSize = 0;
658 }
659 }
660 for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
661 chunkP[chunkNb].compressedSize = LZ4_compress_default(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize, maxCompressedChunkSize);
662 if (chunkP[chunkNb].compressedSize==0) {
663 DISPLAY("ERROR ! %s() = 0 !! \n", "LZ4_compress");
664 exit(1);
665 } }
666
667 /* Decompression Algorithms */
668 for (dAlgNb=0; (dAlgNb <= NB_DECOMPRESSION_ALGORITHMS) && g_decompressionTest; dAlgNb++) {
669 const char* dName = NULL;
670 int (*decompressionFunction)(const char*, char*, int, int) = NULL;
671 double bestTime = 100000000.;
672 int checkResult = 1;
673
674 if ((g_decompressionAlgo != ALL_DECOMPRESSORS) && (g_decompressionAlgo != dAlgNb)) continue;
675
676 switch(dAlgNb)
677 {
678 case 0: DISPLAY("Decompression functions : \n"); continue;
679 case 1: decompressionFunction = local_LZ4_decompress_fast; dName = "LZ4_decompress_fast"; break;
680 case 2: decompressionFunction = local_LZ4_decompress_fast_usingDict_prefix; dName = "LZ4_decompress_fast_usingDict(prefix)"; break;
681 case 3: decompressionFunction = local_LZ4_decompress_fast_usingExtDict; dName = "LZ4_decompress_fast_using(Ext)Dict"; break;
682 case 4: decompressionFunction = LZ4_decompress_safe; dName = "LZ4_decompress_safe"; break;
683 case 5: decompressionFunction = local_LZ4_decompress_safe_withPrefix64k; dName = "LZ4_decompress_safe_withPrefix64k"; break;
684 case 6: decompressionFunction = local_LZ4_decompress_safe_usingDict; dName = "LZ4_decompress_safe_usingDict"; break;
685 case 7: decompressionFunction = local_LZ4_decompress_safe_partial; dName = "LZ4_decompress_safe_partial"; checkResult = 0; break;
686 case 8: decompressionFunction = local_LZ4_decompress_safe_partial_usingDict; dName = "LZ4_decompress_safe_partial_usingDict"; checkResult = 0; break;
687 #ifndef LZ4_DLL_IMPORT
688 case 9: decompressionFunction = local_LZ4_decompress_safe_partial_forceExtDict; dName = "LZ4_decompress_safe_partial_forceExtDict"; checkResult = 0; break;
689 case 10: decompressionFunction = local_LZ4_decompress_safe_forceExtDict; dName = "LZ4_decompress_safe_forceExtDict"; break;
690 #endif
691 case 11:
692 case 12:
693 case 13:
694 if (dAlgNb == 11) { decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress"; } /* can be skipped */
695 if (dAlgNb == 12) { decompressionFunction = local_LZ4F_decompress_followHint; dName = "LZ4F_decompress_followHint"; } /* can be skipped */
696 if (dAlgNb == 13) { decompressionFunction = local_LZ4F_decompress_noHint; dName = "LZ4F_decompress_noHint"; } /* can be skipped */
697 /* prepare compressed data using frame format */
698 { size_t const fcsize = LZ4F_compressFrame(compressed_buff, (size_t)compressedBuffSize, orig_buff, benchedSize, NULL);
699 assert(!LZ4F_isError(fcsize));
700 chunkP[0].origSize = (int)benchedSize;
701 chunkP[0].compressedSize = (int)fcsize;
702 nbChunks = 1;
703 break;
704 }
705 default :
706 continue; /* skip if unknown ID */
707 }
708
709 assert(decompressionFunction != NULL);
710 assert(dName != NULL);
711
712 { size_t i; for (i=0; i<benchedSize; i++) orig_buff[i]=0; } /* zeroing source area, for CRC checking */
713
714 for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
715 double averageTime;
716 clock_t clockTime;
717 U32 crcDecoded;
718
719 PROGRESS("%2i-%-34.34s :%10i ->\r", loopNb, dName, (int)benchedSize);
720
721 nb_loops = 0;
722 clockTime = clock();
723 while(clock() == clockTime);
724 clockTime = clock();
725 while(BMK_GetClockSpan(clockTime) < TIMELOOP) {
726 for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
727 int const decodedSize = decompressionFunction(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer,
728 chunkP[chunkNb].compressedSize, chunkP[chunkNb].origSize);
729 if (chunkP[chunkNb].origSize != decodedSize) {
730 DISPLAY("ERROR ! %s() == %i != %i !! \n",
731 dName, decodedSize, chunkP[chunkNb].origSize);
732 exit(1);
733 } }
734 nb_loops++;
735 }
736 clockTime = BMK_GetClockSpan(clockTime);
737
738 nb_loops += !nb_loops; /* Avoid division by zero */
739 averageTime = (double)clockTime / nb_loops / CLOCKS_PER_SEC;
740 if (averageTime < bestTime) bestTime = averageTime;
741
742 PROGRESS("%2i-%-34.34s :%10i -> %7.1f MB/s\r", loopNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000000);
743
744 /* CRC Checking */
745 crcDecoded = XXH32(orig_buff, benchedSize, 0);
746 if (checkResult && (crcOriginal!=crcDecoded)) {
747 DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n",
748 inFileName, (unsigned)crcOriginal, (unsigned)crcDecoded);
749 exit(1);
750 } }
751
752 DISPLAY("%2i-%-34.34s :%10i -> %7.1f MB/s\n", dAlgNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000000);
753 }
754 }
755 free(orig_buff);
756 free(compressed_buff);
757 free(chunkP);
758 }
759
760 LZ4F_freeDecompressionContext(g_dCtx);
761 if (g_pause) { printf("press enter...\n"); (void)getchar(); }
762
763 return 0;
764 }
765
766
usage(const char * exename)767 static int usage(const char* exename)
768 {
769 DISPLAY( "Usage :\n");
770 DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename);
771 DISPLAY( "Arguments :\n");
772 DISPLAY( " -c : compression tests only\n");
773 DISPLAY( " -d : decompression tests only\n");
774 DISPLAY( " -H/-h : Help (this text + advanced options)\n");
775 return 0;
776 }
777
usage_advanced(void)778 static int usage_advanced(void)
779 {
780 DISPLAY( "\nAdvanced options :\n");
781 DISPLAY( " -c# : test only compression function # [1-%i]\n", NB_COMPRESSION_ALGORITHMS);
782 DISPLAY( " -d# : test only decompression function # [1-%i]\n", NB_DECOMPRESSION_ALGORITHMS);
783 DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS);
784 DISPLAY( " -B# : Block size [4-7](default : 7)\n");
785 return 0;
786 }
787
badusage(const char * exename)788 static int badusage(const char* exename)
789 {
790 DISPLAY("Wrong parameters\n");
791 usage(exename);
792 return 0;
793 }
794
main(int argc,const char ** argv)795 int main(int argc, const char** argv)
796 {
797 int i,
798 filenamesStart=2;
799 const char* exename = argv[0];
800 const char* input_filename=0;
801
802 // Welcome message
803 DISPLAY(WELCOME_MESSAGE);
804
805 if (argc<2) { badusage(exename); return 1; }
806
807 for(i=1; i<argc; i++) {
808 const char* argument = argv[i];
809
810 if(!argument) continue; // Protection if argument empty
811 if (!strcmp(argument, "--no-prompt")) {
812 g_noPrompt = 1;
813 continue;
814 }
815
816 // Decode command (note : aggregated commands are allowed)
817 if (argument[0]=='-') {
818 while (argument[1]!=0) {
819 argument ++;
820
821 switch(argument[0])
822 {
823 // Select compression algorithm only
824 case 'c':
825 g_decompressionTest = 0;
826 while ((argument[1]>= '0') && (argument[1]<= '9')) {
827 g_compressionAlgo *= 10;
828 g_compressionAlgo += argument[1] - '0';
829 argument++;
830 }
831 break;
832
833 // Select decompression algorithm only
834 case 'd':
835 g_compressionTest = 0;
836 while ((argument[1]>= '0') && (argument[1]<= '9')) {
837 g_decompressionAlgo *= 10;
838 g_decompressionAlgo += argument[1] - '0';
839 argument++;
840 }
841 break;
842
843 // Display help on usage
844 case 'h' :
845 case 'H': usage(exename); usage_advanced(); return 0;
846
847 // Modify Block Properties
848 case 'B':
849 while (argument[1]!=0)
850 switch(argument[1])
851 {
852 case '4':
853 case '5':
854 case '6':
855 case '7':
856 { int B = argument[1] - '0';
857 int S = 1 << (8 + 2*B);
858 BMK_setBlocksize(S);
859 argument++;
860 break;
861 }
862 case 'D': argument++; break;
863 default : goto _exit_blockProperties;
864 }
865 _exit_blockProperties:
866 break;
867
868 // Modify Nb Iterations
869 case 'i':
870 if ((argument[1] >='0') && (argument[1] <='9')) {
871 int iters = argument[1] - '0';
872 BMK_setNbIterations(iters);
873 argument++;
874 }
875 break;
876
877 // Pause at the end (hidden option)
878 case 'p': BMK_setPause(); break;
879
880 // Unknown command
881 default : badusage(exename); return 1;
882 }
883 }
884 continue;
885 }
886
887 // first provided filename is input
888 if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
889
890 }
891
892 // No input filename ==> Error
893 if(!input_filename) { badusage(exename); return 1; }
894
895 return fullSpeedBench(argv+filenamesStart, argc-filenamesStart);
896
897 }
898