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/hashfunc.h"
23 /*** Time measurement ***/
25 static timestamp_t timer;
38 log(L_INFO, "Test took %.3fs", get_timer(&timer) / 1000.);
41 /*** Simple 4-byte integer keys ***/
47 #define SORT_KEY_REGULAR struct key1
48 #define SORT_PREFIX(x) s1_##x
50 #define SORT_OUTPUT_FB
52 #define SORT_INT(k) (k).x
54 #include "lib/sorter/sorter.h"
57 test_int(int mode, u64 size)
59 uns N = size ? nextprime(MIN(size/4, 0xffff0000)) : 0;
61 log(L_INFO, ">>> Integers (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
63 struct fastbuf *f = bopen_tmp(65536);
64 for (uns i=0; i<N; i++)
65 bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
69 f = s1_sort(f, NULL, N-1);
72 SORT_XTRACE(2, "Verifying");
73 for (uns i=0; i<N; i++)
77 die("Discrepancy: %u instead of %u", j, i);
82 /*** Integers with merging, but no data ***/
89 static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uns n, void *buf UNUSED)
91 for (uns i=1; i<n; i++)
92 k[0]->cnt += k[i]->cnt;
93 bwrite(f, k[0], sizeof(struct key2));
96 static inline void s2_copy_merged(struct key2 **k, struct fastbuf **d UNUSED, uns n, struct fastbuf *dest)
98 for (uns i=1; i<n; i++)
99 k[0]->cnt += k[i]->cnt;
100 bwrite(dest, k[0], sizeof(struct key2));
103 #define SORT_KEY_REGULAR struct key2
104 #define SORT_PREFIX(x) s2_##x
105 #define SORT_INPUT_FB
106 #define SORT_OUTPUT_FB
108 #define SORT_INT(k) (k).x
110 #include "lib/sorter/sorter.h"
113 test_counted(int mode, u64 size)
115 u64 items = size / sizeof(struct key2);
117 while (items/(2*mult) > 0xffff0000)
119 uns N = items ? nextprime(items/(2*mult)) : 0;
121 log(L_INFO, ">>> Counted integers (%s, N=%u, mult=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult);
123 struct fastbuf *f = bopen_tmp(65536);
124 for (uns m=0; m<mult; m++)
125 for (uns i=0; i<N; i++)
126 for (uns j=0; j<2; j++)
128 bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
134 f = s2_sort(f, NULL, N-1);
137 SORT_XTRACE(2, "Verifying");
138 for (uns i=0; i<N; i++)
142 die("Discrepancy: %u instead of %u", j, i);
145 die("Discrepancy: %u has count %u instead of %u", j, k, mult);
150 /*** Longer records with hashes (similar to Shepherd's index records) ***/
158 static inline int s3_compare(struct key3 *x, struct key3 *y)
160 /* FIXME: Maybe unroll manually? */
161 for (uns i=0; i<4; i++)
162 COMPARE(x->hash[i], y->hash[i]);
166 static inline uns s3_hash(struct key3 *x)
171 #define SORT_KEY_REGULAR struct key3
172 #define SORT_PREFIX(x) s3_##x
173 #define SORT_INPUT_FB
174 #define SORT_OUTPUT_FB
175 #define SORT_HASH_BITS 32
177 #include "lib/sorter/sorter.h"
180 gen_hash_key(int mode, struct key3 *k, uns i)
183 k->payload[0] = 7*i + 13;
184 k->payload[1] = 13*i + 19;
185 k->payload[2] = 19*i + 7;
190 k->hash[1] = k->payload[0];
191 k->hash[2] = k->payload[1];
192 k->hash[3] = k->payload[2];
196 k->hash[1] = k->payload[0];
197 k->hash[2] = k->payload[1];
198 k->hash[3] = k->payload[2];
201 struct MD5Context ctx;
203 MD5Update(&ctx, (byte*) &k->i, 4);
204 MD5Final((byte*) &k->hash, &ctx);
210 test_hashes(int mode, u64 size)
212 uns N = MIN(size / sizeof(struct key3), 0xffffffff);
213 log(L_INFO, ">>> Hashes (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
214 struct key3 k, lastk;
216 struct fastbuf *f = bopen_tmp(65536);
218 for (uns i=0; i<N; i++)
220 gen_hash_key(mode, &k, i);
221 hash_sum += k.hash[3];
222 bwrite(f, &k, sizeof(k));
227 f = s3_sort(f, NULL);
230 SORT_XTRACE(2, "Verifying");
231 for (uns i=0; i<N; i++)
233 int ok = breadb(f, &k, sizeof(k));
235 if (i && s3_compare(&k, &lastk) <= 0)
237 gen_hash_key(mode, &lastk, k.i);
238 if (memcmp(&k, &lastk, sizeof(k)))
240 hash_sum -= k.hash[3];
246 /*** Variable-length records (strings) with and without var-length data ***/
255 static inline int s4_compare(struct key4 *x, struct key4 *y)
257 uns l = MIN(x->len, y->len);
258 int c = memcmp(x->s, y->s, l);
261 COMPARE(x->len, y->len);
265 static inline int s4_read_key(struct fastbuf *f, struct key4 *x)
268 if (x->len == 0xffffffff)
270 ASSERT(x->len < KEY4_MAX);
271 breadb(f, x->s, x->len);
275 static inline void s4_write_key(struct fastbuf *f, struct key4 *x)
277 ASSERT(x->len < KEY4_MAX);
279 bwrite(f, x->s, x->len);
282 #define SORT_KEY struct key4
283 #define SORT_PREFIX(x) s4_##x
284 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
285 #define SORT_INPUT_FB
286 #define SORT_OUTPUT_FB
288 #include "lib/sorter/sorter.h"
290 #define s4b_compare s4_compare
291 #define s4b_read_key s4_read_key
292 #define s4b_write_key s4_write_key
294 static inline uns s4_data_size(struct key4 *x)
296 return x->len ? (x->s[0] ^ 0xad) : 0;
299 #define SORT_KEY struct key4
300 #define SORT_PREFIX(x) s4b_##x
301 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
302 #define SORT_DATA_SIZE(x) s4_data_size(&(x))
303 #define SORT_INPUT_FB
304 #define SORT_OUTPUT_FB
306 #include "lib/sorter/sorter.h"
309 gen_key4(struct key4 *k)
311 k->len = random_max(KEY4_MAX);
312 for (uns i=0; i<k->len; i++)
317 gen_data4(byte *buf, uns len, uns h)
327 test_strings(uns mode, u64 size)
329 uns avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0);
330 uns N = MIN(size / avg_item_size, 0xffffffff);
331 log(L_INFO, ">>> Strings %s(N=%u)", (mode ? "with data " : ""), N);
334 struct key4 k, lastk;
335 byte buf[256], buf2[256];
338 struct fastbuf *f = bopen_tmp(65536);
339 for (uns i=0; i<N; i++)
343 uns h = hash_block(k.s, k.len);
347 gen_data4(buf, s4_data_size(&k), h);
348 bwrite(f, buf, s4_data_size(&k));
354 f = (mode ? s4b_sort : s4_sort)(f, NULL);
357 SORT_XTRACE(2, "Verifying");
358 for (uns i=0; i<N; i++)
360 int ok = s4_read_key(f, &k);
362 uns h = hash_block(k.s, k.len);
363 if (mode && s4_data_size(&k))
365 ok = breadb(f, buf, s4_data_size(&k));
367 gen_data4(buf2, s4_data_size(&k), h);
368 ASSERT(!memcmp(buf, buf2, s4_data_size(&k)));
370 if (i && s4_compare(&k, &lastk) < 0)
379 /*** Graph-like structure with custom presorting ***/
386 static uns s5_N, s5_K, s5_L, s5_i, s5_j;
392 static int s5_gen(struct s5_pair *p)
401 p->x = ((u64)s5_j * s5_K) % s5_N;
402 p->y = ((u64)(s5_i + s5_j) * s5_L) % s5_N;
407 #define ASORT_PREFIX(x) s5m_##x
408 #define ASORT_KEY_TYPE u32
409 #define ASORT_ELT(i) ary[i]
410 #define ASORT_EXTRA_ARGS , u32 *ary
411 #include "lib/arraysort.h"
413 static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uns n, void *buf)
417 for (uns i=0; i<n; i++)
419 memcpy(&a[m], data[i], 4*keys[i]->cnt);
424 bwrite(f, keys[0], sizeof(struct key5));
425 bwrite(f, a, 4*m); /* FIXME: Might overflow here */
428 static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
432 for (uns i=0; i<n; i++)
434 k[i] = bgetl(data[i]);
437 struct key5 key = { .x = keys[0]->x, .cnt = m };
438 bwrite(dest, &key, sizeof(key));
442 for (uns i=1; i<n; i++)
447 k[b] = bgetl(data[b]);
453 static inline int s5p_lt(struct s5_pair x, struct s5_pair y)
455 COMPARE_LT(x.x, y.x);
456 COMPARE_LT(x.y, y.y);
460 /* FIXME: Use smarter internal sorter when it's available */
461 #define ASORT_PREFIX(x) s5p_##x
462 #define ASORT_KEY_TYPE struct s5_pair
463 #define ASORT_ELT(i) ary[i]
464 #define ASORT_LT(x,y) s5p_lt(x,y)
465 #define ASORT_EXTRA_ARGS , struct s5_pair *ary
466 #include "lib/arraysort.h"
468 static int s5_presort(struct fastbuf *dest, void *buf, size_t bufsize)
470 uns max = MIN(bufsize/sizeof(struct s5_pair), 0xffffffff);
471 struct s5_pair *a = buf;
473 while (n<max && s5_gen(&a[n]))
482 while (i < n && a[i].x == a[j].x)
484 struct key5 k = { .x = a[j].x, .cnt = i-j };
485 bwrite(dest, &k, sizeof(k));
487 bputl(dest, a[j++].y);
492 #define SORT_KEY_REGULAR struct key5
493 #define SORT_PREFIX(x) s5_##x
494 #define SORT_DATA_SIZE(k) (4*(k).cnt)
496 #define SORT_INPUT_PRESORT
497 #define SORT_OUTPUT_THIS_FB
498 #define SORT_INT(k) (k).x
500 #include "lib/sorter/sorter.h"
502 #define SORT_KEY_REGULAR struct key5
503 #define SORT_PREFIX(x) s5b_##x
504 #define SORT_DATA_SIZE(k) (4*(k).cnt)
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
512 #include "lib/sorter/sorter.h"
515 test_graph(uns mode, u64 size)
518 while ((u64)N*(N+2)*4 < size)
520 log(L_INFO, ">>> Graph%s (N=%u)", (mode ? "" : " with custom presorting"), N);
526 struct fastbuf *in = NULL;
530 in = bopen_tmp(65536);
533 struct key5 k = { .x = p.x, .cnt = 1 };
534 bwrite(in, &k, sizeof(k));
541 struct fastbuf *f = bopen_tmp(65536);
542 bputl(f, 0xfeedcafe);
543 struct fastbuf *g = (mode ? s5b_sort(in, f, s5_N-1) : s5_sort(NULL, f, s5_N-1));
547 SORT_XTRACE(2, "Verifying");
549 ASSERT(c == 0xfeedcafe);
550 for (uns i=0; i<N; i++)
553 int ok = breadb(f, &k, sizeof(k));
557 for (uns j=0; j<N; j++)
569 run_test(uns i, u64 size)
574 test_int(0, size); break;
576 test_int(1, size); break;
578 test_int(2, size); break;
580 test_counted(0, size); break;
582 test_counted(1, size); break;
584 test_counted(2, size); break;
586 test_hashes(0, size); break;
588 test_hashes(1, size); break;
590 test_hashes(2, size); break;
592 test_strings(0, size); break;
594 test_strings(1, size); break;
596 test_graph(0, size); break;
598 test_graph(1, size); break;
604 main(int argc, char **argv)
611 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS "d:s:t:v", CF_NO_LONG_OPTS, NULL)) >= 0)
615 sorter_debug = atol(optarg);
618 if (cf_parse_u64(optarg, &size))
631 fputs("Usage: sort-test [-v] [-d <debug>] [-s <size>] [-t <test>]\n", stderr);
640 for (uns i=0; i<TMAX; i++)