Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | bench.c - Demo module to benchmark open-source compression algorithms |
| 3 | Copyright (C) Yann Collet 2012-2015 |
| 4 | |
| 5 | GPL v2 License |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License along |
| 18 | with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | |
| 21 | You can contact the author at : |
| 22 | - zstd source repository : https://github.com/Cyan4973/zstd |
| 23 | - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c |
| 24 | */ |
| 25 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 26 | /* ************************************** |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 27 | * Compiler Options |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 28 | ****************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 29 | /* Disable some Visual warning messages */ |
Yann Collet | 6c8b925 | 2015-12-16 02:44:56 +0100 | [diff] [blame] | 30 | #ifdef _MSC_VER |
| 31 | # define _CRT_SECURE_NO_WARNINGS /* fopen */ |
| 32 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 33 | #endif |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 34 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 35 | /* Unix Large Files support (>4GB) */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 36 | #define _FILE_OFFSET_BITS 64 |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 37 | #if (defined(__sun__) && (!defined(__LP64__))) /* Sun Solaris 32-bits requires specific definitions */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 38 | # define _LARGEFILE_SOURCE |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 39 | #elif ! defined(__LP64__) /* No point defining Large file for 64 bit */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 40 | # define _LARGEFILE64_SOURCE |
| 41 | #endif |
| 42 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 43 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 44 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 45 | * Includes |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 46 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 47 | #include <stdlib.h> /* malloc, free */ |
| 48 | #include <string.h> /* memset */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 49 | #include <stdio.h> /* fprintf, fopen, ftello64 */ |
| 50 | #include <sys/types.h> /* stat64 */ |
| 51 | #include <sys/stat.h> /* stat64 */ |
Yann Collet | be39143 | 2016-03-22 23:19:28 +0100 | [diff] [blame] | 52 | #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 53 | |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 54 | /* sleep : posix - windows - others */ |
| 55 | #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) |
Yann Collet | fd69225 | 2016-03-23 02:47:33 +0100 | [diff] [blame] | 56 | # include <unistd.h> /* sleep */ |
| 57 | # include <sys/resource.h> /* setpriority */ |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 58 | # define BMK_sleep(s) sleep(s) |
Yann Collet | fd69225 | 2016-03-23 02:47:33 +0100 | [diff] [blame] | 59 | # define HIGH_PRIORITY setpriority(PRIO_PROCESS, 0, -20) |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 60 | #elif defined(_WIN32) |
| 61 | # include <windows.h> |
| 62 | # define BMK_sleep(s) Sleep(1000*s) |
Yann Collet | fd69225 | 2016-03-23 02:47:33 +0100 | [diff] [blame] | 63 | # define HIGH_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS); |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 64 | #else |
| 65 | # define BMK_sleep(s) /* disabled */ |
Yann Collet | fd69225 | 2016-03-23 02:47:33 +0100 | [diff] [blame] | 66 | # define HIGH_PRIORITY |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 67 | #endif |
| 68 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 69 | #include "mem.h" |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 70 | #include "zstd_static.h" |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 71 | #include "xxhash.h" |
Yann Collet | be39143 | 2016-03-22 23:19:28 +0100 | [diff] [blame] | 72 | #include "datagen.h" /* RDG_genBuffer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 73 | |
| 74 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 75 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 76 | * Compiler specifics |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 77 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 78 | #if !defined(S_ISREG) |
| 79 | # define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) |
| 80 | #endif |
| 81 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 82 | #ifdef _MSC_VER |
| 83 | #define snprintf sprintf_s |
| 84 | #endif |
| 85 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 86 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 87 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 88 | * Constants |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 89 | ***************************************/ |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 90 | #define NBLOOPS 3 |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 91 | #define TIMELOOP_S 1 |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 92 | #define ACTIVEPERIOD_S 70 |
| 93 | #define COOLPERIOD_S 10 |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 94 | |
| 95 | #define KB *(1 <<10) |
| 96 | #define MB *(1 <<20) |
| 97 | #define GB *(1U<<30) |
| 98 | |
Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 99 | static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31)); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 100 | #define DEFAULT_CHUNKSIZE (4 MB) |
| 101 | |
| 102 | static U32 g_compressibilityDefault = 50; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 103 | |
| 104 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 105 | /* ************************************* |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 106 | * console display |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 107 | ***************************************/ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 108 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 109 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 110 | static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ |
| 111 | |
| 112 | |
| 113 | /* ************************************* |
| 114 | * Exceptions |
| 115 | ***************************************/ |
| 116 | #ifndef DEBUG |
| 117 | # define DEBUG 0 |
| 118 | #endif |
| 119 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 120 | #define EXM_THROW(error, ...) \ |
| 121 | { \ |
| 122 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
| 123 | DISPLAYLEVEL(1, "Error %i : ", error); \ |
| 124 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| 125 | DISPLAYLEVEL(1, "\n"); \ |
| 126 | exit(error); \ |
| 127 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 128 | |
| 129 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 130 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 131 | * Benchmark Parameters |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 132 | ***************************************/ |
Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 133 | static U32 g_nbIterations = NBLOOPS; |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 134 | static size_t g_blockSize = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 135 | |
Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 136 | void BMK_SetNbIterations(unsigned nbLoops) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 137 | { |
Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 138 | g_nbIterations = nbLoops; |
| 139 | DISPLAY("- %i iterations -\n", g_nbIterations); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 142 | void BMK_SetBlockSize(size_t blockSize) |
| 143 | { |
| 144 | g_blockSize = blockSize; |
| 145 | DISPLAY("using blocks of size %u KB \n", (U32)(blockSize>>10)); |
| 146 | } |
| 147 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 148 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 149 | /* ******************************************************** |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 150 | * Private functions |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 151 | **********************************************************/ |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 152 | static clock_t BMK_clockSpan( clock_t clockStart ) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 153 | { |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 154 | return clock() - clockStart; /* works even if overflow, span limited to <= ~30mn */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 157 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 158 | static U64 BMK_getFileSize(const char* infilename) |
| 159 | { |
| 160 | int r; |
| 161 | #if defined(_MSC_VER) |
| 162 | struct _stat64 statbuf; |
| 163 | r = _stat64(infilename, &statbuf); |
| 164 | #else |
| 165 | struct stat statbuf; |
| 166 | r = stat(infilename, &statbuf); |
| 167 | #endif |
| 168 | if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */ |
| 169 | return (U64)statbuf.st_size; |
| 170 | } |
| 171 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 172 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 173 | /* ******************************************************** |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 174 | * Bench functions |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 175 | **********************************************************/ |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 176 | typedef struct |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 177 | { |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 178 | const char* srcPtr; |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 179 | size_t srcSize; |
| 180 | char* cPtr; |
| 181 | size_t cRoom; |
| 182 | size_t cSize; |
| 183 | char* resPtr; |
| 184 | size_t resSize; |
| 185 | } blockParam_t; |
| 186 | |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 187 | #define MIN(a,b) ((a)<(b) ? (a) : (b)) |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 188 | #define MAX(a,b) ((a)>(b) ? (a) : (b)) |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 189 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 190 | static int BMK_benchMem(const void* srcBuffer, size_t srcSize, |
| 191 | const char* displayName, int cLevel, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 192 | const size_t* fileSizes, U32 nbFiles, |
| 193 | const void* dictBuffer, size_t dictBufferSize) |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 194 | { |
Yann Collet | d64f435 | 2016-03-21 00:07:42 +0100 | [diff] [blame] | 195 | size_t const blockSize = (g_blockSize ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */ |
| 196 | U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 197 | blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t)); |
Yann Collet | 367060b | 2015-12-17 00:07:10 +0100 | [diff] [blame] | 198 | const size_t maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */ |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 199 | void* const compressedBuffer = malloc(maxCompressedSize); |
| 200 | void* const resultBuffer = malloc(srcSize); |
Yann Collet | 2630a5e | 2016-01-14 19:13:22 +0100 | [diff] [blame] | 201 | ZSTD_CCtx* refCtx = ZSTD_createCCtx(); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 202 | ZSTD_CCtx* ctx = ZSTD_createCCtx(); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 203 | ZSTD_DCtx* refDCtx = ZSTD_createDCtx(); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 204 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 205 | U64 const crcOrig = XXH64(srcBuffer, srcSize, 0); |
| 206 | U32 nbBlocks; |
| 207 | |
| 208 | /* checks */ |
| 209 | if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx) |
| 210 | EXM_THROW(31, "not enough memory"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 211 | |
Yann Collet | c776c46 | 2015-10-29 19:10:54 +0100 | [diff] [blame] | 212 | /* init */ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 213 | if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */ |
Yann Collet | fd69225 | 2016-03-23 02:47:33 +0100 | [diff] [blame] | 214 | HIGH_PRIORITY; |
Yann Collet | c776c46 | 2015-10-29 19:10:54 +0100 | [diff] [blame] | 215 | |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 216 | /* Init blockTable data */ |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 217 | { const char* srcPtr = (const char*)srcBuffer; |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 218 | char* cPtr = (char*)compressedBuffer; |
| 219 | char* resPtr = (char*)resultBuffer; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 220 | U32 fileNb; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 221 | for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) { |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 222 | size_t remaining = fileSizes[fileNb]; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 223 | U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize); |
| 224 | U32 const blockEnd = nbBlocks + nbBlocksforThisFile; |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 225 | for ( ; nbBlocks<blockEnd; nbBlocks++) { |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 226 | size_t const thisBlockSize = MIN(remaining, blockSize); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 227 | blockTable[nbBlocks].srcPtr = srcPtr; |
| 228 | blockTable[nbBlocks].cPtr = cPtr; |
| 229 | blockTable[nbBlocks].resPtr = resPtr; |
| 230 | blockTable[nbBlocks].srcSize = thisBlockSize; |
| 231 | blockTable[nbBlocks].cRoom = ZSTD_compressBound(thisBlockSize); |
| 232 | srcPtr += thisBlockSize; |
| 233 | cPtr += blockTable[nbBlocks].cRoom; |
| 234 | resPtr += thisBlockSize; |
| 235 | remaining -= thisBlockSize; |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 236 | } } } |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 237 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 238 | /* warmimg up memory */ |
Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 239 | RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 240 | |
| 241 | /* Bench */ |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame^] | 242 | { double fastestC = 100000000., fastestD = 100000000.; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 243 | clock_t coolTime = clock(); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 244 | U32 testNb; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 245 | |
| 246 | DISPLAY("\r%79s\r", ""); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 247 | for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) { |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame^] | 248 | size_t cSize; |
| 249 | double ratio = 0.; |
| 250 | clock_t clockStart; |
Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 251 | clock_t const clockLoop = g_nbIterations ? TIMELOOP_S * CLOCKS_PER_SEC : 10; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 252 | |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 253 | /* overheat protection */ |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 254 | if (BMK_clockSpan(coolTime) > ACTIVEPERIOD_S * CLOCKS_PER_SEC) { |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 255 | DISPLAY("\rcooling down ... \r"); |
| 256 | BMK_sleep(COOLPERIOD_S); |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 257 | coolTime = clock(); |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 260 | /* Compression */ |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 261 | DISPLAY("%2i-%-17.17s :%10u ->\r", testNb, displayName, (U32)srcSize); |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 262 | memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 263 | |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 264 | clockStart = clock(); |
| 265 | while (clock() == clockStart); |
| 266 | clockStart = clock(); |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame^] | 267 | { U32 nbLoops; |
| 268 | for (nbLoops = 0 ; BMK_clockSpan(clockStart) < clockLoop ; nbLoops++) { |
| 269 | U32 blockNb; |
| 270 | ZSTD_compressBegin_usingDict(refCtx, dictBuffer, dictBufferSize, cLevel); |
| 271 | for (blockNb=0; blockNb<nbBlocks; blockNb++) { |
| 272 | size_t const rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx, |
| 273 | blockTable[blockNb].cPtr, blockTable[blockNb].cRoom, |
| 274 | blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize); |
| 275 | if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingPreparedCCtx() failed : %s", ZSTD_getErrorName(rSize)); |
| 276 | blockTable[blockNb].cSize = rSize; |
| 277 | } } |
| 278 | { clock_t const clockSpan = BMK_clockSpan(clockStart); |
| 279 | if ((double)clockSpan < fastestC*nbLoops) fastestC = (double)clockSpan / nbLoops; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 280 | } } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 281 | |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 282 | cSize = 0; |
| 283 | { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; } |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 284 | ratio = (double)srcSize / (double)cSize; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 285 | DISPLAY("%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r", |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 286 | testNb, displayName, (U32)srcSize, (U32)cSize, ratio, |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 287 | (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC) ); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 288 | |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame^] | 289 | (void)fastestD; (void)crcOrig; /* unused when decompression disabled */ |
Yann Collet | be39143 | 2016-03-22 23:19:28 +0100 | [diff] [blame] | 290 | #if 1 |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 291 | /* Decompression */ |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 292 | memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 293 | |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 294 | clockStart = clock(); |
| 295 | while (clock() == clockStart); |
| 296 | clockStart = clock(); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 297 | |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame^] | 298 | { U32 nbLoops; |
| 299 | for (nbLoops = 0 ; BMK_clockSpan(clockStart) < clockLoop ; nbLoops++) { |
| 300 | U32 blockNb; |
| 301 | ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize); |
| 302 | for (blockNb=0; blockNb<nbBlocks; blockNb++) { |
| 303 | size_t const regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx, |
| 304 | blockTable[blockNb].resPtr, blockTable[blockNb].srcSize, |
| 305 | blockTable[blockNb].cPtr, blockTable[blockNb].cSize); |
| 306 | if (ZSTD_isError(regenSize)) { |
| 307 | DISPLAY("ZSTD_decompress_usingPreparedDCtx() failed on block %u : %s \n", |
| 308 | blockNb, ZSTD_getErrorName(regenSize)); |
| 309 | break; |
| 310 | } |
| 311 | blockTable[blockNb].resSize = regenSize; |
| 312 | } } |
| 313 | { clock_t const clockSpan = BMK_clockSpan(clockStart); |
| 314 | if ((double)clockSpan < fastestD*nbLoops) fastestD = (double)clockSpan / nbLoops; |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 315 | } } |
| 316 | |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 317 | DISPLAY("%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r", |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 318 | testNb, displayName, (U32)srcSize, (U32)cSize, ratio, |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 319 | (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC), |
| 320 | (double)srcSize / 1000000. / (fastestD / CLOCKS_PER_SEC) ); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 321 | |
| 322 | /* CRC Checking */ |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame^] | 323 | { U64 const crcCheck = XXH64(resultBuffer, srcSize, 0); |
| 324 | if (crcOrig!=crcCheck) { |
| 325 | size_t u; |
| 326 | DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck); |
| 327 | for (u=0; u<srcSize; u++) { |
| 328 | if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) { |
| 329 | U32 segNb, bNb, pos; |
| 330 | size_t bacc = 0; |
| 331 | DISPLAY("Decoding error at pos %u ", (U32)u); |
| 332 | for (segNb = 0; segNb < nbBlocks; segNb++) { |
| 333 | if (bacc + blockTable[segNb].srcSize > u) break; |
| 334 | bacc += blockTable[segNb].srcSize; |
| 335 | } |
| 336 | pos = (U32)(u - bacc); |
| 337 | bNb = pos / (128 KB); |
| 338 | DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos); |
| 339 | break; |
Yann Collet | 03a6dab | 2016-01-21 02:21:17 +0100 | [diff] [blame] | 340 | } |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame^] | 341 | if (u==srcSize-1) { /* should never happen */ |
| 342 | DISPLAY("no difference detected\n"); |
| 343 | } } |
| 344 | break; |
| 345 | } } /* CRC Checking */ |
Yann Collet | e8c6bb1 | 2015-07-26 00:23:57 +0100 | [diff] [blame] | 346 | #endif |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 347 | } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */ |
Yann Collet | 70e4577 | 2016-03-19 18:08:32 +0100 | [diff] [blame] | 348 | DISPLAY("%2i#\n", cLevel); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 349 | } /* Bench */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 350 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 351 | /* clean up */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 352 | free(compressedBuffer); |
| 353 | free(resultBuffer); |
Yann Collet | 2630a5e | 2016-01-14 19:13:22 +0100 | [diff] [blame] | 354 | ZSTD_freeCCtx(refCtx); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 355 | ZSTD_freeCCtx(ctx); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 356 | ZSTD_freeDCtx(refDCtx); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 357 | ZSTD_freeDCtx(dctx); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 362 | static size_t BMK_findMaxMem(U64 requiredMem) |
| 363 | { |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 364 | size_t const step = 64 MB; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 365 | BYTE* testmem = NULL; |
| 366 | |
| 367 | requiredMem = (((requiredMem >> 26) + 1) << 26); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 368 | requiredMem += step; |
Yann Collet | 050efba | 2015-11-03 09:49:30 +0100 | [diff] [blame] | 369 | if (requiredMem > maxMemory) requiredMem = maxMemory; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 370 | |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 371 | do { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 372 | testmem = (BYTE*)malloc((size_t)requiredMem); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 373 | requiredMem -= step; |
| 374 | } while (!testmem); |
| 375 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 376 | free(testmem); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 377 | return (size_t)(requiredMem); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 380 | static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize, |
| 381 | const char* displayName, int cLevel, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 382 | const size_t* fileSizes, unsigned nbFiles, |
| 383 | const void* dictBuffer, size_t dictBufferSize) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 384 | { |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 385 | if (cLevel < 0) { /* range mode : test all levels from 1 to l */ |
Yann Collet | c776c46 | 2015-10-29 19:10:54 +0100 | [diff] [blame] | 386 | int l; |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 387 | for (l=1; l <= -cLevel; l++) { |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 388 | BMK_benchMem(srcBuffer, benchedSize, |
| 389 | displayName, l, |
| 390 | fileSizes, nbFiles, |
| 391 | dictBuffer, dictBufferSize); |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 392 | } |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 393 | return; |
Yann Collet | c776c46 | 2015-10-29 19:10:54 +0100 | [diff] [blame] | 394 | } |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 395 | BMK_benchMem(srcBuffer, benchedSize, |
| 396 | displayName, cLevel, |
| 397 | fileSizes, nbFiles, |
| 398 | dictBuffer, dictBufferSize); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static U64 BMK_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles) |
| 402 | { |
| 403 | U64 total = 0; |
| 404 | unsigned n; |
| 405 | for (n=0; n<nbFiles; n++) |
| 406 | total += BMK_getFileSize(fileNamesTable[n]); |
| 407 | return total; |
| 408 | } |
| 409 | |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame^] | 410 | /*! BMK_loadFiles() : |
| 411 | Loads `buffer` with content of files listed within `fileNamesTable`. |
| 412 | At most, fills `buffer` entirely */ |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 413 | static void BMK_loadFiles(void* buffer, size_t bufferSize, |
| 414 | size_t* fileSizes, |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame^] | 415 | const char** fileNamesTable, unsigned nbFiles) |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 416 | { |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 417 | size_t pos = 0; |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 418 | |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 419 | unsigned n; |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 420 | for (n=0; n<nbFiles; n++) { |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 421 | U64 fileSize = BMK_getFileSize(fileNamesTable[n]); |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame^] | 422 | FILE* const f = fopen(fileNamesTable[n], "rb"); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 423 | if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]); |
| 424 | DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]); |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame^] | 425 | if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */ |
| 426 | { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f); |
| 427 | if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]); |
| 428 | pos += readSize; } |
Yann Collet | a52c98d | 2015-12-16 03:12:31 +0100 | [diff] [blame] | 429 | fileSizes[n] = (size_t)fileSize; |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 430 | fclose(f); |
| 431 | } |
| 432 | } |
| 433 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 434 | static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, |
| 435 | const char* dictFileName, int cLevel) |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 436 | { |
| 437 | void* srcBuffer; |
| 438 | size_t benchedSize; |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 439 | void* dictBuffer = NULL; |
| 440 | size_t dictBufferSize = 0; |
| 441 | size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t)); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 442 | U64 totalSizeToLoad = BMK_getTotalFileSize(fileNamesTable, nbFiles); |
| 443 | char mfName[20] = {0}; |
| 444 | const char* displayName = NULL; |
| 445 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 446 | if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes"); |
| 447 | |
| 448 | /* Load dictionary */ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 449 | if (dictFileName != NULL) { |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 450 | U64 dictFileSize = BMK_getFileSize(dictFileName); |
| 451 | if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName); |
| 452 | dictBufferSize = (size_t)dictFileSize; |
| 453 | dictBuffer = malloc(dictBufferSize); |
| 454 | if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize); |
| 455 | BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1); |
| 456 | } |
| 457 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 458 | /* Memory allocation & restrictions */ |
| 459 | benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3; |
| 460 | if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad; |
| 461 | if (benchedSize < totalSizeToLoad) |
| 462 | DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20)); |
| 463 | srcBuffer = malloc(benchedSize); |
| 464 | if (!srcBuffer) EXM_THROW(12, "not enough memory"); |
| 465 | |
| 466 | /* Load input buffer */ |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 467 | BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 468 | |
| 469 | /* Bench */ |
| 470 | snprintf (mfName, sizeof(mfName), " %u files", nbFiles); |
| 471 | if (nbFiles > 1) displayName = mfName; |
| 472 | else displayName = fileNamesTable[0]; |
| 473 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 474 | BMK_benchCLevel(srcBuffer, benchedSize, |
| 475 | displayName, cLevel, |
| 476 | fileSizes, nbFiles, |
| 477 | dictBuffer, dictBufferSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 478 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 479 | /* clean up */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 480 | free(srcBuffer); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 481 | free(dictBuffer); |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 482 | free(fileSizes); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 486 | static void BMK_syntheticTest(int cLevel, double compressibility) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 487 | { |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 488 | char name[20] = {0}; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 489 | size_t benchedSize = 10000000; |
| 490 | void* srcBuffer = malloc(benchedSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 491 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 492 | /* Memory allocation */ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 493 | if (!srcBuffer) EXM_THROW(21, "not enough memory"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 494 | |
| 495 | /* Fill input buffer */ |
Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 496 | RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 497 | |
| 498 | /* Bench */ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 499 | snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100)); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 500 | BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, &benchedSize, 1, NULL, 0); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 501 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 502 | /* clean up */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 503 | free(srcBuffer); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 507 | int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, |
| 508 | const char* dictFileName, int cLevel) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 509 | { |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 510 | double const compressibility = (double)g_compressibilityDefault / 100; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 511 | |
| 512 | if (nbFiles == 0) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 513 | BMK_syntheticTest(cLevel, compressibility); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 514 | else |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 515 | BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 516 | return 0; |
| 517 | } |
| 518 | |