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