rbtree: add prio tree and interval tree tests
[linux-2.6/libata-dev.git] / lib / prio_tree_test.c
blobc26084ddc6a45534d69e394e6d02df35e9c59a84
1 #include <linux/module.h>
2 #include <linux/prio_tree.h>
3 #include <linux/random.h>
4 #include <asm/timex.h>
6 #define NODES 100
7 #define PERF_LOOPS 100000
8 #define SEARCHES 100
9 #define SEARCH_LOOPS 10000
11 static struct prio_tree_root root;
12 static struct prio_tree_node nodes[NODES];
13 static u32 queries[SEARCHES];
15 static struct rnd_state rnd;
17 static inline unsigned long
18 search(unsigned long query, struct prio_tree_root *root)
20 struct prio_tree_iter iter;
21 unsigned long results = 0;
23 prio_tree_iter_init(&iter, root, query, query);
24 while (prio_tree_next(&iter))
25 results++;
26 return results;
29 static void init(void)
31 int i;
32 for (i = 0; i < NODES; i++) {
33 u32 a = prandom32(&rnd), b = prandom32(&rnd);
34 if (a <= b) {
35 nodes[i].start = a;
36 nodes[i].last = b;
37 } else {
38 nodes[i].start = b;
39 nodes[i].last = a;
42 for (i = 0; i < SEARCHES; i++)
43 queries[i] = prandom32(&rnd);
46 static int prio_tree_test_init(void)
48 int i, j;
49 unsigned long results;
50 cycles_t time1, time2, time;
52 printk(KERN_ALERT "prio tree insert/remove");
54 prandom32_seed(&rnd, 3141592653589793238ULL);
55 INIT_PRIO_TREE_ROOT(&root);
56 init();
58 time1 = get_cycles();
60 for (i = 0; i < PERF_LOOPS; i++) {
61 for (j = 0; j < NODES; j++)
62 prio_tree_insert(&root, nodes + j);
63 for (j = 0; j < NODES; j++)
64 prio_tree_remove(&root, nodes + j);
67 time2 = get_cycles();
68 time = time2 - time1;
70 time = div_u64(time, PERF_LOOPS);
71 printk(" -> %llu cycles\n", (unsigned long long)time);
73 printk(KERN_ALERT "prio tree search");
75 for (j = 0; j < NODES; j++)
76 prio_tree_insert(&root, nodes + j);
78 time1 = get_cycles();
80 results = 0;
81 for (i = 0; i < SEARCH_LOOPS; i++)
82 for (j = 0; j < SEARCHES; j++)
83 results += search(queries[j], &root);
85 time2 = get_cycles();
86 time = time2 - time1;
88 time = div_u64(time, SEARCH_LOOPS);
89 results = div_u64(results, SEARCH_LOOPS);
90 printk(" -> %llu cycles (%lu results)\n",
91 (unsigned long long)time, results);
93 return -EAGAIN; /* Fail will directly unload the module */
96 static void prio_tree_test_exit(void)
98 printk(KERN_ALERT "test exit\n");
101 module_init(prio_tree_test_init)
102 module_exit(prio_tree_test_exit)
104 MODULE_LICENSE("GPL");
105 MODULE_AUTHOR("Michel Lespinasse");
106 MODULE_DESCRIPTION("Prio Tree test");