]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/sort-test.c
Better messages and sort-test controls.
[libucw.git] / lib / 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 "lib/lib.h"
11 #include "lib/getopt.h"
12 #include "lib/conf.h"
13 #include "lib/fastbuf.h"
14 #include "lib/hashfunc.h"
15 #include "lib/md5.h"
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <fcntl.h>
21
22 /*** Time measurement ***/
23
24 static void
25 start(void)
26 {
27   init_timer();
28 }
29
30 static void
31 stop(void)
32 {
33   log(L_INFO, "Test took %.3fs", get_timer() / 1000.);
34 }
35
36 /*** Simple 4-byte integer keys ***/
37
38 struct key1 {
39   u32 x;
40 };
41
42 #define SORT_KEY_REGULAR struct key1
43 #define SORT_PREFIX(x) s1_##x
44 #define SORT_INPUT_FB
45 #define SORT_OUTPUT_FB
46 #define SORT_UNIQUE
47 #define SORT_INT(k) (k).x
48
49 #include "lib/sorter/sorter.h"
50
51 static void
52 test_int(int mode, u64 size)
53 {
54   uns N = nextprime(MIN(size/4, 0xffff0000));
55   uns K = N/4*3;
56   log(L_INFO, ">>> Integers (%s, N=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
57
58   struct fastbuf *f = bopen_tmp(65536);
59   for (uns i=0; i<N; i++)
60     bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
61   brewind(f);
62
63   start();
64   f = s1_sort(f, NULL, N-1);
65   stop();
66
67   SORT_XTRACE(2, "Verifying");
68   for (uns i=0; i<N; i++)
69     {
70       uns j = bgetl(f);
71       if (i != j)
72         die("Discrepancy: %d instead of %d", j, i);
73     }
74   bclose(f);
75 }
76
77 /*** Integers with merging, but no data ***/
78
79 struct key2 {
80   u32 x;
81   u32 cnt;
82 };
83
84 static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uns n, void *buf UNUSED)
85 {
86   for (uns i=1; i<n; i++)
87     k[0]->cnt += k[i]->cnt;
88   bwrite(f, k[0], sizeof(struct key2));
89 }
90
91 static inline void s2_copy_merged(struct key2 **k, struct fastbuf **d UNUSED, uns n, struct fastbuf *dest)
92 {
93   for (uns i=1; i<n; i++)
94     k[0]->cnt += k[i]->cnt;
95   bwrite(dest, k[0], sizeof(struct key2));
96 }
97
98 #define SORT_KEY_REGULAR struct key2
99 #define SORT_PREFIX(x) s2_##x
100 #define SORT_INPUT_FB
101 #define SORT_OUTPUT_FB
102 #define SORT_UNIFY
103 #define SORT_INT(k) (k).x
104
105 #include "lib/sorter/sorter.h"
106
107 static void
108 test_counted(int mode, u64 size)
109 {
110   u64 items = size / sizeof(struct key2);
111   uns mult = 2;
112   while (items/(2*mult) > 0xffff0000)
113     mult++;
114   uns N = nextprime(items/(2*mult));
115   uns K = N/4*3;
116   log(L_INFO, ">>> Counted integers (%s, N=%d, mult=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult);
117
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++)
122         {
123           bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
124           bputl(f, 1);
125         }
126   brewind(f);
127
128   start();
129   f = s2_sort(f, NULL, N-1);
130   stop();
131
132   SORT_XTRACE(2, "Verifying");
133   for (uns i=0; i<N; i++)
134     {
135       uns j = bgetl(f);
136       if (i != j)
137         die("Discrepancy: %d instead of %d", j, i);
138       uns k = bgetl(f);
139       if (k != 2*mult)
140         die("Discrepancy: %d has count %d instead of %d", j, k, mult);
141     }
142   bclose(f);
143 }
144
145 /*** Longer records with hashes (similar to Shepherd's index records) ***/
146
147 struct key3 {
148   u32 hash[4];
149   u32 i;
150   u32 payload[3];
151 };
152
153 static inline int s3_compare(struct key3 *x, struct key3 *y)
154 {
155   /* FIXME: Maybe unroll manually? */
156   for (uns i=0; i<4; i++)
157     COMPARE(x->hash[i], y->hash[i]);
158   return 0;
159 }
160
161 static inline uns s3_hash(struct key3 *x)
162 {
163   return x->hash[0];
164 }
165
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
171
172 #include "lib/sorter/sorter.h"
173
174 static void
175 gen_hash_key(int mode, struct key3 *k, uns i)
176 {
177   k->i = i;
178   k->payload[0] = 7*i + 13;
179   k->payload[1] = 13*i + 19;
180   k->payload[2] = 19*i + 7;
181   switch (mode)
182     {
183     case 0:
184       k->hash[0] = i;
185       k->hash[1] = k->payload[0];
186       k->hash[2] = k->payload[1];
187       k->hash[3] = k->payload[2];
188       break;
189     case 1:
190       k->hash[0] = ~i;
191       k->hash[1] = k->payload[0];
192       k->hash[2] = k->payload[1];
193       k->hash[3] = k->payload[2];
194       break;
195     default: ;
196       struct MD5Context ctx;
197       MD5Init(&ctx);
198       MD5Update(&ctx, (byte*) &k->i, 4);
199       MD5Final((byte*) &k->hash, &ctx);
200       break;
201     }
202 }
203
204 static void
205 test_hashes(int mode, u64 size)
206 {
207   uns N = MIN(size / sizeof(struct key3), 0xffffffff);
208   log(L_INFO, ">>> Hashes (%s, N=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
209   struct key3 k, lastk;
210
211   struct fastbuf *f = bopen_tmp(65536);
212   uns hash_sum = 0;
213   for (uns i=0; i<N; i++)
214     {
215       gen_hash_key(mode, &k, i);
216       hash_sum += k.hash[3];
217       bwrite(f, &k, sizeof(k));
218     }
219   brewind(f);
220
221   start();
222   f = s3_sort(f, NULL);
223   stop();
224
225   SORT_XTRACE(2, "Verifying");
226   for (uns i=0; i<N; i++)
227     {
228       int ok = breadb(f, &k, sizeof(k));
229       ASSERT(ok);
230       if (i && s3_compare(&k, &lastk) <= 0)
231         ASSERT(0);
232       gen_hash_key(mode, &lastk, k.i);
233       if (memcmp(&k, &lastk, sizeof(k)))
234         ASSERT(0);
235       hash_sum -= k.hash[3];
236     }
237   ASSERT(!hash_sum);
238   bclose(f);
239 }
240
241 /*** Variable-length records (strings) with and without var-length data ***/
242
243 #define KEY4_MAX 256
244
245 struct key4 {
246   uns len;
247   byte s[KEY4_MAX];
248 };
249
250 static inline int s4_compare(struct key4 *x, struct key4 *y)
251 {
252   uns l = MIN(x->len, y->len);
253   int c = memcmp(x->s, y->s, l);
254   if (c)
255     return c;
256   COMPARE(x->len, y->len);
257   return 0;
258 }
259
260 static inline int s4_read_key(struct fastbuf *f, struct key4 *x)
261 {
262   x->len = bgetl(f);
263   if (x->len == 0xffffffff)
264     return 0;
265   ASSERT(x->len < KEY4_MAX);
266   breadb(f, x->s, x->len);
267   return 1;
268 }
269
270 static inline void s4_write_key(struct fastbuf *f, struct key4 *x)
271 {
272   ASSERT(x->len < KEY4_MAX);
273   bputl(f, x->len);
274   bwrite(f, x->s, x->len);
275 }
276
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
282
283 #include "lib/sorter/sorter.h"
284
285 #define s4b_compare s4_compare
286 #define s4b_read_key s4_read_key
287 #define s4b_write_key s4_write_key
288
289 static inline uns s4_data_size(struct key4 *x)
290 {
291   return x->len ? (x->s[0] ^ 0xad) : 0;
292 }
293
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
300
301 #include "lib/sorter/sorter.h"
302
303 static void
304 gen_key4(struct key4 *k)
305 {
306   k->len = random_max(KEY4_MAX);
307   for (uns i=0; i<k->len; i++)
308     k->s[i] = random();
309 }
310
311 static void
312 gen_data4(byte *buf, uns len, uns h)
313 {
314   while (len--)
315     {
316       *buf++ = h >> 24;
317       h = h*259309 + 17;
318     }
319 }
320
321 static void
322 test_strings(uns mode, u64 size)
323 {
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=%d)", (mode ? "with data " : ""), N);
327   srand(1);
328
329   struct key4 k, lastk;
330   byte buf[256], buf2[256];
331   uns sum = 0;
332
333   struct fastbuf *f = bopen_tmp(65536);
334   for (uns i=0; i<N; i++)
335     {
336       gen_key4(&k);
337       s4_write_key(f, &k);
338       uns h = hash_block(k.s, k.len);
339       sum += h;
340       if (mode)
341         {
342           gen_data4(buf, s4_data_size(&k), h);
343           bwrite(f, buf, s4_data_size(&k));
344         }
345     }
346   brewind(f);
347
348   start();
349   f = (mode ? s4b_sort : s4_sort)(f, NULL);
350   stop();
351
352   SORT_XTRACE(2, "Verifying");
353   for (uns i=0; i<N; i++)
354     {
355       int ok = s4_read_key(f, &k);
356       ASSERT(ok);
357       uns h = hash_block(k.s, k.len);
358       if (mode && s4_data_size(&k))
359         {
360           ok = breadb(f, buf, s4_data_size(&k));
361           ASSERT(ok);
362           gen_data4(buf2, s4_data_size(&k), h);
363           ASSERT(!memcmp(buf, buf2, s4_data_size(&k)));
364         }
365       if (i && s4_compare(&k, &lastk) < 0)
366         ASSERT(0);
367       sum -= h;
368       lastk = k;
369     }
370   ASSERT(!sum);
371   bclose(f);
372 }
373
374 static void
375 run_test(uns i, u64 size)
376 {
377   switch (i)
378     {
379     case 0:
380       test_int(0, size); break;
381     case 1:
382       test_int(1, size); break;
383     case 2:
384       test_int(2, size); break;
385     case 3:
386       test_counted(0, size); break;
387     case 4:
388       test_counted(1, size); break;
389     case 5:
390       test_counted(2, size); break;
391     case 6:
392       test_hashes(0, size); break;
393     case 7:
394       test_hashes(1, size); break;
395     case 8:
396       test_hashes(2, size); break;
397     case 9:
398       test_strings(0, size); break;
399     case 10:
400       test_strings(1, size); break;
401 #define TMAX 11
402     }
403 }
404
405 int
406 main(int argc, char **argv)
407 {
408   log_init(NULL);
409   int c;
410   u64 size = 10000000;
411   uns t = ~0;
412
413   while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS "s:t:v", CF_NO_LONG_OPTS, NULL)) >= 0)
414     switch (c)
415       {
416       case 's':
417         if (cf_parse_u64(optarg, &size))
418           goto usage;
419         break;
420       case 't':
421         t = atol(optarg);
422         if (t >= TMAX)
423           goto usage;
424         break;
425       case 'v':
426         sorter_trace++;
427         break;
428       default:
429       usage:
430         fputs("Usage: sort-test [-s <size>] [-t <test>]\n", stderr);
431         exit(1);
432       }
433   if (optind != argc)
434     goto usage;
435
436   if (t != ~0U)
437     run_test(t, size);
438   else
439     for (uns i=0; i<TMAX; i++)
440       run_test(i, size);
441
442   return 0;
443 }