2 * UCW Library -- Testing the Sorter
4 * (c) 2007 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/getopt.h"
13 #include "lib/fastbuf.h"
14 #include "lib/ff-binary.h"
15 #include "lib/hashfunc.h"
24 /*** A hack for overriding radix-sorter configuration ***/
26 #ifdef FORCE_RADIX_BITS
27 #undef CONFIG_UCW_RADIX_SORTER_BITS
28 #define CONFIG_UCW_RADIX_SORTER_BITS FORCE_RADIX_BITS
31 /*** Time measurement ***/
33 static timestamp_t timer;
47 msg(L_INFO, "Test %d took %.3fs", test_id, get_timer(&timer) / 1000.);
50 /*** Simple 4-byte integer keys ***/
56 #define SORT_KEY_REGULAR struct key1
57 #define SORT_PREFIX(x) s1_##x
59 #define SORT_OUTPUT_FB
61 #define SORT_INT(k) (k).x
62 #define SORT_DELETE_INPUT 0
64 #include "lib/sorter/sorter.h"
67 test_int(int mode, u64 size)
69 uns N = size ? nextprime(MIN(size/4, 0xffff0000)) : 0;
71 msg(L_INFO, ">>> Integers (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
73 struct fastbuf *f = bopen_tmp(65536);
74 for (uns i=0; i<N; i++)
75 bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
79 f = s1_sort(f, NULL, N-1);
82 SORT_XTRACE(2, "Verifying");
83 for (uns i=0; i<N; i++)
87 die("Discrepancy: %u instead of %u", j, i);
92 /*** Integers with merging, but no data ***/
99 static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uns n, void *buf UNUSED)
101 for (uns i=1; i<n; i++)
102 k[0]->cnt += k[i]->cnt;
103 bwrite(f, k[0], sizeof(struct key2));
106 #define SORT_KEY_REGULAR struct key2
107 #define SORT_PREFIX(x) s2_##x
108 #define SORT_INPUT_FB
109 #define SORT_OUTPUT_FB
111 #define SORT_INT(k) (k).x
113 #include "lib/sorter/sorter.h"
116 test_counted(int mode, u64 size)
118 u64 items = size / sizeof(struct key2);
120 while (items/(2*mult) > 0xffff0000)
122 uns N = items ? nextprime(items/(2*mult)) : 0;
124 msg(L_INFO, ">>> Counted integers (%s, N=%u, mult=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult);
126 struct fastbuf *f = bopen_tmp(65536);
127 for (uns m=0; m<mult; m++)
128 for (uns i=0; i<N; i++)
129 for (uns j=0; j<2; j++)
131 bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
137 f = s2_sort(f, NULL, N-1);
140 SORT_XTRACE(2, "Verifying");
141 for (uns i=0; i<N; i++)
145 die("Discrepancy: %u instead of %u", j, i);
148 die("Discrepancy: %u has count %u instead of %u", j, k, 2*mult);
153 /*** Longer records with hashes (similar to Shepherd's index records) ***/
161 static inline int s3_compare(struct key3 *x, struct key3 *y)
163 COMPARE(x->hash[0], y->hash[0]);
164 COMPARE(x->hash[1], y->hash[1]);
165 COMPARE(x->hash[2], y->hash[2]);
166 COMPARE(x->hash[3], y->hash[3]);
170 static inline uns s3_hash(struct key3 *x)
175 #define SORT_KEY_REGULAR struct key3
176 #define SORT_PREFIX(x) s3_##x
177 #define SORT_INPUT_FB
178 #define SORT_OUTPUT_FB
179 #define SORT_HASH_BITS 32
181 #include "lib/sorter/sorter.h"
184 gen_hash_key(int mode, struct key3 *k, uns i)
187 k->payload[0] = 7*i + 13;
188 k->payload[1] = 13*i + 19;
189 k->payload[2] = 19*i + 7;
194 k->hash[1] = k->payload[0];
195 k->hash[2] = k->payload[1];
196 k->hash[3] = k->payload[2];
200 k->hash[1] = k->payload[0];
201 k->hash[2] = k->payload[1];
202 k->hash[3] = k->payload[2];
205 struct MD5Context ctx;
207 MD5Update(&ctx, (byte*) &k->i, 4);
208 MD5Final((byte*) &k->hash, &ctx);
214 test_hashes(int mode, u64 size)
216 uns N = MIN(size / sizeof(struct key3), 0xffffffff);
217 msg(L_INFO, ">>> Hashes (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
218 struct key3 k, lastk;
220 struct fastbuf *f = bopen_tmp(65536);
222 for (uns i=0; i<N; i++)
224 gen_hash_key(mode, &k, i);
225 hash_sum += k.hash[3];
226 bwrite(f, &k, sizeof(k));
231 f = s3_sort(f, NULL);
234 SORT_XTRACE(2, "Verifying");
235 for (uns i=0; i<N; i++)
237 int ok = breadb(f, &k, sizeof(k));
239 if (i && s3_compare(&k, &lastk) <= 0)
241 gen_hash_key(mode, &lastk, k.i);
242 if (memcmp(&k, &lastk, sizeof(k)))
244 hash_sum -= k.hash[3];
250 /*** Variable-length records (strings) with and without var-length data ***/
259 static inline int s4_compare(struct key4 *x, struct key4 *y)
261 uns l = MIN(x->len, y->len);
262 int c = memcmp(x->s, y->s, l);
265 COMPARE(x->len, y->len);
269 static inline int s4_read_key(struct fastbuf *f, struct key4 *x)
272 if (x->len == 0xffffffff)
274 ASSERT(x->len < KEY4_MAX);
275 breadb(f, x->s, x->len);
279 static inline void s4_write_key(struct fastbuf *f, struct key4 *x)
281 ASSERT(x->len < KEY4_MAX);
283 bwrite(f, x->s, x->len);
286 #define SORT_KEY struct key4
287 #define SORT_PREFIX(x) s4_##x
288 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
289 #define SORT_INPUT_FB
290 #define SORT_OUTPUT_FB
292 #include "lib/sorter/sorter.h"
294 #define s4b_compare s4_compare
295 #define s4b_read_key s4_read_key
296 #define s4b_write_key s4_write_key
298 static inline uns s4_data_size(struct key4 *x)
300 return x->len ? (x->s[0] ^ 0xad) : 0;
303 #define SORT_KEY struct key4
304 #define SORT_PREFIX(x) s4b_##x
305 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
306 #define SORT_DATA_SIZE(x) s4_data_size(&(x))
307 #define SORT_INPUT_FB
308 #define SORT_OUTPUT_FB
310 #include "lib/sorter/sorter.h"
313 gen_key4(struct key4 *k)
315 k->len = random_max(KEY4_MAX);
316 for (uns i=0; i<k->len; i++)
321 gen_data4(byte *buf, uns len, uns h)
331 test_strings(uns mode, u64 size)
333 uns avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0);
334 uns N = MIN(size / avg_item_size, 0xffffffff);
335 msg(L_INFO, ">>> Strings %s(N=%u)", (mode ? "with data " : ""), N);
338 struct key4 k, lastk;
339 byte buf[256], buf2[256];
342 struct fastbuf *f = bopen_tmp(65536);
343 for (uns i=0; i<N; i++)
347 uns h = hash_block(k.s, k.len);
351 gen_data4(buf, s4_data_size(&k), h);
352 bwrite(f, buf, s4_data_size(&k));
358 f = (mode ? s4b_sort : s4_sort)(f, NULL);
361 SORT_XTRACE(2, "Verifying");
362 for (uns i=0; i<N; i++)
364 int ok = s4_read_key(f, &k);
366 uns h = hash_block(k.s, k.len);
367 if (mode && s4_data_size(&k))
369 ok = breadb(f, buf, s4_data_size(&k));
371 gen_data4(buf2, s4_data_size(&k), h);
372 ASSERT(!memcmp(buf, buf2, s4_data_size(&k)));
374 if (i && s4_compare(&k, &lastk) < 0)
383 /*** Graph-like structure with custom presorting ***/
390 static uns s5_N, s5_K, s5_L, s5_i, s5_j;
396 static int s5_gen(struct s5_pair *p)
400 if (!s5_N || s5_i >= s5_N-1)
405 p->x = ((u64)s5_j * s5_K) % s5_N;
406 p->y = ((u64)(s5_i + s5_j) * s5_L) % s5_N;
411 #define ASORT_PREFIX(x) s5m_##x
412 #define ASORT_KEY_TYPE u32
413 #define ASORT_ELT(i) ary[i]
414 #define ASORT_EXTRA_ARGS , u32 *ary
415 #include "lib/arraysort.h"
417 static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uns n, void *buf)
421 for (uns i=0; i<n; i++)
423 memcpy(&a[m], data[i], 4*keys[i]->cnt);
428 bwrite(f, keys[0], sizeof(struct key5));
432 static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
436 for (uns i=0; i<n; i++)
438 k[i] = bgetl(data[i]);
441 struct key5 key = { .x = keys[0]->x, .cnt = m };
442 bwrite(dest, &key, sizeof(key));
446 for (uns i=1; i<n; i++)
451 k[b] = bgetl(data[b]);
457 static inline int s5p_lt(struct s5_pair x, struct s5_pair y)
459 COMPARE_LT(x.x, y.x);
460 COMPARE_LT(x.y, y.y);
464 #define ASORT_PREFIX(x) s5p_##x
465 #define ASORT_KEY_TYPE struct s5_pair
466 #define ASORT_LT(x,y) s5p_lt(x,y)
467 #include "lib/sorter/array.h"
469 static int s5_presort(struct fastbuf *dest, void *buf, size_t bufsize)
471 uns max = MIN(bufsize/sizeof(struct s5_pair), 0xffffffff);
472 struct s5_pair *a = buf;
474 while (n<max && s5_gen(&a[n]))
483 while (i < n && a[i].x == a[j].x)
485 struct key5 k = { .x = a[j].x, .cnt = i-j };
486 bwrite(dest, &k, sizeof(k));
488 bputl(dest, a[j++].y);
493 #define SORT_KEY_REGULAR struct key5
494 #define SORT_PREFIX(x) s5_##x
495 #define SORT_DATA_SIZE(k) (4*(k).cnt)
497 #define SORT_UNIFY_WORKSPACE(k) SORT_DATA_SIZE(k)
498 #define SORT_INPUT_PRESORT
499 #define SORT_OUTPUT_THIS_FB
500 #define SORT_INT(k) (k).x
502 #include "lib/sorter/sorter.h"
504 #define SORT_KEY_REGULAR struct key5
505 #define SORT_PREFIX(x) s5b_##x
506 #define SORT_DATA_SIZE(k) (4*(k).cnt)
508 #define SORT_UNIFY_WORKSPACE(k) SORT_DATA_SIZE(k)
509 #define SORT_INPUT_FB
510 #define SORT_OUTPUT_THIS_FB
511 #define SORT_INT(k) (k).x
512 #define s5b_write_merged s5_write_merged
513 #define s5b_copy_merged s5_copy_merged
515 #include "lib/sorter/sorter.h"
518 test_graph(uns mode, u64 size)
521 while ((u64)N*(N+2)*4 < size)
525 msg(L_INFO, ">>> Graph%s (N=%u)", (mode ? "" : " with custom presorting"), N);
531 struct fastbuf *in = NULL;
535 in = bopen_tmp(65536);
538 struct key5 k = { .x = p.x, .cnt = 1 };
539 bwrite(in, &k, sizeof(k));
546 struct fastbuf *f = bopen_tmp(65536);
547 bputl(f, 0xfeedcafe);
548 struct fastbuf *g = (mode ? s5b_sort(in, f, s5_N-1) : s5_sort(NULL, f, s5_N-1));
552 SORT_XTRACE(2, "Verifying");
554 ASSERT(c == 0xfeedcafe);
555 for (uns i=0; i<N; i++)
558 int ok = breadb(f, &k, sizeof(k));
562 for (uns j=0; j<N; j++)
571 /*** Simple 8-byte integer keys ***/
577 #define SORT_KEY_REGULAR struct key6
578 #define SORT_PREFIX(x) s6_##x
579 #define SORT_INPUT_FB
580 #define SORT_OUTPUT_FB
582 #define SORT_INT64(k) (k).x
584 #include "lib/sorter/sorter.h"
587 test_int64(int mode, u64 size)
589 u64 N = size ? nextprime(MIN(size/8, 0xffff0000)) : 0;
591 msg(L_INFO, ">>> 64-bit integers (%s, N=%llu)", ((char *[]) { "increasing", "decreasing", "random" })[mode], (long long)N);
593 struct fastbuf *f = bopen_tmp(65536);
594 for (u64 i=0; i<N; i++)
595 bputq(f, 777777*((mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N));
599 f = s6_sort(f, NULL, 777777*(N-1));
602 SORT_XTRACE(2, "Verifying");
603 for (u64 i=0; i<N; i++)
607 die("Discrepancy: %llu instead of %llu", (long long)j, 777777*(long long)i);
615 run_test(uns i, u64 size)
621 test_int(0, size); break;
623 test_int(1, size); break;
625 test_int(2, size); break;
627 test_counted(0, size); break;
629 test_counted(1, size); break;
631 test_counted(2, size); break;
633 test_hashes(0, size); break;
635 test_hashes(1, size); break;
637 test_hashes(2, size); break;
639 test_strings(0, size); break;
641 test_strings(1, size); break;
643 test_graph(0, size); break;
645 test_graph(1, size); break;
647 test_int64(0, size); break;
649 test_int64(1, size); break;
651 test_int64(2, size); break;
657 main(int argc, char **argv)
664 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS "d:s:t:v", CF_NO_LONG_OPTS, NULL)) >= 0)
668 sorter_debug = atol(optarg);
671 if (cf_parse_u64(optarg, &size))
677 int f = sepsplit(optarg, ',', w, ARRAY_SIZE(w));
681 for (int i=0; i<f; i++)
695 fputs("Usage: sort-test [-v] [-d <debug>] [-s <size>] [-t <test>]\n", stderr);
701 for (uns i=0; i<TMAX; i++)