]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
bench : changed timer to clock_t, reduced timeloop to 1sec
authorYann Collet <yann.collet.73@gmail.com>
Thu, 17 Mar 2016 18:37:33 +0000 (19:37 +0100)
committerYann Collet <yann.collet.73@gmail.com>
Thu, 17 Mar 2016 18:37:33 +0000 (19:37 +0100)
programs/bench.c
programs/fullbench.c

index 82e28d405889463881fbd140505edd62d9b2a2a7..e819cc24236a8f606b75d335d67f13e6ff187784 100644 (file)
 #  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
 #include <stdio.h>       /* fprintf, fopen, ftello64 */
 #include <sys/types.h>   /* stat64 */
 #include <sys/stat.h>    /* stat64 */
-
-/* Use ftime() if gettimeofday() is not available */
-#if defined(BMK_LEGACY_TIMER)
-#  include <sys/timeb.h>  /* timeb, ftime */
-#else
-#  include <sys/time.h>   /* gettimeofday */
-#endif
+#include <time.h>         /* 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<nbFiles; fileNb++) {
             size_t remaining = fileSizes[fileNb];
-            U32 nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
-            U32 blockEnd = nbBlocks + nbBlocksforThisFile;
+            U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
+            U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
             for ( ; nbBlocks<blockEnd; nbBlocks++) {
                 size_t thisBlockSize = MIN(remaining, blockSize);
                 blockTable[nbBlocks].srcPtr = srcPtr;
@@ -287,30 +243,30 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
         double fastestC = 100000000., fastestD = 100000000.;
         double ratio = 0.;
         U64 crcCheck = 0;
-        int coolTime = BMK_GetMilliStart();
+        clock_t coolTime = clock();
 
         DISPLAY("\r%79s\r", "");
         for (loopNb = 1; loopNb <= nbIterations; loopNb++) {
             int nbLoops;
-            int milliTime;
             U32 blockNb;
+            clock_t clockStart, clockSpan;
 
             /* overheat protection */
-            if (BMK_GetMilliSpan(coolTime) > 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<nbBlocks; blockNb++) {
                     size_t rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx,
@@ -321,25 +277,27 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
                 }
                 nbLoops++;
             }
-            milliTime = BMK_GetMilliSpan(milliTime);
+            clockSpan = BMK_clockSpan(clockStart);
 
             cSize = 0;
             for (blockNb=0; blockNb<nbBlocks; blockNb++)
                 cSize += blockTable[blockNb].cSize;
-            if ((double)milliTime < fastestC*nbLoops) fastestC = (double)milliTime / nbLoops;
+            if ((double)clockSpan < fastestC*nbLoops) fastestC = (double)clockSpan / nbLoops;
             ratio = (double)srcSize / (double)cSize;
-            DISPLAY("%2i-%-17.17s :%10i ->%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<nbBlocks; blockNb++) {
                     size_t regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx,
@@ -353,9 +311,12 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
                     blockTable[blockNb].resSize = regenSize;
             }   }
 
-            milliTime = BMK_GetMilliSpan(milliTime);
-            if ((double)milliTime < fastestD*nbLoops) fastestD = (double)milliTime / nbLoops;
-            DISPLAY("%2i-%-17.17s :%10i ->%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<nbFiles; n++) {
         size_t readSize;
         U64 fileSize = BMK_getFileSize(fileNamesTable[n]);
@@ -464,7 +421,7 @@ static void BMK_loadFiles(void* buffer, size_t bufferSize,
         if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
         DISPLAYLEVEL(2, "Loading %s...       \r", fileNamesTable[n]);
         if (fileSize > 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);
index b3edd54059c166b6230e74d559a031f7bf3067f6..b4afcf11286e21ce0fa42d5dad9be480abecd6c6 100644 (file)
 #  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<dstBuffSize; i++) dstBuff[i]=(BYTE)i; }     /* warming up memory */
 
-    for (loopNb = 1; loopNb <= nbIterations; loopNb++) {
+    { U32 loopNb;
+    for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
         clock_t const timeLoop = TIMELOOP_S * CLOCKS_PER_SEC;
         clock_t clockStart;
         U32 nbRounds;
@@ -405,7 +399,7 @@ static size_t benchMem(const void* src, size_t srcSize, U32 benchNb)
         averageTime = (((double)BMK_clockSpan(clockStart)) / CLOCKS_PER_SEC) / nbRounds;
         if (averageTime < bestTime) bestTime = averageTime;
         DISPLAY("%2i- %-30.30s : %7.1f MB/s  (%9u)\r", loopNb, benchName, (double)srcSize / (1 MB) / bestTime, (U32)benchResult);
-    }
+    }}
     DISPLAY("%2u\n", benchNb);
 
 _cleanOut: