| 1 | /********************************************************************* |
|---|
| 2 | Small benchmark for testing basic capabilities of Blosc. |
|---|
| 3 | |
|---|
| 4 | You can select different degrees of 'randomness' in input buffer, as |
|---|
| 5 | well as external datafiles (uncomment the lines after "For data |
|---|
| 6 | coming from a file" comment). |
|---|
| 7 | |
|---|
| 8 | To compile using GCC: |
|---|
| 9 | |
|---|
| 10 | gcc -O3 -msse2 -o bench bench.c blosc.c blosclz.c shuffle.c |
|---|
| 11 | |
|---|
| 12 | I'm collecting speeds for different machines, so the output of your |
|---|
| 13 | benchmarks and your processor specifications are welcome! |
|---|
| 14 | |
|---|
| 15 | Author: Francesc Alted (faltet@pytables.org) |
|---|
| 16 | |
|---|
| 17 | See LICENSES/BLOSC.txt for details about copyright and rights to use. |
|---|
| 18 | **********************************************************************/ |
|---|
| 19 | |
|---|
| 20 | #include <stdlib.h> |
|---|
| 21 | #include <stdio.h> |
|---|
| 22 | #include <string.h> |
|---|
| 23 | #include <sys/types.h> |
|---|
| 24 | #include <sys/stat.h> |
|---|
| 25 | #include <fcntl.h> |
|---|
| 26 | #ifdef _WIN32 |
|---|
| 27 | #include <time.h> |
|---|
| 28 | #else |
|---|
| 29 | #include <unistd.h> |
|---|
| 30 | #include <sys/time.h> |
|---|
| 31 | #endif |
|---|
| 32 | #include <math.h> |
|---|
| 33 | #include "blosc.h" |
|---|
| 34 | |
|---|
| 35 | #define KB 1024 |
|---|
| 36 | #define MB (1024*1024) |
|---|
| 37 | |
|---|
| 38 | /* #define NCHUNKS (100) */ |
|---|
| 39 | /* #define NITER (10) /\* Number of iterations *\/ */ |
|---|
| 40 | #define NCHUNKS (128) |
|---|
| 41 | #define NITER (10) /* Number of iterations */ |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | #ifdef _WIN32 |
|---|
| 45 | #include <windows.h> |
|---|
| 46 | #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) |
|---|
| 47 | #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 |
|---|
| 48 | #else |
|---|
| 49 | #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | struct timezone |
|---|
| 53 | { |
|---|
| 54 | int tz_minuteswest; /* minutes W of Greenwich */ |
|---|
| 55 | int tz_dsttime; /* type of dst correction */ |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | int gettimeofday(struct timeval *tv, struct timezone *tz) |
|---|
| 59 | { |
|---|
| 60 | FILETIME ft; |
|---|
| 61 | unsigned __int64 tmpres = 0; |
|---|
| 62 | static int tzflag; |
|---|
| 63 | |
|---|
| 64 | if (NULL != tv) |
|---|
| 65 | { |
|---|
| 66 | GetSystemTimeAsFileTime(&ft); |
|---|
| 67 | |
|---|
| 68 | tmpres |= ft.dwHighDateTime; |
|---|
| 69 | tmpres <<= 32; |
|---|
| 70 | tmpres |= ft.dwLowDateTime; |
|---|
| 71 | |
|---|
| 72 | /*converting file time to unix epoch*/ |
|---|
| 73 | tmpres -= DELTA_EPOCH_IN_MICROSECS; |
|---|
| 74 | tmpres /= 10; /*convert into microseconds*/ |
|---|
| 75 | tv->tv_sec = (long)(tmpres / 1000000UL); |
|---|
| 76 | tv->tv_usec = (long)(tmpres % 1000000UL); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | if (NULL != tz) |
|---|
| 80 | { |
|---|
| 81 | if (!tzflag) |
|---|
| 82 | { |
|---|
| 83 | _tzset(); |
|---|
| 84 | tzflag++; |
|---|
| 85 | } |
|---|
| 86 | tz->tz_minuteswest = _timezone / 60; |
|---|
| 87 | tz->tz_dsttime = _daylight; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | return 0; |
|---|
| 91 | } |
|---|
| 92 | #endif /* _WIN32 */ |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | /* Given two timeval stamps, return the difference in seconds */ |
|---|
| 96 | float getseconds(struct timeval last, struct timeval current) { |
|---|
| 97 | int sec, usec; |
|---|
| 98 | |
|---|
| 99 | sec = current.tv_sec - last.tv_sec; |
|---|
| 100 | usec = current.tv_usec - last.tv_usec; |
|---|
| 101 | return (float)(((double)sec + usec*1e-6)/((double)NITER*NCHUNKS)*1e6); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | int get_value(int i, int rshift) { |
|---|
| 106 | int v; |
|---|
| 107 | |
|---|
| 108 | v = (i<<26)^(i<<18)^(i<<11)^(i<<3)^i; |
|---|
| 109 | if (rshift < 32) { |
|---|
| 110 | v &= (1 << rshift) - 1; |
|---|
| 111 | } |
|---|
| 112 | return v; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | void init_buffer(void *src, int size, int rshift) { |
|---|
| 117 | unsigned int i; |
|---|
| 118 | int *_src = (int *)src; |
|---|
| 119 | |
|---|
| 120 | /* To have reproducible results */ |
|---|
| 121 | srand(1); |
|---|
| 122 | |
|---|
| 123 | /* Initialize the original buffer */ |
|---|
| 124 | for (i = 0; i < size/sizeof(int); ++i) { |
|---|
| 125 | /* Choose one below */ |
|---|
| 126 | //_src[i] = 0; |
|---|
| 127 | //_src[i] = 0x01010101; |
|---|
| 128 | //_src[i] = 0x01020304; |
|---|
| 129 | //_src[i] = i * 1/.3; |
|---|
| 130 | //_src[i] = i; |
|---|
| 131 | //_src[i] = rand() >> (32-rshift); |
|---|
| 132 | _src[i] = get_value(i, rshift); |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | do_bench(int nthreads, unsigned int size, int elsize, int rshift) { |
|---|
| 138 | void *src, *srccpy; |
|---|
| 139 | void **dest[NCHUNKS], *dest2; |
|---|
| 140 | int nbytes, cbytes; |
|---|
| 141 | size_t i, j; |
|---|
| 142 | struct timeval last, current; |
|---|
| 143 | float tmemcpy, tshuf, tunshuf; |
|---|
| 144 | int clevel, doshuffle=1; |
|---|
| 145 | unsigned char *orig, *round; |
|---|
| 146 | |
|---|
| 147 | blosc_set_nthreads(nthreads); |
|---|
| 148 | |
|---|
| 149 | /* Initialize buffers */ |
|---|
| 150 | src = malloc(size); |
|---|
| 151 | srccpy = malloc(size); |
|---|
| 152 | dest2 = malloc(size); |
|---|
| 153 | init_buffer(src, size, rshift); |
|---|
| 154 | memcpy(srccpy, src, size); |
|---|
| 155 | for (j = 0; j < NCHUNKS; j++) { |
|---|
| 156 | dest[j] = malloc(size); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | printf("--> %d, %d, %d, %d\n", nthreads, size, elsize, rshift); |
|---|
| 160 | printf("********************** Run info ******************************\n"); |
|---|
| 161 | printf("Blosc version: %s (%s)\n", BLOSC_VERSION_STRING, BLOSC_VERSION_DATE); |
|---|
| 162 | printf("Using random data with %d significant bits (out of 32)\n", rshift); |
|---|
| 163 | printf("Dataset size: %d bytes\tType size: %d bytes\n", size, elsize); |
|---|
| 164 | printf("Working set: %.1f MB\t\t", (size*NCHUNKS) / (float)MB); |
|---|
| 165 | printf("Number of threads: %d\n", nthreads); |
|---|
| 166 | printf("********************** Running benchmarks *********************\n"); |
|---|
| 167 | |
|---|
| 168 | gettimeofday(&last, NULL); |
|---|
| 169 | for (i = 0; i < NITER; i++) { |
|---|
| 170 | for (j = 0; j < NCHUNKS; j++) { |
|---|
| 171 | memcpy(dest[j], src, size); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | gettimeofday(¤t, NULL); |
|---|
| 175 | tmemcpy = getseconds(last, current); |
|---|
| 176 | printf("memcpy(write):\t\t %6.1f us, %.1f MB/s\n", |
|---|
| 177 | tmemcpy, size/(tmemcpy*MB/1e6)); |
|---|
| 178 | |
|---|
| 179 | gettimeofday(&last, NULL); |
|---|
| 180 | for (i = 0; i < NITER; i++) { |
|---|
| 181 | for (j = 0; j < NCHUNKS; j++) { |
|---|
| 182 | memcpy(dest2, dest[j], size); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | gettimeofday(¤t, NULL); |
|---|
| 186 | tmemcpy = getseconds(last, current); |
|---|
| 187 | printf("memcpy(read):\t\t %6.1f us, %.1f MB/s\n", |
|---|
| 188 | tmemcpy, size/(tmemcpy*MB/1e6)); |
|---|
| 189 | |
|---|
| 190 | for (clevel=1; clevel<10; clevel++) { |
|---|
| 191 | |
|---|
| 192 | printf("Compression level: %d\n", clevel); |
|---|
| 193 | |
|---|
| 194 | gettimeofday(&last, NULL); |
|---|
| 195 | for (i = 0; i < NITER; i++) { |
|---|
| 196 | for (j = 0; j < NCHUNKS; j++) { |
|---|
| 197 | cbytes = blosc_compress(clevel, doshuffle, elsize, size, src, dest[j]); |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | gettimeofday(¤t, NULL); |
|---|
| 201 | tshuf = getseconds(last, current); |
|---|
| 202 | printf("comp(write):\t %6.1f us, %.1f MB/s\t ", |
|---|
| 203 | tshuf, size/(tshuf*MB/1e6)); |
|---|
| 204 | printf("Final bytes: %d ", cbytes); |
|---|
| 205 | if (cbytes > 0) { |
|---|
| 206 | printf("Ratio: %3.2f", size/(float)cbytes); |
|---|
| 207 | } |
|---|
| 208 | printf("\n"); |
|---|
| 209 | |
|---|
| 210 | /* Compressor was unable to compress. Copy the buffer manually. */ |
|---|
| 211 | if (cbytes == 0) { |
|---|
| 212 | for (j = 0; j < NCHUNKS; j++) { |
|---|
| 213 | memcpy(dest[j], src, size); |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | gettimeofday(&last, NULL); |
|---|
| 218 | for (i = 0; i < NITER; i++) { |
|---|
| 219 | for (j = 0; j < NCHUNKS; j++) { |
|---|
| 220 | if (cbytes == 0) { |
|---|
| 221 | memcpy(dest2, dest[j], size); |
|---|
| 222 | nbytes = size; |
|---|
| 223 | } |
|---|
| 224 | else { |
|---|
| 225 | nbytes = blosc_decompress(dest[j], dest2, size); |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | gettimeofday(¤t, NULL); |
|---|
| 230 | tunshuf = getseconds(last, current); |
|---|
| 231 | printf("decomp(read):\t %6.1f us, %.1f MB/s\t ", |
|---|
| 232 | tunshuf, nbytes/(tunshuf*MB/1e6)); |
|---|
| 233 | if (nbytes < 0) { |
|---|
| 234 | printf("FAILED. Error code: %d\n", nbytes); |
|---|
| 235 | } |
|---|
| 236 | /* printf("Orig bytes: %d\tFinal bytes: %d\n", cbytes, nbytes); */ |
|---|
| 237 | |
|---|
| 238 | /* Check if data has had a good roundtrip */ |
|---|
| 239 | orig = (unsigned char *)srccpy; |
|---|
| 240 | round = (unsigned char *)dest2; |
|---|
| 241 | for(i = 0; i<size; ++i){ |
|---|
| 242 | if (orig[i] != round[i]) { |
|---|
| 243 | printf("\nError: Original data and round-trip do not match in pos %d\n", |
|---|
| 244 | (int)i); |
|---|
| 245 | printf("Orig--> %x, round-trip--> %x\n", orig[i], round[i]); |
|---|
| 246 | break; |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | if (i == size) printf("OK\n"); |
|---|
| 251 | |
|---|
| 252 | } /* End clevel loop */ |
|---|
| 253 | |
|---|
| 254 | free(src); free(srccpy); free(dest2); |
|---|
| 255 | for (i = 0; i < NCHUNKS; i++) { |
|---|
| 256 | free(dest[i]); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | int main(int argc, char *argv[]) { |
|---|
| 263 | int suite = 0; |
|---|
| 264 | int hard_suite = 0; |
|---|
| 265 | int nthreads = 1; /* The number of threads */ |
|---|
| 266 | unsigned int size = 1024*1024; /* Buffer size */ |
|---|
| 267 | unsigned int elsize = 4; /* Datatype size */ |
|---|
| 268 | int rshift = 20; /* Significant bits */ |
|---|
| 269 | |
|---|
| 270 | if ((argc == 2) && strcmp(argv[1], "suite") == 0) { |
|---|
| 271 | suite = 1; |
|---|
| 272 | } |
|---|
| 273 | else if ((argc == 2) && strcmp(argv[1], "hard_suite") == 0) { |
|---|
| 274 | hard_suite = 1; |
|---|
| 275 | } |
|---|
| 276 | else if (argc >= 2) { |
|---|
| 277 | nthreads = atoi(argv[1]); |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | if (argc >= 3) { |
|---|
| 281 | size = atoi(argv[2])*1024; |
|---|
| 282 | if (size > 2*1024*1024) { |
|---|
| 283 | printf("The test is going to require more than 256 MB of RAM!\n"); |
|---|
| 284 | } |
|---|
| 285 | } |
|---|
| 286 | if (argc >= 4) { |
|---|
| 287 | elsize = atoi(argv[3]); |
|---|
| 288 | } |
|---|
| 289 | if (argc >= 5) { |
|---|
| 290 | rshift = atoi(argv[4]); |
|---|
| 291 | } |
|---|
| 292 | if (argc >= 6) { |
|---|
| 293 | printf("Usage: bench 'suite' | 'hard_suite' | [nthreads [bufsize(KB) [typesize [sbits ]]]]\n"); |
|---|
| 294 | exit(1); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | if (suite) { |
|---|
| 298 | for (nthreads=1; nthreads <= 6; nthreads++) { |
|---|
| 299 | for (size=64*KB; size <= 4*MB; size *=2) { |
|---|
| 300 | for (elsize=1; elsize <= 16; elsize *=2) { |
|---|
| 301 | do_bench(nthreads, size, elsize, rshift); |
|---|
| 302 | } |
|---|
| 303 | } |
|---|
| 304 | } |
|---|
| 305 | } |
|---|
| 306 | else if (hard_suite) { |
|---|
| 307 | for (nthreads=1; nthreads < 10; nthreads++) { |
|---|
| 308 | for (size=32*KB; size < 8*MB; size *=2) { |
|---|
| 309 | for (elsize=1; elsize < 32; elsize *=2) { |
|---|
| 310 | for (rshift=0; rshift < 32; rshift++) { |
|---|
| 311 | do_bench(nthreads, size, elsize, rshift); |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| 317 | else { |
|---|
| 318 | do_bench(nthreads, size, elsize, rshift); |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | /* Free blosc resources */ |
|---|
| 322 | blosc_free_resources(); |
|---|
| 323 | |
|---|
| 324 | return 0; |
|---|
| 325 | } |
|---|