From fee1522086666643d39995ebed807a76784ae14d Mon Sep 17 00:00:00 2001 From: srdja <1875137+srdja@users.noreply.github.com> Date: Wed, 31 Jan 2024 17:57:06 -0800 Subject: [PATCH] Add pool benchmark --- CMakeLists.txt | 1 + test/benchmark/CMakeLists.txt | 11 +++ test/benchmark/pool/CMakeLists.txt | 7 ++ test/benchmark/pool/dpool_bench.c | 148 +++++++++++++++++++++++++++++ 4 files changed, 167 insertions(+) create mode 100644 test/benchmark/CMakeLists.txt create mode 100644 test/benchmark/pool/CMakeLists.txt create mode 100644 test/benchmark/pool/dpool_bench.c diff --git a/CMakeLists.txt b/CMakeLists.txt index ceff89a..8bc387f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,4 +18,5 @@ enable_testing() add_subdirectory(src) add_subdirectory(test/unit) +add_subdirectory(test/benchmark) add_subdirectory(examples) \ No newline at end of file diff --git a/test/benchmark/CMakeLists.txt b/test/benchmark/CMakeLists.txt new file mode 100644 index 0000000..7fa27da --- /dev/null +++ b/test/benchmark/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.5) + +set(CMAKE_DISABLE_IN_SOURCE_BUILD ON) + +if (UNIX) +set(CFLAGS "-Wall -Werror") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CFLAGS}") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CFLAGS}") +endif() + +add_subdirectory(pool) \ No newline at end of file diff --git a/test/benchmark/pool/CMakeLists.txt b/test/benchmark/pool/CMakeLists.txt new file mode 100644 index 0000000..1745018 --- /dev/null +++ b/test/benchmark/pool/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.5) +project(cc_pool_bench) + +include_directories(${PROJECT_SOURCE_DIR}/include ${collectc_INCLUDE_DIRS}) + +add_executable(dpool_bench dpool_bench.c) +target_link_libraries(dpool_bench collectc) \ No newline at end of file diff --git a/test/benchmark/pool/dpool_bench.c b/test/benchmark/pool/dpool_bench.c new file mode 100644 index 0000000..b654f1e --- /dev/null +++ b/test/benchmark/pool/dpool_bench.c @@ -0,0 +1,148 @@ +#include +#include +#include +#include "memory/cc_dynamic_pool.h" +#include "cc_list.h" +#include "cc_slist.h" + +// 500 MB +#define POOL_SIZE 500000000 + +void bench_malloc() +{ + int val = 100; + + printf("Runing system malloc test...\n"); + printf("Populating list... "); + clock_t t_start = clock(); + + CC_SList* list; + cc_slist_new(&list); + + for (int i = 0; i < 10000000; i++) { + cc_slist_add(list, (void*) & val); + } + clock_t t_end = clock(); + double t_delta = (double)(t_end - t_start)/CLOCKS_PER_SEC; + printf("done in %Lf sec.\n", t_delta); + + printf("Reading list... "); + t_start = clock(); + + CC_SLIST_FOREACH(v, list, { + (int*)v += 1; + }); + t_end = clock(); + t_delta = (double)(t_end - t_start)/CLOCKS_PER_SEC; + + cc_slist_destroy(list); + printf("done in %Lf sec.\n\n", t_delta); +} + +CC_DynamicPool* pool; + + +void pool_free(void* ptr) +{ + cc_dynamic_pool_free(ptr, pool); +} + +void *pool_malloc(size_t size) +{ + return cc_dynamic_pool_malloc(size, pool); +} + +void *pool_calloc(size_t count, size_t size) +{ + return cc_dynamic_pool_calloc(count, size, pool); +} + + +void bench_pool_packed() +{ + cc_dynamic_pool_new(POOL_SIZE, &pool); + int val = 100; + + printf("Runing packed pool test...\n"); + printf("Populating list... "); + clock_t t_start = clock(); + + CC_SList* list; + CC_SListConf conf; + conf.mem_alloc = pool_malloc; + conf.mem_calloc = pool_calloc; + conf.mem_free = pool_free; + cc_slist_new_conf(&conf, &list); + + for (int i = 0; i < 10000000; i++) { + cc_slist_add(list, (void*)&val); + } + clock_t t_end = clock(); + double t_delta = (double)(t_end - t_start)/CLOCKS_PER_SEC; + printf("done in %Lf sec.\n", t_delta); + + printf("Reading list... "); + t_start = clock(); + + CC_SLIST_FOREACH(v, list, { + (int*)v += 1; + }); + t_end = clock(); + t_delta = (double)(t_end - t_start)/CLOCKS_PER_SEC; + + printf("done in %f sec.\n\n", t_delta); + + cc_dynamic_pool_destroy(pool); +} + + +void bench_pool_aligned() +{ + CC_DynamicPoolConf pconf; + cc_dynamic_pool_conf_init(&pconf); + pconf.alignment_boundary = false; + pconf.alignment_boundary = sizeof(size_t); + cc_dynamic_pool_new_conf(POOL_SIZE, &pconf, &pool); + int val = 100; + + printf("Runing aligned pool test...\n"); + printf("Populating list... "); + clock_t t_start = clock(); + + CC_SList* list; + CC_SListConf conf; + conf.mem_alloc = pool_malloc; + conf.mem_calloc = pool_calloc; + conf.mem_free = pool_free; + cc_slist_new_conf(&conf, &list); + + for (int i = 0; i < 10000000; i++) { + cc_slist_add(list, (void*)&val); + } + clock_t t_end = clock(); + double t_delta = (double)(t_end - t_start)/CLOCKS_PER_SEC; + printf("done in %Lf sec.\n", t_delta); + + printf("Reading list... "); + t_start = clock(); + + CC_SLIST_FOREACH(v, list, { + (int*)v += 1; + }); + t_end = clock(); + t_delta = (double)(t_end - t_start)/CLOCKS_PER_SEC; + + printf("done in %f sec.\n\n", t_delta); + + cc_dynamic_pool_destroy(pool); +} + + +int main(int argc, char** argv) +{ + bench_pool_aligned(); + bench_pool_packed(); + bench_malloc(); + + return 0; +} \ No newline at end of file