Skip to content

Commit 31ed79a

Browse files
Add benchmark example
1 parent ed220f9 commit 31ed79a

File tree

7 files changed

+88
-3
lines changed

7 files changed

+88
-3
lines changed

Example/Benchmark/Source/main.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <Iyp/WaitFreeRingBufferUtilities/wait-free-ring-buffer-utilities.inl>
2+
3+
#include <vector>
4+
#include <thread>
5+
#include <atomic>
6+
#include <iostream>
7+
#include <chrono>
8+
#include <cstdlib>
9+
10+
int main()
11+
{
12+
constexpr static std::size_t RingSize = 4096;
13+
using RingBufferType = Iyp::WaitFreeRingBufferUtilities::RingBuffer<std::size_t,
14+
Iyp::WaitFreeRingBufferUtilities::AccessRequirements::MULTI_CONSUMER |
15+
Iyp::WaitFreeRingBufferUtilities::AccessRequirements::MULTI_PRODUCER,
16+
RingSize>;
17+
constexpr std::size_t PushCount = 4096 * RingSize;
18+
constexpr std::size_t NumberOfPusherThreads = 4;
19+
constexpr std::size_t NumberOfPopperThreads = 4;
20+
21+
std::vector<std::thread> pushers;
22+
std::vector<std::thread> poppers;
23+
RingBufferType ring;
24+
25+
for (std::size_t thread_number = 0; thread_number < NumberOfPopperThreads; thread_number++)
26+
{
27+
poppers.emplace_back([&ring, PushCount, NumberOfPusherThreads, NumberOfPopperThreads]() {
28+
for (std::size_t i = 0; i < PushCount * NumberOfPusherThreads / NumberOfPopperThreads; i++)
29+
while (!ring.pop())
30+
{
31+
}
32+
});
33+
}
34+
35+
std::atomic<bool> do_push;
36+
37+
for (std::size_t thread_number = 0; thread_number < NumberOfPusherThreads; thread_number++)
38+
{
39+
pushers.emplace_back([&ring, &do_push, PushCount]() mutable {
40+
while (!do_push.load(std::memory_order_relaxed))
41+
{
42+
}
43+
44+
for (std::size_t i = 0; i < PushCount; i++)
45+
while (!ring.push(i))
46+
{
47+
}
48+
});
49+
}
50+
51+
const auto start_time = std::chrono::steady_clock::now();
52+
do_push.store(true, std::memory_order_relaxed);
53+
54+
for (auto &popper : poppers)
55+
popper.join();
56+
57+
const auto end_time = std::chrono::steady_clock::now();
58+
59+
std::cout << "Processed " << PushCount * NumberOfPusherThreads << " elements in ";
60+
std::cout << std::chrono::nanoseconds{end_time - start_time}.count() << " nano-seconds." << std::endl;
61+
62+
for (auto &pusher : pushers)
63+
pusher.join();
64+
}

Projects/CMake/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ add_subdirectory(googletest)
1616
add_subdirectory(boost)
1717
add_subdirectory(WaitFreeRingBufferUtilities)
1818
add_subdirectory(Test)
19+
add_subdirectory(Example)
1920

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
project(Benchmark CXX)
2+
3+
file(GLOB_RECURSE CPP_FILES ${REPO_ROOT}/Example/${PROJECT_NAME}/Source/*.cpp)
4+
add_executable(${PROJECT_NAME} ${CPP_FILES})
5+
find_package(Threads REQUIRED)
6+
target_link_libraries(${PROJECT_NAME} WaitFreeRingBufferUtilities Threads::Threads)
7+
8+
if(WIN32)
9+
install(TARGETS ${PROJECT_NAME}
10+
LIBRARY DESTINATION Lib
11+
RUNTIME DESTINATION Bin
12+
ARCHIVE DESTINATION Lib)
13+
else()
14+
install(TARGETS ${PROJECT_NAME}
15+
LIBRARY DESTINATION lib
16+
RUNTIME DESTINATION bin
17+
ARCHIVE DESTINATION lib)
18+
endif()

Projects/CMake/Example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(Benchmark)

Projects/CMake/Test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ include_directories(${LOCAL_INCLUDE_DIR})
55

66
file(GLOB_RECURSE CPP_FILES ${REPO_ROOT}/${PROJECT_NAME}/Source/*.cpp)
77
add_executable(${PROJECT_NAME} ${CPP_FILES})
8-
target_link_libraries(${PROJECT_NAME} gtest gtest_main WaitFreeRingBufferUtilities)
8+
find_package(Threads REQUIRED)
9+
target_link_libraries(${PROJECT_NAME} gtest gtest_main WaitFreeRingBufferUtilities Threads::Threads)
910

1011
if (MSVC)
1112
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
add_subdirectory(${REPO_ROOT}/Dependencies/boost/detail boost)
1+
add_subdirectory(${REPO_ROOT}/Dependencies/boost/detail boost/)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
add_subdirectory(${REPO_ROOT}/Dependencies/boost/move boost)
1+
add_subdirectory(${REPO_ROOT}/Dependencies/boost/move boost/)

0 commit comments

Comments
 (0)