Organize bench_reference.cpp with doctest

This commit is contained in:
2025-09-11 12:32:25 -04:00
parent 2bc17cbfe6
commit 5adbf8eee2
2 changed files with 19 additions and 33 deletions

View File

@@ -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)

View File

@@ -2,6 +2,7 @@
#include <thread>
#include <vector>
#include <doctest/doctest.h>
#include <nanobench.h>
#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<Ref<TestObject>>(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<Ref<TestObject>>(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<Ref<TestObject>>(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<Ref<TestObject>>(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<Ref<TestObject>>(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<Ref<TestObject>>(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<Ref<TestObject>>(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<Ref<TestObject>>(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<Ref<TestObject>>(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<Ref<TestObject>>(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<Ref<TestObject>>(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;
}