]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/zstd/tests/fullbench.c
MFV r333789: libpcap 1.9.0 (pre-release)
[FreeBSD/FreeBSD.git] / sys / contrib / zstd / tests / fullbench.c
1 /*
2  * Copyright (c) 2015-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
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.
9  */
10
11
12 /*_************************************
13 *  Includes
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 */
19
20 #include "mem.h"         /* U32 */
21 #ifndef ZSTD_DLL_IMPORT
22     #include "zstd_internal.h"   /* ZSTD_blockHeaderSize, blockType_e, KB, MB */
23 #else
24     #define KB *(1 <<10)
25     #define MB *(1 <<20)
26     #define GB *(1U<<30)
27     typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
28 #endif
29 #define ZSTD_STATIC_LINKING_ONLY  /* ZSTD_compressBegin, ZSTD_compressContinue, etc. */
30 #include "zstd.h"        /* ZSTD_versionString */
31 #include "util.h"        /* time functions */
32 #include "datagen.h"
33
34
35 /*_************************************
36 *  Constants
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__
41
42 #define NBLOOPS    6
43 #define TIMELOOP_S 2
44
45 #define KNUTH      2654435761U
46 #define MAX_MEM    (1984 MB)
47
48 #define COMPRESSIBILITY_DEFAULT 0.50
49 static const size_t g_sampleSize = 10000000;
50
51
52 /*_************************************
53 *  Macros
54 **************************************/
55 #define DISPLAY(...)  fprintf(stderr, __VA_ARGS__)
56
57
58 /*_************************************
59 *  Benchmark Parameters
60 **************************************/
61 static U32 g_nbIterations = NBLOOPS;
62 static double g_compressibility = COMPRESSIBILITY_DEFAULT;
63
64 static void BMK_SetNbIterations(U32 nbLoops)
65 {
66     g_nbIterations = nbLoops;
67     DISPLAY("- %i iterations -\n", g_nbIterations);
68 }
69
70
71 /*_*******************************************************
72 *  Private functions
73 *********************************************************/
74 static size_t BMK_findMaxMem(U64 requiredMem)
75 {
76     size_t const step = 64 MB;
77     void* testmem = NULL;
78
79     requiredMem = (((requiredMem >> 26) + 1) << 26);
80     if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
81
82     requiredMem += step;
83     do {
84         testmem = malloc ((size_t)requiredMem);
85         requiredMem -= step;
86     } while (!testmem);
87
88     free (testmem);
89     return (size_t) requiredMem;
90 }
91
92
93 /*_*******************************************************
94 *  Benchmark wrappers
95 *********************************************************/
96 size_t local_ZSTD_compress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
97 {
98     (void)buff2;
99     return ZSTD_compress(dst, dstSize, src, srcSize, 1);
100 }
101
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)
104 {
105     (void)src; (void)srcSize;
106     return ZSTD_decompress(dst, dstSize, buff2, g_cSize);
107 }
108
109 static ZSTD_DCtx* g_zdc = NULL;
110
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)
114 {
115     (void)src; (void)srcSize; (void)dst; (void)dstSize;
116     return ZSTD_decodeLiteralsBlock((ZSTD_DCtx*)g_zdc, buff2, g_cSize);
117 }
118
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)
121 {
122     int nbSeq;
123     (void)src; (void)srcSize; (void)dst; (void)dstSize;
124     return ZSTD_decodeSeqHeaders(g_zdc, &nbSeq, buff2, g_cSize);
125 }
126 #endif
127
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)
130 {
131     ZSTD_outBuffer buffOut;
132     ZSTD_inBuffer buffIn;
133     (void)buff2;
134     ZSTD_initCStream(g_cstream, 1);
135     buffOut.dst = dst;
136     buffOut.size = dstCapacity;
137     buffOut.pos = 0;
138     buffIn.src = src;
139     buffIn.size = srcSize;
140     buffIn.pos = 0;
141     ZSTD_compressStream(g_cstream, &buffOut, &buffIn);
142     ZSTD_endStream(g_cstream, &buffOut);
143     return buffOut.pos;
144 }
145
146 static size_t local_ZSTD_compress_generic_end(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
147 {
148     ZSTD_outBuffer buffOut;
149     ZSTD_inBuffer buffIn;
150     (void)buff2;
151     ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
152     buffOut.dst = dst;
153     buffOut.size = dstCapacity;
154     buffOut.pos = 0;
155     buffIn.src = src;
156     buffIn.size = srcSize;
157     buffIn.pos = 0;
158     ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
159     return buffOut.pos;
160 }
161
162 static size_t local_ZSTD_compress_generic_continue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
163 {
164     ZSTD_outBuffer buffOut;
165     ZSTD_inBuffer buffIn;
166     (void)buff2;
167     ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
168     buffOut.dst = dst;
169     buffOut.size = dstCapacity;
170     buffOut.pos = 0;
171     buffIn.src = src;
172     buffIn.size = srcSize;
173     buffIn.pos = 0;
174     ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
175     ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
176     return buffOut.pos;
177 }
178
179 static size_t local_ZSTD_compress_generic_T2_end(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
180 {
181     ZSTD_outBuffer buffOut;
182     ZSTD_inBuffer buffIn;
183     (void)buff2;
184     ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
185     ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_nbWorkers, 2);
186     buffOut.dst = dst;
187     buffOut.size = dstCapacity;
188     buffOut.pos = 0;
189     buffIn.src = src;
190     buffIn.size = srcSize;
191     buffIn.pos = 0;
192     while (ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {}
193     return buffOut.pos;
194 }
195
196 static size_t local_ZSTD_compress_generic_T2_continue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
197 {
198     ZSTD_outBuffer buffOut;
199     ZSTD_inBuffer buffIn;
200     (void)buff2;
201     ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
202     ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_nbWorkers, 2);
203     buffOut.dst = dst;
204     buffOut.size = dstCapacity;
205     buffOut.pos = 0;
206     buffIn.src = src;
207     buffIn.size = srcSize;
208     buffIn.pos = 0;
209     ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
210     while(ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {}
211     return buffOut.pos;
212 }
213
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)
216 {
217     ZSTD_outBuffer buffOut;
218     ZSTD_inBuffer buffIn;
219     (void)src; (void)srcSize;
220     ZSTD_initDStream(g_dstream);
221     buffOut.dst = dst;
222     buffOut.size = dstCapacity;
223     buffOut.pos = 0;
224     buffIn.src = buff2;
225     buffIn.size = g_cSize;
226     buffIn.pos = 0;
227     ZSTD_decompressStream(g_dstream, &buffOut, &buffIn);
228     return buffOut.pos;
229 }
230
231 static ZSTD_CCtx* g_zcc = NULL;
232
233 #ifndef ZSTD_DLL_IMPORT
234 size_t local_ZSTD_compressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
235 {
236     (void)buff2;
237     ZSTD_compressBegin(g_zcc, 1 /* compressionLevel */);
238     return ZSTD_compressEnd(g_zcc, dst, dstCapacity, src, srcSize);
239 }
240
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)
243 {
244     BYTE firstBlockBuf[FIRST_BLOCK_SIZE];
245
246     (void)buff2;
247     memcpy(firstBlockBuf, src, FIRST_BLOCK_SIZE);
248     ZSTD_compressBegin(g_zcc, 1);
249
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;
254     }
255     return ZSTD_compressEnd(g_zcc, dst, dstCapacity, (const BYTE*)src + FIRST_BLOCK_SIZE, srcSize - FIRST_BLOCK_SIZE);
256 }
257
258 size_t local_ZSTD_decompressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
259 {
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;
265
266     (void)src; (void)srcSize;
267     ZSTD_decompressBegin(g_zdc);
268     while (ip < iend) {
269         size_t const iSize = ZSTD_nextSrcSizeToDecompress(g_zdc);
270         size_t const decodedSize = ZSTD_decompressContinue(g_zdc, op, remainingCapacity, ip, iSize);
271         ip += iSize;
272         regeneratedSize += decodedSize;
273         op += decodedSize;
274         remainingCapacity -= decodedSize;
275     }
276
277     return regeneratedSize;
278 }
279 #endif
280
281
282 /*_*******************************************************
283 *  Bench functions
284 *********************************************************/
285 static size_t benchMem(const void* src, size_t srcSize, U32 benchNb)
286 {
287     BYTE*  dstBuff;
288     size_t const dstBuffSize = ZSTD_compressBound(srcSize);
289     void*  buff2;
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.;
293
294     /* Selection */
295     switch(benchNb)
296     {
297     case 1:
298         benchFunction = local_ZSTD_compress; benchName = "compress(1)";
299         break;
300     case 2:
301         benchFunction = local_ZSTD_decompress; benchName = "decompress";
302         break;
303 #ifndef ZSTD_DLL_IMPORT
304     case 11:
305         benchFunction = local_ZSTD_compressContinue; benchName = "compressContinue(1)";
306         break;
307     case 12:
308         benchFunction = local_ZSTD_compressContinue_extDict; benchName = "compressContinue_extDict";
309         break;
310     case 13:
311         benchFunction = local_ZSTD_decompressContinue; benchName = "decompressContinue";
312         break;
313     case 31:
314         benchFunction = local_ZSTD_decodeLiteralsBlock; benchName = "decodeLiteralsBlock";
315         break;
316     case 32:
317         benchFunction = local_ZSTD_decodeSeqHeaders; benchName = "decodeSeqHeaders";
318         break;
319 #endif
320     case 41:
321         benchFunction = local_ZSTD_compressStream; benchName = "compressStream(1)";
322         break;
323     case 42:
324         benchFunction = local_ZSTD_decompressStream; benchName = "decompressStream";
325         break;
326     case 51:
327         benchFunction = local_ZSTD_compress_generic_continue; benchName = "compress_generic, continue";
328         break;
329     case 52:
330         benchFunction = local_ZSTD_compress_generic_end; benchName = "compress_generic, end";
331         break;
332     case 61:
333         benchFunction = local_ZSTD_compress_generic_T2_continue; benchName = "compress_generic, -T2, continue";
334         break;
335     case 62:
336         benchFunction = local_ZSTD_compress_generic_T2_end; benchName = "compress_generic, -T2, end";
337         break;
338     default :
339         return 0;
340     }
341
342     /* Allocation */
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);
348         return 12;
349     }
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();
354
355     /* Preparation */
356     switch(benchNb)
357     {
358     case 2:
359         g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
360         break;
361 #ifndef ZSTD_DLL_IMPORT
362     case 13 :
363         g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
364         break;
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");
375                 goto _cleanOut;
376             }
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);
381             break;
382         }
383     case 32:   /* ZSTD_decodeSeqHeaders */
384         {   blockProperties_t bp;
385             ZSTD_frameHeader zfp;
386             const BYTE* ip = dstBuff;
387             const BYTE* iend;
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");
397                 goto _cleanOut;
398             }
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 */
403             g_cSize = iend-ip;
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 */
406             break;
407         }
408 #else
409     case 31:
410         goto _cleanOut;
411 #endif
412     case 42 :
413         g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
414         break;
415
416     /* test functions */
417     /* convention: test functions have ID > 100 */
418
419     default : ;
420     }
421
422      /* warming up memory */
423     { size_t i; for (i=0; i<dstBuffSize; i++) dstBuff[i]=(BYTE)i; }
424
425     /* benchmark loop */
426     {   U32 loopNb;
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;
434             U32 roundNb;
435
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));
443                     exit(1);
444             }   }
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",
452                             loopNb, benchName,
453                             (double)srcSize / (1 MB) / bestTime,
454                             (U32)benchResult);
455                 } else {
456                     assert(nbRounds < 40000000);  /* avoid overflow */
457                     nbRounds *= 100;
458                 }
459     }   }   }
460     DISPLAY("%2u\n", benchNb);
461
462 _cleanOut:
463     free(dstBuff);
464     free(buff2);
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;
469     return 0;
470 }
471
472
473 static int benchSample(U32 benchNb)
474 {
475     size_t const benchedSize = g_sampleSize;
476     const char* name = "Sample 10MiB";
477
478     /* Allocation */
479     void* origBuff = malloc(benchedSize);
480     if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; }
481
482     /* Fill buffer */
483     RDG_genBuffer(origBuff, benchedSize, g_compressibility, 0.0, 0);
484
485     /* bench */
486     DISPLAY("\r%79s\r", "");
487     DISPLAY(" %s : \n", name);
488     if (benchNb)
489         benchMem(origBuff, benchedSize, benchNb);
490     else
491         for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb);
492
493     free(origBuff);
494     return 0;
495 }
496
497
498 static int benchFiles(const char** fileNamesTable, const int nbFiles, U32 benchNb)
499 {
500     /* Loop for each file */
501     int fileIdx;
502     for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
503         const char* const inFileName = fileNamesTable[fileIdx];
504         FILE* const inFile = fopen( inFileName, "rb" );
505         U64   inFileSize;
506         size_t benchedSize;
507         void* origBuff;
508
509         /* Check file existence */
510         if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
511
512         /* Memory allocation & restrictions */
513         inFileSize = UTIL_getFileSize(inFileName);
514         if (inFileSize == UTIL_FILESIZE_UNKNOWN) {
515             DISPLAY( "Cannot measure size of %s\n", inFileName);
516             fclose(inFile);
517             return 11;
518         }
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));
523
524         /* Alloc */
525         origBuff = malloc(benchedSize);
526         if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; }
527
528         /* Fill input buffer */
529         DISPLAY("Loading %s...       \r", inFileName);
530         {
531             size_t readSize = fread(origBuff, 1, benchedSize, inFile);
532             fclose(inFile);
533             if (readSize != benchedSize) {
534                 DISPLAY("\nError: problem reading file '%s' !!    \n", inFileName);
535                 free(origBuff);
536                 return 13;
537         }   }
538
539         /* bench */
540         DISPLAY("\r%79s\r", "");
541         DISPLAY(" %s : \n", inFileName);
542         if (benchNb)
543             benchMem(origBuff, benchedSize, benchNb);
544         else
545             for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb);
546
547         free(origBuff);
548     }
549
550     return 0;
551 }
552
553
554 static int usage(const char* exename)
555 {
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");
560     return 0;
561 }
562
563 static int usage_advanced(const char* exename)
564 {
565     usage(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);
570     return 0;
571 }
572
573 static int badusage(const char* exename)
574 {
575     DISPLAY("Wrong parameters\n");
576     usage(exename);
577     return 1;
578 }
579
580 int main(int argc, const char** argv)
581 {
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;
586
587     DISPLAY(WELCOME_MESSAGE);
588     if (argc<1) return badusage(exename);
589
590     for(i=1; i<argc; i++) {
591         const char* argument = argv[i];
592         assert(argument != NULL);
593
594         /* Commands (note : aggregated commands are allowed) */
595         if (argument[0]=='-') {
596
597             while (argument[1]!=0) {
598                 argument++;
599
600                 switch(argument[0])
601                 {
602                     /* Display help on usage */
603                 case 'h':
604                 case 'H': return usage_advanced(exename);
605
606                     /* Pause at the end (hidden option) */
607                 case 'p': main_pause = 1; break;
608
609                     /* Select specific algorithm to bench */
610                 case 'b':
611                     benchNb = 0;
612                     while ((argument[1]>= '0') && (argument[1]<= '9')) {
613                         benchNb *= 10;
614                         benchNb += argument[1] - '0';
615                         argument++;
616                     }
617                     break;
618
619                     /* Modify Nb Iterations */
620                 case 'i':
621                     if ((argument[1] >='0') && (argument[1] <='9')) {
622                         int iters = argument[1] - '0';
623                         BMK_SetNbIterations(iters);
624                         argument++;
625                     }
626                     break;
627
628                     /* Select compressibility of synthetic sample */
629                 case 'P':
630                     {   U32 proba32 = 0;
631                         while ((argument[1]>= '0') && (argument[1]<= '9')) {
632                             proba32 *= 10;
633                             proba32 += argument[1] - '0';
634                             argument++;
635                         }
636                         g_compressibility = (double)proba32 / 100.;
637                     }
638                     break;
639
640                     /* Unknown command */
641                 default : return badusage(exename);
642                 }
643             }
644             continue;
645         }
646
647         /* first provided filename is input */
648         if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
649     }
650
651     if (filenamesStart==0)   /* no input file */
652         result = benchSample(benchNb);
653     else
654         result = benchFiles(argv+filenamesStart, argc-filenamesStart, benchNb);
655
656     if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }
657
658     return result;
659 }