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

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

Oskar Lappi's avatar
Oskar Lappi committed
#include "streaming_iterator_producer.hpp"
#include "streaming_producer.hpp"
Oskar Lappi's avatar
Oskar Lappi committed
#include "synchronous_producer.hpp"

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
using ProdChannel = Producer_AsyncQueue<produce_duration>;
using ProdBuffer = Producer_AsyncBuffer<produce_duration>;
Oskar Lappi's avatar
Oskar Lappi committed
static void sync_producer(benchmark::State &state) {
  ProdSync prod_sync{};
  for (auto _ : state) {
Oskar Lappi's avatar
Oskar Lappi committed
    prod_sync.produce(n_items);
Oskar Lappi's avatar
Oskar Lappi committed
    Item item;
Oskar Lappi's avatar
Oskar Lappi committed
    while ((item = prod_sync.consume()).id != -1) {
Oskar Lappi's avatar
Oskar Lappi committed
      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;
Oskar Lappi's avatar
Oskar Lappi committed
    while ((item = prod_str.consume()).id != -1) {
Oskar Lappi's avatar
Oskar Lappi committed
      std::this_thread::sleep_for(std::chrono::milliseconds{consume_duration});
    };
    t.join();
  }
}
BENCHMARK(stream_producer)->Unit(benchmark::kMillisecond);

Oskar Lappi's avatar
Oskar Lappi committed
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);

BENCHMARK_MAIN();