From 5adbf8eee297060ec25261d85912d60f5ed7515e Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Thu, 11 Sep 2025 12:32:25 -0400 Subject: [PATCH] Organize bench_reference.cpp with doctest --- CMakeLists.txt | 3 ++- benchmarks/bench_reference.cpp | 49 ++++++++++++---------------------- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4bbd21..9e3b2da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -277,6 +277,7 @@ target_compile_options(test_reference PRIVATE -UNDEBUG) add_test(NAME reference_tests COMMAND test_reference) add_executable(bench_reference benchmarks/bench_reference.cpp) -target_link_libraries(bench_reference nanobench_impl Threads::Threads) +target_link_libraries(bench_reference doctest_impl nanobench_impl + Threads::Threads) target_include_directories(bench_reference PRIVATE src) add_test(NAME reference_benchmarks COMMAND bench_reference) diff --git a/benchmarks/bench_reference.cpp b/benchmarks/bench_reference.cpp index d3005fb..649bcbb 100644 --- a/benchmarks/bench_reference.cpp +++ b/benchmarks/bench_reference.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include "reference.hpp" @@ -280,7 +281,7 @@ void benchmark_strong_copy_with_weak_contention(ankerl::nanobench::Bench &bench, } // anonymous namespace -void compare_creation() { +TEST_CASE("Creation performance comparison") { ankerl::nanobench::Bench bench; bench.title("Creation performance comparison"); bench.relative(true); @@ -288,7 +289,7 @@ void compare_creation() { benchmark_creation>(bench); } -void compare_copy() { +TEST_CASE("Copy performance comparison") { ankerl::nanobench::Bench bench; bench.title("Copy performance comparison"); bench.relative(true); @@ -296,7 +297,7 @@ void compare_copy() { benchmark_copy>(bench); } -void compare_move() { +TEST_CASE("Move performance comparison") { ankerl::nanobench::Bench bench; bench.title("Move performance comparison"); bench.relative(true); @@ -304,7 +305,7 @@ void compare_move() { benchmark_move>(bench); } -void compare_weak_copy() { +TEST_CASE("Weak copy performance comparison") { ankerl::nanobench::Bench bench; bench.title("Weak copy performance comparison"); bench.relative(true); @@ -312,7 +313,7 @@ void compare_weak_copy() { benchmark_weak_copy>(bench); } -void compare_weak_move() { +TEST_CASE("Weak move performance comparison") { ankerl::nanobench::Bench bench; bench.title("Weak move performance comparison"); bench.relative(true); @@ -320,7 +321,7 @@ void compare_weak_move() { benchmark_weak_move>(bench); } -void compare_dereference() { +TEST_CASE("Dereference performance comparison") { ankerl::nanobench::Bench bench; bench.title("Dereference performance comparison"); bench.relative(true); @@ -328,7 +329,7 @@ void compare_dereference() { benchmark_dereference>(bench); } -void compare_weak_lock_success() { +TEST_CASE("Weak lock success performance comparison") { ankerl::nanobench::Bench bench; bench.title("Weak lock success performance comparison"); bench.relative(true); @@ -336,7 +337,7 @@ void compare_weak_lock_success() { benchmark_weak_lock_success>(bench); } -void compare_weak_lock_failure() { +TEST_CASE("Weak lock failure performance comparison") { ankerl::nanobench::Bench bench; bench.title("Weak lock failure performance comparison"); bench.relative(true); @@ -344,7 +345,8 @@ void compare_weak_lock_failure() { benchmark_weak_lock_failure>(bench); } -void compare_multithreaded_copy(int num_threads) { +TEST_CASE("Copy performance under contention") { + const int num_threads = 3; ankerl::nanobench::Bench bench; bench.title("Copy performance under contention"); bench.relative(true); @@ -353,7 +355,8 @@ void compare_multithreaded_copy(int num_threads) { benchmark_multithreaded_copy>(bench, num_threads); } -void compare_multithreaded_weak_lock(int num_threads) { +TEST_CASE("Weak lock performance under contention") { + const int num_threads = 3; ankerl::nanobench::Bench bench; bench.title("Weak lock performance under contention"); bench.relative(true); @@ -363,7 +366,8 @@ void compare_multithreaded_weak_lock(int num_threads) { benchmark_multithreaded_weak_lock>(bench, num_threads); } -void compare_weak_copy_with_strong_contention(int num_threads) { +TEST_CASE("Weak copy performance under strong reference contention") { + const int num_threads = 3; ankerl::nanobench::Bench bench; bench.title("Weak copy performance under strong reference contention"); bench.relative(true); @@ -374,7 +378,8 @@ void compare_weak_copy_with_strong_contention(int num_threads) { num_threads); } -void compare_strong_copy_with_weak_contention(int num_threads) { +TEST_CASE("Strong copy performance under weak reference contention") { + const int num_threads = 3; ankerl::nanobench::Bench bench; bench.title("Strong copy performance under weak reference contention"); bench.relative(true); @@ -384,23 +389,3 @@ void compare_strong_copy_with_weak_contention(int num_threads) { benchmark_strong_copy_with_weak_contention>(bench, num_threads); } - -int main() { - const int num_threads = 3; - - // Each comparison has std::shared_ptr as 100% baseline - compare_creation(); - compare_copy(); - compare_move(); - compare_weak_copy(); - compare_weak_move(); - compare_dereference(); - compare_weak_lock_success(); - compare_weak_lock_failure(); - compare_multithreaded_copy(num_threads); - compare_multithreaded_weak_lock(num_threads); - compare_weak_copy_with_strong_contention(num_threads); - compare_strong_copy_with_weak_contention(num_threads); - - return 0; -}