Skip to content
Snippets Groups Projects
main.cpp 1.23 KiB
Newer Older
#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();