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