From fa82ce3b1ed68aee071fe094bc5c82a168736091 Mon Sep 17 00:00:00 2001 From: Emiel Por Date: Fri, 6 Dec 2024 09:27:47 -0800 Subject: [PATCH] Add benchmarks for FreeListAllocator. --- benchmarks/CMakeLists.txt | 6 ++ benchmarks/free_list_allocator.cpp | 91 ++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 benchmarks/free_list_allocator.cpp diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index bc716bd2..695b327e 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -24,7 +24,13 @@ add_executable(timestamp timestamp.cpp) target_include_directories(timestamp PUBLIC ../catkit_core) target_link_libraries(timestamp PUBLIC catkit_core) +# Free list allocator benchmark +add_executable(free_list_allocator free_list_allocator.cpp) +target_include_directories(free_list_allocator PUBLIC ../catkit_core) +target_link_libraries(free_list_allocator PUBLIC catkit_core) + # Add install files install(TARGETS datastream_latency DESTINATION bin) install(TARGETS datastream_submit DESTINATION bin) install(TARGETS timestamp DESTINATION bin) +install(TARGETS free_list_allocator DESTINATION bin) diff --git a/benchmarks/free_list_allocator.cpp b/benchmarks/free_list_allocator.cpp new file mode 100644 index 00000000..5ffd62be --- /dev/null +++ b/benchmarks/free_list_allocator.cpp @@ -0,0 +1,91 @@ +#include "FreeListAllocator.h" +#include "Timing.h" +#include + +void benchmark_linux_scalability() +{ + typedef FreeListAllocator<16384, 32> Allocator; + + const size_t N = 10000000; + + auto *handles = new Allocator::BlockHandle[N]; + + Allocator allocator(size_t(32) * N); + + auto start = GetTimeStamp(); + + for (size_t i = 0; i < N; ++i) + { + handles[i] = allocator.Allocate(16); + } + + for (size_t i = 0; i < N; ++i) + { + allocator.Deallocate(handles[i]); + } + + auto end = GetTimeStamp(); + + std::cout << "Linux Scalability:" << std::endl; + std::cout << "Time: " << (end - start) / 1e9 << " sec" << std::endl; + std::cout << "Throughput: " << 2 * N / ((end - start) / 1e9) << " ops/s" << std::endl; + + delete[] handles; +} + +void benchmark_larson() +{ + const size_t ALIGNMENT = 32; + typedef FreeListAllocator<16384, ALIGNMENT> Allocator; + + const size_t N = 10000000; + const size_t M = 1000; + const size_t MIN_SIZE = 16; + const size_t MAX_SIZE = 128; + + auto *handles = new Allocator::BlockHandle[M]; + for (size_t i = 0; i < M; ++i) + { + handles[i] = -1; + } + + auto *indices = new size_t[N]; + auto *sizes = new size_t[N]; + for (size_t i = 0; i < N; ++i) + { + indices[i] = rand() % M; + sizes[i] = (MIN_SIZE + (rand() % (MAX_SIZE - MIN_SIZE))) * ALIGNMENT; + } + + Allocator allocator(size_t(1024) * 1024 * 1024); + + auto start = GetTimeStamp(); + + for (size_t i = 0; i < N; ++i) + { + size_t index = indices[i]; + size_t size = sizes[i]; + + if (handles[index] != -1) + { + allocator.Deallocate(handles[index]); + } + + handles[index] = allocator.Allocate(size); + } + + auto end = GetTimeStamp(); + std::cout << "Larson benchmark:" << std::endl; + std::cout << "Time: " << (end - start) / 1e9 << " sec" << std::endl; + std::cout << "Throughput: " << (N * 2 - M) / ((end - start) / 1e9) << " ops/s" << std::endl; + + delete[] handles; +} + +int main(int argc, char **argv) +{ + benchmark_linux_scalability(); + benchmark_larson(); + + return 0; +}