Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <iostream>
#include <benchmark/benchmark.h>
#include <fmt/core.h>
#include "synchronous_producer.hpp"
#include "streaming_producer.hpp"
#include <chrono>
#include <thread>
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>;
static void sync_producer(benchmark::State& state) {
ProdSync prod_sync{};
for (auto _ : state) {
std::thread t(&ProdSync::produce, &prod_sync, n_items);
t.join();
Item item;
do {
item = prod_sync.consume();
std::this_thread::sleep_for(std::chrono::milliseconds{consume_duration});
} while (item.id != -1);
}
}
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;
do {
item = prod_str.consume();
std::this_thread::sleep_for(std::chrono::milliseconds{consume_duration});
} while (item.id != -1);
t.join();
}
}
BENCHMARK(stream_producer)->Unit(benchmark::kMillisecond);
BENCHMARK_MAIN();