From 9b15767084b9e4b9a18ea76db70c6b3b73c9e9b4 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sun, 28 May 2023 14:38:21 +0800 Subject: [PATCH] add: [concur] benchmark for queue --- benchmark/benchmark_concur.cpp | 66 ++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 benchmark/benchmark_concur.cpp diff --git a/benchmark/benchmark_concur.cpp b/benchmark/benchmark_concur.cpp new file mode 100644 index 0000000..712db08 --- /dev/null +++ b/benchmark/benchmark_concur.cpp @@ -0,0 +1,66 @@ + +#include +#include +#include +#include + +#include "benchmark/benchmark.h" + +#include "libconcur/queue.h" + +namespace { + +void concur_queue_1v1(benchmark::State &state) { + using namespace concur; + queue que; + std::atomic_bool stop = false; + auto producer = std::async(std::launch::async, [&stop, &que] { + for (std::int64_t i = 0; !stop.load(std::memory_order_relaxed); ++i) { + (void)que.push(i); + std::this_thread::yield(); + } + }); + + for (auto _ : state) { + std::int64_t i; + while (!que.pop(i)) std::this_thread::yield(); + } + + stop = true; + producer.wait(); +} + +void concur_queue_NvN(benchmark::State &state) { + using namespace concur; + + static queue que; + static std::atomic_int run = 0; + static std::vector prods; + + if (state.thread_index() == 0) { + run = state.threads(); + prods.resize(state.threads()); + auto producer = [] { + for (std::int64_t i = 0; run.load(std::memory_order_relaxed) > 0; ++i) { + (void)que.push(i); + std::this_thread::yield(); + } + }; + for (auto &p : prods) p = std::thread(producer); + } + + for (auto _ : state) { + std::int64_t i; + while (!que.pop(i)) std::this_thread::yield(); + } + run -= 1; + + if (state.thread_index() == 0) { + for (auto &p : prods) p.join(); + } +} + +} // namespace + +BENCHMARK(concur_queue_1v1); +BENCHMARK(concur_queue_NvN)->ThreadRange(1, 16); \ No newline at end of file