Skip to content

Commit

Permalink
Add pool benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
srdja committed Feb 1, 2024
1 parent 4e8862d commit fee1522
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ enable_testing()

add_subdirectory(src)
add_subdirectory(test/unit)
add_subdirectory(test/benchmark)
add_subdirectory(examples)
11 changes: 11 additions & 0 deletions test/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions test/benchmark/pool/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
148 changes: 148 additions & 0 deletions test/benchmark/pool/dpool_bench.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#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;
}

0 comments on commit fee1522

Please sign in to comment.