2 * Copyright (c) 2015-present, Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
12 /*_************************************
14 **************************************/
15 #include "util.h" /* Compiler options, UTIL_GetFileSize */
16 #include <stdlib.h> /* malloc */
17 #include <stdio.h> /* fprintf, fopen, ftello64 */
18 #include <assert.h> /* assert */
20 #include "mem.h" /* U32 */
21 #ifndef ZSTD_DLL_IMPORT
22 #include "zstd_internal.h" /* ZSTD_decodeSeqHeaders, ZSTD_blockHeaderSize, blockType_e, KB, MB */
27 typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
29 #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressBegin, ZSTD_compressContinue, etc. */
30 #include "zstd.h" /* ZSTD_versionString */
31 #include "util.h" /* time functions */
33 #include "benchfn.h" /* CustomBench*/
34 #include "benchzstd.h" /* MB_UNIT */
37 /*_************************************
39 **************************************/
40 #define PROGRAM_DESCRIPTION "Zstandard speed analyzer"
41 #define AUTHOR "Yann Collet"
42 #define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, ZSTD_versionString(), (int)(sizeof(void*)*8), AUTHOR, __DATE__
47 #define KNUTH 2654435761U
48 #define MAX_MEM (1984 MB)
50 #define DEFAULT_CLEVEL 1
52 #define COMPRESSIBILITY_DEFAULT 0.50
53 static const size_t g_sampleSize = 10000000;
55 #define TIMELOOP_NANOSEC (1*1000000000ULL) /* 1 second */
58 /*_************************************
60 **************************************/
61 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
64 /*_************************************
65 * Benchmark Parameters
66 **************************************/
67 static unsigned g_nbIterations = NBLOOPS;
68 static double g_compressibility = COMPRESSIBILITY_DEFAULT;
70 static void BMK_SetNbIterations(unsigned nbLoops)
72 g_nbIterations = nbLoops;
73 DISPLAY("- %i iterations -\n", g_nbIterations);
77 /*_*******************************************************
79 *********************************************************/
80 static size_t BMK_findMaxMem(U64 requiredMem)
82 size_t const step = 64 MB;
85 requiredMem = (((requiredMem >> 26) + 1) << 26);
86 if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
90 testmem = malloc ((size_t)requiredMem);
95 return (size_t) requiredMem;
99 /*_*******************************************************
101 *********************************************************/
103 static ZSTD_CCtx* g_zcc = NULL;
106 local_ZSTD_compress(const void* src, size_t srcSize,
107 void* dst, size_t dstSize,
111 ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
113 p.cParams = *(ZSTD_compressionParameters*)buff2;
114 return ZSTD_compress_advanced (g_zcc, dst, dstSize, src, srcSize, NULL ,0, p);
115 //return ZSTD_compress(dst, dstSize, src, srcSize, cLevel);
118 static size_t g_cSize = 0;
119 static size_t local_ZSTD_decompress(const void* src, size_t srcSize,
120 void* dst, size_t dstSize,
123 (void)src; (void)srcSize;
124 return ZSTD_decompress(dst, dstSize, buff2, g_cSize);
127 static ZSTD_DCtx* g_zdc = NULL;
129 #ifndef ZSTD_DLL_IMPORT
130 extern size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* ctx, const void* src, size_t srcSize);
131 static size_t local_ZSTD_decodeLiteralsBlock(const void* src, size_t srcSize, void* dst, size_t dstSize, void* buff2)
133 (void)src; (void)srcSize; (void)dst; (void)dstSize;
134 return ZSTD_decodeLiteralsBlock((ZSTD_DCtx*)g_zdc, buff2, g_cSize);
137 static size_t local_ZSTD_decodeSeqHeaders(const void* src, size_t srcSize, void* dst, size_t dstSize, void* buff2)
140 (void)src; (void)srcSize; (void)dst; (void)dstSize;
141 return ZSTD_decodeSeqHeaders(g_zdc, &nbSeq, buff2, g_cSize);
145 static ZSTD_CStream* g_cstream= NULL;
147 local_ZSTD_compressStream(const void* src, size_t srcSize,
148 void* dst, size_t dstCapacity,
151 ZSTD_outBuffer buffOut;
152 ZSTD_inBuffer buffIn;
154 ZSTD_frameParameters f = {1 /* contentSizeHeader*/, 0, 0};
156 p.cParams = *(ZSTD_compressionParameters*)buff2;
157 ZSTD_initCStream_advanced(g_cstream, NULL, 0, p, ZSTD_CONTENTSIZE_UNKNOWN);
159 buffOut.size = dstCapacity;
162 buffIn.size = srcSize;
164 ZSTD_compressStream(g_cstream, &buffOut, &buffIn);
165 ZSTD_endStream(g_cstream, &buffOut);
170 local_ZSTD_compress_generic_end(const void* src, size_t srcSize,
171 void* dst, size_t dstCapacity,
175 return ZSTD_compress2(g_cstream, dst, dstCapacity, src, srcSize);
179 local_ZSTD_compress_generic_continue(const void* src, size_t srcSize,
180 void* dst, size_t dstCapacity,
183 ZSTD_outBuffer buffOut;
184 ZSTD_inBuffer buffIn;
187 buffOut.size = dstCapacity;
190 buffIn.size = srcSize;
192 ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
193 ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
198 local_ZSTD_compress_generic_T2_end(const void* src, size_t srcSize,
199 void* dst, size_t dstCapacity,
203 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_nbWorkers, 2);
204 return ZSTD_compress2(g_cstream, dst, dstCapacity, src, srcSize);
208 local_ZSTD_compress_generic_T2_continue(const void* src, size_t srcSize,
209 void* dst, size_t dstCapacity,
212 ZSTD_outBuffer buffOut;
213 ZSTD_inBuffer buffIn;
215 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_nbWorkers, 2);
217 buffOut.size = dstCapacity;
220 buffIn.size = srcSize;
222 ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
223 while(ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {}
227 static ZSTD_DStream* g_dstream= NULL;
229 local_ZSTD_decompressStream(const void* src, size_t srcSize,
230 void* dst, size_t dstCapacity,
233 ZSTD_outBuffer buffOut;
234 ZSTD_inBuffer buffIn;
235 (void)src; (void)srcSize;
236 ZSTD_initDStream(g_dstream);
238 buffOut.size = dstCapacity;
241 buffIn.size = g_cSize;
243 ZSTD_decompressStream(g_dstream, &buffOut, &buffIn);
247 #ifndef ZSTD_DLL_IMPORT
248 static size_t local_ZSTD_compressContinue(const void* src, size_t srcSize,
249 void* dst, size_t dstCapacity,
253 ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
255 p.cParams = *(ZSTD_compressionParameters*)buff2;
256 ZSTD_compressBegin_advanced(g_zcc, NULL, 0, p, srcSize);
257 return ZSTD_compressEnd(g_zcc, dst, dstCapacity, src, srcSize);
260 #define FIRST_BLOCK_SIZE 8
261 static size_t local_ZSTD_compressContinue_extDict(const void* src, size_t srcSize,
262 void* dst, size_t dstCapacity,
265 BYTE firstBlockBuf[FIRST_BLOCK_SIZE];
268 ZSTD_frameParameters f = { 1, 0, 0 };
270 p.cParams = *(ZSTD_compressionParameters*)buff2;
271 ZSTD_compressBegin_advanced(g_zcc, NULL, 0, p, srcSize);
272 memcpy(firstBlockBuf, src, FIRST_BLOCK_SIZE);
274 { size_t const compressResult = ZSTD_compressContinue(g_zcc,
276 firstBlockBuf, FIRST_BLOCK_SIZE);
277 if (ZSTD_isError(compressResult)) {
278 DISPLAY("local_ZSTD_compressContinue_extDict error : %s\n",
279 ZSTD_getErrorName(compressResult));
280 return compressResult;
282 dst = (BYTE*)dst + compressResult;
283 dstCapacity -= compressResult;
285 return ZSTD_compressEnd(g_zcc, dst, dstCapacity,
286 (const BYTE*)src + FIRST_BLOCK_SIZE,
287 srcSize - FIRST_BLOCK_SIZE);
290 static size_t local_ZSTD_decompressContinue(const void* src, size_t srcSize,
291 void* dst, size_t dstCapacity,
294 size_t regeneratedSize = 0;
295 const BYTE* ip = (const BYTE*)buff2;
296 const BYTE* const iend = ip + g_cSize;
297 BYTE* op = (BYTE*)dst;
298 size_t remainingCapacity = dstCapacity;
300 (void)src; (void)srcSize; /* unused */
301 ZSTD_decompressBegin(g_zdc);
303 size_t const iSize = ZSTD_nextSrcSizeToDecompress(g_zdc);
304 size_t const decodedSize = ZSTD_decompressContinue(g_zdc, op, remainingCapacity, ip, iSize);
306 regeneratedSize += decodedSize;
308 remainingCapacity -= decodedSize;
311 return regeneratedSize;
316 /*_*******************************************************
318 *********************************************************/
319 static size_t benchMem(unsigned benchNb,
320 const void* src, size_t srcSize,
321 int cLevel, ZSTD_compressionParameters cparams)
323 size_t dstBuffSize = ZSTD_compressBound(srcSize);
327 const char* benchName;
328 BMK_benchFn_t benchFunction;
335 benchFunction = local_ZSTD_compress; benchName = "compress";
338 benchFunction = local_ZSTD_decompress; benchName = "decompress";
340 #ifndef ZSTD_DLL_IMPORT
342 benchFunction = local_ZSTD_compressContinue; benchName = "compressContinue";
345 benchFunction = local_ZSTD_compressContinue_extDict; benchName = "compressContinue_extDict";
348 benchFunction = local_ZSTD_decompressContinue; benchName = "decompressContinue";
351 benchFunction = local_ZSTD_decodeLiteralsBlock; benchName = "decodeLiteralsBlock";
354 benchFunction = local_ZSTD_decodeSeqHeaders; benchName = "decodeSeqHeaders";
358 benchFunction = local_ZSTD_compressStream; benchName = "compressStream";
361 benchFunction = local_ZSTD_decompressStream; benchName = "decompressStream";
364 benchFunction = local_ZSTD_compress_generic_continue; benchName = "compress_generic, continue";
367 benchFunction = local_ZSTD_compress_generic_end; benchName = "compress_generic, end";
370 benchFunction = local_ZSTD_compress_generic_T2_continue; benchName = "compress_generic, -T2, continue";
373 benchFunction = local_ZSTD_compress_generic_T2_end; benchName = "compress_generic, -T2, end";
380 dstBuff = (BYTE*)malloc(dstBuffSize);
381 dstBuff2 = malloc(dstBuffSize);
382 if ((!dstBuff) || (!dstBuff2)) {
383 DISPLAY("\nError: not enough memory!\n");
384 free(dstBuff); free(dstBuff2);
388 if (g_zcc==NULL) g_zcc = ZSTD_createCCtx();
389 if (g_zdc==NULL) g_zdc = ZSTD_createDCtx();
390 if (g_cstream==NULL) g_cstream = ZSTD_createCStream();
391 if (g_dstream==NULL) g_dstream = ZSTD_createDStream();
393 /* DISPLAY("params: cLevel %d, wlog %d hlog %d clog %d slog %d mml %d tlen %d strat %d \n",
394 cLevel, cparams->windowLog, cparams->hashLog, cparams->chainLog, cparams->searchLog,
395 cparams->minMatch, cparams->targetLength, cparams->strategy); */
397 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_compressionLevel, cLevel);
398 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_windowLog, cparams.windowLog);
399 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_hashLog, cparams.hashLog);
400 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_chainLog, cparams.chainLog);
401 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_searchLog, cparams.searchLog);
402 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_minMatch, cparams.minMatch);
403 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_targetLength, cparams.targetLength);
404 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_strategy, cparams.strategy);
407 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_compressionLevel, cLevel);
408 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_windowLog, cparams.windowLog);
409 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_hashLog, cparams.hashLog);
410 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_chainLog, cparams.chainLog);
411 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_searchLog, cparams.searchLog);
412 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_minMatch, cparams.minMatch);
413 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_targetLength, cparams.targetLength);
414 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_strategy, cparams.strategy);
423 g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, cLevel);
425 #ifndef ZSTD_DLL_IMPORT
433 g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, cLevel);
435 case 31: /* ZSTD_decodeLiteralsBlock */
436 { blockProperties_t bp;
437 ZSTD_frameHeader zfp;
438 size_t frameHeaderSize, skippedSize;
439 g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, cLevel);
440 frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_FRAMEHEADERSIZE_MIN);
441 if (frameHeaderSize==0) frameHeaderSize = ZSTD_FRAMEHEADERSIZE_MIN;
442 ZSTD_getcBlockSize(dstBuff+frameHeaderSize, dstBuffSize, &bp); /* Get 1st block type */
443 if (bp.blockType != bt_compressed) {
444 DISPLAY("ZSTD_decodeLiteralsBlock : impossible to test on this sample (not compressible)\n");
447 skippedSize = frameHeaderSize + ZSTD_blockHeaderSize;
448 memcpy(buff2, dstBuff+skippedSize, g_cSize-skippedSize);
449 srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
450 ZSTD_decompressBegin(g_zdc);
453 case 32: /* ZSTD_decodeSeqHeaders */
454 { blockProperties_t bp;
455 ZSTD_frameHeader zfp;
456 const BYTE* ip = dstBuff;
458 size_t frameHeaderSize, cBlockSize;
459 ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, cLevel); /* it would be better to use direct block compression here */
460 g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, cLevel);
461 frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_FRAMEHEADERSIZE_MIN);
462 if (frameHeaderSize==0) frameHeaderSize = ZSTD_FRAMEHEADERSIZE_MIN;
463 ip += frameHeaderSize; /* Skip frame Header */
464 cBlockSize = ZSTD_getcBlockSize(ip, dstBuffSize, &bp); /* Get 1st block type */
465 if (bp.blockType != bt_compressed) {
466 DISPLAY("ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible)\n");
469 iend = ip + ZSTD_blockHeaderSize + cBlockSize; /* End of first block */
470 ip += ZSTD_blockHeaderSize; /* skip block header */
471 ZSTD_decompressBegin(g_zdc);
472 ip += ZSTD_decodeLiteralsBlock(g_zdc, ip, iend-ip); /* skip literal segment */
474 memcpy(buff2, ip, g_cSize); /* copy rest of block (it starts by SeqHeader) */
475 srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
486 g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, cLevel);
490 /* convention: test functions have ID > 100 */
495 /* warming up dstBuff */
496 { size_t i; for (i=0; i<dstBuffSize; i++) dstBuff[i]=(BYTE)i; }
499 { BMK_timedFnState_t* const tfs = BMK_createTimedFnState(g_nbIterations * 1000, 1000);
500 void* const avoidStrictAliasingPtr = &dstBuff;
501 BMK_benchParams_t bp;
502 BMK_runTime_t bestResult;
503 bestResult.sumOfReturn = 0;
504 bestResult.nanoSecPerRun = (unsigned long long)(-1LL);
507 bp.benchFn = benchFunction;
508 bp.benchPayload = buff2;
510 bp.initPayload = NULL;
511 bp.errorFn = ZSTD_isError;
513 bp.srcBuffers = &src;
514 bp.srcSizes = &srcSize;
515 bp.dstBuffers = (void* const*) avoidStrictAliasingPtr; /* circumvent strict aliasing warning on gcc-8,
516 * because gcc considers that `void* const *` and `void**` are 2 different types */
517 bp.dstCapacities = &dstBuffSize;
518 bp.blockResults = NULL;
521 BMK_runOutcome_t const bOutcome = BMK_benchTimedFn(tfs, bp);
523 if (!BMK_isSuccessful_runOutcome(bOutcome)) {
524 DISPLAY("ERROR benchmarking function ! ! \n");
529 { BMK_runTime_t const newResult = BMK_extract_runTime(bOutcome);
530 if (newResult.nanoSecPerRun < bestResult.nanoSecPerRun )
531 bestResult.nanoSecPerRun = newResult.nanoSecPerRun;
532 DISPLAY("\r%2u#%-29.29s:%8.1f MB/s (%8u) ",
534 (double)srcSize * TIMELOOP_NANOSEC / bestResult.nanoSecPerRun / MB_UNIT,
535 (unsigned)newResult.sumOfReturn );
538 if ( BMK_isCompleted_TimedFn(tfs) ) break;
540 BMK_freeTimedFnState(tfs);
547 ZSTD_freeCCtx(g_zcc); g_zcc=NULL;
548 ZSTD_freeDCtx(g_zdc); g_zdc=NULL;
549 ZSTD_freeCStream(g_cstream); g_cstream=NULL;
550 ZSTD_freeDStream(g_dstream); g_dstream=NULL;
555 static int benchSample(U32 benchNb,
556 int cLevel, ZSTD_compressionParameters cparams)
558 size_t const benchedSize = g_sampleSize;
559 const char* const name = "Sample 10MiB";
562 void* const origBuff = malloc(benchedSize);
563 if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; }
566 RDG_genBuffer(origBuff, benchedSize, g_compressibility, 0.0, 0);
569 DISPLAY("\r%70s\r", "");
570 DISPLAY(" %s : \n", name);
572 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
573 } else { /* 0 == run all tests */
574 for (benchNb=0; benchNb<100; benchNb++) {
575 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
583 static int benchFiles(U32 benchNb,
584 const char** fileNamesTable, const int nbFiles,
585 int cLevel, ZSTD_compressionParameters cparams)
587 /* Loop for each file */
589 for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
590 const char* const inFileName = fileNamesTable[fileIdx];
591 FILE* const inFile = fopen( inFileName, "rb" );
594 /* Check file existence */
595 if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
597 /* Memory allocation & restrictions */
598 { U64 const inFileSize = UTIL_getFileSize(inFileName);
599 if (inFileSize == UTIL_FILESIZE_UNKNOWN) {
600 DISPLAY( "Cannot measure size of %s\n", inFileName);
604 benchedSize = BMK_findMaxMem(inFileSize*3) / 3;
605 if ((U64)benchedSize > inFileSize)
606 benchedSize = (size_t)inFileSize;
607 if ((U64)benchedSize < inFileSize) {
608 DISPLAY("Not enough memory for '%s' full size; testing %u MB only... \n",
609 inFileName, (unsigned)(benchedSize>>20));
613 { void* const origBuff = malloc(benchedSize);
614 if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; }
616 /* Fill input buffer */
617 DISPLAY("Loading %s... \r", inFileName);
618 { size_t const readSize = fread(origBuff, 1, benchedSize, inFile);
620 if (readSize != benchedSize) {
621 DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
627 DISPLAY("\r%70s\r", ""); /* blank line */
628 DISPLAY(" %s : \n", inFileName);
630 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
632 for (benchNb=0; benchNb<100; benchNb++) {
633 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
644 /*_*******************************************************
646 *********************************************************/
648 #define ERROR_OUT(msg) { DISPLAY("%s \n", msg); exit(1); }
650 static unsigned readU32FromChar(const char** stringPtr)
652 const char errorMsg[] = "error: numeric value too large";
654 while ((**stringPtr >='0') && (**stringPtr <='9')) {
655 unsigned const max = (((unsigned)(-1)) / 10) - 1;
656 if (result > max) ERROR_OUT(errorMsg);
657 result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
659 if ((**stringPtr=='K') || (**stringPtr=='M')) {
660 unsigned const maxK = ((unsigned)(-1)) >> 10;
661 if (result > maxK) ERROR_OUT(errorMsg);
663 if (**stringPtr=='M') {
664 if (result > maxK) ERROR_OUT(errorMsg);
667 (*stringPtr)++; /* skip `K` or `M` */
668 if (**stringPtr=='i') (*stringPtr)++;
669 if (**stringPtr=='B') (*stringPtr)++;
674 static unsigned longCommandWArg(const char** stringPtr, const char* longCommand)
676 size_t const comSize = strlen(longCommand);
677 int const result = !strncmp(*stringPtr, longCommand, comSize);
678 if (result) *stringPtr += comSize;
683 /*_*******************************************************
685 *********************************************************/
687 static int usage(const char* exename)
689 DISPLAY( "Usage :\n");
690 DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename);
691 DISPLAY( "Arguments :\n");
692 DISPLAY( " -H/-h : Help (this text + advanced options)\n");
696 static int usage_advanced(const char* exename)
699 DISPLAY( "\nAdvanced options :\n");
700 DISPLAY( " -b# : test only function # \n");
701 DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS);
702 DISPLAY( " -P# : sample compressibility (default : %.1f%%)\n", COMPRESSIBILITY_DEFAULT * 100);
703 DISPLAY( " -l# : benchmark functions at that compression level (default : %i)\n", DEFAULT_CLEVEL);
704 DISPLAY( " --zstd : custom parameter selection. Format same as zstdcli \n");
708 static int badusage(const char* exename)
710 DISPLAY("Wrong parameters\n");
715 int main(int argc, const char** argv)
717 int argNb, filenamesStart=0, result;
718 const char* const exename = argv[0];
719 const char* input_filename = NULL;
720 U32 benchNb = 0, main_pause = 0;
721 int cLevel = DEFAULT_CLEVEL;
722 ZSTD_compressionParameters cparams = ZSTD_getCParams(cLevel, 0, 0);
724 DISPLAY(WELCOME_MESSAGE);
725 if (argc<1) return badusage(exename);
727 for (argNb=1; argNb<argc; argNb++) {
728 const char* argument = argv[argNb];
729 assert(argument != NULL);
731 if (longCommandWArg(&argument, "--zstd=")) {
733 if (longCommandWArg(&argument, "windowLog=") || longCommandWArg(&argument, "wlog=")) { cparams.windowLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
734 if (longCommandWArg(&argument, "chainLog=") || longCommandWArg(&argument, "clog=")) { cparams.chainLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
735 if (longCommandWArg(&argument, "hashLog=") || longCommandWArg(&argument, "hlog=")) { cparams.hashLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
736 if (longCommandWArg(&argument, "searchLog=") || longCommandWArg(&argument, "slog=")) { cparams.searchLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
737 if (longCommandWArg(&argument, "minMatch=") || longCommandWArg(&argument, "mml=")) { cparams.minMatch = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
738 if (longCommandWArg(&argument, "targetLength=") || longCommandWArg(&argument, "tlen=")) { cparams.targetLength = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
739 if (longCommandWArg(&argument, "strategy=") || longCommandWArg(&argument, "strat=")) { cparams.strategy = (ZSTD_strategy)(readU32FromChar(&argument)); if (argument[0]==',') { argument++; continue; } else break; }
740 if (longCommandWArg(&argument, "level=") || longCommandWArg(&argument, "lvl=")) { cLevel = (int)readU32FromChar(&argument); cparams = ZSTD_getCParams(cLevel, 0, 0); if (argument[0]==',') { argument++; continue; } else break; }
741 DISPLAY("invalid compression parameter \n");
745 /* check end of string */
746 if (argument[0] != 0) {
747 DISPLAY("invalid --zstd= format \n");
753 } else if (argument[0]=='-') { /* Commands (note : aggregated commands are allowed) */
755 while (argument[0]!=0) {
759 /* Display help on usage */
761 case 'H': return usage_advanced(exename);
763 /* Pause at the end (hidden option) */
764 case 'p': main_pause = 1; break;
766 /* Select specific algorithm to bench */
769 benchNb = readU32FromChar(&argument);
772 /* Modify Nb Iterations */
775 BMK_SetNbIterations((int)readU32FromChar(&argument));
778 /* Select compressibility of synthetic sample */
781 g_compressibility = (double)readU32FromChar(&argument) / 100.;
785 cLevel = readU32FromChar(&argument);
786 cparams = ZSTD_getCParams(cLevel, 0, 0);
789 /* Unknown command */
790 default : return badusage(exename);
796 /* first provided filename is input */
797 if (!input_filename) { input_filename=argument; filenamesStart=argNb; continue; }
802 if (filenamesStart==0) /* no input file */
803 result = benchSample(benchNb, cLevel, cparams);
805 result = benchFiles(benchNb, argv+filenamesStart, argc-filenamesStart, cLevel, cparams);
807 if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }