source: trunk/src/bench.c @ 70

Revision 70, 5.9 KB checked in by faltet, 3 years ago (diff)

bench.c tests data on a file now.

Line 
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 NITER  (1000)               /* Number of iterations */
38
39
40#ifdef _WIN32
41#include <windows.h>
42#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
43  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
44#else
45  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
46#endif
47
48struct timezone
49{
50  int  tz_minuteswest; /* minutes W of Greenwich */
51  int  tz_dsttime;     /* type of dst correction */
52};
53
54int gettimeofday(struct timeval *tv, struct timezone *tz)
55{
56  FILETIME ft;
57  unsigned __int64 tmpres = 0;
58  static int tzflag;
59
60  if (NULL != tv)
61  {
62    GetSystemTimeAsFileTime(&ft);
63
64    tmpres |= ft.dwHighDateTime;
65    tmpres <<= 32;
66    tmpres |= ft.dwLowDateTime;
67
68    /*converting file time to unix epoch*/
69    tmpres -= DELTA_EPOCH_IN_MICROSECS;
70    tmpres /= 10;  /*convert into microseconds*/
71    tv->tv_sec = (long)(tmpres / 1000000UL);
72    tv->tv_usec = (long)(tmpres % 1000000UL);
73  }
74
75  if (NULL != tz)
76  {
77    if (!tzflag)
78    {
79      _tzset();
80      tzflag++;
81    }
82    tz->tz_minuteswest = _timezone / 60;
83    tz->tz_dsttime = _daylight;
84  }
85
86  return 0;
87}
88#endif   /* _WIN32 */
89
90
91/* Given two timeval stamps, return the difference in seconds */
92float getseconds(struct timeval last, struct timeval current) {
93  int sec, usec;
94
95  sec = current.tv_sec - last.tv_sec;
96  usec = current.tv_usec - last.tv_usec;
97  return (float)(((double)sec + usec*1e-6)/(double)NITER*1e6);
98}
99
100
101int main(void) {
102  int nbytes, cbytes;
103  void *src, *srccpy, *dest, *dest2;
104  size_t i;
105  struct timeval last, current;
106  float tmemcpy, tshuf, tunshuf;
107  int *_src;
108  int *_srccpy;
109  int rshift = 22;              /* For random data */
110  //int rshift = 20;              /* For random data */
111  int clevel;
112  int doshuffle = 1;            /* Shuffle? */
113  int fd;
114  int status;
115  char *filename = "25Kelem-4B-typesize.data";
116  unsigned int size = 100*1000; /* Buffer size */
117  unsigned int elsize = 4;      /* Datatype size */
118  unsigned char *orig, *round;
119
120  src = malloc(size);
121  srccpy = malloc(size);
122  dest = malloc(size);
123  dest2 = malloc(size);
124
125  srand(1);
126
127  /* Initialize the original buffer */
128  /* _src = (int *)src; */
129  /* _srccpy = (int *)srccpy; */
130  /* elsize = sizeof(int); */
131  /* for(i = 0; i < size/elsize; ++i) { */
132  /*   /\* Choose one below *\/ */
133  /*   /\* _src[i] = 1; *\/ */
134  /*   /\* _src[i] = 0x01010101; *\/ */
135  /*   /\* _src[i] = 0x01020304; *\/ */
136  /*   /\* _src[i] = i * 1/.3; *\/ */
137  /*   /\* _src[i] = i; *\/ */
138  /*   _src[i] = rand() >> rshift; */
139  /* } */
140
141  /* For data coming from a file */
142  fd = open(filename, 0);
143  status = read(fd, src, size);
144  if (status == -1) {
145    perror(NULL);
146  }
147  close(fd);
148
149  printf("********************** Setup info *****************************\n");
150  printf("Blosc version: %s (%s)\n", BLOSC_VERSION_STRING, BLOSC_VERSION_DATE);
151/*  printf("Using random data with %d significant bits (out of 32)\n", 32-rshift); */
152  printf("Using data coming from file: %s\n", filename);
153  printf("Dataset size: %d bytes\t Type size: %d bytes\n", size, elsize);
154  printf("Shuffle active?  %s\n", doshuffle ? "Yes" : "No");
155  printf("********************** Running benchmarks *********************\n");
156
157  memcpy(srccpy, src, size);
158
159  gettimeofday(&last, NULL);
160  for (i = 0; i < NITER; i++) {
161    memcpy(srccpy, src, size);
162  }
163  gettimeofday(&current, NULL);
164  tmemcpy = getseconds(last, current);
165  printf("memcpy:\t\t %6.1f us, %.1f MB/s\n", tmemcpy, size/(tmemcpy*MB/1e6));
166
167  //blosc_init_threads(1);
168
169  for (clevel=1; clevel<10; clevel++) {
170
171    printf("Compression level: %d\n", clevel);
172
173    gettimeofday(&last, NULL);
174    for (i = 0; i < NITER; i++) {
175      cbytes = blosc_compress(clevel, doshuffle, elsize, size, src, dest);
176    }
177    gettimeofday(&current, NULL);
178    tshuf = getseconds(last, current);
179    printf("compression:\t %6.1f us, %.1f MB/s\t  ",
180           tshuf, size/(tshuf*MB/1e6));
181    printf("Final bytes: %d  ", cbytes);
182    if (cbytes > 0) {
183      printf("Compr ratio: %3.2f", size/(float)cbytes);
184    }
185    printf("\n");
186
187    gettimeofday(&last, NULL);
188    for (i = 0; i < NITER; i++)
189      if (cbytes == 0) {
190        memcpy(dest2, src, size);
191        nbytes = size;
192      }
193      else {
194        nbytes = blosc_decompress(dest, dest2, size);
195      }
196    gettimeofday(&current, NULL);
197    tunshuf = getseconds(last, current);
198    printf("decompression:\t %6.1f us, %.1f MB/s\t  ",
199           tunshuf, nbytes/(tunshuf*MB/1e6));
200    if (nbytes < 0) {
201      printf("FAILED.  Error code: %d\n", nbytes);
202    }
203    /* printf("Orig bytes: %d\tFinal bytes: %d\n", cbytes, nbytes); */
204
205    /* Check if data has had a good roundtrip */
206    orig = (unsigned char *)srccpy;
207    round = (unsigned char *)dest2;
208    for(i = 0; i<size; ++i){
209      if (orig[i] != round[i]) {
210        printf("\nError: Original data and round-trip do not match in pos %d\n",
211               (int)i);
212        printf("Orig--> %x, round-trip--> %x\n", orig[i], round[i]);
213        goto out;
214      }
215    }
216
217    printf("OK\n");
218
219  } /* End clevel loop */
220
221 out:
222  free(src); free(srccpy); free(dest); free(dest2);
223  return 0;
224}
Note: See TracBrowser for help on using the repository browser.