From: Yann Collet Date: Thu, 17 Mar 2016 18:37:33 +0000 (+0100) Subject: bench : changed timer to clock_t, reduced timeloop to 1sec X-Git-Tag: v0.6.0^2~17^2~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=699b14db1bf58d99a4fc4fd7d9e8044255fd6607;p=thirdparty%2Fzstd.git bench : changed timer to clock_t, reduced timeloop to 1sec --- diff --git a/programs/bench.c b/programs/bench.c index 82e28d405..e819cc242 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -40,11 +40,6 @@ # define _LARGEFILE64_SOURCE #endif -/* S_ISREG & gettimeofday() are not supported by MSVC */ -#if defined(_MSC_VER) || defined(_WIN32) -# define BMK_LEGACY_TIMER 1 -#endif - /* ************************************* * Includes @@ -54,13 +49,7 @@ #include /* fprintf, fopen, ftello64 */ #include /* stat64 */ #include /* stat64 */ - -/* Use ftime() if gettimeofday() is not available */ -#if defined(BMK_LEGACY_TIMER) -# include /* timeb, ftime */ -#else -# include /* gettimeofday */ -#endif +#include /* clock_t, clock, CLOCKS_PER_SEC */ /* sleep : posix - windows - others */ #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) @@ -95,7 +84,7 @@ * Constants ***************************************/ #define NBLOOPS 3 -#define TIMELOOP_MS 2500 +#define TIMELOOP_S 1 #define ACTIVEPERIOD_S 70 #define COOLPERIOD_S 10 @@ -156,44 +145,11 @@ void BMK_SetBlockSize(size_t blockSize) /* ******************************************************** * Private functions **********************************************************/ - -#if defined(BMK_LEGACY_TIMER) - -static int BMK_GetMilliStart(void) +static clock_t BMK_clockSpan( clock_t clockStart ) { - /* Based on Legacy ftime() - * Rolls over every ~ 12.1 days (0x100000/24/60/60) - * Use GetMilliSpan to correct for rollover */ - struct timeb tb; - int nCount; - ftime( &tb ); - nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000); - return nCount; + return clock() - clockStart; /* works even if overflow, span limited to <= ~30mn */ } -#else - -static int BMK_GetMilliStart(void) -{ - /* Based on newer gettimeofday() - * Use GetMilliSpan to correct for rollover */ - struct timeval tv; - int nCount; - gettimeofday(&tv, NULL); - nCount = (int) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000); - return nCount; -} - -#endif - - -static int BMK_GetMilliSpan( int nTimeStart ) -{ - int nSpan = BMK_GetMilliStart() - nTimeStart; - if ( nSpan < 0 ) - nSpan += 0x100000 * 1000; - return nSpan; -} static U64 BMK_getFileSize(const char* infilename) { @@ -255,14 +211,14 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, /* Init blockTable data */ { - U32 fileNb; const char* srcPtr = (const char*)srcBuffer; char* cPtr = (char*)compressedBuffer; char* resPtr = (char*)resultBuffer; + U32 fileNb; for (fileNb=0; fileNb ACTIVEPERIOD_S*1000) { + if (BMK_clockSpan(coolTime) > ACTIVEPERIOD_S * CLOCKS_PER_SEC) { DISPLAY("\rcooling down ... \r"); BMK_sleep(COOLPERIOD_S); - coolTime = BMK_GetMilliStart(); + coolTime = clock(); } /* Compression */ DISPLAY("%2i-%-17.17s :%10u ->\r", loopNb, displayName, (U32)srcSize); - memset(compressedBuffer, 0xE5, maxCompressedSize); + memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */ nbLoops = 0; - milliTime = BMK_GetMilliStart(); - while (BMK_GetMilliStart() == milliTime); - milliTime = BMK_GetMilliStart(); - while (BMK_GetMilliSpan(milliTime) < TIMELOOP_MS) { + clockStart = clock(); + while (clock() == clockStart); + clockStart = clock(); + while (BMK_clockSpan(clockStart) < TIMELOOP_S * CLOCKS_PER_SEC) { ZSTD_compressBegin_advanced(refCtx, dictBuffer, dictBufferSize, ZSTD_getParams(cLevel, MAX(dictBufferSize, largestBlockSize))); for (blockNb=0; blockNb%10i (%5.3f),%6.1f MB/s\r", loopNb, displayName, (int)srcSize, (int)cSize, ratio, (double)srcSize / fastestC / 1000.); + DISPLAY("%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r", + loopNb, displayName, (U32)srcSize, (U32)cSize, ratio, + (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC) ); #if 1 /* Decompression */ memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */ nbLoops = 0; - milliTime = BMK_GetMilliStart(); - while (BMK_GetMilliStart() == milliTime); - milliTime = BMK_GetMilliStart(); + clockStart = clock(); + while (clock() == clockStart); + clockStart = clock(); - for ( ; BMK_GetMilliSpan(milliTime) < TIMELOOP_MS; nbLoops++) { + for ( ; BMK_clockSpan(clockStart) < TIMELOOP_S * CLOCKS_PER_SEC; nbLoops++) { ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize); for (blockNb=0; blockNb%10i (%5.3f),%6.1f MB/s ,%6.1f MB/s\r", loopNb, displayName, (int)srcSize, (int)cSize, ratio, (double)srcSize / fastestC / 1000., (double)srcSize / fastestD / 1000.); + clockSpan = BMK_clockSpan(clockStart); + if ((double)clockSpan < fastestD*nbLoops) fastestD = (double)clockSpan / nbLoops; + DISPLAY("%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r", + loopNb, displayName, (U32)srcSize, (U32)cSize, ratio, + (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC), + (double)srcSize / 1000000. / (fastestD / CLOCKS_PER_SEC) ); /* CRC Checking */ _findError: @@ -385,10 +346,7 @@ _findError: #endif } - if (crcOrig == crcCheck) - DISPLAY("%2i-%-17.17s :%10i ->%10i (%5.3f),%6.1f MB/s ,%6.1f MB/s \n", cLevel, displayName, (int)srcSize, (int)cSize, ratio, (double)srcSize / fastestC / 1000., (double)srcSize / fastestD / 1000.); - else - DISPLAY("%2i-\n", cLevel); + DISPLAY("%2i-\n", cLevel); } /* clean up */ @@ -424,7 +382,7 @@ static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize, const size_t* fileSizes, unsigned nbFiles, const void* dictBuffer, size_t dictBufferSize) { - if (cLevel < 0) { + if (cLevel < 0) { /* range mode : test all levels from 1 to l */ int l; for (l=1; l <= -cLevel; l++) { BMK_benchMem(srcBuffer, benchedSize, @@ -451,12 +409,11 @@ static U64 BMK_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles) static void BMK_loadFiles(void* buffer, size_t bufferSize, size_t* fileSizes, - const char** fileNamesTable, unsigned nbFiles) + const char** fileNamesTable, unsigned const nbFiles) { - BYTE* buff = (BYTE*)buffer; size_t pos = 0; - unsigned n; + unsigned n; for (n=0; n bufferSize-pos) fileSize = bufferSize-pos; - readSize = fread(buff+pos, 1, (size_t)fileSize, f); + readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f); if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]); pos += readSize; fileSizes[n] = (size_t)fileSize; @@ -548,7 +505,7 @@ static void BMK_syntheticTest(int cLevel, double compressibility) int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, const char* dictFileName, int cLevel) { - double compressibility = (double)g_compressibilityDefault / 100; + double const compressibility = (double)g_compressibilityDefault / 100; if (nbFiles == 0) BMK_syntheticTest(cLevel, compressibility); diff --git a/programs/fullbench.c b/programs/fullbench.c index b3edd5405..b4afcf112 100644 --- a/programs/fullbench.c +++ b/programs/fullbench.c @@ -37,11 +37,6 @@ # define _LARGEFILE64_SOURCE #endif -/* S_ISREG & gettimeofday() are not supported by MSVC */ -#if defined(_MSC_VER) || defined(_WIN32) -# define BMK_LEGACY_TIMER 1 -#endif - /*_************************************ * Includes @@ -103,13 +98,13 @@ static const size_t g_sampleSize = 10000000; /*_************************************ * Benchmark Parameters **************************************/ -static int nbIterations = NBLOOPS; +static U32 g_nbIterations = NBLOOPS; static double g_compressibility = COMPRESSIBILITY_DEFAULT; -void BMK_SetNbIterations(int nbLoops) +static void BMK_SetNbIterations(U32 nbLoops) { - nbIterations = nbLoops; - DISPLAY("- %i iterations -\n", nbIterations); + g_nbIterations = nbLoops; + DISPLAY("- %i iterations -\n", g_nbIterations); } @@ -118,27 +113,26 @@ void BMK_SetNbIterations(int nbLoops) *********************************************************/ static clock_t BMK_clockSpan( clock_t clockStart ) { - const clock_t clockEnd = clock(); - return clockEnd - clockStart; /* overflow possible */ + return clock() - clockStart; /* works even if overflow, span limited to <= ~30mn */ } static size_t BMK_findMaxMem(U64 requiredMem) { - size_t step = 64 MB; + const size_t step = 64 MB; void* testmem = NULL; requiredMem = (((requiredMem >> 26) + 1) << 26); if (requiredMem > MAX_MEM) requiredMem = MAX_MEM; - requiredMem += 2*step; - while (!testmem) { - requiredMem -= step; + requiredMem += step; + do { testmem = malloc ((size_t)requiredMem); - } + requiredMem -= step; + } while (!testmem); free (testmem); - return (size_t) (requiredMem - step); + return (size_t) requiredMem; } @@ -268,7 +262,6 @@ static size_t benchMem(const void* src, size_t srcSize, U32 benchNb) BYTE* dstBuff; size_t dstBuffSize; BYTE* buff2; - int loopNb; const char* benchName; size_t (*benchFunction)(void* dst, size_t dstSize, void* verifBuff, const void* src, size_t srcSize); double bestTime = 100000000.; @@ -386,7 +379,8 @@ static size_t benchMem(const void* src, size_t srcSize, U32 benchNb) { size_t i; for (i=0; i