]> mj.ucw.cz Git - libucw.git/blobdiff - lib/sorter/sort-test.c
Added a couple of tests with the old sorter to have reference for speed benchmarks.
[libucw.git] / lib / sorter / sort-test.c
index 5c932b65a9f70af6ca2fa48aeadffff3131ad234..5464f744fd1eb226d2af7df4a82d8b43805021fb 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "lib/lib.h"
 #include "lib/getopt.h"
 
 #include "lib/lib.h"
 #include "lib/getopt.h"
+#include "lib/conf.h"
 #include "lib/fastbuf.h"
 #include "lib/hashfunc.h"
 #include "lib/md5.h"
 #include "lib/fastbuf.h"
 #include "lib/hashfunc.h"
 #include "lib/md5.h"
 #include <stdio.h>
 #include <string.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
 #include <fcntl.h>
+#include <unistd.h>
+
+/*** Time measurement ***/
+
+static void
+start(void)
+{
+  sync();
+  init_timer();
+}
+
+static void
+stop(void)
+{
+  sync();
+  log(L_INFO, "Test took %.3fs", get_timer() / 1000.);
+}
 
 /*** Simple 4-byte integer keys ***/
 
 
 /*** Simple 4-byte integer keys ***/
 
@@ -34,26 +52,27 @@ struct key1 {
 #include "lib/sorter/sorter.h"
 
 static void
 #include "lib/sorter/sorter.h"
 
 static void
-test_int(int mode, uns N)
+test_int(int mode, u64 size)
 {
 {
-  N = nextprime(N);
+  uns N = size ? nextprime(MIN(size/4, 0xffff0000)) : 0;
   uns K = N/4*3;
   uns K = N/4*3;
-  log(L_INFO, "Integers (%s, N=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
+  log(L_INFO, ">>> Integers (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
 
   struct fastbuf *f = bopen_tmp(65536);
   for (uns i=0; i<N; i++)
     bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
   brewind(f);
 
 
   struct fastbuf *f = bopen_tmp(65536);
   for (uns i=0; i<N; i++)
     bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
   brewind(f);
 
-  log(L_INFO, "Sorting");
+  start();
   f = s1_sort(f, NULL, N-1);
   f = s1_sort(f, NULL, N-1);
+  stop();
 
 
-  log(L_INFO, "Verifying");
+  SORT_XTRACE(2, "Verifying");
   for (uns i=0; i<N; i++)
     {
       uns j = bgetl(f);
       if (i != j)
   for (uns i=0; i<N; i++)
     {
       uns j = bgetl(f);
       if (i != j)
-       die("Discrepancy: %d instead of %d", j, i);
+       die("Discrepancy: %u instead of %u", j, i);
     }
   bclose(f);
 }
     }
   bclose(f);
 }
@@ -89,33 +108,39 @@ static inline void s2_copy_merged(struct key2 **k, struct fastbuf **d UNUSED, un
 #include "lib/sorter/sorter.h"
 
 static void
 #include "lib/sorter/sorter.h"
 
 static void
-test_counted(int mode, uns N)
+test_counted(int mode, u64 size)
 {
 {
-  N = nextprime(N/4);
+  u64 items = size / sizeof(struct key2);
+  uns mult = 2;
+  while (items/(2*mult) > 0xffff0000)
+    mult++;
+  uns N = items ? nextprime(items/(2*mult)) : 0;
   uns K = N/4*3;
   uns K = N/4*3;
-  log(L_INFO, "Counted integers (%s, N=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
+  log(L_INFO, ">>> Counted integers (%s, N=%u, mult=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult);
 
   struct fastbuf *f = bopen_tmp(65536);
 
   struct fastbuf *f = bopen_tmp(65536);
-  for (uns i=0; i<2*N; i++)
-    for (uns j=0; j<2; j++)
-      {
-       bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
-       bputl(f, 1);
-      }
+  for (uns m=0; m<mult; m++)
+    for (uns i=0; i<N; i++)
+      for (uns j=0; j<2; j++)
+       {
+         bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
+         bputl(f, 1);
+       }
   brewind(f);
 
   brewind(f);
 
-  log(L_INFO, "Sorting");
+  start();
   f = s2_sort(f, NULL, N-1);
   f = s2_sort(f, NULL, N-1);
+  stop();
 
 
-  log(L_INFO, "Verifying");
+  SORT_XTRACE(2, "Verifying");
   for (uns i=0; i<N; i++)
     {
       uns j = bgetl(f);
       if (i != j)
   for (uns i=0; i<N; i++)
     {
       uns j = bgetl(f);
       if (i != j)
-       die("Discrepancy: %d instead of %d", j, i);
+       die("Discrepancy: %u instead of %u", j, i);
       uns k = bgetl(f);
       uns k = bgetl(f);
-      if (k != 4)
-       die("Discrepancy: %d has count %d instead of 4", j, k);
+      if (k != 2*mult)
+       die("Discrepancy: %u has count %u instead of %u", j, k, mult);
     }
   bclose(f);
 }
     }
   bclose(f);
 }
@@ -180,9 +205,10 @@ gen_hash_key(int mode, struct key3 *k, uns i)
 }
 
 static void
 }
 
 static void
-test_hashes(int mode, uns N)
+test_hashes(int mode, u64 size)
 {
 {
-  log(L_INFO, "Hashes (%s, N=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
+  uns N = MIN(size / sizeof(struct key3), 0xffffffff);
+  log(L_INFO, ">>> Hashes (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
   struct key3 k, lastk;
 
   struct fastbuf *f = bopen_tmp(65536);
   struct key3 k, lastk;
 
   struct fastbuf *f = bopen_tmp(65536);
@@ -195,10 +221,11 @@ test_hashes(int mode, uns N)
     }
   brewind(f);
 
     }
   brewind(f);
 
-  log(L_INFO, "Sorting");
+  start();
   f = s3_sort(f, NULL);
   f = s3_sort(f, NULL);
+  stop();
 
 
-  log(L_INFO, "Verifying");
+  SORT_XTRACE(2, "Verifying");
   for (uns i=0; i<N; i++)
     {
       int ok = breadb(f, &k, sizeof(k));
   for (uns i=0; i<N; i++)
     {
       int ok = breadb(f, &k, sizeof(k));
@@ -295,9 +322,11 @@ gen_data4(byte *buf, uns len, uns h)
 }
 
 static void
 }
 
 static void
-test_strings(uns mode, uns N)
+test_strings(uns mode, u64 size)
 {
 {
-  log(L_INFO, "Strings %s(N=%d)", (mode ? "with data " : ""), N);
+  uns avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0);
+  uns N = MIN(size / avg_item_size, 0xffffffff);
+  log(L_INFO, ">>> Strings %s(N=%u)", (mode ? "with data " : ""), N);
   srand(1);
 
   struct key4 k, lastk;
   srand(1);
 
   struct key4 k, lastk;
@@ -319,10 +348,11 @@ test_strings(uns mode, uns N)
     }
   brewind(f);
 
     }
   brewind(f);
 
-  log(L_INFO, "Sorting");
+  start();
   f = (mode ? s4b_sort : s4_sort)(f, NULL);
   f = (mode ? s4b_sort : s4_sort)(f, NULL);
+  stop();
 
 
-  log(L_INFO, "Verifying");
+  SORT_XTRACE(2, "Verifying");
   for (uns i=0; i<N; i++)
     {
       int ok = s4_read_key(f, &k);
   for (uns i=0; i<N; i++)
     {
       int ok = s4_read_key(f, &k);
@@ -344,31 +374,270 @@ test_strings(uns mode, uns N)
   bclose(f);
 }
 
   bclose(f);
 }
 
+/*** Graph-like structure with custom presorting ***/
+
+struct key5 {
+  u32 x;
+  u32 cnt;
+};
+
+static uns s5_N, s5_K, s5_L, s5_i, s5_j;
+
+struct s5_pair {
+  uns x, y;
+};
+
+static int s5_gen(struct s5_pair *p)
+{
+  if (s5_j >= s5_N)
+    {
+      if (s5_i >= s5_N-1)
+       return 0;
+      s5_j = 0;
+      s5_i++;
+    }
+  p->x = ((u64)s5_j * s5_K) % s5_N;
+  p->y = ((u64)(s5_i + s5_j) * s5_L) % s5_N;
+  s5_j++;
+  return 1;
+}
+
+#define ASORT_PREFIX(x) s5m_##x
+#define ASORT_KEY_TYPE u32
+#define ASORT_ELT(i) ary[i]
+#define ASORT_EXTRA_ARGS , u32 *ary
+#include "lib/arraysort.h"
+
+static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uns n, void *buf)
+{
+  /* FIXME: Allow mode where this function is not defined? */
+  u32 *a = buf;
+  uns m = 0;
+  for (uns i=0; i<n; i++)
+    {
+      memcpy(&a[m], data[i], 4*keys[i]->cnt);
+      m += keys[i]->cnt;
+    }
+  s5m_sort(m, a);
+  keys[0]->cnt = m;
+  bwrite(f, keys[0], sizeof(struct key5));
+  bwrite(f, a, 4*m);                   /* FIXME: Might overflow here */
+}
+
+static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
+{
+  u32 k[n];
+  uns m = 0;
+  for (uns i=0; i<n; i++)
+    {
+      k[i] = bgetl(data[i]);
+      m += keys[i]->cnt;
+    }
+  struct key5 key = { .x = keys[0]->x, .cnt = m };
+  bwrite(dest, &key, sizeof(key));
+  while (key.cnt--)
+    {
+      uns b = 0;
+      for (uns i=1; i<n; i++)
+       if (k[i] < k[b])
+         b = i;
+      bputl(dest, k[b]);
+      if (--keys[b]->cnt)
+       k[b] = bgetl(data[b]);
+      else
+       k[b] = ~0U;
+    }
+}
+
+static inline int s5p_lt(struct s5_pair x, struct s5_pair y)
+{
+  COMPARE_LT(x.x, y.x);
+  COMPARE_LT(x.y, y.y);
+  return 0;
+}
+
+/* FIXME: Use smarter internal sorter when it's available */
+#define ASORT_PREFIX(x) s5p_##x
+#define ASORT_KEY_TYPE struct s5_pair
+#define ASORT_ELT(i) ary[i]
+#define ASORT_LT(x,y) s5p_lt(x,y)
+#define ASORT_EXTRA_ARGS , struct s5_pair *ary
+#include "lib/arraysort.h"
+
+static int s5_presort(struct fastbuf *dest, void *buf, size_t bufsize)
+{
+  uns max = MIN(bufsize/sizeof(struct s5_pair), 0xffffffff);
+  struct s5_pair *a = buf;
+  uns n = 0;
+  while (n<max && s5_gen(&a[n]))
+    n++;
+  if (!n)
+    return 0;
+  s5p_sort(n, a);
+  uns i = 0;
+  while (i < n)
+    {
+      uns j = i;
+      while (i < n && a[i].x == a[j].x)
+       i++;
+      struct key5 k = { .x = a[j].x, .cnt = i-j };
+      bwrite(dest, &k, sizeof(k));
+      while (j < i)
+       bputl(dest, a[j++].y);
+    }
+  return 1;
+}
+
+#define SORT_KEY_REGULAR struct key5
+#define SORT_PREFIX(x) s5_##x
+#define SORT_DATA_SIZE(k) (4*(k).cnt)
+#define SORT_UNIFY
+#define SORT_INPUT_PRESORT
+#define SORT_OUTPUT_THIS_FB
+#define SORT_INT(k) (k).x
+
+#include "lib/sorter/sorter.h"
+
+#define SORT_KEY_REGULAR struct key5
+#define SORT_PREFIX(x) s5b_##x
+#define SORT_DATA_SIZE(k) (4*(k).cnt)
+#define SORT_UNIFY
+#define SORT_INPUT_FB
+#define SORT_OUTPUT_THIS_FB
+#define SORT_INT(k) (k).x
+#define s5b_write_merged s5_write_merged
+#define s5b_copy_merged s5_copy_merged
+
+#include "lib/sorter/sorter.h"
+
+static void
+test_graph(uns mode, u64 size)
+{
+  uns N = 3;
+  while ((u64)N*(N+2)*4 < size)
+    N = nextprime(N);
+  log(L_INFO, ">>> Graph%s (N=%u)", (mode ? "" : " with custom presorting"), N);
+  s5_N = N;
+  s5_K = N/4*3;
+  s5_L = N/3*2;
+  s5_i = s5_j = 0;
+
+  struct fastbuf *in = NULL;
+  if (mode)
+    {
+      struct s5_pair p;
+      in = bopen_tmp(65536);
+      while (s5_gen(&p))
+       {
+         struct key5 k = { .x = p.x, .cnt = 1 };
+         bwrite(in, &k, sizeof(k));
+         bputl(in, p.y);
+       }
+      brewind(in);
+    }
+
+  start();
+  struct fastbuf *f = bopen_tmp(65536);
+  bputl(f, 0xfeedcafe);
+  struct fastbuf *g = (mode ? s5b_sort(in, f, s5_N-1) : s5_sort(NULL, f, s5_N-1));
+  ASSERT(f == g);
+  stop();
+
+  SORT_XTRACE(2, "Verifying");
+  uns c = bgetl(f);
+  ASSERT(c == 0xfeedcafe);
+  for (uns i=0; i<N; i++)
+    {
+      struct key5 k;
+      int ok = breadb(f, &k, sizeof(k));
+      ASSERT(ok);
+      ASSERT(k.x == i);
+      ASSERT(k.cnt == N);
+      for (uns j=0; j<N; j++)
+       {
+         uns y = bgetl(f);
+         ASSERT(y == j);
+       }
+    }
+  bclose(f);
+}
+
+/*** Main ***/
+
+static void
+run_test(uns i, u64 size)
+{
+  switch (i)
+    {
+    case 0:
+      test_int(0, size); break;
+    case 1:
+      test_int(1, size); break;
+    case 2:
+      test_int(2, size); break;
+    case 3:
+      test_counted(0, size); break;
+    case 4:
+      test_counted(1, size); break;
+    case 5:
+      test_counted(2, size); break;
+    case 6:
+      test_hashes(0, size); break;
+    case 7:
+      test_hashes(1, size); break;
+    case 8:
+      test_hashes(2, size); break;
+    case 9:
+      test_strings(0, size); break;
+    case 10:
+      test_strings(1, size); break;
+    case 11:
+      test_graph(0, size); break;
+    case 12:
+      test_graph(1, size); break;
+#define TMAX 13
+    }
+}
+
 int
 main(int argc, char **argv)
 {
   log_init(NULL);
 int
 main(int argc, char **argv)
 {
   log_init(NULL);
-  if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 ||
-      optind != argc)
-  {
-    fputs("This program supports only the following command-line arguments:\n" CF_USAGE, stderr);
-    exit(1);
-  }
-
-  uns N = 100000;
-#if 0
-  test_int(0, N);
-  test_int(1, N);
-  test_int(2, N);
-  test_counted(0, N);
-  test_counted(1, N);
-  test_counted(2, N);
-  test_hashes(0, N);
-  test_hashes(1, N);
-  test_hashes(2, N);
-  test_strings(0, N);
-#endif
-  test_strings(1, N);
+  int c;
+  u64 size = 10000000;
+  uns t = ~0;
+
+  while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS "d:s:t:v", CF_NO_LONG_OPTS, NULL)) >= 0)
+    switch (c)
+      {
+      case 'd':
+       sorter_debug = atol(optarg);
+       break;
+      case 's':
+       if (cf_parse_u64(optarg, &size))
+         goto usage;
+       break;
+      case 't':
+       t = atol(optarg);
+       if (t >= TMAX)
+         goto usage;
+       break;
+      case 'v':
+       sorter_trace++;
+       break;
+      default:
+      usage:
+       fputs("Usage: sort-test [-v] [-d <debug>] [-s <size>] [-t <test>]\n", stderr);
+       exit(1);
+      }
+  if (optind != argc)
+    goto usage;
+
+  if (t != ~0U)
+    run_test(t, size);
+  else
+    for (uns i=0; i<TMAX; i++)
+      run_test(i, size);
 
   return 0;
 }
 
   return 0;
 }