]> mj.ucw.cz Git - libucw.git/blob - ucw/sorter/sort-test.c
Bring interface of sorter/array-simple.h in line with sorter/array.h.
[libucw.git] / ucw / sorter / sort-test.c
1 /*
2  *      UCW Library -- Testing the Sorter
3  *
4  *      (c) 2007 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "ucw/lib.h"
11 #include "ucw/getopt.h"
12 #include "ucw/conf.h"
13 #include "ucw/fastbuf.h"
14 #include "ucw/ff-binary.h"
15 #include "ucw/hashfunc.h"
16 #include "ucw/md5.h"
17 #include "ucw/string.h"
18 #include "ucw/prime.h"
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25
26 /*** A hack for overriding radix-sorter configuration ***/
27
28 #ifdef FORCE_RADIX_BITS
29 #undef CONFIG_UCW_RADIX_SORTER_BITS
30 #define CONFIG_UCW_RADIX_SORTER_BITS FORCE_RADIX_BITS
31 #endif
32
33 /*** Time measurement ***/
34
35 static timestamp_t timer;
36 static uns test_id;
37
38 static void
39 start(void)
40 {
41   sync();
42   init_timer(&timer);
43 }
44
45 static void
46 stop(void)
47 {
48   sync();
49   msg(L_INFO, "Test %d took %.3fs", test_id, get_timer(&timer) / 1000.);
50 }
51
52 /*** Simple 4-byte integer keys ***/
53
54 struct key1 {
55   u32 x;
56 };
57
58 #define SORT_KEY_REGULAR struct key1
59 #define SORT_PREFIX(x) s1_##x
60 #define SORT_INPUT_FB
61 #define SORT_OUTPUT_FB
62 #define SORT_UNIQUE
63 #define SORT_INT(k) (k).x
64 #define SORT_DELETE_INPUT 0
65
66 #include "ucw/sorter/sorter.h"
67
68 static void
69 test_int(int mode, u64 size)
70 {
71   uns N = size ? nextprime(MIN(size/4, 0xffff0000)) : 0;
72   uns K = N/4*3;
73   msg(L_INFO, ">>> Integers (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
74
75   struct fastbuf *f = bopen_tmp(65536);
76   for (uns i=0; i<N; i++)
77     bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
78   brewind(f);
79
80   start();
81   f = s1_sort(f, NULL, N-1);
82   stop();
83
84   SORT_XTRACE(2, "Verifying");
85   for (uns i=0; i<N; i++)
86     {
87       uns j = bgetl(f);
88       if (i != j)
89         die("Discrepancy: %u instead of %u", j, i);
90     }
91   bclose(f);
92 }
93
94 /*** Integers with merging, but no data ***/
95
96 struct key2 {
97   u32 x;
98   u32 cnt;
99 };
100
101 static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uns n, void *buf UNUSED)
102 {
103   for (uns i=1; i<n; i++)
104     k[0]->cnt += k[i]->cnt;
105   bwrite(f, k[0], sizeof(struct key2));
106 }
107
108 #define SORT_KEY_REGULAR struct key2
109 #define SORT_PREFIX(x) s2_##x
110 #define SORT_INPUT_FB
111 #define SORT_OUTPUT_FB
112 #define SORT_UNIFY
113 #define SORT_INT(k) (k).x
114
115 #include "ucw/sorter/sorter.h"
116
117 static void
118 test_counted(int mode, u64 size)
119 {
120   u64 items = size / sizeof(struct key2);
121   uns mult = 2;
122   while (items/(2*mult) > 0xffff0000)
123     mult++;
124   uns N = items ? nextprime(items/(2*mult)) : 0;
125   uns K = N/4*3;
126   msg(L_INFO, ">>> Counted integers (%s, N=%u, mult=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult);
127
128   struct fastbuf *f = bopen_tmp(65536);
129   for (uns m=0; m<mult; m++)
130     for (uns i=0; i<N; i++)
131       for (uns j=0; j<2; j++)
132         {
133           bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
134           bputl(f, 1);
135         }
136   brewind(f);
137
138   start();
139   f = s2_sort(f, NULL, N-1);
140   stop();
141
142   SORT_XTRACE(2, "Verifying");
143   for (uns i=0; i<N; i++)
144     {
145       uns j = bgetl(f);
146       if (i != j)
147         die("Discrepancy: %u instead of %u", j, i);
148       uns k = bgetl(f);
149       if (k != 2*mult)
150         die("Discrepancy: %u has count %u instead of %u", j, k, 2*mult);
151     }
152   bclose(f);
153 }
154
155 /*** Longer records with hashes (similar to Shepherd's index records) ***/
156
157 struct key3 {
158   u32 hash[4];
159   u32 i;
160   u32 payload[3];
161 };
162
163 static inline int s3_compare(struct key3 *x, struct key3 *y)
164 {
165   COMPARE(x->hash[0], y->hash[0]);
166   COMPARE(x->hash[1], y->hash[1]);
167   COMPARE(x->hash[2], y->hash[2]);
168   COMPARE(x->hash[3], y->hash[3]);
169   return 0;
170 }
171
172 static inline uns s3_hash(struct key3 *x)
173 {
174   return x->hash[0];
175 }
176
177 #define SORT_KEY_REGULAR struct key3
178 #define SORT_PREFIX(x) s3_##x
179 #define SORT_INPUT_FB
180 #define SORT_OUTPUT_FB
181 #define SORT_HASH_BITS 32
182
183 #include "ucw/sorter/sorter.h"
184
185 static void
186 gen_hash_key(int mode, struct key3 *k, uns i)
187 {
188   k->i = i;
189   k->payload[0] = 7*i + 13;
190   k->payload[1] = 13*i + 19;
191   k->payload[2] = 19*i + 7;
192   switch (mode)
193     {
194     case 0:
195       k->hash[0] = i;
196       k->hash[1] = k->payload[0];
197       k->hash[2] = k->payload[1];
198       k->hash[3] = k->payload[2];
199       break;
200     case 1:
201       k->hash[0] = ~i;
202       k->hash[1] = k->payload[0];
203       k->hash[2] = k->payload[1];
204       k->hash[3] = k->payload[2];
205       break;
206     default: ;
207       md5_hash_buffer((byte *) &k->hash, (byte *) &k->i, 4);
208       break;
209     }
210 }
211
212 static void
213 test_hashes(int mode, u64 size)
214 {
215   uns N = MIN(size / sizeof(struct key3), 0xffffffff);
216   msg(L_INFO, ">>> Hashes (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
217   struct key3 k, lastk;
218
219   struct fastbuf *f = bopen_tmp(65536);
220   uns hash_sum = 0;
221   for (uns i=0; i<N; i++)
222     {
223       gen_hash_key(mode, &k, i);
224       hash_sum += k.hash[3];
225       bwrite(f, &k, sizeof(k));
226     }
227   brewind(f);
228
229   start();
230   f = s3_sort(f, NULL);
231   stop();
232
233   SORT_XTRACE(2, "Verifying");
234   for (uns i=0; i<N; i++)
235     {
236       int ok = breadb(f, &k, sizeof(k));
237       ASSERT(ok);
238       if (i && s3_compare(&k, &lastk) <= 0)
239         ASSERT(0);
240       gen_hash_key(mode, &lastk, k.i);
241       if (memcmp(&k, &lastk, sizeof(k)))
242         ASSERT(0);
243       hash_sum -= k.hash[3];
244     }
245   ASSERT(!hash_sum);
246   bclose(f);
247 }
248
249 /*** Variable-length records (strings) with and without var-length data ***/
250
251 #define KEY4_MAX 256
252
253 struct key4 {
254   uns len;
255   byte s[KEY4_MAX];
256 };
257
258 static inline int s4_compare(struct key4 *x, struct key4 *y)
259 {
260   uns l = MIN(x->len, y->len);
261   int c = memcmp(x->s, y->s, l);
262   if (c)
263     return c;
264   COMPARE(x->len, y->len);
265   return 0;
266 }
267
268 static inline int s4_read_key(struct fastbuf *f, struct key4 *x)
269 {
270   x->len = bgetl(f);
271   if (x->len == 0xffffffff)
272     return 0;
273   ASSERT(x->len < KEY4_MAX);
274   breadb(f, x->s, x->len);
275   return 1;
276 }
277
278 static inline void s4_write_key(struct fastbuf *f, struct key4 *x)
279 {
280   ASSERT(x->len < KEY4_MAX);
281   bputl(f, x->len);
282   bwrite(f, x->s, x->len);
283 }
284
285 #define SORT_KEY struct key4
286 #define SORT_PREFIX(x) s4_##x
287 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
288 #define SORT_INPUT_FB
289 #define SORT_OUTPUT_FB
290
291 #include "ucw/sorter/sorter.h"
292
293 #define s4b_compare s4_compare
294 #define s4b_read_key s4_read_key
295 #define s4b_write_key s4_write_key
296
297 static inline uns s4_data_size(struct key4 *x)
298 {
299   return x->len ? (x->s[0] ^ 0xad) : 0;
300 }
301
302 #define SORT_KEY struct key4
303 #define SORT_PREFIX(x) s4b_##x
304 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
305 #define SORT_DATA_SIZE(x) s4_data_size(&(x))
306 #define SORT_INPUT_FB
307 #define SORT_OUTPUT_FB
308
309 #include "ucw/sorter/sorter.h"
310
311 static void
312 gen_key4(struct key4 *k)
313 {
314   k->len = random_max(KEY4_MAX);
315   for (uns i=0; i<k->len; i++)
316     k->s[i] = random();
317 }
318
319 static void
320 gen_data4(byte *buf, uns len, uns h)
321 {
322   while (len--)
323     {
324       *buf++ = h >> 24;
325       h = h*259309 + 17;
326     }
327 }
328
329 static void
330 test_strings(uns mode, u64 size)
331 {
332   uns avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0);
333   uns N = MIN(size / avg_item_size, 0xffffffff);
334   msg(L_INFO, ">>> Strings %s(N=%u)", (mode ? "with data " : ""), N);
335   srand(1);
336
337   struct key4 k, lastk;
338   byte buf[256], buf2[256];
339   uns sum = 0;
340
341   struct fastbuf *f = bopen_tmp(65536);
342   for (uns i=0; i<N; i++)
343     {
344       gen_key4(&k);
345       s4_write_key(f, &k);
346       uns h = hash_block(k.s, k.len);
347       sum += h;
348       if (mode)
349         {
350           gen_data4(buf, s4_data_size(&k), h);
351           bwrite(f, buf, s4_data_size(&k));
352         }
353     }
354   brewind(f);
355
356   start();
357   f = (mode ? s4b_sort : s4_sort)(f, NULL);
358   stop();
359
360   SORT_XTRACE(2, "Verifying");
361   for (uns i=0; i<N; i++)
362     {
363       int ok = s4_read_key(f, &k);
364       ASSERT(ok);
365       uns h = hash_block(k.s, k.len);
366       if (mode && s4_data_size(&k))
367         {
368           ok = breadb(f, buf, s4_data_size(&k));
369           ASSERT(ok);
370           gen_data4(buf2, s4_data_size(&k), h);
371           ASSERT(!memcmp(buf, buf2, s4_data_size(&k)));
372         }
373       if (i && s4_compare(&k, &lastk) < 0)
374         ASSERT(0);
375       sum -= h;
376       lastk = k;
377     }
378   ASSERT(!sum);
379   bclose(f);
380 }
381
382 /*** Graph-like structure with custom presorting ***/
383
384 struct key5 {
385   u32 x;
386   u32 cnt;
387 };
388
389 static uns s5_N, s5_K, s5_L, s5_i, s5_j;
390
391 struct s5_pair {
392   uns x, y;
393 };
394
395 static int s5_gen(struct s5_pair *p)
396 {
397   if (s5_j >= s5_N)
398     {
399       if (!s5_N || s5_i >= s5_N-1)
400         return 0;
401       s5_j = 0;
402       s5_i++;
403     }
404   p->x = ((u64)s5_j * s5_K) % s5_N;
405   p->y = ((u64)(s5_i + s5_j) * s5_L) % s5_N;
406   s5_j++;
407   return 1;
408 }
409
410 #define ASORT_PREFIX(x) s5m_##x
411 #define ASORT_KEY_TYPE u32
412 #include "ucw/sorter/array-simple.h"
413
414 static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uns n, void *buf)
415 {
416   u32 *a = buf;
417   uns m = 0;
418   for (uns i=0; i<n; i++)
419     {
420       memcpy(&a[m], data[i], 4*keys[i]->cnt);
421       m += keys[i]->cnt;
422     }
423   s5m_sort(a, m);
424   keys[0]->cnt = m;
425   bwrite(f, keys[0], sizeof(struct key5));
426   bwrite(f, a, 4*m);
427 }
428
429 static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
430 {
431   u32 k[n];
432   uns m = 0;
433   for (uns i=0; i<n; i++)
434     {
435       k[i] = bgetl(data[i]);
436       m += keys[i]->cnt;
437     }
438   struct key5 key = { .x = keys[0]->x, .cnt = m };
439   bwrite(dest, &key, sizeof(key));
440   while (key.cnt--)
441     {
442       uns b = 0;
443       for (uns i=1; i<n; i++)
444         if (k[i] < k[b])
445           b = i;
446       bputl(dest, k[b]);
447       if (--keys[b]->cnt)
448         k[b] = bgetl(data[b]);
449       else
450         k[b] = ~0U;
451     }
452 }
453
454 static inline int s5p_lt(struct s5_pair x, struct s5_pair y)
455 {
456   COMPARE_LT(x.x, y.x);
457   COMPARE_LT(x.y, y.y);
458   return 0;
459 }
460
461 #define ASORT_PREFIX(x) s5p_##x
462 #define ASORT_KEY_TYPE struct s5_pair
463 #define ASORT_LT(x,y) s5p_lt(x,y)
464 #include "ucw/sorter/array.h"
465
466 static int s5_presort(struct fastbuf *dest, void *buf, size_t bufsize)
467 {
468   uns max = MIN(bufsize/sizeof(struct s5_pair), 0xffffffff);
469   struct s5_pair *a = buf;
470   uns n = 0;
471   while (n<max && s5_gen(&a[n]))
472     n++;
473   if (!n)
474     return 0;
475   s5p_sort(a, n);
476   uns i = 0;
477   while (i < n)
478     {
479       uns j = i;
480       while (i < n && a[i].x == a[j].x)
481         i++;
482       struct key5 k = { .x = a[j].x, .cnt = i-j };
483       bwrite(dest, &k, sizeof(k));
484       while (j < i)
485         bputl(dest, a[j++].y);
486     }
487   return 1;
488 }
489
490 #define SORT_KEY_REGULAR struct key5
491 #define SORT_PREFIX(x) s5_##x
492 #define SORT_DATA_SIZE(k) (4*(k).cnt)
493 #define SORT_UNIFY
494 #define SORT_UNIFY_WORKSPACE(k) SORT_DATA_SIZE(k)
495 #define SORT_INPUT_PRESORT
496 #define SORT_OUTPUT_THIS_FB
497 #define SORT_INT(k) (k).x
498
499 #include "ucw/sorter/sorter.h"
500
501 #define SORT_KEY_REGULAR struct key5
502 #define SORT_PREFIX(x) s5b_##x
503 #define SORT_DATA_SIZE(k) (4*(k).cnt)
504 #define SORT_UNIFY
505 #define SORT_UNIFY_WORKSPACE(k) SORT_DATA_SIZE(k)
506 #define SORT_INPUT_FB
507 #define SORT_OUTPUT_THIS_FB
508 #define SORT_INT(k) (k).x
509 #define s5b_write_merged s5_write_merged
510 #define s5b_copy_merged s5_copy_merged
511
512 #include "ucw/sorter/sorter.h"
513
514 static void
515 test_graph(uns mode, u64 size)
516 {
517   uns N = 3;
518   while ((u64)N*(N+2)*4 < size)
519     N = nextprime(N);
520   if (!size)
521     N = 0;
522   msg(L_INFO, ">>> Graph%s (N=%u)", (mode ? "" : " with custom presorting"), N);
523   s5_N = N;
524   s5_K = N/4*3;
525   s5_L = N/3*2;
526   s5_i = s5_j = 0;
527
528   struct fastbuf *in = NULL;
529   if (mode)
530     {
531       struct s5_pair p;
532       in = bopen_tmp(65536);
533       while (s5_gen(&p))
534         {
535           struct key5 k = { .x = p.x, .cnt = 1 };
536           bwrite(in, &k, sizeof(k));
537           bputl(in, p.y);
538         }
539       brewind(in);
540     }
541
542   start();
543   struct fastbuf *f = bopen_tmp(65536);
544   bputl(f, 0xfeedcafe);
545   struct fastbuf *g = (mode ? s5b_sort(in, f, s5_N-1) : s5_sort(NULL, f, s5_N-1));
546   ASSERT(f == g);
547   stop();
548
549   SORT_XTRACE(2, "Verifying");
550   uns c = bgetl(f);
551   ASSERT(c == 0xfeedcafe);
552   for (uns i=0; i<N; i++)
553     {
554       struct key5 k;
555       int ok = breadb(f, &k, sizeof(k));
556       ASSERT(ok);
557       ASSERT(k.x == i);
558       ASSERT(k.cnt == N);
559       for (uns j=0; j<N; j++)
560         {
561           uns y = bgetl(f);
562           ASSERT(y == j);
563         }
564     }
565   bclose(f);
566 }
567
568 /*** Simple 8-byte integer keys ***/
569
570 struct key6 {
571   u64 x;
572 };
573
574 #define SORT_KEY_REGULAR struct key6
575 #define SORT_PREFIX(x) s6_##x
576 #define SORT_INPUT_FB
577 #define SORT_OUTPUT_FB
578 #define SORT_UNIQUE
579 #define SORT_INT64(k) (k).x
580
581 #include "ucw/sorter/sorter.h"
582
583 static void
584 test_int64(int mode, u64 size)
585 {
586   u64 N = size ? nextprime(MIN(size/8, 0xffff0000)) : 0;
587   u64 K = N/4*3;
588   msg(L_INFO, ">>> 64-bit integers (%s, N=%llu)", ((char *[]) { "increasing", "decreasing", "random" })[mode], (long long)N);
589
590   struct fastbuf *f = bopen_tmp(65536);
591   for (u64 i=0; i<N; i++)
592     bputq(f, 777777*((mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N));
593   brewind(f);
594
595   start();
596   f = s6_sort(f, NULL, 777777*(N-1));
597   stop();
598
599   SORT_XTRACE(2, "Verifying");
600   for (u64 i=0; i<N; i++)
601     {
602       u64 j = bgetq(f);
603       if (777777*i != j)
604         die("Discrepancy: %llu instead of %llu", (long long)j, 777777*(long long)i);
605     }
606   bclose(f);
607 }
608
609 /*** Main ***/
610
611 static void
612 run_test(uns i, u64 size)
613 {
614   test_id = i;
615   switch (i)
616     {
617     case 0:
618       test_int(0, size); break;
619     case 1:
620       test_int(1, size); break;
621     case 2:
622       test_int(2, size); break;
623     case 3:
624       test_counted(0, size); break;
625     case 4:
626       test_counted(1, size); break;
627     case 5:
628       test_counted(2, size); break;
629     case 6:
630       test_hashes(0, size); break;
631     case 7:
632       test_hashes(1, size); break;
633     case 8:
634       test_hashes(2, size); break;
635     case 9:
636       test_strings(0, size); break;
637     case 10:
638       test_strings(1, size); break;
639     case 11:
640       test_graph(0, size); break;
641     case 12:
642       test_graph(1, size); break;
643     case 13:
644       test_int64(0, size); break;
645     case 14:
646       test_int64(1, size); break;
647     case 15:
648       test_int64(2, size); break;
649 #define TMAX 16
650     }
651 }
652
653 int
654 main(int argc, char **argv)
655 {
656   log_init(NULL);
657   int c;
658   u64 size = 10000000;
659   uns t = ~0;
660
661   while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS "d:s:t:v", CF_NO_LONG_OPTS, NULL)) >= 0)
662     switch (c)
663       {
664       case 'd':
665         sorter_debug = atol(optarg);
666         break;
667       case 's':
668         if (cf_parse_u64(optarg, &size))
669           goto usage;
670         break;
671       case 't':
672           {
673             char *w[32];
674             int f = str_sepsplit(optarg, ',', w, ARRAY_SIZE(w));
675             if (f < 0)
676               goto usage;
677             t = 0;
678             for (int i=0; i<f; i++)
679               {
680                 int j = atol(w[i]);
681                 if (j >= TMAX)
682                   goto usage;
683                 t |= 1 << j;
684               }
685           }
686         break;
687       case 'v':
688         sorter_trace++;
689         break;
690       default:
691       usage:
692         fputs("Usage: sort-test [-v] [-d <debug>] [-s <size>] [-t <test>]\n", stderr);
693         exit(1);
694       }
695   if (optind != argc)
696     goto usage;
697
698   for (uns i=0; i<TMAX; i++)
699     if (t & (1 << i))
700       run_test(i, size);
701
702   return 0;
703 }