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 /*** Time measurement ***/
26 static timestamp_t timer;
39 log(L_INFO, "Test took %.3fs", get_timer(&timer) / 1000.);
42 /*** Simple 4-byte integer keys ***/
48 #define SORT_KEY_REGULAR struct key1
49 #define SORT_PREFIX(x) s1_##x
51 #define SORT_OUTPUT_FB
53 #define SORT_INT(k) (k).x
54 #define SORT_DELETE_INPUT 0
56 #include "lib/sorter/sorter.h"
59 test_int(int mode, u64 size)
61 uns N = size ? nextprime(MIN(size/4, 0xffff0000)) : 0;
63 log(L_INFO, ">>> Integers (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
65 struct fastbuf *f = bopen_tmp(65536);
66 for (uns i=0; i<N; i++)
67 bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
71 f = s1_sort(f, NULL, N-1);
74 SORT_XTRACE(2, "Verifying");
75 for (uns i=0; i<N; i++)
79 die("Discrepancy: %u instead of %u", j, i);
84 /*** Integers with merging, but no data ***/
91 static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uns n, void *buf UNUSED)
93 for (uns i=1; i<n; i++)
94 k[0]->cnt += k[i]->cnt;
95 bwrite(f, k[0], sizeof(struct key2));
98 #define SORT_KEY_REGULAR struct key2
99 #define SORT_PREFIX(x) s2_##x
100 #define SORT_INPUT_FB
101 #define SORT_OUTPUT_FB
103 #define SORT_INT(k) (k).x
105 #include "lib/sorter/sorter.h"
108 test_counted(int mode, u64 size)
110 u64 items = size / sizeof(struct key2);
112 while (items/(2*mult) > 0xffff0000)
114 uns N = items ? nextprime(items/(2*mult)) : 0;
116 log(L_INFO, ">>> Counted integers (%s, N=%u, mult=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult);
118 struct fastbuf *f = bopen_tmp(65536);
119 for (uns m=0; m<mult; m++)
120 for (uns i=0; i<N; i++)
121 for (uns j=0; j<2; j++)
123 bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
129 f = s2_sort(f, NULL, N-1);
132 SORT_XTRACE(2, "Verifying");
133 for (uns i=0; i<N; i++)
137 die("Discrepancy: %u instead of %u", j, i);
140 die("Discrepancy: %u has count %u instead of %u", j, k, mult);
145 /*** Longer records with hashes (similar to Shepherd's index records) ***/
153 static inline int s3_compare(struct key3 *x, struct key3 *y)
155 /* FIXME: Maybe unroll manually? */
156 for (uns i=0; i<4; i++)
157 COMPARE(x->hash[i], y->hash[i]);
161 static inline uns s3_hash(struct key3 *x)
166 #define SORT_KEY_REGULAR struct key3
167 #define SORT_PREFIX(x) s3_##x
168 #define SORT_INPUT_FB
169 #define SORT_OUTPUT_FB
170 #define SORT_HASH_BITS 32
172 #include "lib/sorter/sorter.h"
175 gen_hash_key(int mode, struct key3 *k, uns i)
178 k->payload[0] = 7*i + 13;
179 k->payload[1] = 13*i + 19;
180 k->payload[2] = 19*i + 7;
185 k->hash[1] = k->payload[0];
186 k->hash[2] = k->payload[1];
187 k->hash[3] = k->payload[2];
191 k->hash[1] = k->payload[0];
192 k->hash[2] = k->payload[1];
193 k->hash[3] = k->payload[2];
196 struct MD5Context ctx;
198 MD5Update(&ctx, (byte*) &k->i, 4);
199 MD5Final((byte*) &k->hash, &ctx);
205 test_hashes(int mode, u64 size)
207 uns N = MIN(size / sizeof(struct key3), 0xffffffff);
208 log(L_INFO, ">>> Hashes (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
209 struct key3 k, lastk;
211 struct fastbuf *f = bopen_tmp(65536);
213 for (uns i=0; i<N; i++)
215 gen_hash_key(mode, &k, i);
216 hash_sum += k.hash[3];
217 bwrite(f, &k, sizeof(k));
222 f = s3_sort(f, NULL);
225 SORT_XTRACE(2, "Verifying");
226 for (uns i=0; i<N; i++)
228 int ok = breadb(f, &k, sizeof(k));
230 if (i && s3_compare(&k, &lastk) <= 0)
232 gen_hash_key(mode, &lastk, k.i);
233 if (memcmp(&k, &lastk, sizeof(k)))
235 hash_sum -= k.hash[3];
241 /*** Variable-length records (strings) with and without var-length data ***/
250 static inline int s4_compare(struct key4 *x, struct key4 *y)
252 uns l = MIN(x->len, y->len);
253 int c = memcmp(x->s, y->s, l);
256 COMPARE(x->len, y->len);
260 static inline int s4_read_key(struct fastbuf *f, struct key4 *x)
263 if (x->len == 0xffffffff)
265 ASSERT(x->len < KEY4_MAX);
266 breadb(f, x->s, x->len);
270 static inline void s4_write_key(struct fastbuf *f, struct key4 *x)
272 ASSERT(x->len < KEY4_MAX);
274 bwrite(f, x->s, x->len);
277 #define SORT_KEY struct key4
278 #define SORT_PREFIX(x) s4_##x
279 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
280 #define SORT_INPUT_FB
281 #define SORT_OUTPUT_FB
283 #include "lib/sorter/sorter.h"
285 #define s4b_compare s4_compare
286 #define s4b_read_key s4_read_key
287 #define s4b_write_key s4_write_key
289 static inline uns s4_data_size(struct key4 *x)
291 return x->len ? (x->s[0] ^ 0xad) : 0;
294 #define SORT_KEY struct key4
295 #define SORT_PREFIX(x) s4b_##x
296 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
297 #define SORT_DATA_SIZE(x) s4_data_size(&(x))
298 #define SORT_INPUT_FB
299 #define SORT_OUTPUT_FB
301 #include "lib/sorter/sorter.h"
304 gen_key4(struct key4 *k)
306 k->len = random_max(KEY4_MAX);
307 for (uns i=0; i<k->len; i++)
312 gen_data4(byte *buf, uns len, uns h)
322 test_strings(uns mode, u64 size)
324 uns avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0);
325 uns N = MIN(size / avg_item_size, 0xffffffff);
326 log(L_INFO, ">>> Strings %s(N=%u)", (mode ? "with data " : ""), N);
329 struct key4 k, lastk;
330 byte buf[256], buf2[256];
333 struct fastbuf *f = bopen_tmp(65536);
334 for (uns i=0; i<N; i++)
338 uns h = hash_block(k.s, k.len);
342 gen_data4(buf, s4_data_size(&k), h);
343 bwrite(f, buf, s4_data_size(&k));
349 f = (mode ? s4b_sort : s4_sort)(f, NULL);
352 SORT_XTRACE(2, "Verifying");
353 for (uns i=0; i<N; i++)
355 int ok = s4_read_key(f, &k);
357 uns h = hash_block(k.s, k.len);
358 if (mode && s4_data_size(&k))
360 ok = breadb(f, buf, s4_data_size(&k));
362 gen_data4(buf2, s4_data_size(&k), h);
363 ASSERT(!memcmp(buf, buf2, s4_data_size(&k)));
365 if (i && s4_compare(&k, &lastk) < 0)
374 /*** Graph-like structure with custom presorting ***/
381 static uns s5_N, s5_K, s5_L, s5_i, s5_j;
387 static int s5_gen(struct s5_pair *p)
396 p->x = ((u64)s5_j * s5_K) % s5_N;
397 p->y = ((u64)(s5_i + s5_j) * s5_L) % s5_N;
402 #define ASORT_PREFIX(x) s5m_##x
403 #define ASORT_KEY_TYPE u32
404 #define ASORT_ELT(i) ary[i]
405 #define ASORT_EXTRA_ARGS , u32 *ary
406 #include "lib/arraysort.h"
408 static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uns n, void *buf)
412 for (uns i=0; i<n; i++)
414 memcpy(&a[m], data[i], 4*keys[i]->cnt);
419 bwrite(f, keys[0], sizeof(struct key5));
420 bwrite(f, a, 4*m); /* FIXME: Might overflow here */
423 static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
427 for (uns i=0; i<n; i++)
429 k[i] = bgetl(data[i]);
432 struct key5 key = { .x = keys[0]->x, .cnt = m };
433 bwrite(dest, &key, sizeof(key));
437 for (uns i=1; i<n; i++)
442 k[b] = bgetl(data[b]);
448 static inline int s5p_lt(struct s5_pair x, struct s5_pair y)
450 COMPARE_LT(x.x, y.x);
451 COMPARE_LT(x.y, y.y);
455 /* FIXME: Use smarter internal sorter when it's available */
456 #define ASORT_PREFIX(x) s5p_##x
457 #define ASORT_KEY_TYPE struct s5_pair
458 #define ASORT_ELT(i) ary[i]
459 #define ASORT_LT(x,y) s5p_lt(x,y)
460 #define ASORT_EXTRA_ARGS , struct s5_pair *ary
461 #include "lib/arraysort.h"
463 static int s5_presort(struct fastbuf *dest, void *buf, size_t bufsize)
465 uns max = MIN(bufsize/sizeof(struct s5_pair), 0xffffffff);
466 struct s5_pair *a = buf;
468 while (n<max && s5_gen(&a[n]))
477 while (i < n && a[i].x == a[j].x)
479 struct key5 k = { .x = a[j].x, .cnt = i-j };
480 bwrite(dest, &k, sizeof(k));
482 bputl(dest, a[j++].y);
487 #define SORT_KEY_REGULAR struct key5
488 #define SORT_PREFIX(x) s5_##x
489 #define SORT_DATA_SIZE(k) (4*(k).cnt)
491 #define SORT_UNIFY_WORKSPACE(k) SORT_DATA_SIZE(k)
492 #define SORT_INPUT_PRESORT
493 #define SORT_OUTPUT_THIS_FB
494 #define SORT_INT(k) (k).x
496 #include "lib/sorter/sorter.h"
498 #define SORT_KEY_REGULAR struct key5
499 #define SORT_PREFIX(x) s5b_##x
500 #define SORT_DATA_SIZE(k) (4*(k).cnt)
502 #define SORT_UNIFY_WORKSPACE(k) SORT_DATA_SIZE(k)
503 #define SORT_INPUT_FB
504 #define SORT_OUTPUT_THIS_FB
505 #define SORT_INT(k) (k).x
506 #define s5b_write_merged s5_write_merged
507 #define s5b_copy_merged s5_copy_merged
509 #include "lib/sorter/sorter.h"
512 test_graph(uns mode, u64 size)
515 while ((u64)N*(N+2)*4 < size)
517 log(L_INFO, ">>> Graph%s (N=%u)", (mode ? "" : " with custom presorting"), N);
523 struct fastbuf *in = NULL;
527 in = bopen_tmp(65536);
530 struct key5 k = { .x = p.x, .cnt = 1 };
531 bwrite(in, &k, sizeof(k));
538 struct fastbuf *f = bopen_tmp(65536);
539 bputl(f, 0xfeedcafe);
540 struct fastbuf *g = (mode ? s5b_sort(in, f, s5_N-1) : s5_sort(NULL, f, s5_N-1));
544 SORT_XTRACE(2, "Verifying");
546 ASSERT(c == 0xfeedcafe);
547 for (uns i=0; i<N; i++)
550 int ok = breadb(f, &k, sizeof(k));
554 for (uns j=0; j<N; j++)
563 /*** Simple 8-byte integer keys ***/
569 #define SORT_KEY_REGULAR struct key6
570 #define SORT_PREFIX(x) s6_##x
571 #define SORT_INPUT_FB
572 #define SORT_OUTPUT_FB
574 #define SORT_INT64(k) (k).x
576 #include "lib/sorter/sorter.h"
579 test_int64(int mode, u64 size)
581 u64 N = size ? nextprime(MIN(size/8, 0xffff0000)) : 0;
583 log(L_INFO, ">>> 64-bit integers (%s, N=%llu)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
585 struct fastbuf *f = bopen_tmp(65536);
586 for (u64 i=0; i<N; i++)
587 bputq(f, 777777*((mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N));
591 f = s6_sort(f, NULL, 777777*(N-1));
594 SORT_XTRACE(2, "Verifying");
595 for (u64 i=0; i<N; i++)
599 die("Discrepancy: %llu instead of %llu", j, 777777*i);
607 run_test(uns i, u64 size)
612 test_int(0, size); break;
614 test_int(1, size); break;
616 test_int(2, size); break;
618 test_counted(0, size); break;
620 test_counted(1, size); break;
622 test_counted(2, size); break;
624 test_hashes(0, size); break;
626 test_hashes(1, size); break;
628 test_hashes(2, size); break;
630 test_strings(0, size); break;
632 test_strings(1, size); break;
634 test_graph(0, size); break;
636 test_graph(1, size); break;
638 test_int64(0, size); break;
640 test_int64(1, size); break;
642 test_int64(2, size); break;
648 main(int argc, char **argv)
655 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS "d:s:t:v", CF_NO_LONG_OPTS, NULL)) >= 0)
659 sorter_debug = atol(optarg);
662 if (cf_parse_u64(optarg, &size))
675 fputs("Usage: sort-test [-v] [-d <debug>] [-s <size>] [-t <test>]\n", stderr);
684 for (uns i=0; i<TMAX; i++)