Skip to content

Commit 8529383

Browse files
committed
feat examples: added cache warmup on some benchmarks
Signed-off-by: John Sanpe <sanpeqf@gmail.com>
1 parent 42e03f4 commit 8529383

File tree

3 files changed

+110
-26
lines changed

3 files changed

+110
-26
lines changed

examples/hashmap/benchmark.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#include <bfdev/hashmap.h>
1313
#include "../time.h"
1414

15-
#define TEST_LOOP 10
15+
#define TEST_LOOP 3
16+
#define TEST_WARMUP 32
1617
#define TEST_SIZE 1000000
1718

1819
struct test_node {
@@ -101,6 +102,16 @@ main(int argc, const char *argv[])
101102
0;
102103
);
103104

105+
bfdev_log_notice("Warmup cache...\n");
106+
for (loop = 0; loop < TEST_WARMUP; ++loop) {
107+
for (count = 0; count < TEST_SIZE; ++count) {
108+
value = nodes[count].value;
109+
hnode = bfdev_hashmap_find(&test_map, (void *)value);
110+
if (!hnode)
111+
return 1;
112+
}
113+
}
114+
104115
for (loop = 0; loop < TEST_LOOP; ++loop) {
105116
bfdev_log_info("Find nodes loop%u...\n", loop);
106117
EXAMPLE_TIME_STATISTICAL(

examples/radix/benchmark.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#include <bfdev/radix.h>
1313
#include "../time.h"
1414

15-
#define TEST_LOOP 10
15+
#define TEST_LOOP 3
16+
#define TEST_WARMUP 32
1617
#define TEST_SIZE 1000000
1718

1819
static
@@ -52,6 +53,16 @@ main(int argc, const char *argv[])
5253
0;
5354
);
5455

56+
bfdev_log_notice("Warmup cache...\n");
57+
for (loop = 0; loop < TEST_WARMUP; ++loop) {
58+
for (count = 0; count < TEST_SIZE; ++count) {
59+
if (!bfdev_radix_find(&root, count)) {
60+
bfdev_log_err("Warmup cache failed!\n");
61+
return 1;
62+
}
63+
}
64+
}
65+
5566
for (loop = 0; loop < TEST_LOOP; ++loop) {
5667
bfdev_log_info("Find nodes loop%u...\n", loop);
5768
EXAMPLE_TIME_STATISTICAL(
@@ -67,6 +78,35 @@ main(int argc, const char *argv[])
6778
);
6879
}
6980

81+
bfdev_log_info("For each nodes:\n");
82+
EXAMPLE_TIME_STATISTICAL(
83+
uintptr_t index;
84+
void *value;
85+
86+
count = 0;
87+
bfdev_radix_for_each(value, &root, &index) {
88+
if (count++ != index) {
89+
bfdev_log_info("Index verification failed!\n");
90+
return 1;
91+
}
92+
93+
if (*(unsigned int *)value != data[index]) {
94+
bfdev_log_info("Data verification failed!\n");
95+
return 1;
96+
}
97+
}
98+
99+
if (count != TEST_SIZE) {
100+
bfdev_log_info("Index size error!\n");
101+
return 1;
102+
}
103+
104+
0;
105+
);
106+
107+
count = root.tree.level;
108+
bfdev_log_info("\tradix level: %u\n", count);
109+
70110
bfdev_log_info("Free nodes:\n");
71111
EXAMPLE_TIME_STATISTICAL(
72112
for (count = 0; count < TEST_SIZE; ++count) {

examples/rbtree/benchmark.c

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
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

1923
struct bench_node {
@@ -43,6 +47,7 @@ node_dump(struct bench_node *node)
4347

4448
#if RB_CACHED
4549
static 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
5358
static 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
8086
demo_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

92112
int
93113
main(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

Comments
 (0)