summaryrefslogtreecommitdiff
path: root/ccan/btree/test/run-benchmark.c
blob: 62e59f805fa36c9aa4203e13a5fe10588d9a4435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* Include the main header first, to test it works */
#include <ccan/btree/btree.h>
/* Include the C files directly. */
#include <ccan/btree/btree.c>
#include <ccan/tap/tap.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

uint32_t rand32_state = 0;

/*
 * Finds a pseudorandom 32-bit number from 0 to 2^32-1 .
 * Uses the BCPL linear congruential generator method.
 */
static uint32_t rand32(void)
{
	rand32_state *= (uint32_t)0x7FF8A3ED;
	rand32_state += (uint32_t)0x2AA01D31;
	return rand32_state;
}

static void scramble(void *base, size_t nmemb, size_t size)
{
	char *i = base;
	char *o;
	size_t sd;
	for (;nmemb>1;nmemb--) {
		o = i + size*(rand32()%nmemb);
		for (sd=size;sd--;) {
			char tmp = *o;
			*o++ = *i;
			*i++ = tmp;
		}
	}
}

struct test_item {
	size_t key;
	uint32_t value;
};

/* For ordering a btree of test_item pointers. */
static btree_search_implement (
	order_by_key,
	const struct test_item *,
	,
	a == b,
	a < b
)

/* For qsorting an array of test_item pointers. */
static int compare_test_item(const void *ap, const void *bp)
{
	const struct test_item *a = *(const struct test_item * const*)ap;
	const struct test_item *b = *(const struct test_item * const*)bp;
	if (a == b)
		return 0;
	if (a < b)
		return -1;
	return 1;
}

/*
 * If lr == 0, make sure iter points to the item given.
 * If lr == 1, make sure iter points to after the item given.
 */
static int check_iter(btree_iterator iter_orig, const void *item, int lr)
{
	btree_iterator iter = {*iter_orig};
	if (iter->item != item)
		return 0;
	if (lr) {
		if (!btree_prev(iter))
			return 0;
	} else {
		if (!btree_deref(iter))
			return 0;
	}
	if (iter->item != item)
		return 0;
	if (iter->node->item[iter->k] != iter->item)
		return 0;
	
	return 1;
}

/*
 * Returns 1 on insert, 0 on duplicate,
 * -1 on bad iterator returned by find, and
 * -2 on bad iterator returned by insert.
 */
static int insert_test_item(struct btree *btree, struct test_item *item)
{
	btree_iterator iter;
	int lr;
	
	/* Find the first or last matching item, randomly choosing between the two. */
	lr = rand32() & 1;
	if (btree_find_lr(btree, item, iter, lr)) {
		if (!check_iter(iter, item, lr))
			return -1;
		return 0;
	}
	
	btree_insert_at(iter, item);
	
	if (iter->item != item)
		return -2;
	
	return 1;
}

/*
 * Returns 1 on remove, 0 on missing,
 * -1 on bad iterator returned by find, and
 * -2 on bad iterator returned by remove.
 */
static int remove_test_item(struct btree *btree, struct test_item *item)
{
	btree_iterator iter;
	
	if (!btree_find(btree, item, iter))
		return 0;
	
	if (!check_iter(iter, item, 0))
		return -1;
	
	btree_remove_at(iter);
	
	if (iter->item != item)
		return -2;
	
	return 1;
}

static struct {
	size_t success;
	
	size_t excess;
	size_t duplicate;
	size_t missing;
	size_t incorrect;
	size_t failed;
	
	size_t bad_iter_find;
	size_t bad_iter_insert;
	size_t bad_iter_remove;
	size_t bad_iter_next;
} stats;

static void clear_stats(void) {
	memset(&stats, 0, sizeof(stats));
}

static int print_stats(const char *success_label, size_t expected_success) {
	int failed = 0;
	
	printf("      %s:  \t%zu\n", success_label, stats.success);
	if (stats.success != expected_success)
		failed = 1;
	
	if (stats.excess)
		failed = 1, printf("      Excess:     \t%zu\n", stats.excess);
	if (stats.duplicate)
		failed = 1, printf("      Duplicate:  \t%zu\n", stats.duplicate);
	if (stats.missing)
		failed = 1, printf("      Missing:    \t%zu\n", stats.missing);
	if (stats.incorrect)
		failed = 1, printf("      Incorrect:  \t%zu\n", stats.incorrect);
	if (stats.failed)
		failed = 1, printf("      Failed:     \t%zu\n", stats.failed);
	
	if (stats.bad_iter_find || stats.bad_iter_insert ||
	    stats.bad_iter_remove || stats.bad_iter_next) {
		failed = 1;
		printf("      Bad iterators yielded by:\n");
		if (stats.bad_iter_find)
			printf("          btree_find_lr(): %zu\n", stats.bad_iter_find);
		if (stats.bad_iter_insert)
			printf("          btree_insert_at(): %zu\n", stats.bad_iter_insert);
		if (stats.bad_iter_remove)
			printf("          btree_remove_at(): %zu\n", stats.bad_iter_remove);
		if (stats.bad_iter_next)
			printf("          btree_next(): %zu\n", stats.bad_iter_next);
	}
	
	return !failed;
}

