From 8529383d2d431d553fe4645d7e60388ca03dafb8 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 3 Jan 2025 03:57:30 +0800 Subject: [PATCH] feat examples: added cache warmup on some benchmarks Signed-off-by: John Sanpe --- examples/hashmap/benchmark.c | 13 +++++- examples/radix/benchmark.c | 42 ++++++++++++++++++- examples/rbtree/benchmark.c | 81 +++++++++++++++++++++++++----------- 3 files changed, 110 insertions(+), 26 deletions(-) diff --git a/examples/hashmap/benchmark.c b/examples/hashmap/benchmark.c index c8cfcd1e..df527115 100644 --- a/examples/hashmap/benchmark.c +++ b/examples/hashmap/benchmark.c @@ -12,7 +12,8 @@ #include #include "../time.h" -#define TEST_LOOP 10 +#define TEST_LOOP 3 +#define TEST_WARMUP 32 #define TEST_SIZE 1000000 struct test_node { @@ -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( diff --git a/examples/radix/benchmark.c b/examples/radix/benchmark.c index 2ed55835..da781321 100644 --- a/examples/radix/benchmark.c +++ b/examples/radix/benchmark.c @@ -12,7 +12,8 @@ #include #include "../time.h" -#define TEST_LOOP 10 +#define TEST_LOOP 3 +#define TEST_WARMUP 32 #define TEST_SIZE 1000000 static @@ -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( @@ -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) { diff --git a/examples/rbtree/benchmark.c b/examples/rbtree/benchmark.c index f8c0c194..86d73328 100644 --- a/examples/rbtree/benchmark.c +++ b/examples/rbtree/benchmark.c @@ -10,10 +10,14 @@ #include #include #include +#include #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 { @@ -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 @@ -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 @@ -80,24 +86,37 @@ 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; } @@ -105,22 +124,41 @@ main(int argc, const char *argv[]) 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( @@ -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"); @@ -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; }