More digits in output for total docs
[idzebra-moved-to-github.git] / isamb / benchisamb.c
1 /* $Id: benchisamb.c,v 1.4 2006-12-11 10:02:14 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         zebra_timing_t t = zebra_timing_create();
138
139         zebra_timing_start(t);
140         for (i = 0; i<number_of_trees; i++)
141         {
142
143             /* insert a number of entries */
144             ri.no = 0;
145           
146             ri.val = (rand());
147             if (RAND_MAX < 65536)  
148                 ri.val = ri.val + 65536*rand();
149
150             // ri.val = number_of_elements * round;
151             ri.max = number_of_elements;
152             
153             isamc_i.clientData = &ri;
154             isamc_i.read_item = code_read;
155             
156             isamb_merge (isb, &isamc_p[i] , &isamc_i);
157
158             if (0)
159                 isamb_dump(isb, isamc_p[i], log_pr);
160         }
161         zebra_timing_stop(t);
162         printf("%3d %8.6f %5.2f %5.2f\n",
163                round+1,
164                zebra_timing_get_real(t),
165                zebra_timing_get_user(t),
166                zebra_timing_get_sys(t));
167         zebra_timing_destroy(&t);
168     }
169     xfree(isamc_p);
170 }
171
172 void exit_usage(void)
173 {
174     fprintf(stderr, "benchisamb [-r rounds] [-n items] [-i isams]\n");
175     exit(1);
176 }
177
178 int main(int argc, char **argv)
179 {
180     BFiles bfs;
181     ISAMB isb;
182     ISAMC_M method;
183     int ret;
184     char *arg;
185     int number_of_rounds = 10;
186     int number_of_items = 1000;
187     int number_of_isams = 1000;
188     int extra_size = 0;
189     zebra_timing_t t = 0;
190     
191     while ((ret = options("z:r:n:i:", argv, argc, &arg)) != -2)
192     {
193         switch(ret)
194         {
195         case 'r':
196             number_of_rounds = atoi(arg);
197             break;
198         case 'n':
199             number_of_items = atoi(arg);
200             break;
201         case 'i':
202             number_of_isams = atoi(arg);
203             break;
204         case 'z':
205             extra_size = atoi(arg);
206             break;
207         case 0:
208             fprintf(stderr, "bad arg: %s\n", arg);
209             exit_usage();
210         default:
211             fprintf(stderr, "bad option.\n");
212             exit_usage();
213         }
214     }
215         
216     /* setup method (attributes) */
217     method.compare_item = compare_item;
218     method.log_item = log_item;
219     method.codec.start = code_start;
220     method.codec.encode = code_item;
221     method.codec.decode = code_item;
222     method.codec.reset = code_reset;
223     method.codec.stop = code_stop;
224
225     t = zebra_timing_create();
226     
227     zebra_timing_start(t);
228
229     /* create block system */
230     bfs = bfs_create(0, 0);
231     if (!bfs)
232     {
233         yaz_log(YLOG_WARN, "bfs_create failed");
234         exit(1);
235     }
236
237     bf_reset(bfs);
238
239     /* create isam handle */
240     isb = isamb_open (bfs, "isamb", 1, &method, 0);
241     if (!isb)
242     {
243         yaz_log(YLOG_WARN, "isamb_open failed");
244         exit(2);
245     }
246     bench_insert(isb, number_of_isams, number_of_rounds, number_of_items,
247                  extra_size);
248     
249     isamb_close(isb);
250
251     /* exit block system */
252     bfs_destroy(bfs);
253
254     zebra_timing_stop(t);
255     printf("Total %8.6f %5.2f %5.2f\n",
256            zebra_timing_get_real(t),
257            zebra_timing_get_user(t),
258            zebra_timing_get_sys(t));
259     zebra_timing_destroy(&t);
260     exit(0);
261     return 0;
262 }
263 /*
264  * Local variables:
265  * c-basic-offset: 4
266  * indent-tabs-mode: nil
267  * End:
268  * vim: shiftwidth=4 tabstop=8 expandtab
269  */
270