static void benchmark(size_t max_per_trial, size_t round_count, int random_counts)
{
	struct test_item **test_item;
	struct test_item *test_item_array;
	size_t i, count;
	size_t round = 0;
	
	test_item = malloc(max_per_trial * sizeof(*test_item));
	test_item_array = malloc(max_per_trial * sizeof(*test_item_array));
	
	if (!test_item || !test_item_array) {
		fail("Not enough memory for %zu keys per trial\n",
			max_per_trial);
		return;
	}
	
	/* Initialize test_item pointers. */
	for (i=0; i<max_per_trial; i++)
		test_item[i] = &test_item_array[i];
	
	/*
	 * If round_count is not zero, run round_count trials.
	 * Otherwise, run forever.
	 */
	for (round = 1; round_count==0 || round <= round_count; round++) {
		struct btree *btree;
		btree_iterator iter;
		
		printf("Round %zu\n", round);
		
		if (random_counts)
			count = rand32() % (max_per_trial+1);
		else
			count = max_per_trial;
		
		/*
		 * Initialize test items by giving them sequential keys and
		 * random values. Scramble them so the order of insertion
		 * will be random.
		 */
		for (i=0; i<count; i++) {
			test_item[i]->key = i;
			test_item[i]->value = rand32();
		}
		scramble(test_item, count, sizeof(*test_item));
		
		btree = btree_new(order_by_key);
		
		clear_stats();
		printf("   Inserting %zu items...\n", count);
		for (i=0; i<count; i++) {
			switch (insert_test_item(btree, test_item[i])) {
				case 1: stats.success++; break;
				case 0: stats.duplicate++; break;
				case -1: stats.bad_iter_find++; break;
				case -2: stats.bad_iter_insert++; break;
				default: stats.failed++; break;
			}
		}
		ok1(print_stats("Inserted", count));
		
		scramble(test_item, count, sizeof(*test_item));
		
		printf("   Finding %zu items...\n", count);
		clear_stats();
		for (i=0; i<count; i++) {
			int lr = rand32() & 1;
			
			if (!btree_find_lr(btree, test_item[i], iter, lr)) {
				stats.missing++;
				continue;
			}
			
			if (!check_iter(iter, test_item[i], lr)) {
				stats.bad_iter_find++;
				continue;
			}
			
			stats.success++;
		}
		ok1(print_stats("Retrieved", count));
		
		qsort(test_item, count, sizeof(*test_item), compare_test_item);
		
		printf("   Traversing forward through %zu items...\n", count);
		clear_stats();
		i = 0;
		for (btree_begin(btree, iter); btree_next(iter);) {
			if (i >= count) {
				stats.excess++;
				continue;
			}
			
			if (iter->item == test_item[i])
				stats.success++;
			else
				stats.incorrect++;
			
			i++;
		}
		ok1(print_stats("Retrieved", count));
		
		printf("   Traversing backward through %zu items...\n", count);
		clear_stats();
		i = count;
		for (btree_end(btree, iter); btree_prev(iter);) {
			if (!i) {
				stats.excess++;
				continue;
			}
			i--;
			
			if (iter->item == test_item[i])
				stats.success++;
			else
				stats.incorrect++;
		}
		ok1(print_stats("Retrieved", count));
		
		ok1(btree->count == count);
		
		//static int remove_test_item(struct btree *btree, struct test_item *item);
		scramble(test_item, count, sizeof(*test_item));
		
		printf("   Deleting %zu items...\n", count);
		clear_stats();
		for (i=0; i<count; i++) {
			int s = remove_test_item(btree, test_item[i]);
			if (s != 1)
				printf("remove_test_item failed\n");
			switch (s) {
				case 1: stats.success++; break;
				case 0: stats.missing++; break;
				case -1: stats.bad_iter_find++; break;
				case -2: stats.bad_iter_remove++; break;
				default: stats.failed++; break;
			}
		}
		ok1(btree->count == 0);
		ok1(print_stats("Deleted", count));
		ok1(btree->root->depth == 0 && btree->root->count == 0);
		
		btree_delete(btree);
	}
	
	free(test_item);
	free(test_item_array);
}

int main(void)
{
	plan_tests(32);
	
	benchmark(300000, 4, 0);
	
	return exit_status();
}