dht reimplemented, 66% net execution time overall
[henge/apc.git] / src / testston.c
index 0f0f1f9..0c0fc19 100644 (file)
@@ -1,7 +1,10 @@
 #include <stdlib.h> //malloc
 #include <stdio.h> //print
 #include <string.h> //memcpy
+#include <time.h>   //clock
 #include "../ston/ston.h"
+#define XXH_PRIVATE_API
+#include "../xxHash/xxhash.h"
 
 /* ansi terminal colors */
 #define RED     "\x1b[31m"
 #define CLRC    "\x1b[0m" //clear current color
 #define CLRCN   CLRC"\n"
 
-#define print_val(val) do {                    \
-    for (i = 0; i < (size_t)columns; i++)      \
-      printf("["YELLOW"%x"CLRC"]",val[i]);     \
-    printf("\n");                              \
-  } while(0)
-
-#define check_ht(_HT)                                                  \
-  if ((_HT) == NULL)                                                   \
-    { fprintf(stderr,RED"Could not allocate ht32"CLRCN);               \
-      return -1;                                                       \
-    }                                                                  \
-  else                                                                 \
-    printf("ht_size: [units:%i][bytes:%li]\n",ston_ht_size(ht),ston_ht32_size(ht)); \
-
-#define check_dht(_HT)                                                 \
-  if ((_HT) == NULL)                                                   \
-    { fprintf(stderr,RED"Could not allocate dht32"CLRCN);              \
-      return -1;                                                       \
-    }                                                                  \
-  else                                                                 \
-    printf("ht_size: [units:%i][bytes:%i]\n",ston_dht_units(_HT,_HT->header.start_depth),ston_dht_bytes(_HT,_HT->header.start_depth)); \
+#define startclock(_STR) do {                  \
+    start = clock();                           \
+    printf(_STR": ");                          \
+  } while (0)
 
