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_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 */
35 /*_************************************
37 **************************************/
38 #define PROGRAM_DESCRIPTION "Zstandard speed analyzer"
39 #define AUTHOR "Yann Collet"
40 #define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, ZSTD_versionString(), (int)(sizeof(void*)*8), AUTHOR, __DATE__
45 #define KNUTH 2654435761U
46 #define MAX_MEM (1984 MB)
48 #define COMPRESSIBILITY_DEFAULT 0.50
49 static const size_t g_sampleSize = 10000000;
52 /*_************************************
54 **************************************/
55 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
58 /*_************************************
59 * Benchmark Parameters
60 **************************************/
61 static U32 g_nbIterations = NBLOOPS;
62 static double g_compressibility = COMPRESSIBILITY_DEFAULT;
64 static void BMK_SetNbIterations(U32 nbLoops)
66 g_nbIterations = nbLoops;
67 DISPLAY("- %i iterations -\n", g_nbIterations);
71 /*_*******************************************************
73 *********************************************************/
74 static size_t BMK_findMaxMem(U64 requiredMem)
76 size_t const step = 64 MB;
79 requiredMem = (((requiredMem >> 26) + 1) << 26);
80 if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
84 testmem = malloc ((size_t)requiredMem);
89 return (size_t) requiredMem;
93 /*_*******************************************************
95 *********************************************************/
96 size_t local_ZSTD_compress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
99 return ZSTD_compress(dst, dstSize, src, srcSize, 1);
102 static size_t g_cSize = 0;
103 size_t local_ZSTD_decompress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
105 (void)src; (void)srcSize;
106 return ZSTD_decompress(dst, dstSize, buff2, g_cSize);
109 static ZSTD_DCtx* g_zdc = NULL;
111 #ifndef ZSTD_DLL_IMPORT
112 extern size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* ctx, const void* src, size_t srcSize);
113 size_t local_ZSTD_decodeLiteralsBlock(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
115 (void)src; (void)srcSize; (void)dst; (void)dstSize;
116 return ZSTD_decodeLiteralsBlock((ZSTD_DCtx*)g_zdc, buff2, g_cSize);
119 extern size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeq, const void* src, size_t srcSize);
120 size_t local_ZSTD_decodeSeqHeaders(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
123 (void)src; (void)srcSize; (void)dst; (void)dstSize;
124 return ZSTD_decodeSeqHeaders(g_zdc, &nbSeq, buff2, g_cSize);
128 static ZSTD_CStream* g_cstream= NULL;
129 size_t local_ZSTD_compressStream(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
131 ZSTD_outBuffer buffOut;
132 ZSTD_inBuffer buffIn;
134 ZSTD_initCStream(g_cstream, 1);
136 buffOut.size = dstCapacity;
139 buffIn.size = srcSize;
141 ZSTD_compressStream(g_cstream, &buffOut, &buffIn);
142 ZSTD_endStream(g_cstream, &buffOut);
146 static size_t local_ZSTD_compress_generic_end(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
148 ZSTD_outBuffer buffOut;
149 ZSTD_inBuffer buffIn;
151 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
153 buffOut.size = dstCapacity;
156 buffIn.size = srcSize;
158 ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
162 static size_t local_ZSTD_compress_generic_continue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
164 ZSTD_outBuffer buffOut;
165 ZSTD_inBuffer buffIn;
167 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
169 buffOut.size = dstCapacity;
172 buffIn.size = srcSize;
174 ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
175 ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
179 static size_t local_ZSTD_compress_generic_T2_end(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
181 ZSTD_outBuffer buffOut;
182 ZSTD_inBuffer buffIn;
184 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
185 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_nbWorkers, 2);
187 buffOut.size = dstCapacity;
190 buffIn.size = srcSize;
192 while (ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {}
196 static size_t local_ZSTD_compress_generic_T2_continue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
198 ZSTD_outBuffer buffOut;
199 ZSTD_inBuffer buffIn;
201 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
202 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_nbWorkers, 2);
204 buffOut.size = dstCapacity;
207 buffIn.size = srcSize;
209 ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
210 while(ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {}
214 static ZSTD_DStream* g_dstream= NULL;
215 static size_t local_ZSTD_decompressStream(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
217 ZSTD_outBuffer buffOut;
218 ZSTD_inBuffer buffIn;
219 (void)src; (void)srcSize;
220 ZSTD_initDStream(g_dstream);
222 buffOut.size = dstCapacity;
225 buffIn.size = g_cSize;
227 ZSTD_decompressStream(g_dstream, &buffOut, &buffIn);
231 static ZSTD_CCtx* g_zcc = NULL;
233 #ifndef ZSTD_DLL_IMPORT
234 size_t local_ZSTD_compressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
237 ZSTD_compressBegin(g_zcc, 1 /* compressionLevel */);
238 return ZSTD_compressEnd(g_zcc, dst, dstCapacity, src, srcSize);
241 #define FIRST_BLOCK_SIZE 8
242 size_t local_ZSTD_compressContinue_extDict(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
244 BYTE firstBlockBuf[FIRST_BLOCK_SIZE];
247 memcpy(firstBlockBuf, src, FIRST_BLOCK_SIZE);
248 ZSTD_compressBegin(g_zcc, 1);
250 { size_t const compressResult = ZSTD_compressContinue(g_zcc, dst, dstCapacity, firstBlockBuf, FIRST_BLOCK_SIZE);
251 if (ZSTD_isError(compressResult)) { DISPLAY("local_ZSTD_compressContinue_extDict error : %s\n", ZSTD_getErrorName(compressResult)); return compressResult; }
252 dst = (BYTE*)dst + compressResult;
253 dstCapacity -= compressResult;
255 return ZSTD_compressEnd(g_zcc, dst, dstCapacity, (const BYTE*)src + FIRST_BLOCK_SIZE, srcSize - FIRST_BLOCK_SIZE);
258 size_t local_ZSTD_decompressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
260 size_t regeneratedSize = 0;
261 const BYTE* ip = (const BYTE*)buff2;
262 const BYTE* const iend = ip + g_cSize;
263 BYTE* op = (BYTE*)dst;
264 size_t remainingCapacity = dstCapacity;
266 (void)src; (void)srcSize;
267 ZSTD_decompressBegin(g_zdc);
269 size_t const iSize = ZSTD_nextSrcSizeToDecompress(g_zdc);
270 size_t const decodedSize = ZSTD_decompressContinue(g_zdc, op, remainingCapacity, ip, iSize);
272 regeneratedSize += decodedSize;
274 remainingCapacity -= decodedSize;
277 return regeneratedSize;
282 /*_*******************************************************
284 *********************************************************/
285 static size_t benchMem(const void* src, size_t srcSize, U32 benchNb)
288 size_t const dstBuffSize = ZSTD_compressBound(srcSize);
290 const char* benchName;
291 size_t (*benchFunction)(void* dst, size_t dstSize, void* verifBuff, const void* src, size_t srcSize);
292 double bestTime = 100000000.;
298 benchFunction = local_ZSTD_compress; benchName = "compress(1)";
301 benchFunction = local_ZSTD_decompress; benchName = "decompress";
303 #ifndef ZSTD_DLL_IMPORT
305 benchFunction = local_ZSTD_compressContinue; benchName = "compressContinue(1)";
308 benchFunction = local_ZSTD_compressContinue_extDict; benchName = "compressContinue_extDict";
311 benchFunction = local_ZSTD_decompressContinue; benchName = "decompressContinue";
314 benchFunction = local_ZSTD_decodeLiteralsBlock; benchName = "decodeLiteralsBlock";
317 benchFunction = local_ZSTD_decodeSeqHeaders; benchName = "decodeSeqHeaders";
321 benchFunction = local_ZSTD_compressStream; benchName = "compressStream(1)";
324 benchFunction = local_ZSTD_decompressStream; benchName = "decompressStream";
327 benchFunction = local_ZSTD_compress_generic_continue; benchName = "compress_generic, continue";
330 benchFunction = local_ZSTD_compress_generic_end; benchName = "compress_generic, end";
333 benchFunction = local_ZSTD_compress_generic_T2_continue; benchName = "compress_generic, -T2, continue";
336 benchFunction = local_ZSTD_compress_generic_T2_end; benchName = "compress_generic, -T2, end";
343 dstBuff = (BYTE*)malloc(dstBuffSize);
344 buff2 = malloc(dstBuffSize);
345 if ((!dstBuff) || (!buff2)) {
346 DISPLAY("\nError: not enough memory!\n");
347 free(dstBuff); free(buff2);
350 if (g_zcc==NULL) g_zcc = ZSTD_createCCtx();
351 if (g_zdc==NULL) g_zdc = ZSTD_createDCtx();
352 if (g_cstream==NULL) g_cstream = ZSTD_createCStream();
353 if (g_dstream==NULL) g_dstream = ZSTD_createDStream();
359 g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
361 #ifndef ZSTD_DLL_IMPORT
363 g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
365 case 31: /* ZSTD_decodeLiteralsBlock */
366 { blockProperties_t bp;
367 ZSTD_frameHeader zfp;
368 size_t frameHeaderSize, skippedSize;
369 g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1);
370 frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_frameHeaderSize_min);
371 if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min;
372 ZSTD_getcBlockSize(dstBuff+frameHeaderSize, dstBuffSize, &bp); /* Get 1st block type */
373 if (bp.blockType != bt_compressed) {
374 DISPLAY("ZSTD_decodeLiteralsBlock : impossible to test on this sample (not compressible)\n");
377 skippedSize = frameHeaderSize + ZSTD_blockHeaderSize;
378 memcpy(buff2, dstBuff+skippedSize, g_cSize-skippedSize);
379 srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
380 ZSTD_decompressBegin(g_zdc);
383 case 32: /* ZSTD_decodeSeqHeaders */
384 { blockProperties_t bp;
385 ZSTD_frameHeader zfp;
386 const BYTE* ip = dstBuff;
388 size_t frameHeaderSize, cBlockSize;
389 ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); /* it would be better to use direct block compression here */
390 g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1);
391 frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_frameHeaderSize_min);
392 if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min;
393 ip += frameHeaderSize; /* Skip frame Header */
394 cBlockSize = ZSTD_getcBlockSize(ip, dstBuffSize, &bp); /* Get 1st block type */
395 if (bp.blockType != bt_compressed) {
396 DISPLAY("ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible)\n");
399 iend = ip + ZSTD_blockHeaderSize + cBlockSize; /* End of first block */
400 ip += ZSTD_blockHeaderSize; /* skip block header */
401 ZSTD_decompressBegin(g_zdc);
402 ip += ZSTD_decodeLiteralsBlock(g_zdc, ip, iend-ip); /* skip literal segment */
404 memcpy(buff2, ip, g_cSize); /* copy rest of block (it starts by SeqHeader) */
405 srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
413 g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
417 /* convention: test functions have ID > 100 */
422 /* warming up memory */
423 { size_t i; for (i=0; i<dstBuffSize; i++) dstBuff[i]=(BYTE)i; }
427 U32 nbRounds = (U32)((50 MB) / (srcSize+1)) + 1; /* initial conservative speed estimate */
428 # define TIME_SEC_MICROSEC (1*1000000ULL) /* 1 second */
429 # define TIME_SEC_NANOSEC (1*1000000000ULL) /* 1 second */
430 DISPLAY("%2i- %-30.30s : \r", benchNb, benchName);
431 for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
432 UTIL_time_t clockStart;
433 size_t benchResult=0;
436 UTIL_sleepMilli(5); /* give processor time to other processes */
437 UTIL_waitForNextTick();
438 clockStart = UTIL_getTime();
439 for (roundNb=0; roundNb < nbRounds; roundNb++) {
440 benchResult = benchFunction(dstBuff, dstBuffSize, buff2, src, srcSize);
441 if (ZSTD_isError(benchResult)) {
442 DISPLAY("ERROR ! %s() => %s !! \n", benchName, ZSTD_getErrorName(benchResult));
445 { U64 const clockSpanNano = UTIL_clockSpanNano(clockStart);
446 double const averageTime = (double)clockSpanNano / TIME_SEC_NANOSEC / nbRounds;
447 if (clockSpanNano > 0) {
448 if (averageTime < bestTime) bestTime = averageTime;
449 assert(bestTime > (1./2000000000));
450 nbRounds = (U32)(1. / bestTime); /* aim for 1 sec */
451 DISPLAY("%2i- %-30.30s : %7.1f MB/s (%9u)\r",
453 (double)srcSize / (1 MB) / bestTime,
456 assert(nbRounds < 40000000); /* avoid overflow */
460 DISPLAY("%2u\n", benchNb);
465 ZSTD_freeCCtx(g_zcc); g_zcc=NULL;
466 ZSTD_freeDCtx(g_zdc); g_zdc=NULL;
467 ZSTD_freeCStream(g_cstream); g_cstream=NULL;
468 ZSTD_freeDStream(g_dstream); g_dstream=NULL;
473 static int benchSample(U32 benchNb)
475 size_t const benchedSize = g_sampleSize;
476 const char* name = "Sample 10MiB";
479 void* origBuff = malloc(benchedSize);
480 if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; }
483 RDG_genBuffer(origBuff, benchedSize, g_compressibility, 0.0, 0);
486 DISPLAY("\r%79s\r", "");
487 DISPLAY(" %s : \n", name);
489 benchMem(origBuff, benchedSize, benchNb);
491 for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb);
498 static int benchFiles(const char** fileNamesTable, const int nbFiles, U32 benchNb)
500 /* Loop for each file */
502 for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
503 const char* const inFileName = fileNamesTable[fileIdx];
504 FILE* const inFile = fopen( inFileName, "rb" );
509 /* Check file existence */
510 if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
512 /* Memory allocation & restrictions */
513 inFileSize = UTIL_getFileSize(inFileName);
514 if (inFileSize == UTIL_FILESIZE_UNKNOWN) {
515 DISPLAY( "Cannot measure size of %s\n", inFileName);
519 benchedSize = BMK_findMaxMem(inFileSize*3) / 3;
520 if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
521 if (benchedSize < inFileSize)
522 DISPLAY("Not enough memory for '%s' full size; testing %u MB only...\n", inFileName, (U32)(benchedSize>>20));
525 origBuff = malloc(benchedSize);
526 if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; }
528 /* Fill input buffer */
529 DISPLAY("Loading %s... \r", inFileName);
531 size_t readSize = fread(origBuff, 1, benchedSize, inFile);
533 if (readSize != benchedSize) {
534 DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
540 DISPLAY("\r%79s\r", "");
541 DISPLAY(" %s : \n", inFileName);
543 benchMem(origBuff, benchedSize, benchNb);
545 for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb);
554 static int usage(const char* exename)
556 DISPLAY( "Usage :\n");
557 DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename);
558 DISPLAY( "Arguments :\n");
559 DISPLAY( " -H/-h : Help (this text + advanced options)\n");
563 static int usage_advanced(const char* exename)
566 DISPLAY( "\nAdvanced options :\n");
567 DISPLAY( " -b# : test only function # \n");
568 DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS);
569 DISPLAY( " -P# : sample compressibility (default : %.1f%%)\n", COMPRESSIBILITY_DEFAULT * 100);
573 static int badusage(const char* exename)
575 DISPLAY("Wrong parameters\n");
580 int main(int argc, const char** argv)
582 int i, filenamesStart=0, result;
583 const char* exename = argv[0];
584 const char* input_filename = NULL;
585 U32 benchNb = 0, main_pause = 0;
587 DISPLAY(WELCOME_MESSAGE);
588 if (argc<1) return badusage(exename);
590 for(i=1; i<argc; i++) {
591 const char* argument = argv[i];
592 assert(argument != NULL);
594 /* Commands (note : aggregated commands are allowed) */
595 if (argument[0]=='-') {
597 while (argument[1]!=0) {
602 /* Display help on usage */
604 case 'H': return usage_advanced(exename);
606 /* Pause at the end (hidden option) */
607 case 'p': main_pause = 1; break;
609 /* Select specific algorithm to bench */
612 while ((argument[1]>= '0') && (argument[1]<= '9')) {
614 benchNb += argument[1] - '0';
619 /* Modify Nb Iterations */
621 if ((argument[1] >='0') && (argument[1] <='9')) {
622 int iters = argument[1] - '0';
623 BMK_SetNbIterations(iters);
628 /* Select compressibility of synthetic sample */
631 while ((argument[1]>= '0') && (argument[1]<= '9')) {
633 proba32 += argument[1] - '0';
636 g_compressibility = (double)proba32 / 100.;
640 /* Unknown command */
641 default : return badusage(exename);
647 /* first provided filename is input */
648 if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
651 if (filenamesStart==0) /* no input file */
652 result = benchSample(benchNb);
654 result = benchFiles(argv+filenamesStart, argc-filenamesStart, benchNb);
656 if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }