]> git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/tests/fullbench.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / 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
19 #include "mem.h" /* U32 */
20 #ifndef ZSTD_DLL_IMPORT
21 #include "zstd_internal.h" /* ZSTD_blockHeaderSize, blockType_e, KB, MB */
22 #else
23 #define KB *(1 <<10)
24 #define MB *(1 <<20)
25 #define GB *(1U<<30)
26 typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
27 #endif
28 #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressBegin, ZSTD_compressContinue, etc. */
29 #include "zstd.h" /* ZSTD_versionString */
30 #include "util.h" /* time functions */
31 #include "datagen.h"
32
33
34 /*_************************************
35 * Constants
36 **************************************/
37 #define PROGRAM_DESCRIPTION "Zstandard speed analyzer"
38 #define AUTHOR "Yann Collet"
39 #define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, ZSTD_versionString(), (int)(sizeof(void*)*8), AUTHOR, __DATE__
40
41 #define NBLOOPS 6
42 #define TIMELOOP_S 2
43
44 #define KNUTH 2654435761U
45 #define MAX_MEM (1984 MB)
46
47 #define COMPRESSIBILITY_DEFAULT 0.50
48 static const size_t g_sampleSize = 10000000;
49
50
51 /*_************************************
52 * Macros
53 **************************************/
54 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
55
56
57 /*_************************************
58 * Benchmark Parameters
59 **************************************/
60 static U32 g_nbIterations = NBLOOPS;
61 static double g_compressibility = COMPRESSIBILITY_DEFAULT;
62
63 static void BMK_SetNbIterations(U32 nbLoops)
64 {
65 g_nbIterations = nbLoops;
66 DISPLAY("- %i iterations -\n", g_nbIterations);
67 }
68
69
70 /*_*******************************************************
71 * Private functions
72 *********************************************************/
73 static size_t BMK_findMaxMem(U64 requiredMem)
74 {
75 size_t const step = 64 MB;
76 void* testmem = NULL;
77
78 requiredMem = (((requiredMem >> 26) + 1) << 26);
79 if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
80
81 requiredMem += step;
82 do {
83 testmem = malloc ((size_t)requiredMem);
84 requiredMem -= step;
85 } while (!testmem);
86
87 free (testmem);
88 return (size_t) requiredMem;
89 }
90
91
92 /*_*******************************************************
93 * Benchmark wrappers
94 *********************************************************/
95 size_t local_ZSTD_compress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
96 {
97 (void)buff2;
98 return ZSTD_compress(dst, dstSize, src, srcSize, 1);
99 }
100
101 static size_t g_cSize = 0;
102 size_t local_ZSTD_decompress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
103 {
104 (void)src; (void)srcSize;
105 return ZSTD_decompress(dst, dstSize, buff2, g_cSize);
106 }
107
108 static ZSTD_DCtx* g_zdc = NULL;
109
110 #ifndef ZSTD_DLL_IMPORT
111 extern size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* ctx, const void* src, size_t srcSize);
112 size_t local_ZSTD_decodeLiteralsBlock(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
113 {
114 (void)src; (void)srcSize; (void)dst; (void)dstSize;
115 return ZSTD_decodeLiteralsBlock((ZSTD_DCtx*)g_zdc, buff2, g_cSize);
116 }
117
118 extern size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeq, const void* src, size_t srcSize);
119 size_t local_ZSTD_decodeSeqHeaders(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
120 {
121 int nbSeq;
122 (void)src; (void)srcSize; (void)dst; (void)dstSize;
123 return ZSTD_decodeSeqHeaders(g_zdc, &nbSeq, buff2, g_cSize);
124 }
125 #endif
126
127 static ZSTD_CStream* g_cstream= NULL;
128 size_t local_ZSTD_compressStream(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
129 {
130 ZSTD_outBuffer buffOut;
131 ZSTD_inBuffer buffIn;
132 (void)buff2;
133 ZSTD_initCStream(g_cstream, 1);
134 buffOut.dst = dst;
135 buffOut.size = dstCapacity;
136 buffOut.pos = 0;
137 buffIn.src = src;
138 buffIn.size = srcSize;
139 buffIn.pos = 0;
140 ZSTD_compressStream(g_cstream, &buffOut, &buffIn);
141 ZSTD_endStream(g_cstream, &buffOut);
142 return buffOut.pos;
143 }
144
145 static size_t local_ZSTD_compress_generic_end(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
146 {
147 ZSTD_outBuffer buffOut;
148 ZSTD_inBuffer buffIn;
149 (void)buff2;
150 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
151 buffOut.dst = dst;
152 buffOut.size = dstCapacity;
153 buffOut.pos = 0;
154 buffIn.src = src;
155 buffIn.size = srcSize;
156 buffIn.pos = 0;
157 ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
158 return buffOut.pos;
159 }
160
161 static size_t local_ZSTD_compress_generic_continue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
162 {
163 ZSTD_outBuffer buffOut;
164 ZSTD_inBuffer buffIn;
165 (void)buff2;
166 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
167 buffOut.dst = dst;
168 buffOut.size = dstCapacity;
169 buffOut.pos = 0;
170 buffIn.src = src;
171 buffIn.size = srcSize;
172 buffIn.pos = 0;
173 ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
174 ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
175 return buffOut.pos;
176 }
177
178 static size_t local_ZSTD_compress_generic_T2_end(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
179 {
180 ZSTD_outBuffer buffOut;
181 ZSTD_inBuffer buffIn;
182 (void)buff2;
183 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
184 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_nbThreads, 2);
185 buffOut.dst = dst;
186 buffOut.size = dstCapacity;
187 buffOut.pos = 0;
188 buffIn.src = src;
189 buffIn.size = srcSize;
190 buffIn.pos = 0;
191 while (ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {}
192 return buffOut.pos;
193 }
194
195 static size_t local_ZSTD_compress_generic_T2_continue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
196 {
197 ZSTD_outBuffer buffOut;
198 ZSTD_inBuffer buffIn;
199 (void)buff2;
200 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
201 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_nbThreads, 2);
202 buffOut.dst = dst;
203 buffOut.size = dstCapacity;
204 buffOut.pos = 0;
205 buffIn.src = src;
206 buffIn.size = srcSize;
207 buffIn.pos = 0;
208 ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
209 while(ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {}
210 return buffOut.pos;
211 }
212
213 static ZSTD_DStream* g_dstream= NULL;
214 static size_t local_ZSTD_decompressStream(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
215 {
216 ZSTD_outBuffer buffOut;
217 ZSTD_inBuffer buffIn;
218 (void)src; (void)srcSize;
219 ZSTD_initDStream(g_dstream);
220 buffOut.dst = dst;
221 buffOut.size = dstCapacity;
222 buffOut.pos = 0;
223 buffIn.src = buff2;
224 buffIn.size = g_cSize;
225 buffIn.pos = 0;
226 ZSTD_decompressStream(g_dstream, &buffOut, &buffIn);
227 return buffOut.pos;
228 }
229
230 static ZSTD_CCtx* g_zcc = NULL;
231
232 #ifndef ZSTD_DLL_IMPORT
233 size_t local_ZSTD_compressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
234 {
235 (void)buff2;
236 ZSTD_compressBegin(g_zcc, 1);
237 return ZSTD_compressEnd(g_zcc, dst, dstCapacity, src, srcSize);
238 }
239
240 #define FIRST_BLOCK_SIZE 8
241 size_t local_ZSTD_compressContinue_extDict(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
242 {
243 BYTE firstBlockBuf[FIRST_BLOCK_SIZE];
244
245 (void)buff2;
246 memcpy(firstBlockBuf, src, FIRST_BLOCK_SIZE);
247 ZSTD_compressBegin(g_zcc, 1);
248
249 { size_t const compressResult = ZSTD_compressContinue(g_zcc, dst, dstCapacity, firstBlockBuf, FIRST_BLOCK_SIZE);
250 if (ZSTD_isError(compressResult)) { DISPLAY("local_ZSTD_compressContinue_extDict error : %s\n", ZSTD_getErrorName(compressResult)); return compressResult; }
251 dst = (BYTE*)dst + compressResult;
252 dstCapacity -= compressResult;
253 }
254 return ZSTD_compressEnd(g_zcc, dst, dstCapacity, (const BYTE*)src + FIRST_BLOCK_SIZE, srcSize - FIRST_BLOCK_SIZE);
255 }
256
257 size_t local_ZSTD_decompressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
258 {
259 size_t regeneratedSize = 0;
260 const BYTE* ip = (const BYTE*)buff2;
261 const BYTE* const iend = ip + g_cSize;
262 BYTE* op = (BYTE*)dst;
263 size_t remainingCapacity = dstCapacity;
264
265 (void)src; (void)srcSize;
266 ZSTD_decompressBegin(g_zdc);
267 while (ip < iend) {
268 size_t const iSize = ZSTD_nextSrcSizeToDecompress(g_zdc);
269 size_t const decodedSize = ZSTD_decompressContinue(g_zdc, op, remainingCapacity, ip, iSize);
270 ip += iSize;
271 regeneratedSize += decodedSize;
272 op += decodedSize;
273 remainingCapacity -= decodedSize;
274 }
275
276 return regeneratedSize;
277 }
278 #endif
279
280
281 /*_*******************************************************
282 * Bench functions
283 *********************************************************/
284 static size_t benchMem(const void* src, size_t srcSize, U32 benchNb)
285 {
286 BYTE* dstBuff;
287 size_t const dstBuffSize = ZSTD_compressBound(srcSize);
288 void* buff2;
289 const char* benchName;
290 size_t (*benchFunction)(void* dst, size_t dstSize, void* verifBuff, const void* src, size_t srcSize);
291 double bestTime = 100000000.;
292
293 /* Selection */
294 switch(benchNb)
295 {
296 case 1:
297 benchFunction = local_ZSTD_compress; benchName = "compress(1)";
298 break;
299 case 2:
300 benchFunction = local_ZSTD_decompress; benchName = "decompress";
301 break;
302 #ifndef ZSTD_DLL_IMPORT
303 case 11:
304 benchFunction = local_ZSTD_compressContinue; benchName = "compressContinue(1)";
305 break;
306 case 12:
307 benchFunction = local_ZSTD_compressContinue_extDict; benchName = "compressContinue_extDict";
308 break;
309 case 13:
310 benchFunction = local_ZSTD_decompressContinue; benchName = "decompressContinue";
311 break;
312 case 31:
313 benchFunction = local_ZSTD_decodeLiteralsBlock; benchName = "decodeLiteralsBlock";
314 break;
315 case 32:
316 benchFunction = local_ZSTD_decodeSeqHeaders; benchName = "decodeSeqHeaders";
317 break;
318 #endif
319 case 41:
320 benchFunction = local_ZSTD_compressStream; benchName = "compressStream(1)";
321 break;
322 case 42:
323 benchFunction = local_ZSTD_decompressStream; benchName = "decompressStream";
324 break;
325 case 51:
326 benchFunction = local_ZSTD_compress_generic_continue; benchName = "compress_generic, continue";
327 break;
328 case 52:
329 benchFunction = local_ZSTD_compress_generic_end; benchName = "compress_generic, end";
330 break;
331 case 61:
332 benchFunction = local_ZSTD_compress_generic_T2_continue; benchName = "compress_generic, -T2, continue";
333 break;
334 case 62:
335 benchFunction = local_ZSTD_compress_generic_T2_end; benchName = "compress_generic, -T2, end";
336 break;
337 default :
338 return 0;
339 }
340
341 /* Allocation */
342 dstBuff = (BYTE*)malloc(dstBuffSize);
343 buff2 = malloc(dstBuffSize);
344 if ((!dstBuff) || (!buff2)) {
345 DISPLAY("\nError: not enough memory!\n");
346 free(dstBuff); free(buff2);
347 return 12;
348 }
349 if (g_zcc==NULL) g_zcc = ZSTD_createCCtx();
350 if (g_zdc==NULL) g_zdc = ZSTD_createDCtx();
351 if (g_cstream==NULL) g_cstream = ZSTD_createCStream();
352 if (g_dstream==NULL) g_dstream = ZSTD_createDStream();
353
354 /* Preparation */
355 switch(benchNb)
356 {
357 case 2:
358 g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
359 break;
360 #ifndef ZSTD_DLL_IMPORT
361 case 13 :
362 g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
363 break;
364 case 31: /* ZSTD_decodeLiteralsBlock */
365 { blockProperties_t bp;
366 ZSTD_frameHeader zfp;
367 size_t frameHeaderSize, skippedSize;
368 g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1);
369 frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_frameHeaderSize_min);
370 if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min;
371 ZSTD_getcBlockSize(dstBuff+frameHeaderSize, dstBuffSize, &bp); /* Get 1st block type */
372 if (bp.blockType != bt_compressed) {
373 DISPLAY("ZSTD_decodeLiteralsBlock : impossible to test on this sample (not compressible)\n");
374 goto _cleanOut;
375 }
376 skippedSize = frameHeaderSize + ZSTD_blockHeaderSize;
377 memcpy(buff2, dstBuff+skippedSize, g_cSize-skippedSize);
378 srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
379 ZSTD_decompressBegin(g_zdc);
380 break;
381 }
382 case 32: /* ZSTD_decodeSeqHeaders */
383 { blockProperties_t bp;
384 ZSTD_frameHeader zfp;
385 const BYTE* ip = dstBuff;
386 const BYTE* iend;
387 size_t frameHeaderSize, cBlockSize;
388 ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); /* it would be better to use direct block compression here */
389 g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1);
390 frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_frameHeaderSize_min);
391 if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min;
392 ip += frameHeaderSize; /* Skip frame Header */
393 cBlockSize = ZSTD_getcBlockSize(ip, dstBuffSize, &bp); /* Get 1st block type */
394 if (bp.blockType != bt_compressed) {
395 DISPLAY("ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible)\n");
396 goto _cleanOut;
397 }
398 iend = ip + ZSTD_blockHeaderSize + cBlockSize; /* End of first block */
399 ip += ZSTD_blockHeaderSize; /* skip block header */
400 ZSTD_decompressBegin(g_zdc);
401 ip += ZSTD_decodeLiteralsBlock(g_zdc, ip, iend-ip); /* skip literal segment */
402 g_cSize = iend-ip;
403 memcpy(buff2, ip, g_cSize); /* copy rest of block (it starts by SeqHeader) */
404 srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
405 break;
406 }
407 #else
408 case 31:
409 goto _cleanOut;
410 #endif
411 case 42 :
412 g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
413 break;
414
415 /* test functions */
416 /* by convention, test functions can be added > 100 */
417
418 default : ;
419 }
420
421 { size_t i; for (i=0; i<dstBuffSize; i++) dstBuff[i]=(BYTE)i; } /* warming up memory */
422
423 { U32 loopNb;
424 # define TIME_SEC_MICROSEC (1*1000000ULL) /* 1 second */
425 U64 const clockLoop = TIMELOOP_S * TIME_SEC_MICROSEC;
426 DISPLAY("%2i- %-30.30s : \r", benchNb, benchName);
427 for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
428 UTIL_time_t clockStart;
429 size_t benchResult=0;
430 U32 nbRounds;
431
432 UTIL_sleepMilli(1); /* give processor time to other processes */
433 UTIL_waitForNextTick();
434 clockStart = UTIL_getTime();
435 for (nbRounds=0; UTIL_clockSpanMicro(clockStart) < clockLoop; nbRounds++) {
436 benchResult = benchFunction(dstBuff, dstBuffSize, buff2, src, srcSize);
437 if (ZSTD_isError(benchResult)) { DISPLAY("ERROR ! %s() => %s !! \n", benchName, ZSTD_getErrorName(benchResult)); exit(1); }
438 }
439 { U64 const clockSpanMicro = UTIL_clockSpanMicro(clockStart);
440 double const averageTime = (double)clockSpanMicro / TIME_SEC_MICROSEC / nbRounds;
441 if (averageTime < bestTime) bestTime = averageTime;
442 DISPLAY("%2i- %-30.30s : %7.1f MB/s (%9u)\r", loopNb, benchName, (double)srcSize / (1 MB) / bestTime, (U32)benchResult);
443 } } }
444 DISPLAY("%2u\n", benchNb);
445
446 _cleanOut:
447 free(dstBuff);
448 free(buff2);
449 ZSTD_freeCCtx(g_zcc); g_zcc=NULL;
450 ZSTD_freeDCtx(g_zdc); g_zdc=NULL;
451 ZSTD_freeCStream(g_cstream); g_cstream=NULL;
452 ZSTD_freeDStream(g_dstream); g_dstream=NULL;
453 return 0;
454 }
455
456
457 static int benchSample(U32 benchNb)
458 {
459 size_t const benchedSize = g_sampleSize;
460 const char* name = "Sample 10MiB";
461
462 /* Allocation */
463 void* origBuff = malloc(benchedSize);
464 if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; }
465
466 /* Fill buffer */
467 RDG_genBuffer(origBuff, benchedSize, g_compressibility, 0.0, 0);
468
469 /* bench */
470 DISPLAY("\r%79s\r", "");
471 DISPLAY(" %s : \n", name);
472 if (benchNb)
473 benchMem(origBuff, benchedSize, benchNb);
474 else
475 for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb);
476
477 free(origBuff);
478 return 0;
479 }
480
481
482 static int benchFiles(const char** fileNamesTable, const int nbFiles, U32 benchNb)
483 {
484 /* Loop for each file */
485 int fileIdx;
486 for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
487 const char* inFileName = fileNamesTable[fileIdx];
488 FILE* inFile = fopen( inFileName, "rb" );
489 U64 inFileSize;
490 size_t benchedSize;
491 void* origBuff;
492
493 /* Check file existence */
494 if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
495
496 /* Memory allocation & restrictions */
497 inFileSize = UTIL_getFileSize(inFileName);
498 benchedSize = BMK_findMaxMem(inFileSize*3) / 3;
499 if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
500 if (benchedSize < inFileSize)
501 DISPLAY("Not enough memory for '%s' full size; testing %u MB only...\n", inFileName, (U32)(benchedSize>>20));
502
503 /* Alloc */
504 origBuff = malloc(benchedSize);
505 if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; }
506
507 /* Fill input buffer */
508 DISPLAY("Loading %s... \r", inFileName);
509 {
510 size_t readSize = fread(origBuff, 1, benchedSize, inFile);
511 fclose(inFile);
512 if (readSize != benchedSize) {
513 DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
514 free(origBuff);
515 return 13;
516 } }
517
518 /* bench */
519 DISPLAY("\r%79s\r", "");
520 DISPLAY(" %s : \n", inFileName);
521 if (benchNb)
522 benchMem(origBuff, benchedSize, benchNb);
523 else
524 for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb);
525
526 free(origBuff);
527 }
528
529 return 0;
530 }
531
532
533 static int usage(const char* exename)
534 {
535 DISPLAY( "Usage :\n");
536 DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename);
537 DISPLAY( "Arguments :\n");
538 DISPLAY( " -H/-h : Help (this text + advanced options)\n");
539 return 0;
540 }
541
542 static int usage_advanced(const char* exename)
543 {
544 usage(exename);
545 DISPLAY( "\nAdvanced options :\n");
546 DISPLAY( " -b# : test only function # \n");
547 DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS);
548 DISPLAY( " -P# : sample compressibility (default : %.1f%%)\n", COMPRESSIBILITY_DEFAULT * 100);
549 return 0;
550 }
551
552 static int badusage(const char* exename)
553 {
554 DISPLAY("Wrong parameters\n");
555 usage(exename);
556 return 1;
557 }
558
559 int main(int argc, const char** argv)
560 {
561 int i, filenamesStart=0, result;
562 const char* exename = argv[0];
563 const char* input_filename = NULL;
564 U32 benchNb = 0, main_pause = 0;
565
566 DISPLAY(WELCOME_MESSAGE);
567 if (argc<1) return badusage(exename);
568
569 for(i=1; i<argc; i++) {
570 const char* argument = argv[i];
571 if(!argument) continue; /* Protection if argument empty */
572
573 /* Commands (note : aggregated commands are allowed) */
574 if (argument[0]=='-') {
575
576 while (argument[1]!=0) {
577 argument++;
578
579 switch(argument[0])
580 {
581 /* Display help on usage */
582 case 'h':
583 case 'H': return usage_advanced(exename);
584
585 /* Pause at the end (hidden option) */
586 case 'p': main_pause = 1; break;
587
588 /* Select specific algorithm to bench */
589 case 'b':
590 benchNb = 0;
591 while ((argument[1]>= '0') && (argument[1]<= '9')) {
592 benchNb *= 10;
593 benchNb += argument[1] - '0';
594 argument++;
595 }
596 break;
597
598 /* Modify Nb Iterations */
599 case 'i':
600 if ((argument[1] >='0') && (argument[1] <='9')) {
601 int iters = argument[1] - '0';
602 BMK_SetNbIterations(iters);
603 argument++;
604 }
605 break;
606
607 /* Select compressibility of synthetic sample */
608 case 'P':
609 { U32 proba32 = 0;
610 while ((argument[1]>= '0') && (argument[1]<= '9')) {
611 proba32 *= 10;
612 proba32 += argument[1] - '0';
613 argument++;
614 }
615 g_compressibility = (double)proba32 / 100.;
616 }
617 break;
618
619 /* Unknown command */
620 default : return badusage(exename);
621 }
622 }
623 continue;
624 }
625
626 /* first provided filename is input */
627 if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
628 }
629
630 if (filenamesStart==0) /* no input file */
631 result = benchSample(benchNb);
632 else
633 result = benchFiles(argv+filenamesStart, argc-filenamesStart, benchNb);
634
635 if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }
636
637 return result;
638 }