f252a4fdae34c50c5ea08181c2e49166018fe8c2
[idzebra-moved-to-github.git] / isamb / benchisamb.c
1 /* $Id: benchisamb.c,v 1.1 2006-12-09 08:03:57 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, a, sizeof(int));
55     memcpy(&ib, b, 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     memcpy (*dst, *src, sizeof(int));
71     (*dst) += sizeof(int);
72     (*src) += sizeof(int);
73 }
74
75 void code_reset(void *p)
76 {
77 }
78 void code_stop(void *p)
79 {
80 }
81
82 struct read_info {
83     int val;
84     int step;
85
86     int no;
87     int max;
88     int insertMode;
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     memcpy (*dst, &x, sizeof(int));
102     (*dst)+=sizeof(int);
103
104     ri->val = ri->val + ri->step;
105     *insertMode = ri->insertMode;
106
107 #if 0
108     yaz_log(YLOG_LOG, "%d %5d", ri->insertMode, x);
109 #endif
110     return 1;
111 }
112
113 void bench_insert(ISAMB isb, int number_of_trees,
114                   int number_of_rounds, int number_of_elements)
115 {
116     ISAMC_I isamc_i;
117     ISAM_P *isamc_p = xmalloc(sizeof(ISAM_P) * number_of_trees);
118     struct read_info ri;
119     int round, i;
120
121     for (i = 0; i<number_of_trees; i++)
122         isamc_p[i] = 0; /* initially, is empty */
123
124     ri.val = 0;
125     ri.step = 1;
126     ri.insertMode = 1;
127     
128     for (round = 0; round < number_of_rounds; round++)
129     {
130 #if HAVE_SYS_TIMES_H
131 #if HAVE_SYS_TIME_H
132         struct tms tms1, tms2;
133         struct timeval start_time, end_time;
134         double usec;
135         times(&tms1);
136         gettimeofday(&start_time, 0);
137 #endif
138 #endif
139         for (i = 0; i<number_of_trees; i++)
140         {
141
142             /* insert a number of entries */
143             ri.no = 0;
144             
145             ri.val = (rand());
146             // ri.val = number_of_elements * round;
147             ri.max = number_of_elements;
148             
149             isamc_i.clientData = &ri;
150             isamc_i.read_item = code_read;
151             
152             isamb_merge (isb, &isamc_p[i] , &isamc_i);
153
154 #if 0
155             isamb_dump(isb, isamc_p[i], log_pr);
156 #endif       
157         }
158 #if HAVE_SYS_TIMES_H
159 #if HAVE_SYS_TIME_H      
160         gettimeofday(&end_time, 0);
161         times(&tms2);
162         
163         usec = (end_time.tv_sec - start_time.tv_sec) * 1000000.0 +
164             end_time.tv_usec - start_time.tv_usec;
165         
166         printf("%3d %8.6f %5.2f %5.2f\n",
167                  round+1,
168                  usec / 1000000,
169                  (double) (tms2.tms_utime - tms1.tms_utime)/100,
170                  (double) (tms2.tms_stime - tms1.tms_stime)/100);
171 #endif
172 #endif
173     }
174     xfree(isamc_p);
175 }
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
189     while ((ret = options("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 0:
203             fprintf(stderr, "bad arg: %s\n", arg);
204             exit(1);
205         default:
206             fprintf(stderr, "bad option. %s\n", ret);
207             exit(1);
208         }
209     }
210         
211     /* setup method (attributes) */
212     method.compare_item = compare_item;
213     method.log_item = log_item;
214     method.codec.start = code_start;
215     method.codec.encode = code_item;
216     method.codec.decode = code_item;
217     method.codec.reset = code_reset;
218     method.codec.stop = code_stop;
219
220     /* create block system */
221     bfs = bfs_create(0, 0);
222     if (!bfs)
223     {
224         yaz_log(YLOG_WARN, "bfs_create failed");
225         exit(1);
226     }
227
228     bf_reset(bfs);
229
230     /* create isam handle */
231     isb = isamb_open (bfs, "isamb", 1, &method, 0);
232     if (!isb)
233     {
234         yaz_log(YLOG_WARN, "isamb_open failed");
235         exit(2);
236     }
237     bench_insert(isb, number_of_isams, number_of_rounds, number_of_items);
238     
239     isamb_close(isb);
240
241     /* exit block system */
242     bfs_destroy(bfs);
243     exit(0);
244     return 0;
245 }
246 /*
247  * Local variables:
248  * c-basic-offset: 4
249  * indent-tabs-mode: nil
250  * End:
251  * vim: shiftwidth=4 tabstop=8 expandtab
252  */
253