Remove Ubuntu karmic from build
[idzebra-moved-to-github.git] / isamb / benchisamb.c
index f252a4f..a726e62 100644 (file)
@@ -1,8 +1,5 @@
-/* $Id: benchisamb.c,v 1.1 2006-12-09 08:03:57 adam Exp $
-   Copyright (C) 1995-2006
-   Index Data ApS
-
-This file is part of the Zebra server.
+/* This file is part of the Zebra server.
+   Copyright (C) 1994-2011 Index Data
 
 Zebra is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -20,6 +17,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 */
 
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 #include <yaz/options.h>
 #if HAVE_SYS_TIMES_H
 #include <sys/times.h>
@@ -32,6 +32,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <string.h>
 #include <yaz/log.h>
 #include <yaz/xmalloc.h>
+#include <yaz/timing.h>
 #include <idzebra/isamb.h>
 #include <assert.h>
 
@@ -51,8 +52,8 @@ int compare_item(const void *a, const void *b)
 {
     int ia, ib;
 
-    memcpy(&ia, a, sizeof(int));
-    memcpy(&ib, b, sizeof(int));
+    memcpy(&ia, (const char *) a + 1, sizeof(int));
+    memcpy(&ib, (const char *) b + 1, sizeof(int));
     if (ia > ib)
        return 1;
     if (ia < ib)
@@ -67,9 +68,10 @@ void *code_start(void)
 
 void code_item(void *p, char **dst, const char **src)
 {
-    memcpy (*dst, *src, sizeof(int));
-    (*dst) += sizeof(int);
-    (*src) += sizeof(int);
+    int sz = **src;
+    memcpy (*dst, *src, sz);
+    (*dst) += sz;
+    (*src) += sz;
 }
 
 void code_reset(void *p)
@@ -86,6 +88,7 @@ struct read_info {
     int no;
     int max;
     int insertMode;
+    int sz;
 };
 
 int code_read(void *vp, char **dst, int *insertMode)
@@ -98,8 +101,11 @@ int code_read(void *vp, char **dst, int *insertMode)
     ri->no++;
 
     x = ri->val;
-    memcpy (*dst, &x, sizeof(int));
-    (*dst)+=sizeof(int);
+    memset(*dst, 0, ri->sz);
+    **dst = ri->sz;
+    memcpy(*dst + 1, &x, sizeof(int));
+
+    (*dst) += ri->sz;
 
     ri->val = ri->val + ri->step;
     *insertMode = ri->insertMode;
@@ -111,7 +117,8 @@ int code_read(void *vp, char **dst, int *insertMode)
 }
 
 void bench_insert(ISAMB isb, int number_of_trees,
-                  int number_of_rounds, int number_of_elements)
+                  int number_of_rounds, int number_of_elements,
+                  int extra_size)
 {
     ISAMC_I isamc_i;
     ISAM_P *isamc_p = xmalloc(sizeof(ISAM_P) * number_of_trees);
@@ -124,25 +131,23 @@ void bench_insert(ISAMB isb, int number_of_trees,
     ri.val = 0;
     ri.step = 1;
     ri.insertMode = 1;
+    ri.sz = sizeof(int) + 1 + extra_size;
     
     for (round = 0; round < number_of_rounds; round++)
     {
-#if HAVE_SYS_TIMES_H
-#if HAVE_SYS_TIME_H
-        struct tms tms1, tms2;
-        struct timeval start_time, end_time;
-        double usec;
-        times(&tms1);
-        gettimeofday(&start_time, 0);
-#endif
-#endif
+        yaz_timing_t t = yaz_timing_create();
+
+        yaz_timing_start(t);
         for (i = 0; i<number_of_trees; i++)
         {
 
             /* insert a number of entries */
             ri.no = 0;
-            
+          
             ri.val = (rand());
+            if (RAND_MAX < 65536)  
+                ri.val = ri.val + 65536*rand();
+
             // ri.val = number_of_elements * round;
             ri.max = number_of_elements;
             
@@ -151,29 +156,25 @@ void bench_insert(ISAMB isb, int number_of_trees,
             
             isamb_merge (isb, &isamc_p[i] , &isamc_i);
 
-#if 0
-            isamb_dump(isb, isamc_p[i], log_pr);
-#endif       
+            if (0)
+                isamb_dump(isb, isamc_p[i], log_pr);
         }
-#if HAVE_SYS_TIMES_H
-#if HAVE_SYS_TIME_H      
-        gettimeofday(&end_time, 0);
-        times(&tms2);
-        
-        usec = (end_time.tv_sec - start_time.tv_sec) * 1000000.0 +
-            end_time.tv_usec - start_time.tv_usec;
-        
+        yaz_timing_stop(t);
         printf("%3d %8.6f %5.2f %5.2f\n",
-                 round+1,
-                 usec / 1000000,
-                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
-                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
-#endif
-#endif
+               round+1,
+               yaz_timing_get_real(t),
+               yaz_timing_get_user(t),
+               yaz_timing_get_sys(t));
+        yaz_timing_destroy(&t);
     }
     xfree(isamc_p);
 }
 
+void exit_usage(void)
+{
+    fprintf(stderr, "benchisamb [-r rounds] [-n items] [-i isams]\n");
+    exit(1);
+}
 
 int main(int argc, char **argv)
 {
@@ -185,8 +186,10 @@ int main(int argc, char **argv)
     int number_of_rounds = 10;
     int number_of_items = 1000;
     int number_of_isams = 1000;
-
-    while ((ret = options("r:n:i:", argv, argc, &arg)) != -2)
+    int extra_size = 0;
+    yaz_timing_t t = 0;
+    
+    while ((ret = options("z:r:n:i:", argv, argc, &arg)) != -2)
     {
         switch(ret)
         {
@@ -199,12 +202,15 @@ int main(int argc, char **argv)
         case 'i':
             number_of_isams = atoi(arg);
             break;
+        case 'z':
+            extra_size = atoi(arg);
+            break;
         case 0:
             fprintf(stderr, "bad arg: %s\n", arg);
-            exit(1);
+            exit_usage();
         default:
-            fprintf(stderr, "bad option. %s\n", ret);
-            exit(1);
+            fprintf(stderr, "bad option.\n");
+            exit_usage();
         }
     }
        
@@ -217,6 +223,10 @@ int main(int argc, char **argv)
     method.codec.reset = code_reset;
     method.codec.stop = code_stop;
 
+    t = yaz_timing_create();
+    
+    yaz_timing_start(t);
+
     /* create block system */
     bfs = bfs_create(0, 0);
     if (!bfs)
@@ -234,18 +244,27 @@ int main(int argc, char **argv)
        yaz_log(YLOG_WARN, "isamb_open failed");
        exit(2);
     }
-    bench_insert(isb, number_of_isams, number_of_rounds, number_of_items);
+    bench_insert(isb, number_of_isams, number_of_rounds, number_of_items,
+                 extra_size);
     
     isamb_close(isb);
 
     /* exit block system */
     bfs_destroy(bfs);
+
+    yaz_timing_stop(t);
+    printf("Total %8.6f %5.2f %5.2f\n",
+           yaz_timing_get_real(t),
+           yaz_timing_get_user(t),
+           yaz_timing_get_sys(t));
+    yaz_timing_destroy(&t);
     exit(0);
     return 0;
 }
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab