Skip to content

Commit

Permalink
feat examples: added cache warmup on some benchmarks
Browse files Browse the repository at this point in the history
Signed-off-by: John Sanpe <[email protected]>
  • Loading branch information
sanpeqf committed Jan 2, 2025
1 parent 42e03f4 commit 8529383
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 26 deletions.
13 changes: 12 additions & 1 deletion examples/hashmap/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#include <bfdev/hashmap.h>
#include "../time.h"

#define TEST_LOOP 10
#define TEST_LOOP 3
#define TEST_WARMUP 32
#define TEST_SIZE 1000000

struct test_node {
Expand Down Expand Up @@ -101,6 +102,16 @@ main(int argc, const char *argv[])
0;
);

bfdev_log_notice("Warmup cache...\n");
for (loop = 0; loop < TEST_WARMUP; ++loop) {
for (count = 0; count < TEST_SIZE; ++count) {
value = nodes[count].value;
hnode = bfdev_hashmap_find(&test_map, (void *)value);
if (!hnode)
return 1;
}
}

for (loop = 0; loop < TEST_LOOP; ++loop) {
bfdev_log_info("Find nodes loop%u...\n", loop);
EXAMPLE_TIME_STATISTICAL(
Expand Down
42 changes: 41 additions & 1 deletion examples/radix/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#include <bfdev/radix.h>
#include "../time.h"

#define TEST_LOOP 10
#define TEST_LOOP 3
#define TEST_WARMUP 32
#define TEST_SIZE 1000000

static
Expand Down Expand Up @@ -52,6 +53,16 @@ main(int argc, const char *argv[])
0;
);

bfdev_log_notice("Warmup cache...\n");
for (loop = 0; loop < TEST_WARMUP; ++loop) {
for (count = 0; count < TEST_SIZE; ++count) {
if (!bfdev_radix_find(&root, count)) {
bfdev_log_err("Warmup cache failed!\n");
return 1;
}
}
}

for (loop = 0; loop < TEST_LOOP; ++loop) {
bfdev_log_info("Find nodes loop%u...\n", loop);
EXAMPLE_TIME_STATISTICAL(
Expand All @@ -67,6 +78,35 @@ main(int argc, const char *argv[])
);
}

bfdev_log_info("For each nodes:\n");
EXAMPLE_TIME_STATISTICAL(
uintptr_t index;
void *value;

count = 0;
bfdev_radix_for_each(value, &root, &index) {
if (count++ != index) {
bfdev_log_info("Index verification failed!\n");
return 1;
}

if (*(unsigned int *)value != data[index]) {
bfdev_log_info("Data verification failed!\n");
return 1;
}
}

if (count != TEST_SIZE) {
bfdev_log_info("Index size error!\n");
return 1;
}

0;
);

count = root.tree.level;
bfdev_log_info("\tradix level: %u\n", count);

bfdev_log_info("Free nodes:\n");
EXAMPLE_TIME_STATISTICAL(
for (count = 0; count < TEST_SIZE; ++count) {
Expand Down
81 changes: 57 additions & 24 deletions examples/rbtree/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
#include <stdlib.h>
#include <bfdev/log.h>
#include <bfdev/rbtree.h>
#include <bfdev/macro.h>
#include "../time.h"

#define RB_DEBUG 0
#define RB_CACHED 1

#define TEST_LOOP 3
#define TEST_WARMUP 32
#define TEST_LEN 1000000

struct bench_node {
Expand Down Expand Up @@ -43,6 +47,7 @@ node_dump(struct bench_node *node)

#if RB_CACHED
static BFDEV_RB_ROOT_CACHED(bench_root);
# define bc_root (&(bench_root).root)
# define bc_insert bfdev_rb_cached_insert
# define bc_delete bfdev_rb_cached_delete
# define bc_for_each_entry bfdev_rb_cached_for_each_entry
Expand All @@ -51,6 +56,7 @@ static BFDEV_RB_ROOT_CACHED(bench_root);
# define bc_deepth(cached) test_deepth((cached)->root.node)
#else
static BFDEV_RB_ROOT(bench_root);
# define bc_root (&bench_root)
# define bc_insert bfdev_rb_insert
# define bc_delete bfdev_rb_delete
# define bc_for_each_entry bfdev_rb_for_each_entry
Expand Down Expand Up @@ -80,47 +86,79 @@ static long
demo_cmp(const bfdev_rb_node_t *node1,
const bfdev_rb_node_t *node2, void *pdata)
{
struct bench_node *test1, *test2;
unsigned long test1, test2;

test1 = rb_to_bench(node1);
test2 = rb_to_bench(node2);
test1 = rb_to_bench(node1)->data;
test2 = rb_to_bench(node2)->data;

/* Ignoring conflicts */
return test1->data < test2->data ? -1 : 1;
return bfdev_cmp(test1 > test2);
}

static long
demo_find(const bfdev_rb_node_t *node, void *pdata)
{
unsigned long test1, test2;

test1 = rb_to_bench(node)->data;
test2 = (unsigned long)pdata;

if (test1 == test2)
return 0;

return bfdev_cmp(test1 > test2);
}

int
main(int argc, const char *argv[])
{
struct bench_node *node, *tmp;
unsigned int count;
void *block;
struct bench_node *nodes, *node;
unsigned int count, loop;

node = block = malloc(sizeof(*node) * TEST_LEN);
if (!block) {
nodes = malloc(sizeof(*node) * TEST_LEN);
if (!nodes) {
bfdev_log_err("Insufficient memory!\n");
return 1;
}

srand(time(NULL));
bfdev_log_info("Generate %u node:\n", TEST_LEN);
for (count = 0; count < TEST_LEN; ++count) {
node[count].data = ((uint64_t)rand() << 32) | rand();
nodes[count].data = ((uint64_t)rand() << 32) | rand();
#if RB_DEBUG
node[count].num = count + 1;
bfdev_log_info("\t%08d: 0x%016lx\n", node->num, node->data);
nodes[count].num = count + 1;
bfdev_log_info("\t%08d: 0x%016lx\n", nodes->num, nodes->data);
#endif
}

bfdev_log_info("Insert nodes:\n");
EXAMPLE_TIME_STATISTICAL(
for (count = 0; count < TEST_LEN; ++count)
bc_insert(&bench_root, &node[count].node, demo_cmp, NULL);
bc_insert(&bench_root, &nodes[count].node, demo_cmp, NULL);
0;
);
count = bc_deepth(&bench_root);
bfdev_log_info("\trb deepth: %u\n", count);

bfdev_log_notice("Warnup cache:\n");
for (loop = 0; loop < TEST_WARMUP; ++loop) {
for (count = 0; count < TEST_LEN; ++count) {
if (!bfdev_rb_find(bc_root, (void *)nodes[count].data, &demo_find))
return 1;
}
}

for (loop = 0; loop < TEST_LOOP; ++loop) {
bfdev_log_info("Find nodes loop%u...\n", loop);
EXAMPLE_TIME_STATISTICAL(
for (count = 0; count < TEST_LEN; ++count) {
if (!bfdev_rb_find(bc_root, (void *)nodes[count].data, &demo_find))
return 1;
}
0;
);
}

count = 0;
bfdev_log_info("Middle iteration:\n");
EXAMPLE_TIME_STATISTICAL(
Expand All @@ -130,7 +168,10 @@ main(int argc, const char *argv[])
}
0;
);

bfdev_log_info("\ttotal num: %u\n", count);
if (count != TEST_LEN)
return 1;

count = 0;
bfdev_log_info("Postorder iteration:\n");
Expand All @@ -141,21 +182,13 @@ main(int argc, const char *argv[])
}
0;
);
bfdev_log_info("\ttotal num: %u\n", count);

count = 0;
bfdev_log_info("Postorder safe iteration:\n");
EXAMPLE_TIME_STATISTICAL(
bc_post_for_each_entry_safe(node, tmp, &bench_root, node) {
node_dump(node);
count++;
}
0;
);
bfdev_log_info("\ttotal num: %u\n", count);
if (count != TEST_LEN)
return 1;

bfdev_log_info("Done.\n");
free(block);
free(nodes);

return 0;
}

0 comments on commit 8529383

Please sign in to comment.