| 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 MB (1024*1024) |
|---|
| 36 | |
|---|
| 37 | /* #define NCHUNKS (100) */ |
|---|
| 38 | /* #define NITER (10) /\* Number of iterations *\/ */ |
|---|
| 39 | #define NCHUNKS (128) |
|---|
| 40 | #define NITER (10) /* Number of iterations */ |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | #ifdef _WIN32 |
|---|
| 44 | #include <windows.h> |
|---|
| 45 | #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) |
|---|
| 46 | #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 |
|---|
| 47 | #else |
|---|
| 48 | #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL |
|---|
| 49 | #endif |
|---|
| 50 | |
|---|
| 51 | struct timezone |
|---|
| 52 | { |
|---|
| 53 | int tz_minuteswest; /* minutes W of Greenwich */ |
|---|
| 54 | int tz_dsttime; /* type of dst correction */ |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | int gettimeofday(struct timeval *tv, struct timezone *tz) |
|---|
| 58 | { |
|---|
| 59 | FILETIME ft; |
|---|
| 60 | unsigned __int64 tmpres = 0; |
|---|
| 61 | static int tzflag; |
|---|
| 62 | |
|---|
| 63 | if (NULL != tv) |
|---|
| 64 | { |
|---|
| 65 | GetSystemTimeAsFileTime(&ft); |
|---|
| 66 | |
|---|
| 67 | tmpres |= ft.dwHighDateTime; |
|---|
| 68 | tmpres <<= 32; |
|---|
| 69 | tmpres |= ft.dwLowDateTime; |
|---|
| 70 | |
|---|
| 71 | /*converting file time to unix epoch*/ |
|---|
| 72 | tmpres -= DELTA_EPOCH_IN_MICROSECS; |
|---|
| 73 | tmpres /= 10; /*convert into microseconds*/ |
|---|
| 74 | tv->tv_sec = (long)(tmpres / 1000000UL); |
|---|
| 75 | tv->tv_usec = (long)(tmpres % 1000000UL); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | if (NULL != tz) |
|---|
| 79 | { |
|---|
| 80 | if (!tzflag) |
|---|
| 81 | { |
|---|
| 82 | _tzset(); |
|---|
| 83 | tzflag++; |
|---|
| 84 | } |
|---|
| 85 | tz->tz_minuteswest = _timezone / 60; |
|---|
| 86 | tz->tz_dsttime = _daylight; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | return 0; |
|---|
| 90 | } |
|---|
| 91 | #endif /* _WIN32 */ |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | /* Given two timeval stamps, return the difference in seconds */ |
|---|
| 95 | float getseconds(struct timeval last, struct timeval current) { |
|---|
| 96 | int sec, usec; |
|---|
| 97 | |
|---|
| 98 | sec = current.tv_sec - last.tv_sec; |
|---|
| 99 | usec = current.tv_usec - last.tv_usec; |
|---|
| 100 | return (float)(((double)sec + usec*1e-6)/((double)NITER*NCHUNKS)*1e6); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | int get_value(int i, int rshift) { |
|---|
| 105 | int v; |
|---|
| 106 | |
|---|
| 107 | v = (i<<26)^(i<<18)^(i<<11)^(i<<3)^i; |
|---|
| 108 | v &= (1 << rshift) - 1; |
|---|
| 109 | return v; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | void init_buffer(void *src, int size, int rshift) { |
|---|
| 114 | unsigned int i; |
|---|
| 115 | int *_src = (int *)src; |
|---|
| 116 | |
|---|
| 117 | /* To have reproducible results */ |
|---|
| 118 | srand(1); |
|---|
| 119 | |
|---|
| 120 | /* Initialize the original buffer */ |
|---|
| 121 | for (i = 0; i < size/sizeof(int); ++i) { |
|---|
| 122 | /* Choose one below */ |
|---|
| 123 | //_src[i] = 0; |
|---|
| 124 | //_src[i] = 0x01010101; |
|---|
| 125 | //_src[i] = 0x01020304; |
|---|
| 126 | //_src[i] = i * 1/.3; |
|---|
| 127 | //_src[i] = i; |
|---|
| 128 | //_src[i] = rand() >> (32-rshift); |
|---|
| 129 | _src[i] = get_value(i, rshift); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | int main(int argc, char *argv[]) { |
|---|
| 135 | int nbytes, cbytes; |
|---|
| 136 | void *src, *srccpy; |
|---|
| 137 | void **dest[NCHUNKS], *dest2; |
|---|
| 138 | size_t i, j; |
|---|
| 139 | struct timeval last, current; |
|---|
| 140 | float tmemcpy, tshuf, tunshuf; |
|---|
| 141 | int clevel; |
|---|
| 142 | unsigned int size = 1024*1024; /* Buffer size */ |
|---|
| 143 | unsigned int elsize = 4; /* Datatype size */ |
|---|
| 144 | int rshift = 20; /* Significant bits */ |
|---|
| 145 | int nthreads = 1; /* The number of threads */ |
|---|
| 146 | int doshuffle = 1; /* Shuffle? */ |
|---|
| 147 | unsigned char *orig, *round; |
|---|
| 148 | |
|---|
| 149 | if (argc >= 2) { |
|---|
| 150 | nthreads = atoi(argv[1]); |
|---|
| 151 | } |
|---|
| 152 | if (argc >= 3) { |
|---|
| 153 | size = atoi(argv[2])*1024; |
|---|
| 154 | if (size > 2*1024*1024) { |
|---|
| 155 | printf("The test is going to require more than 256 MB of RAM!\n"); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | if (argc >= 4) { |
|---|
| 159 | elsize = atoi(argv[3]); |
|---|
| 160 | } |
|---|
| 161 | if (argc >= 5) { |
|---|
| 162 | rshift = atoi(argv[4]); |
|---|
| 163 | } |
|---|
| 164 | if (argc >= 6) { |
|---|
| 165 | doshuffle = atoi(argv[5]); |
|---|
| 166 | } |
|---|
| 167 | if (argc >= 7) { |
|---|
| 168 | printf("Usage: bench [nthreads [bufsize(KB) [typesize [sbits [shuf]]]]]\n"); |
|---|
| 169 | exit(1); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | blosc_set_nthreads(nthreads); |
|---|
| 173 | //blosc_set_blocksize(64*1024); |
|---|
| 174 | |
|---|
| 175 | src = malloc(size); |
|---|
| 176 | srccpy = malloc(size); |
|---|
| 177 | dest2 = malloc(size); |
|---|
| 178 | |
|---|
| 179 | /* Initialize buffers */ |
|---|
| 180 | init_buffer(src, size, rshift); |
|---|
| 181 | memcpy(srccpy, src, size); |
|---|
| 182 | for (j = 0; j < NCHUNKS; j++) { |
|---|
| 183 | dest[j] = malloc(size); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | printf("********************** Setup info *****************************\n"); |
|---|
| 187 | printf("Blosc version: %s (%s)\n", BLOSC_VERSION_STRING, BLOSC_VERSION_DATE); |
|---|
| 188 | printf("Using random data with %d significant bits (out of 32)\n", rshift); |
|---|
| 189 | printf("Dataset size: %d bytes\tType size: %d bytes\n", size, elsize); |
|---|
| 190 | printf("Shuffle active? %s\t\t", doshuffle ? "Yes" : "No"); |
|---|
| 191 | printf("Number of threads: %d\n", nthreads); |
|---|
| 192 | printf("********************** Running benchmarks *********************\n"); |
|---|
| 193 | |
|---|
| 194 | gettimeofday(&last, NULL); |
|---|
| 195 | for (i = 0; i < NITER; i++) { |
|---|
| 196 | for (j = 0; j < NCHUNKS; j++) { |
|---|
| 197 | memcpy(dest[j], src, size); |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | gettimeofday(¤t, NULL); |
|---|
| 201 | tmemcpy = getseconds(last, current); |
|---|
| 202 | printf("memcpy(write):\t\t %6.1f us, %.1f MB/s\n", |
|---|
| 203 | tmemcpy, size/(tmemcpy*MB/1e6)); |
|---|
| 204 | |
|---|
| 205 | gettimeofday(&last, NULL); |
|---|
| 206 | for (i = 0; i < NITER; i++) { |
|---|
| 207 | for (j = 0; j < NCHUNKS; j++) { |
|---|
| 208 | memcpy(dest2, dest[j], size); |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | gettimeofday(¤t, NULL); |
|---|
| 212 | tmemcpy = getseconds(last, current); |
|---|
| 213 | printf("memcpy(read):\t\t %6.1f us, %.1f MB/s\n", |
|---|
| 214 | tmemcpy, size/(tmemcpy*MB/1e6)); |
|---|
| 215 | |
|---|
| 216 | for (clevel=1; clevel<10; clevel++) { |
|---|
| 217 | |
|---|
| 218 | printf("Compression level: %d\n", clevel); |
|---|
| 219 | //blosc_set_nthreads(clevel); |
|---|
| 220 | |
|---|
| 221 | gettimeofday(&last, NULL); |
|---|
| 222 | for (i = 0; i < NITER; i++) { |
|---|
| 223 | for (j = 0; j < NCHUNKS; j++) { |
|---|
| 224 | cbytes = blosc_compress(clevel, doshuffle, elsize, size, src, dest[j]); |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | gettimeofday(¤t, NULL); |
|---|
| 228 | tshuf = getseconds(last, current); |
|---|
| 229 | printf("compression(write):\t %6.1f us, %.1f MB/s\t ", |
|---|
| 230 | tshuf, size/(tshuf*MB/1e6)); |
|---|
| 231 | printf("Final bytes: %d ", cbytes); |
|---|
| 232 | if (cbytes > 0) { |
|---|
| 233 | printf("Compr ratio: %3.2f", size/(float)cbytes); |
|---|
| 234 | } |
|---|
| 235 | printf("\n"); |
|---|
| 236 | |
|---|
| 237 | /* Compressor was unable to compress. Copy the buffer manually. */ |
|---|
| 238 | if (cbytes == 0) { |
|---|
| 239 | for (j = 0; j < NCHUNKS; j++) { |
|---|
| 240 | memcpy(dest[j], src, size); |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | gettimeofday(&last, NULL); |
|---|
| 245 | for (i = 0; i < NITER; i++) { |
|---|
| 246 | for (j = 0; j < NCHUNKS; j++) { |
|---|
| 247 | if (cbytes == 0) { |
|---|
| 248 | memcpy(dest2, dest[j], size); |
|---|
| 249 | nbytes = size; |
|---|
| 250 | } |
|---|
| 251 | else { |
|---|
| 252 | nbytes = blosc_decompress(dest[j], dest2, size); |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | gettimeofday(¤t, NULL); |
|---|
| 257 | tunshuf = getseconds(last, current); |
|---|
| 258 | printf("decompression(read):\t %6.1f us, %.1f MB/s\t ", |
|---|
| 259 | tunshuf, nbytes/(tunshuf*MB/1e6)); |
|---|
| 260 | if (nbytes < 0) { |
|---|
| 261 | printf("FAILED. Error code: %d\n", nbytes); |
|---|
| 262 | } |
|---|
| 263 | /* printf("Orig bytes: %d\tFinal bytes: %d\n", cbytes, nbytes); */ |
|---|
| 264 | |
|---|
| 265 | /* Check if data has had a good roundtrip */ |
|---|
| 266 | orig = (unsigned char *)srccpy; |
|---|
| 267 | round = (unsigned char *)dest2; |
|---|
| 268 | for(i = 0; i<size; ++i){ |
|---|
| 269 | if (orig[i] != round[i]) { |
|---|
| 270 | printf("\nError: Original data and round-trip do not match in pos %d\n", |
|---|
| 271 | (int)i); |
|---|
| 272 | printf("Orig--> %x, round-trip--> %x\n", orig[i], round[i]); |
|---|
| 273 | goto out; |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | printf("OK\n"); |
|---|
| 278 | |
|---|
| 279 | } /* End clevel loop */ |
|---|
| 280 | |
|---|
| 281 | out: |
|---|
| 282 | free(src); free(srccpy); free(dest2); |
|---|
| 283 | for (i = 0; i < NCHUNKS; i++) { |
|---|
| 284 | free(dest[i]); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | /* Free blosc resources */ |
|---|
| 288 | blosc_free_resources(); |
|---|
| 289 | |
|---|
| 290 | return 0; |
|---|
| 291 | } |
|---|