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
55 #include "lib/sorter/sorter.h"
58 test_int(int mode, u64 size)
60 uns N = size ? nextprime(MIN(size/4, 0xffff0000)) : 0;
62 log(L_INFO, ">>> Integers (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
64 struct fastbuf *f = bopen_tmp(65536);
65 for (uns i=0; i<N; i++)
66 bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
70 f = s1_sort(f, NULL, N-1);
73 SORT_XTRACE(2, "Verifying");
74 for (uns i=0; i<N; i++)
78 die("Discrepancy: %u instead of %u", j, i);
83 /*** Integers with merging, but no data ***/
90 static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uns n, void *buf UNUSED)
92 for (uns i=1; i<n; i++)
93 k[0]->cnt += k[i]->cnt;
94 bwrite(f, k[0], sizeof(struct key2));
97 static inline void s2_copy_merged(struct key2 **k, struct fastbuf **d UNUSED, uns n, struct fastbuf *dest)
99 for (uns i=1; i<n; i++)
100 k[0]->cnt += k[i]->cnt;
101 bwrite(dest, k[0], sizeof(struct key2));
104 #define SORT_KEY_REGULAR struct key2
105 #define SORT_PREFIX(x) s2_##x
106 #define SORT_INPUT_FB
107 #define SORT_OUTPUT_FB
109 #define SORT_INT(k) (k).x
111 #include "lib/sorter/sorter.h"
114 test_counted(int mode, u64 size)
116 u64 items = size / sizeof(struct key2);
118 while (items/(2*mult) > 0xffff0000)
120 uns N = items ? nextprime(items/(2*mult)) : 0;
122 log(L_INFO, ">>> Counted integers (%s, N=%u, mult=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult);
124 struct fastbuf *f = bopen_tmp(65536);
125 for (uns m=0; m<mult; m++)
126 for (uns i=0; i<N; i++)
127 for (uns j=0; j<2; j++)
129 bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
135 f = s2_sort(f, NULL, N-1);
138 SORT_XTRACE(2, "Verifying");
139 for (uns i=0; i<N; i++)
143 die("Discrepancy: %u instead of %u", j, i);
146 die("Discrepancy: %u has count %u instead of %u", j, k, mult);
151 /*** Longer records with hashes (similar to Shepherd's index records) ***/
159 static inline int s3_compare(struct key3 *x, struct key3 *y)
161 /* FIXME: Maybe unroll manually? */
162 for (uns i=0; i<4; i++)
163 COMPARE(x->hash[i], y->hash[i]);
167 static inline uns s3_hash(struct key3 *x)
172 #define SORT_KEY_REGULAR struct key3
173 #define SORT_PREFIX(x) s3_##x
174 #define SORT_INPUT_FB
175 #define SORT_OUTPUT_FB
176 #define SORT_HASH_BITS 32
178 #include "lib/sorter/sorter.h"
181 gen_hash_key(int mode, struct key3 *k, uns i)
184 k->payload[0] = 7*i + 13;
185 k->payload[1] = 13*i + 19;
186 k->payload[2] = 19*i + 7;
191 k->hash[1] = k->payload[0];
192 k->hash[2] = k->payload[1];
193 k->hash[3] = k->payload[2];
197 k->hash[1] = k->payload[0];
198 k->hash[2] = k->payload[1];
199 k->hash[3] = k->payload[2];
202 struct MD5Context ctx;
204 MD5Update(&ctx, (byte*) &k->i, 4);
205 MD5Final((byte*) &k->hash, &ctx);
211 test_hashes(int mode, u64 size)
213 uns N = MIN(size / sizeof(struct key3), 0xffffffff);
214 log(L_INFO, ">>> Hashes (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
215 struct key3 k, lastk;
217 struct fastbuf *f = bopen_tmp(65536);
219 for (uns i=0; i<N; i++)
221 gen_hash_key(mode, &k, i);
222 hash_sum += k.hash[3];
223 bwrite(f, &k, sizeof(k));
228 f = s3_sort(f, NULL);
231 SORT_XTRACE(2, "Verifying");
232 for (uns i=0; i<N; i++)
234 int ok = breadb(f, &k, sizeof(k));
236 if (i && s3_compare(&k, &lastk) <= 0)
238 gen_hash_key(mode, &lastk, k.i);
239 if (memcmp(&k, &lastk, sizeof(k)))
241 hash_sum -= k.hash[3];
247 /*** Variable-length records (strings) with and without var-length data ***/
256 static inline int s4_compare(struct key4 *x, struct key4 *y)
258 uns l = MIN(x->len, y->len);
259 int c = memcmp(x->s, y->s, l);
262 COMPARE(x->len, y->len);
266 static inline int s4_read_key(struct fastbuf *f, struct key4 *x)
269 if (x->len == 0xffffffff)
271 ASSERT(x->len < KEY4_MAX);
272 breadb(f, x->s, x->len);
276 static inline void s4_write_key(struct fastbuf *f, struct key4 *x)
278 ASSERT(x->len < KEY4_MAX);
280 bwrite(f, x->s, x->len);
283 #define SORT_KEY struct key4
284 #define SORT_PREFIX(x) s4_##x
285 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
286 #define SORT_INPUT_FB
287 #define SORT_OUTPUT_FB
289 #include "lib/sorter/sorter.h"
291 #define s4b_compare s4_compare
292 #define s4b_read_key s4_read_key
293 #define s4b_write_key s4_write_key
295 static inline uns s4_data_size(struct key4 *x)
297 return x->len ? (x->s[0] ^ 0xad) : 0;
300 #define SORT_KEY struct key4
301 #define SORT_PREFIX(x) s4b_##x
302 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
303 #define SORT_DATA_SIZE(x) s4_data_size(&(x))
304 #define SORT_INPUT_FB
305 #define SORT_OUTPUT_FB
307 #include "lib/sorter/sorter.h"
310 gen_key4(struct key4 *k)
312 k->len = random_max(KEY4_MAX);
313 for (uns i=0; i<k->len; i++)
318 gen_data4(byte *buf, uns len, uns h)
328 test_strings(uns mode, u64 size)
330 uns avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0);
331 uns N = MIN(size / avg_item_size, 0xffffffff);
332 log(L_INFO, ">>> Strings %s(N=%u)", (mode ? "with data " : ""), N);
335 struct key4 k, lastk;
336 byte buf[256], buf2[256];
339 struct fastbuf *f = bopen_tmp(65536);
340 for (uns i=0; i<N; i++)
344 uns h = hash_block(k.s, k.len);
348 gen_data4(buf, s4_data_size(&k), h);
349 bwrite(f, buf, s4_data_size(&k));
355 f = (mode ? s4b_sort : s4_sort)(f, NULL);
358 SORT_XTRACE(2, "Verifying");
359 for (uns i=0; i<N; i++)
361 int ok = s4_read_key(f, &k);
363 uns h = hash_block(k.s, k.len);
364 if (mode && s4_data_size(&k))
366 ok = breadb(f, buf, s4_data_size(&k));
368 gen_data4(buf2, s4_data_size(&k), h);
369 ASSERT(!memcmp(buf, buf2, s4_data_size(&k)));
371 if (i && s4_compare(&k, &lastk) < 0)
380 /*** Graph-like structure with custom presorting ***/
387 static uns s5_N, s5_K, s5_L, s5_i, s5_j;
393 static int s5_gen(struct s5_pair *p)
402 p->x = ((u64)s5_j * s5_K) % s5_N;
403 p->y = ((u64)(s5_i + s5_j) * s5_L) % s5_N;
408 #define ASORT_PREFIX(x) s5m_##x
409 #define ASORT_KEY_TYPE u32
410 #define ASORT_ELT(i) ary[i]
411 #define ASORT_EXTRA_ARGS , u32 *ary
412 #include "lib/arraysort.h"
414 static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uns n, void *buf)
418 for (uns i=0; i<n; i++)
420 memcpy(&a[m], data[i], 4*keys[i]->cnt);
425 bwrite(f, keys[0], sizeof(struct key5));
426 bwrite(f, a, 4*m); /* FIXME: Might overflow here */
429 static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
433 for (uns i=0; i<n; i++)
435 k[i] = bgetl(data[i]);
438 struct key5 key = { .x = keys[0]->x, .cnt = m };
439 bwrite(dest, &key, sizeof(key));
443 for (uns i=1; i<n; i++)
448 k[b] = bgetl(data[b]);
454 static inline int s5p_lt(struct s5_pair x, struct s5_pair y)
456 COMPARE_LT(x.x, y.x);
457 COMPARE_LT(x.y, y.y);
461 /* FIXME: Use smarter internal sorter when it's available */
462 #define ASORT_PREFIX(x) s5p_##x
463 #define ASORT_KEY_TYPE struct s5_pair
464 #define ASORT_ELT(i) ary[i]
465 #define ASORT_LT(x,y) s5p_lt(x,y)
466 #define ASORT_EXTRA_ARGS , struct s5_pair *ary
467 #include "lib/arraysort.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_INPUT_PRESORT
498 #define SORT_OUTPUT_THIS_FB
499 #define SORT_INT(k) (k).x
501 #include "lib/sorter/sorter.h"
503 #define SORT_KEY_REGULAR struct key5
504 #define SORT_PREFIX(x) s5b_##x
505 #define SORT_DATA_SIZE(k) (4*(k).cnt)
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
513 #include "lib/sorter/sorter.h"
516 test_graph(uns mode, u64 size)
519 while ((u64)N*(N+2)*4 < size)
521 log(L_INFO, ">>> Graph%s (N=%u)", (mode ? "" : " with custom presorting"), N);
527 struct fastbuf *in = NULL;
531 in = bopen_tmp(65536);
534 struct key5 k = { .x = p.x, .cnt = 1 };
535 bwrite(in, &k, sizeof(k));
542 struct fastbuf *f = bopen_tmp(65536);
543 bputl(f, 0xfeedcafe);
544 struct fastbuf *g = (mode ? s5b_sort(in, f, s5_N-1) : s5_sort(NULL, f, s5_N-1));
548 SORT_XTRACE(2, "Verifying");
550 ASSERT(c == 0xfeedcafe);
551 for (uns i=0; i<N; i++)
554 int ok = breadb(f, &k, sizeof(k));
558 for (uns j=0; j<N; j++)
570 run_test(uns i, u64 size)
575 test_int(0, size); break;
577 test_int(1, size); break;
579 test_int(2, size); break;
581 test_counted(0, size); break;
583 test_counted(1, size); break;
585 test_counted(2, size); break;
587 test_hashes(0, size); break;
589 test_hashes(1, size); break;
591 test_hashes(2, size); break;
593 test_strings(0, size); break;
595 test_strings(1, size); break;
597 test_graph(0, size); break;
599 test_graph(1, size); break;
605 main(int argc, char **argv)
612 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS "d:s:t:v", CF_NO_LONG_OPTS, NULL)) >= 0)
616 sorter_debug = atol(optarg);
619 if (cf_parse_u64(optarg, &size))
632 fputs("Usage: sort-test [-v] [-d <debug>] [-s <size>] [-t <test>]\n", stderr);
641 for (uns i=0; i<TMAX; i++)