1010#include <stdlib.h>
1111#include <bfdev/log.h>
1212#include <bfdev/rbtree.h>
13+ #include <bfdev/macro.h>
1314#include "../time.h"
1415
1516#define RB_DEBUG 0
1617#define RB_CACHED 1
18+
19+ #define TEST_LOOP 3
20+ #define TEST_WARMUP 32
1721#define TEST_LEN 1000000
1822
1923struct bench_node {
@@ -43,6 +47,7 @@ node_dump(struct bench_node *node)
4347
4448#if RB_CACHED
4549static BFDEV_RB_ROOT_CACHED (bench_root );
50+ # define bc_root (&(bench_root).root)
4651# define bc_insert bfdev_rb_cached_insert
4752# define bc_delete bfdev_rb_cached_delete
4853# define bc_for_each_entry bfdev_rb_cached_for_each_entry
@@ -51,6 +56,7 @@ static BFDEV_RB_ROOT_CACHED(bench_root);
5156# define bc_deepth (cached ) test_deepth((cached)->root.node)
5257#else
5358static BFDEV_RB_ROOT (bench_root );
59+ # define bc_root (&bench_root)
5460# define bc_insert bfdev_rb_insert
5561# define bc_delete bfdev_rb_delete
5662# define bc_for_each_entry bfdev_rb_for_each_entry
@@ -80,47 +86,79 @@ static long
8086demo_cmp (const bfdev_rb_node_t * node1 ,
8187 const bfdev_rb_node_t * node2 , void * pdata )
8288{
83- struct bench_node * test1 , * test2 ;
89+ unsigned long test1 , test2 ;
8490
85- test1 = rb_to_bench (node1 );
86- test2 = rb_to_bench (node2 );
91+ test1 = rb_to_bench (node1 )-> data ;
92+ test2 = rb_to_bench (node2 )-> data ;
8793
8894 /* Ignoring conflicts */
89- return test1 -> data < test2 -> data ? -1 : 1 ;
95+ return bfdev_cmp (test1 > test2 );
96+ }
97+
98+ static long
99+ demo_find (const bfdev_rb_node_t * node , void * pdata )
100+ {
101+ unsigned long test1 , test2 ;
102+
103+ test1 = rb_to_bench (node )-> data ;
104+ test2 = (unsigned long )pdata ;
105+
106+ if (test1 == test2 )
107+ return 0 ;
108+
109+ return bfdev_cmp (test1 > test2 );
90110}
91111
92112int
93113main (int argc , const char * argv [])
94114{
95- struct bench_node * node , * tmp ;
96- unsigned int count ;
97- void * block ;
115+ struct bench_node * nodes , * node ;
116+ unsigned int count , loop ;
98117
99- node = block = malloc (sizeof (* node ) * TEST_LEN );
100- if (!block ) {
118+ nodes = malloc (sizeof (* node ) * TEST_LEN );
119+ if (!nodes ) {
101120 bfdev_log_err ("Insufficient memory!\n" );
102121 return 1 ;
103122 }
104123
105124 srand (time (NULL ));
106125 bfdev_log_info ("Generate %u node:\n" , TEST_LEN );
107126 for (count = 0 ; count < TEST_LEN ; ++ count ) {
108- node [count ].data = ((uint64_t )rand () << 32 ) | rand ();
127+ nodes [count ].data = ((uint64_t )rand () << 32 ) | rand ();
109128#if RB_DEBUG
110- node [count ].num = count + 1 ;
111- bfdev_log_info ("\t%08d: 0x%016lx\n" , node -> num , node -> data );
129+ nodes [count ].num = count + 1 ;
130+ bfdev_log_info ("\t%08d: 0x%016lx\n" , nodes -> num , nodes -> data );
112131#endif
113132 }
114133
115134 bfdev_log_info ("Insert nodes:\n" );
116135 EXAMPLE_TIME_STATISTICAL (
117136 for (count = 0 ; count < TEST_LEN ; ++ count )
118- bc_insert (& bench_root , & node [count ].node , demo_cmp , NULL );
137+ bc_insert (& bench_root , & nodes [count ].node , demo_cmp , NULL );
119138 0 ;
120139 );
121140 count = bc_deepth (& bench_root );
122141 bfdev_log_info ("\trb deepth: %u\n" , count );
123142
143+ bfdev_log_notice ("Warnup cache:\n" );
144+ for (loop = 0 ; loop < TEST_WARMUP ; ++ loop ) {
145+ for (count = 0 ; count < TEST_LEN ; ++ count ) {
146+ if (!bfdev_rb_find (bc_root , (void * )nodes [count ].data , & demo_find ))
147+ return 1 ;
148+ }
149+ }
150+
151+ for (loop = 0 ; loop < TEST_LOOP ; ++ loop ) {
152+ bfdev_log_info ("Find nodes loop%u...\n" , loop );
153+ EXAMPLE_TIME_STATISTICAL (
154+ for (count = 0 ; count < TEST_LEN ; ++ count ) {
155+ if (!bfdev_rb_find (bc_root , (void * )nodes [count ].data , & demo_find ))
156+ return 1 ;
157+ }
158+ 0 ;
159+ );
160+ }
161+
124162 count = 0 ;
125163 bfdev_log_info ("Middle iteration:\n" );
126164 EXAMPLE_TIME_STATISTICAL (
@@ -130,7 +168,10 @@ main(int argc, const char *argv[])
130168 }
131169 0 ;
132170 );
171+
133172 bfdev_log_info ("\ttotal num: %u\n" , count );
173+ if (count != TEST_LEN )
174+ return 1 ;
134175
135176 count = 0 ;
136177 bfdev_log_info ("Postorder iteration:\n" );
@@ -141,21 +182,13 @@ main(int argc, const char *argv[])
141182 }
142183 0 ;
143184 );
144- bfdev_log_info ("\ttotal num: %u\n" , count );
145185
146- count = 0 ;
147- bfdev_log_info ("Postorder safe iteration:\n" );
148- EXAMPLE_TIME_STATISTICAL (
149- bc_post_for_each_entry_safe (node , tmp , & bench_root , node ) {
150- node_dump (node );
151- count ++ ;
152- }
153- 0 ;
154- );
155186 bfdev_log_info ("\ttotal num: %u\n" , count );
187+ if (count != TEST_LEN )
188+ return 1 ;
156189
157190 bfdev_log_info ("Done.\n" );
158- free (block );
191+ free (nodes );
159192
160193 return 0 ;
161194}
0 commit comments