e0dd385ccd062a660b4562639db598aefde227ce
[idzebra-moved-to-github.git] / isamb / benchisamb.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 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 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
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 <yaz/timing.h>
36 #include <idzebra/isamb.h>
37 #include <assert.h>
38
39 static void log_item(int level, const void *b, const char *txt)
40 {
41     int x;
42     memcpy(&x, b, sizeof(int));
43     yaz_log(YLOG_LOG, "%s %d", txt, x);
44 }
45
46 static void log_pr(const char *txt)
47 {
48     yaz_log(YLOG_LOG, "%s", txt);
49 }
50
51 int compare_item(const void *a, const void *b)
52 {
53     int ia, ib;
54
55     memcpy(&ia, (const char *) a + 1, sizeof(int));
56     memcpy(&ib, (const char *) b + 1, sizeof(int));
57     if (ia > ib)
58         return 1;
59     if (ia < ib)
60         return -1;
61    return 0;
62 }
63
64 void *code_start(void)
65 {
66     return 0;
67 }
68
69 void code_item(void *p, char **dst, const char **src)
70 {
71     int sz = **src;
72     memcpy (*dst, *src, sz);
73     (*dst) += sz;
74     (*src) += sz;
75 }
76
77 void code_reset(void *p)
78 {
79 }
80 void code_stop(void *p)
81 {
82 }
83
84 struct read_info {
85     int val;
86     int step;
87
88     int no;
89     int max;
90     int insertMode;
91     int sz;
92 };
93
94 int code_read(void *vp, char **dst, int *insertMode)
95 {
96     struct read_info *ri = (struct read_info *)vp;
97     int x;
98
99     if (ri->no >= ri->max)
100         return 0;
101     ri->no++;
102
103     x = ri->val;
104     memset(*dst, 0, ri->sz);
105     **dst = ri->sz;
106     memcpy(*dst + 1, &x, sizeof(int));
107
108     (*dst) += ri->sz;
109
110     ri->val = ri->val + ri->step;
111     *insertMode = ri->insertMode;
112
113 #if 0
114     yaz_log(YLOG_LOG, "%d %5d", ri->insertMode, x);
115 #endif
116     return 1;
117 }
118
119 void bench_insert(ISAMB isb, int number_of_trees,
120                   int number_of_rounds, int number_of_elements,
121                   int extra_size)
122 {
123     ISAMC_I isamc_i;
124     ISAM_P *isamc_p = xmalloc(sizeof(ISAM_P) * number_of_trees);
125     struct read_info ri;
126     int round, i;
127
128     for (i = 0; i<number_of_trees; i++)
129         isamc_p[i] = 0; /* initially, is empty */
130
131     ri.val = 0;
132     ri.step = 1;
133     ri.insertMode = 1;
134     ri.sz = sizeof(int) + 1 + extra_size;
135
136     for (round = 0; round < number_of_rounds; round++)
137     {
138         yaz_timing_t t = yaz_timing_create();
139
140         yaz_timing_start(t);
141         for (i = 0; i<number_of_trees; i++)
142         {
143
144             /* insert a number of entries */
145             ri.no = 0;
146
147             ri.val = (rand());
148             if (RAND_MAX < 65536)
149                 ri.val = ri.val + 65536*rand();
150
151             // ri.val = number_of_elements * round;
152             ri.max = number_of_elements;
153
154             isamc_i.clientData = &ri;
155             isamc_i.read_item = code_read;
156
157             isamb_merge (isb, &isamc_p[i] , &isamc_i);
158
159             if (0)
160                 isamb_dump(isb, isamc_p[i], log_pr);
161         }
162         yaz_timing_stop(t);
163         printf("%3d %8.6f %5.2f %5.2f\n",
164                round+1,
165                yaz_timing_get_real(t),
166                yaz_timing_get_user(t),
167                yaz_timing_get_sys(t));
168         yaz_timing_destroy(&t);
169     }
170     xfree(isamc_p);
171 }
172
173 void exit_usage(void)
174 {
175     fprintf(stderr, "benchisamb [-r rounds] [-n items] [-i isams]\n");
176     exit(1);
177 }
178
179 int main(int argc, char **argv)
180 {
181     BFiles bfs;
182     ISAMB isb;
183     ISAMC_M method;
184     int ret;
185     char *arg;
186     int number_of_rounds = 10;
187     int number_of_items = 1000;
188     int number_of_isams = 1000;
189     int extra_size = 0;
190     yaz_timing_t t = 0;
191
192     while ((ret = options("z:r:n:i:", argv, argc, &arg)) != -2)
193     {
194         switch(ret)
195         {
196         case 'r':
197             number_of_rounds = atoi(arg);
198             break;
199         case 'n':
200             number_of_items = atoi(arg);
201             break;
202         case 'i':
203             number_of_isams = atoi(arg);
204             break;
205         case 'z':
206             extra_size = atoi(arg);
207             break;
208         case 0:
209             fprintf(stderr, "bad arg: %s\n", arg);
210             exit_usage();
211         default:
212             fprintf(stderr, "bad option.\n");
213             exit_usage();
214         }
215     }
216
217     /* setup method (attributes) */
218     method.compare_item = compare_item;
219     method.log_item = log_item;
220     method.codec.start = code_start;
221     method.codec.encode = code_item;
222     method.codec.decode = code_item;
223     method.codec.reset = code_reset;
224     method.codec.stop = code_stop;
225
226     t = yaz_timing_create();
227
228     yaz_timing_start(t);
229
230     /* create block system */
231     bfs = bfs_create(0, 0);
232     if (!bfs)
233     {
234         yaz_log(YLOG_WARN, "bfs_create failed");
235         exit(1);
236     }
237
238     bf_reset(bfs);
239
240     /* create isam handle */
241     isb = isamb_open (bfs, "isamb", 1, &method, 0);
242     if (!isb)
243     {
244         yaz_log(YLOG_WARN, "isamb_open failed");
245         exit(2);
246     }
247     bench_insert(isb, number_of_isams, number_of_rounds, number_of_items,
248                  extra_size);
249
250     isamb_close(isb);
251
252     /* exit block system */
253     bfs_destroy(bfs);
254
255     yaz_timing_stop(t);
256     printf("Total %8.6f %5.2f %5.2f\n",
257            yaz_timing_get_real(t),
258            yaz_timing_get_user(t),
259            yaz_timing_get_sys(t));
260     yaz_timing_destroy(&t);
261     exit(0);
262     return 0;
263 }
264 /*
265  * Local variables:
266  * c-basic-offset: 4
267  * c-file-style: "Stroustrup"
268  * indent-tabs-mode: nil
269  * End:
270  * vim: shiftwidth=4 tabstop=8 expandtab
271  */
272