Skip to content

Commit

Permalink
output
Browse files Browse the repository at this point in the history
  • Loading branch information
KjellKod committed Dec 15, 2023
1 parent 19e3dad commit d4d34ee
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions benchmark/benchmark_main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <iomanip>
#include <iostream>
#include "benchmark_functions.hpp"
#include "benchmark_runs.hpp"
Expand All @@ -8,29 +9,69 @@ namespace {
const size_t kGoodSizedQueueSize = (2 << 16); // 65536
const size_t kNumberOfItems = 1000000;

struct benchmark_result {
int runs;
int num_producer_threads;
int num_consumer_threads;
size_t messages_per_iteration;
double mean_msgs_per_second;
double min_msgs_per_second;
double max_msgs_per_second;
std::string comment;
};
} // namespace

void benchmark_spsc() {
void print_result(const benchmark_result& result) {
std::cout << std::setw(2) << result.runs << ", "
<< std::setw(5) << result.num_producer_threads << ", "
<< std::setw(6) << result.num_consumer_threads << ", "
<< std::setw(16) << result.mean_msgs_per_second << ", "
<< std::setw(14) << result.min_msgs_per_second << ", "
<< std::setw(14) << result.max_msgs_per_second << ", "
<< std::setw(17) << result.comment << std::endl;
}

benchmark_result benchmark_spsc() {
const int kRuns = 33;
std::vector<benchmark::result_t> results;
double total_duration_ns = 0.0;
double min_msgs_per_second = std::numeric_limits<double>::max();
double max_msgs_per_second = std::numeric_limits<double>::min();
double total_msgs_per_second = 0.0;

for (int i = 0; i < kRuns; ++i) {
auto queue = queue_api::CreateQueue<spsc::flexible::circular_fifo<unsigned int>>(kGoodSizedQueueSize);
auto result = benchmark::runSPSC(queue, kNumberOfItems);
results.push_back(result);
total_duration_ns += result.elapsed_time_in_ns;

double duration_s = result.elapsed_time_in_ns / 1e9; // convert ns to seconds
double msgs_per_second = kNumberOfItems / duration_s; // messages per second for this run
total_msgs_per_second += msgs_per_second;
min_msgs_per_second = std::min(min_msgs_per_second, msgs_per_second);
max_msgs_per_second = std::max(max_msgs_per_second, msgs_per_second);
}

double total_duration_s = total_duration_ns / 1e9; // convert ns to seconds
std::cout << kRuns << " runs x " << kNumberOfItems << " took " << total_duration_s << " seconds \n";
double calls_per_second = (kRuns * kNumberOfItems) / (total_duration_s); // average calls per second
double average_ops_duration_ns = total_duration_ns / (kNumberOfItems * kRuns); // average call duration in nanoseconds
double mean_msgs_per_second = total_msgs_per_second / kRuns; // mean messages per second

benchmark_result result;
result.runs = kRuns;
result.num_producer_threads = 1;
result.num_consumer_threads = 1;
result.messages_per_iteration = kNumberOfItems;
result.mean_msgs_per_second = mean_msgs_per_second;
result.min_msgs_per_second = min_msgs_per_second;
result.max_msgs_per_second = max_msgs_per_second;
result.comment = "SPSC benchmark";

std::cout << "Calls per second: " << calls_per_second << std::endl;
std::cout << "Average push/pop duration: " << average_ops_duration_ns << " ns" << std::endl;
return result;
}

int main() {
benchmark_spsc();
// Print the headers
std::cout << "#runs,\t#p,\t#c,\t#msgs/s,\t#min_msgs/s,\t#max_msgs/s,\tcomment" << std::endl;
auto spsc_result = benchmark_spsc();
print_result(spsc_result);

return 0;
}

0 comments on commit d4d34ee

Please sign in to comment.