1 /* $Id: tstisamb.c,v 1.26 2006-12-07 21:13:56 adam Exp $
2 Copyright (C) 1995-2006
5 This file is part of the Zebra server.
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
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
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
24 #include <sys/times.h>
33 #include <yaz/xmalloc.h>
34 #include <idzebra/isamb.h>
37 static void log_item(int level, const void *b, const char *txt)
40 memcpy(&x, b, sizeof(int));
41 yaz_log(YLOG_DEBUG, "%s %d", txt, x);
44 static void log_pr(const char *txt)
46 yaz_log(YLOG_DEBUG, "%s", txt);
49 int compare_item(const void *a, const void *b)
53 memcpy(&ia, a, sizeof(int));
54 memcpy(&ib, b, sizeof(int));
62 void *code_start(void)
67 void code_item(void *p, char **dst, const char **src)
69 memcpy (*dst, *src, sizeof(int));
70 (*dst) += sizeof(int);
71 (*src) += sizeof(int);
74 void code_reset(void *p)
77 void code_stop(void *p)
90 int code_read(void *vp, char **dst, int *insertMode)
92 struct read_info *ri = (struct read_info *)vp;
95 if (ri->no >= ri->max)
100 memcpy (*dst, &x, sizeof(int));
103 ri->val = ri->val + ri->step;
104 *insertMode = ri->insertMode;
107 yaz_log(YLOG_LOG, "%d %5d", ri->insertMode, x);
112 void bench_insert(ISAMB isb, int number_of_trees,
113 int number_of_rounds, int number_of_elements)
116 ISAM_P *isamc_p = xmalloc(sizeof(ISAM_P) * number_of_trees);
120 for (i = 0; i<number_of_trees; i++)
121 isamc_p[i] = 0; /* initially, is empty */
127 for (round = 0; round < number_of_rounds; round++)
131 struct tms tms1, tms2;
132 struct timeval start_time, end_time;
135 gettimeofday(&start_time, 0);
138 for (i = 0; i<number_of_trees; i++)
141 /* insert a number of entries */
145 ri.max = number_of_elements;
147 isamc_i.clientData = &ri;
148 isamc_i.read_item = code_read;
151 isamb_merge (isb, &isamc_p[i] , &isamc_i);
155 gettimeofday(&end_time, 0);
158 usec = (end_time.tv_sec - start_time.tv_sec) * 1000000.0 +
159 end_time.tv_usec - start_time.tv_usec;
161 yaz_log (YLOG_LOG, "round=%d times: %5.4f %5.2f %5.2f",
164 (double) (tms2.tms_utime - tms1.tms_utime)/100,
165 (double) (tms2.tms_stime - tms1.tms_stime)/100);
171 void tst_insert(ISAMB isb, int n)
180 /* insert a number of entries */
188 isamc_i.clientData = &ri;
189 isamc_i.read_item = code_read;
191 isamc_p = 0; /* new list */
192 isamb_merge (isb, &isamc_p , &isamc_i);
194 /* read the entries */
195 pp = isamb_pp_open (isb, isamc_p, 1);
198 while(isamb_pp_read (pp, key_buf))
201 memcpy (&x, key_buf, sizeof(int));
204 yaz_log(YLOG_WARN, "isamb_pp_read. n=%d Got %d (expected %d)",
209 yaz_log(YLOG_LOG, "isamb_pp_read. n=%d Got %d",
214 if (ri.val != ri.max)
216 yaz_log(YLOG_WARN, "ri.max != ri.max (%d != %d)", ri.val, ri.max);
219 isamb_dump(isb, isamc_p, log_pr);
224 /* delete a number of entries (even ones) */
232 isamc_i.clientData = &ri;
233 isamc_i.read_item = code_read;
235 isamb_merge (isb, &isamc_p , &isamc_i);
237 /* delete a number of entries (odd ones) */
245 isamc_i.clientData = &ri;
246 isamc_i.read_item = code_read;
248 isamb_merge (isb, &isamc_p, &isamc_i);
252 yaz_log(YLOG_WARN, "isamb_merge did not return empty list n=%d",
258 void tst_forward(ISAMB isb, int n)
266 /* insert a number of entries */
274 isamc_i.clientData = &ri;
275 isamc_i.read_item = code_read;
278 isamb_merge (isb, &isamc_p, &isamc_i);
280 /* read the entries */
281 pp = isamb_pp_open (isb, isamc_p, 1);
283 for (i = 0; i<ri.max; i +=2 )
287 isamb_pp_forward(pp, &x, &xu);
288 if (x != xu && xu != x+1)
290 yaz_log(YLOG_WARN, "isamb_pp_forward (1). Got %d (expected %d)",
298 pp = isamb_pp_open (isb, isamc_p, 1);
299 for (i = 0; i<ri.max; i += 100)
303 isamb_pp_forward(pp, &x, &xu);
304 if (x != xu && xu != x+1)
306 yaz_log(YLOG_WARN, "isamb_pp_forward (2). Got %d (expected %d)",
314 isamb_unlink(isb, isamc_p);
317 void tst_x(ISAMB isb)
323 isamc_i.clientData = &ri;
324 isamc_i.read_item = code_read;
332 isamb_merge (isb, &isamb_p , &isamc_i);
341 isamb_merge (isb, &isamb_p , &isamc_i);
344 void tst_append(ISAMB isb, int n)
352 for (i = 0; i < n; i += chunk)
354 /* insert a number of entries */
362 isamc_i.clientData = &ri;
363 isamc_i.read_item = code_read;
365 isamb_merge (isb, &isamb_p , &isamc_i);
370 struct random_read_info {
377 int tst_random_read(void *vp, char **dst, int *insertMode)
379 struct random_read_info *ri = (struct random_read_info *)vp;
382 while(ri->idx < ri->max && ri->delta[ri->idx] == ri->level)
387 if (ri->idx >= ri->max)
390 if (ri->delta[ri->idx] > 0)
401 memcpy (*dst, &x, sizeof(int));
404 yaz_log(YLOG_DEBUG, "%d %5d", *insertMode, x);
408 void tst_random(ISAMB isb, int n, int rounds, int max_dups)
412 int *freq = malloc(sizeof(int) * n);
413 int *delta = malloc(sizeof(int) * n);
415 for (i = 0; i<n; i++)
418 for (j = 0; j<rounds; j++)
420 yaz_log(YLOG_DEBUG, "round %d", j);
421 for (i = 0; i<n; i++)
424 delta[i] = (rand() % (1+max_dups)) - freq[i];
431 struct random_read_info ri;
438 isamc_i.clientData = &ri;
439 isamc_i.read_item = tst_random_read;
441 isamb_merge (isb, &isamb_p , &isamc_i);
444 yaz_log(YLOG_DEBUG, "dump %d", j);
445 isamb_dump(isb, isamb_p, log_pr);
447 yaz_log(YLOG_DEBUG, "----------------------------");
448 for (i = 0; i<n; i++)
453 for (i = 0; i<n; i++)
456 yaz_log(YLOG_WARN, "isamb_merge returned 0, but "
457 "freq is non-empty");
466 ISAMB_PP pp = isamb_pp_open (isb, isamb_p, 1);
468 yaz_log(YLOG_DEBUG, "test %d", j);
470 while(isamb_pp_read (pp, key_buf))
473 memcpy (&x, key_buf, sizeof(int));
474 yaz_log(YLOG_DEBUG, "Got %d", x);
475 while (idx < n && freq[idx] == level)
482 yaz_log(YLOG_WARN, "tst_random: Extra item: %d", x);
487 yaz_log(YLOG_WARN, "tst_random: Mismatch %d != %d",
493 while (idx < n && freq[idx] == level)
500 yaz_log(YLOG_WARN, "tst_random: Missing item: %d", idx);
510 /* \fn void tst_minsert(ISAMB isb, int n)
511 \brief insert inserts n identical keys, removes n/2, then n-n/2 ..
512 \param isb ISAMB handle
513 \param n number of keys
515 void tst_minsert(ISAMB isb, int n)
521 isamc_i.clientData = &ri;
523 /* all have same value = 1 */
527 isamc_i.read_item = code_read;
534 isamb_merge (isb, &isamb_p , &isamc_i);
536 isamb_dump(isb, isamb_p, log_pr);
543 isamb_merge (isb, &isamb_p , &isamc_i);
550 isamb_merge (isb, &isamb_p , &isamc_i);
553 yaz_log(YLOG_WARN, "tst_minsert: isamb_merge should be empty n=%d",
559 /* tests for identical keys.. ISAMB does not handle that, so some of the
562 static void identical_keys_tests(ISAMB isb)
565 tst_minsert(isb, 10);
568 tst_minsert(isb, 600); /* still fails */
571 tst_random(isb, 20, 200, 1);
574 tst_random(isb, 5, 200, 2);
578 tst_random(isb, 250, 10, 4);
581 /* fails if both are executed */
582 tst_random(isb, 20000, 10, 4);
583 tst_random(isb, 20000, 10, 10);
586 tst_random(isb, 250, 100, 10);
590 int main(int argc, char **argv)
597 yaz_log_init_level(YLOG_ALL);
599 /* setup method (attributes) */
600 method.compare_item = compare_item;
601 method.log_item = log_item;
602 method.codec.start = code_start;
603 method.codec.encode = code_item;
604 method.codec.decode = code_item;
605 method.codec.reset = code_reset;
606 method.codec.stop = code_stop;
608 /* create block system */
609 bfs = bfs_create(0, 0);
612 yaz_log(YLOG_WARN, "bfs_create failed");
618 /* create isam handle */
619 isb = isamb_open (bfs, "isamb", 1, &method, 0);
622 yaz_log(YLOG_WARN, "isamb_open failed");
626 bench_insert(isb, 1000, 100, 10000);
631 tst_insert(isb, 100);
632 tst_insert(isb, 500);
633 tst_insert(isb, 10000);
635 tst_forward(isb, 10000);
639 tst_append(isb, 1000);
643 identical_keys_tests(isb);
647 /* exit block system */
655 * indent-tabs-mode: nil
657 * vim: shiftwidth=4 tabstop=8 expandtab