Newer
Older
#include <benchmark/benchmark.h>
#include <fmt/core.h>
static constexpr size_t n_items = 10;
constexpr size_t produce_duration = 20;
constexpr size_t consume_duration = 20;
using ProdSync = Producer_Sync<produce_duration>;
using ProdStream = Producer_Streaming<produce_duration>;
using ProdChannel = Producer_AsyncQueue<produce_duration>;
using ProdBuffer = Producer_AsyncBuffer<produce_duration>;
static void sync_producer(benchmark::State &state) {
ProdSync prod_sync{};
for (auto _ : state) {
std::this_thread::sleep_for(std::chrono::milliseconds{consume_duration});
};
}
}
BENCHMARK(sync_producer)->Unit(benchmark::kMillisecond);
static void stream_producer(benchmark::State &state) {
ProdStream prod_str{};
for (auto _ : state) {
prod_str.reset();
std::thread t(&ProdStream::produce, &prod_str, n_items);
Item item;
std::this_thread::sleep_for(std::chrono::milliseconds{consume_duration});
};
t.join();
}
}
BENCHMARK(stream_producer)->Unit(benchmark::kMillisecond);
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
static void channel_producer_block(benchmark::State &state) {
ProdChannel prod_chan{};
for (auto _ : state) {
prod_chan.reset();
std::thread t(&ProdChannel::produce, &prod_chan, n_items);
Item item;
t.join();
while ((item = prod_chan.consume()).id != -1) {
std::this_thread::sleep_for(std::chrono::milliseconds{consume_duration});
};
}
}
BENCHMARK(channel_producer_block)->Unit(benchmark::kMillisecond);
static void channel_producer(benchmark::State &state) {
ProdChannel prod_chan{};
for (auto _ : state) {
prod_chan.reset();
std::thread t(&ProdChannel::produce, &prod_chan, n_items);
Item item;
while ((item = prod_chan.consume()).id != -1) {
std::this_thread::sleep_for(std::chrono::milliseconds{consume_duration});
};
t.join();
}
}
BENCHMARK(channel_producer)->Unit(benchmark::kMillisecond);
static void buffer_producer(benchmark::State &state) {
ProdBuffer prod{};
for (auto _ : state) {
prod.reset();
std::thread t(&ProdBuffer::produce, &prod, n_items);
Item item;
while ((item = prod.consume()).id != -1) {
std::this_thread::sleep_for(std::chrono::milliseconds{consume_duration});
};
t.join();
}
}
BENCHMARK(buffer_producer)->Unit(benchmark::kMillisecond);
static void async_func_producer(benchmark::State &state) {
for (auto _ : state) {
auto ch = produce_items<produce_duration>(n_items);
for (Item item = ch->consume(); item.id != -1; item = ch->consume()) {
std::this_thread::sleep_for(std::chrono::milliseconds{consume_duration});
};
// delete ch;
}
}
BENCHMARK(async_func_producer)->Unit(benchmark::kMillisecond);
static void async_iterator_producer(benchmark::State &state) {
for (auto _ : state) {
auto ch = produce_items<produce_duration>(n_items);
for (auto item : *ch) {
std::this_thread::sleep_for(std::chrono::milliseconds{consume_duration});
};
delete ch;
}
}
BENCHMARK(async_iterator_producer)->Unit(benchmark::kMillisecond);