+#define startclockf(_STR,...) do {             \
+    start = clock();                           \
+    printf(_STR": ",__VA_ARGS__);              \
+  } while (0)
+  
+#define endclock() do {                                          \
+    end = clock();                                       \
+    printf("%5f",(end - start)/(double)CLOCKS_PER_SEC);   \
+  } while (0)
+
+#define endclockn() do {                                 \
+    end = clock();                                       \
+    printf("%5f\n",(end - start)/(double)CLOCKS_PER_SEC); \
+  } while (0)
+
+#define $($)#$
+#define do_test(_COL,_COUNT,_HT,_FREE,_HTNEW,_HTINSERT,_HTROW,_SEED) do { \
+    printf("[ " $(_HT) " ] ");                                         \
+    columns = _COL;                                                    \
+    count = _COUNT;                                                    \
+    _HT = _HTNEW;                                                      \
+    startclockf("Filling " $(_HT) " with %i entries",count);           \
+    while (count--)                                                    \
+      { key = XXH32(&count,4,_SEED);                                   \
+       val[0] = key;                                                   \
+       for (i = 0; i < columns; i++)                                   \
+         val[i] = key - i;                                             \
+       _HTINSERT(_HT,key,val,0,columns);                               \
+      }                                                                        \
+    endclockn();                                                       \
+    count = _COUNT;                                                    \
+    fail = 0;                                                          \
+    startclock("Reading entries");                                     \
+    while (count--)                                                    \
+      { key = XXH32(&count,4,_SEED);                                   \
+       htval = _HTROW(_HT,key);                                        \
+       if (htval == NULL)                                              \
+         { fail++;                                                     \
+           printf("Row returned null\n");                              \
+           break;                                                      \
+         }                                                             \
+       if (*htval != key)                                              \
+         fail++;                                                       \
+       for (i = 1; i < columns; i++)                                   \
+         if (htval[i] != key - i)                                      \
+           fail++;                                                     \
+      }                                                                        \
+    endclock();                                                                \
+    if (fail)                                                          \
+      printf(RED"\nFAIL"CLRC"(%i)\n", fail);                           \
+    else                                                               \
+      printf(GREEN"\nPASS"CLRCN);                                      \
+    _FREE(_HT);                                                                \
+  } while (0)
 
 int main(int argc, char* argv[])
 { static ston_ht   ht;
-  uint32_t* htval, previous_val;
+  static ston_dht  dht;
   uint32_t  val[255], key;
-  size_t    idx;
+  uint32_t* htval;
   size_t    i;
-  size_t    elements;
   uint16_t  columns;
   int       fail;
-
-  printf("up2pow [5] = [%i]\n",ston_up2pow(5));
-  for (i = 0; i < 77; i += 7)
-    printf("trailing0 [%X] = [%x]\n",(uint32_t)i,ston_trailing0(i));
-
-  columns = 2;
-  elements = 1;
-  ht = ston_ht32_new(columns,elements,0,malloc);
-  check_ht(ht);
-  key = 0xFF;
-  val[0] = key;
-  val[1] = 0x5;
-  printf("ht32_insertx: [%x] = ", key);
-  print_val(val);
-  ston_ht32_insertx(ht,key,val,0,columns);
-  idx = 1;
-  htval = ston_ht32_row(ht,key);
-  printf("Read value [%x] matches: %s"CLRCN, htval[idx],
-        (htval[idx] == val[idx]) ? GREEN"PASS" : RED"FAIL");
-
-  val[1] = 0x2;
-  previous_val = ston_ht32_insert(ht,key,1,val[1]);
-  printf("ht32_insert: [%x] = ", key);
-  print_val(val);
-  printf("Previous value [%x] matches [5]: %s"CLRCN, previous_val,
-        (previous_val == 0x5) ? GREEN"PASS" : RED"FAIL");
-
-  free(ht);
-
-  columns = 4;
-  elements = 20;
-  ht = ston_ht32_new(columns,elements,0,malloc);
-  check_ht(ht);
-  printf("Filling hashtable with %i entries\n", (int)elements);
-  for(key = 0xCEED; elements--; key *= 7)
-    { val[0] = key;
-      for(i = 1; i < columns; i++)
-       val[i] = i * key;
-      ston_ht32_insertx(ht,key,val,0,columns);
-    }
-  elements = 20;
-  printf("Reading entries: ");
-  fail = 0;
-  for(key = 0xCEED; elements--; key *= 7)
-    { htval = ston_ht32_row(ht,key);
-      if (*htval != key)
-       fail++;
-      for(i = 1; i < columns; i++)
-       if (htval[i] != (uint32_t)(i * key))
-         fail++;
-    }
-  if (fail)
-    printf(RED"FAIL"CLRC"(%i)\n", fail);
-  else
-    printf(GREEN"PASS"CLRCN);
-  int max_capacity = ston_up2pow(20 << 1) - 20;
-  int cap = max_capacity;
-  printf("Overfilling hashtable with %i entries\n", max_capacity);
-  for(key = 0xCEED2; cap--; key *= 13)
-    { val[0] = key;
-      for(i = 1; i < columns; i++)
-       val[i] = key * -i;
-      ston_ht32_insertx(ht,key,val,0,columns);
-    }
-  printf("Reading entries: ");
-  cap = max_capacity;
-  fail = 0;
-  for(key = 0xCEED2; cap--; key *= 13)
-    { htval = ston_ht32_row(ht,key);
-      if (*htval != key)
-       fail++;
-      for(i = 1; i < columns; i++)
-       if (htval[i] != (uint32_t)(key * -i))
-         fail++;
-    }
-  if (fail)
-    printf(RED"FAIL"CLRC"(%i)\n", fail);
-  else
-    printf(GREEN"PASS"CLRCN);
-
-  cap = 20;
-  printf("Post-capacity insertion of %i\n",cap);
-  for (key = 0xCEED3; cap--; key *= 23)
-    { val[0] = key;
-      for(i = 1; i < columns; i++)
-       val[i] = key * -i;
-      size_t count = ston_ht32_insertx(ht,key,val,0,columns);
-      printf("Insertion %2i wrote %i bytes: %s"CLRCN, (int)cap, (int) count,
-            (count == 0) ? GREEN"PASS" : RED"FAIL");
-    }
-  
-
-  printf("Refilling hashtable with %i entries\n", max_capacity);
-  cap = max_capacity;
-  for(key = 0xCEED2; cap--; key *= 13)
-    { val[0] = key;
-      for(i = 1; i < columns; i++)
-       val[i] = key * i;
-      ston_ht32_insertx(ht,key,val,0,columns);
-    }
-  printf("Reading entries: ");
-  cap = max_capacity;
-  fail = 0;
-  for(key = 0xCEED2; cap--; key *= 13)
-    { htval = ston_ht32_row(ht,key);
-      if (*htval != key)
-       fail ++;
-      for (i = 1; i < columns; i++)
-       if (htval[i] != (uint32_t)(key * i))
-         fail++;
-    }
-  if (fail)
-    printf(RED"FAIL"CLRC"(%i)\n", fail);
-  else
-    printf(GREEN"PASS"CLRCN);
-
-  free(ht);
-
-
-  printf("\n--------- DHT ----------\n\n");
-
-  ston_dht dht;
-  elements = 500;
-  columns = 6;
-  dht = ston_dht32_new(columns, malloc, free);
-  check_dht(dht);
-  elements = 50000;
-  printf("Filling Dynamic hashtable with %i entries\n", (int)elements);
-  for(key = 0xCEED; elements--; key *= 7)
-    { val[0] = key;
-      for(i = 1; i < columns; i++)
-       val[i] = i * key;
-      ston_dht32_insertx(dht,key,val,0,columns);
-    }
-  elements = 50000;
-  printf("Reading entries: ");
-  fail = 0;
-  for(key = 0xCEED; elements--; key *= 7)
-    { htval = ston_dht32_row(dht,key);
-      if (*htval != key)
-       fail++;
-      for(i = 1; i < columns; i++)
-       if (htval[i] != (uint32_t)(i * key))
-         fail++;
-    }
-  if (fail)
-    printf(RED"FAIL"CLRC"(%i)\n", fail);
-  else
-    printf(GREEN"PASS"CLRCN);
-  max_capacity = 100000;
-  cap = max_capacity;
-  printf("Overfilling hashtable with %i entries\n", max_capacity);
-  for(key = 0xCEED2; cap--; key *= 13)
-    { val[0] = key;
-      for(i = 1; i < columns; i++)
-       val[i] = key * -i;
-      ston_dht32_insertx(dht,key,val,0,columns);
-    }
-  printf("Reading entries: ");
-  cap = max_capacity;
-  fail = 0;
-  for(key = 0xCEED2; cap--; key *= 13)
-    { htval = ston_dht32_row(dht,key);
-      if (*htval != key)
-       fail++;
-      for(i = 1; i < columns; i++)
-       if (htval[i] != (uint32_t)(key * -i))
-         fail++;
-    }
-  if (fail)
-    printf(RED"FAIL"CLRC"(%i)\n", fail);
-  else
-    printf(GREEN"PASS"CLRCN);
-
-  max_capacity = 5000000;
-  printf("Refilling hashtable with %i entries\n", max_capacity);
-  cap = max_capacity;
-  for(key = 0xCEED2; cap--; key *= 13)
-    { val[0] = key;
-      for(i = 1; i < columns; i++)
-       val[i] = key * i;
-      ston_dht32_insertx(dht,key,val,0,columns);
-    }
-  printf("Reading entries: ");
-  cap = max_capacity;
-  fail = 0;
-  for(key = 0xCEED2; cap--; key *= 13)
-    { htval = ston_dht32_row(dht,key);
-      if (*htval != key)
-       fail ++;
-      for (i = 1; i < columns; i++)
-       if (htval[i] != (uint32_t)(key * i))
-         fail++;
-    }
-  if (fail)
-    printf(RED"FAIL"CLRC"(%i)\n", fail);
-  else
-    printf(GREEN"PASS"CLRCN);
-
-  ston_dht_free(dht);
-  
-  return 0;
+  clock_t   start, end;
+  int       count;
+  printf("\nLow usage:\n");
+  do_test(2,200,ht,free,ston_ht32_new(2,200,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+  do_test(5,200,ht,free,ston_ht32_new(5,200,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+  do_test(2,200,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+  do_test(5,200,dht,ston_dht_free,ston_dht32_new(5,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+
+  printf("\nLow-mid usage:\n");
+  do_test(2,10000,ht,free,ston_ht32_new(2,10000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+  do_test(5,10000,ht,free,ston_ht32_new(5,10000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+  do_test(2,10000,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+  do_test(5,10000,dht,ston_dht_free,ston_dht32_new(5,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+
+  printf("\nMid usage:\n");
+  do_test(2,500000,ht,free,ston_ht32_new(2,500000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+  do_test(5,500000,ht,free,ston_ht32_new(5,500000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+  do_test(2,500000,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+  do_test(5,500000,dht,ston_dht_free,ston_dht32_new(5,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+
+  printf("\nMid-high usage:\n");
+  do_test(2,9000000,ht,free,ston_ht32_new(2,9000000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+  do_test(5,9000000,ht,free,ston_ht32_new(5,9000000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+  do_test(2,9000000,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+  do_test(2,9000000,dht,ston_dht_free,ston_dht32_new(5,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+
+  /* printf("\nHigh usage:\n"); */
+  /* do_test(2,90000000,ht,free,ston_ht32_new(2,90000000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED); */
+  /* do_test(2,90000000,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED); */
+
+  /* printf("\nHuge usage:\n"); */
+  /* do_test(2,100000000,ht,free,ston_ht32_new(2,100000000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED); */
+  /* do_test(2,100000000,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED); */
+
+  printf("\nDONE\n");
 }