mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
add: [concur] benchmark for queue
This commit is contained in:
parent
f6af394195
commit
9b15767084
66
benchmark/benchmark_concur.cpp
Normal file
66
benchmark/benchmark_concur.cpp
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
|
||||||
|
#include <thread>
|
||||||
|
#include <future>
|
||||||
|
#include <atomic>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "benchmark/benchmark.h"
|
||||||
|
|
||||||
|
#include "libconcur/queue.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void concur_queue_1v1(benchmark::State &state) {
|
||||||
|
using namespace concur;
|
||||||
|
queue<std::int64_t, relation::single, relation::single> 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<std::int64_t, relation::multi, relation::multi> que;
|
||||||
|
static std::atomic_int run = 0;
|
||||||
|
static std::vector<std::thread> 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);
|
||||||
Loading…
x
Reference in New Issue
Block a user