Use rand() twice on platforms where RAND_MAX < 2^16.
[idzebra-moved-to-github.git] / isamb / benchisamb.c
1 /* $Id: benchisamb.c,v 1.3 2006-12-10 20:59:52 adam Exp $
2    Copyright (C) 1995-2006
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <yaz/options.h>
24 #if HAVE_SYS_TIMES_H
25 #include <sys/times.h>
26 #endif
27 #if HAVE_SYS_TIME_H
28 #include <sys/time.h>
29 #endif
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <yaz/log.h>
34 #include <yaz/xmalloc.h>
35 #include <idzebra/isamb.h>
36 #include <assert.h>
37
38 static void log_item(int level, const void *b, const char *txt)
39 {
40     int x;
41     memcpy(&x, b, sizeof(int));
42     yaz_log(YLOG_LOG, "%s %d", txt, x);
43 }
44
45 static void log_pr(const char *txt)
46 {
47     yaz_log(YLOG_LOG, "%s", txt);
48 }
49
50 int compare_item(const void *a, const void *b)
51 {
52     int ia, ib;
53
54     memcpy(&ia, (const char *) a + 1, sizeof(int));
55     memcpy(&ib, (const char *) b + 1, sizeof(int));
56     if (ia > ib)
57         return 1;
58     if (ia < ib)
59         return -1;
60    return 0;
61 }
62
63 void *code_start(void)
64 {
65     return 0;
66 }
67
68 void code_item(void *p, char **dst, const char **src)
69 {
70     int sz = **src;
71     memcpy (*dst, *src, sz);
72     (*dst) += sz;
73     (*src) += sz;
74 }
75
76 void code_reset(void *p)
77 {
78 }
79 void code_stop(void *p)
80 {
81 }
82
83 struct read_info {
84     int val;
85     int step;
86
87     int no;
88     int max;
89     int insertMode;
90     int sz;
91 };
92
93 int code_read(void *vp, char **dst, int *insertMode)
94 {
95     struct read_info *ri = (struct read_info *)vp;
96     int x;
97
98     if (ri->no >= ri->max)
99         return 0;
100     ri->no++;
101
102     x = ri->val;
103     memset(*dst, 0, ri->sz);
104     **dst = ri->sz;
105     memcpy(*dst + 1, &x, sizeof(int));
106
107     (*dst) += ri->sz;
108
109     ri->val = ri->val + ri->step;
110     *insertMode = ri->insertMode;
111
112 #if 0
113     yaz_log(YLOG_LOG, "%d %5d", ri->insertMode, x);
114 #endif
115     return 1;
116 }
117
118 void bench_insert(ISAMB isb, int number_of_trees,
119                   int number_of_rounds, int number_of_elements,
120                   int extra_size)
121 {
122     ISAMC_I isamc_i;
123     ISAM_P *isamc_p = xmalloc(sizeof(ISAM_P) * number_of_trees);
124     struct read_info ri;
125     int round, i;
126
127     for (i = 0; i<number_of_trees; i++)
128         isamc_p[i] = 0; /* initially, is empty */
129
130     ri.val = 0;
131     ri.step = 1;
132     ri.insertMode = 1;
133     ri.sz = sizeof(int) + 1 + extra_size;
134     
135     for (round = 0; round < number_of_rounds; round++)
136     {
137 #if HAVE_SYS_TIMES_H
138 #if HAVE_SYS_TIME_H
139         struct tms tms1, tms2;
140         struct timeval start_time, end_time;
141         double usec;
142         times(&tms1);
143         gettimeofday(&start_time, 0);
144 #endif
145 #endif
146         for (i = 0; i<number_of_trees; i++)
147         {
148
149             /* insert a number of entries */
150             ri.no = 0;
151           
152             ri.val = (rand());
153             if (RAND_MAX < 65536)  
154                 ri.val = ri.val + 65536*rand();
155
156             // ri.val = number_of_elements * round;
157             ri.max = number_of_elements;
158             
159             isamc_i.clientData = &ri;
160             isamc_i.read_item = code_read;
161             
162             isamb_merge (isb, &isamc_p[i] , &isamc_i);
163
164             if (0)
165                 isamb_dump(isb, isamc_p[i], log_pr);
166         }
167 #if HAVE_SYS_TIMES_H
168 #if HAVE_SYS_TIME_H      
169         gettimeofday(&end_time, 0);
170         times(&tms2);
171         
172         usec = (end_time.tv_sec - start_time.tv_sec) * 1000000.0 +
173             end_time.tv_usec - start_time.tv_usec;
174         
175         printf("%3d %8.6f %5.2f %5.2f\n",
176                  round+1,
177                  usec / 1000000,
178                  (double) (tms2.tms_utime - tms1.tms_utime)/100,
179                  (double) (tms2.tms_stime - tms1.tms_stime)/100);
180 #endif
181 #endif
182     }
183     xfree(isamc_p);
184 }
185
186 void exit_usage(void)
187 {
188     fprintf(stderr, "benchisamb [-r rounds] [-n items] [-i isams]\n");
189     exit(1);
190 }
191
192 int main(int argc, char **argv)
193 {
194     BFiles bfs;
195     ISAMB isb;
196     ISAMC_M method;
197     int ret;
198     char *arg;
199     int number_of_rounds = 10;
200     int number_of_items = 1000;
201     int number_of_isams = 1000;
202     int extra_size = 0;
203
204     while ((ret = options("z:r:n:i:", argv, argc, &arg)) != -2)
205     {
206         switch(ret)
207         {
208         case 'r':
209             number_of_rounds = atoi(arg);
210             break;
211         case 'n':
212             number_of_items = atoi(arg);
213             break;
214         case 'i':
215             number_of_isams = atoi(arg);
216             break;
217         case 'z':
218             extra_size = atoi(arg);
219             break;
220         case 0:
221             fprintf(stderr, "bad arg: %s\n", arg);
222             exit_usage();
223         default:
224             fprintf(stderr, "bad option.\n");
225             exit_usage();
226         }
227     }
228         
229     /* setup method (attributes) */
230     method.compare_item = compare_item;
231     method.log_item = log_item;
232     method.codec.start = code_start;
233     method.codec.encode = code_item;
234     method.codec.decode = code_item;
235     method.codec.reset = code_reset;
236     method.codec.stop = code_stop;
237
238     /* create block system */
239     bfs = bfs_create(0, 0);
240     if (!bfs)
241     {
242         yaz_log(YLOG_WARN, "bfs_create failed");
243         exit(1);
244     }
245
246     bf_reset(bfs);
247
248     /* create isam handle */
249     isb = isamb_open (bfs, "isamb", 1, &method, 0);
250     if (!isb)
251     {
252         yaz_log(YLOG_WARN, "isamb_open failed");
253         exit(2);
254     }
255     bench_insert(isb, number_of_isams, number_of_rounds, number_of_items,
256                  extra_size);
257     
258     isamb_close(isb);
259
260     /* exit block system */
261     bfs_destroy(bfs);
262     exit(0);
263     return 0;
264 }
265 /*
266  * Local variables:
267  * c-basic-offset: 4
268  * indent-tabs-mode: nil
269  * End:
270  * vim: shiftwidth=4 tabstop=8 expandtab
271  */
272