Skip to content
Snippets Groups Projects
main.cpp 1.33 KiB
Newer Older
#include <iostream>
Oskar Lappi's avatar
Oskar Lappi committed
#include <stdio.h>

#include <benchmark/benchmark.h>
#include <fmt/core.h>

#include "streaming_producer.hpp"
Oskar Lappi's avatar
Oskar Lappi committed
#include "synchronous_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>;

Oskar Lappi's avatar
Oskar Lappi committed
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;
    while (true) {
      item = prod_sync.consume();
      if (item.id == -1) {
        break;
      }
      std::this_thread::sleep_for(std::chrono::milliseconds{consume_duration});
    };
  }
}
BENCHMARK(sync_producer)->Unit(benchmark::kMillisecond);

Oskar Lappi's avatar
Oskar Lappi committed
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;
    while (true) {
      item = prod_str.consume();
      if (item.id == -1) {
        break;
      }
      std::this_thread::sleep_for(std::chrono::milliseconds{consume_duration});
    };
    t.join();
  }
}
BENCHMARK(stream_producer)->Unit(benchmark::kMillisecond);

BENCHMARK_MAIN();