Add bandwidth benchmarking and reporting for BAQ bulk operations
This commit is contained in:
@@ -5,14 +5,98 @@
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr std::size_t kElementCount = 1024 * 1024;
|
||||
|
||||
struct BandwidthBenchmark {
|
||||
std::string name;
|
||||
std::size_t bytes_per_operation;
|
||||
};
|
||||
|
||||
std::vector<BandwidthBenchmark> &bandwidth_benchmarks() {
|
||||
static std::vector<BandwidthBenchmark> benchmarks;
|
||||
return benchmarks;
|
||||
}
|
||||
|
||||
void record_bandwidth_benchmark(
|
||||
std::string const &name,
|
||||
std::size_t bytes_per_operation
|
||||
) {
|
||||
auto &benchmarks = bandwidth_benchmarks();
|
||||
auto it = std::ranges::find(benchmarks, name, &BandwidthBenchmark::name);
|
||||
if (it == benchmarks.end()) {
|
||||
benchmarks.push_back({name, bytes_per_operation});
|
||||
} else {
|
||||
it->bytes_per_operation = bytes_per_operation;
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t bytes_per_operation_for(std::string const &name) {
|
||||
auto const &benchmarks = bandwidth_benchmarks();
|
||||
auto it = std::ranges::find(benchmarks, name, &BandwidthBenchmark::name);
|
||||
return it == benchmarks.end() ? 0 : it->bytes_per_operation;
|
||||
}
|
||||
|
||||
struct ScaledBandwidth {
|
||||
double value;
|
||||
char const *unit;
|
||||
};
|
||||
|
||||
ScaledBandwidth scale_bandwidth(double bytes_per_second) {
|
||||
char const *units[] = {"B/s", "KB/s", "MB/s", "GB/s", "TB/s"};
|
||||
std::size_t unit_index = 0;
|
||||
while (bytes_per_second >= 1000.0 && unit_index + 1 < std::size(units)) {
|
||||
bytes_per_second /= 1000.0;
|
||||
++unit_index;
|
||||
}
|
||||
return {bytes_per_second, units[unit_index]};
|
||||
}
|
||||
|
||||
int bandwidth_precision(double value) {
|
||||
if (value >= 100.0) {
|
||||
return 1;
|
||||
}
|
||||
if (value >= 10.0) {
|
||||
return 2;
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
|
||||
class BandwidthReporter : public Catch::EventListenerBase {
|
||||
public:
|
||||
using Catch::EventListenerBase::EventListenerBase;
|
||||
|
||||
void benchmarkEnded(Catch::BenchmarkStats<> const &stats) override {
|
||||
auto const bytes_per_operation = bytes_per_operation_for(stats.info.name);
|
||||
auto const seconds_per_operation = stats.mean.point.count() * 1e-9;
|
||||
if (bytes_per_operation == 0 || seconds_per_operation <= 0.0) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto const bandwidth =
|
||||
scale_bandwidth(static_cast<double>(bytes_per_operation) /
|
||||
seconds_per_operation);
|
||||
auto const old_flags = std::cout.flags();
|
||||
auto const old_precision = std::cout.precision();
|
||||
std::cout << "\n bandwidth: " << std::fixed
|
||||
<< std::setprecision(bandwidth_precision(bandwidth.value))
|
||||
<< bandwidth.value << ' ' << bandwidth.unit << " ("
|
||||
<< bytes_per_operation << " bytes/op)\n";
|
||||
std::cout.flags(old_flags);
|
||||
std::cout.precision(old_precision);
|
||||
}
|
||||
};
|
||||
|
||||
CATCH_REGISTER_LISTENER(BandwidthReporter);
|
||||
|
||||
template <std::size_t Bits>
|
||||
struct BaqFixture {
|
||||
static constexpr std::uint8_t max_value =
|
||||
@@ -82,23 +166,35 @@ template <std::size_t Bits>
|
||||
void run_benchmarks(char const *label) {
|
||||
BaqFixture<Bits> f;
|
||||
CAPTURE(label, Bits, f.src.size(), f.packed_ref.size());
|
||||
auto const bytes_per_operation = f.src.size() + f.packed_ref.size();
|
||||
|
||||
BENCHMARK(std::string(label) + " pack read/write_bits loop") {
|
||||
auto const pack_reference_name =
|
||||
std::string(label) + " pack read/write_bits loop";
|
||||
auto const pack_bulk_name = std::string(label) + " pack bitserial bulk";
|
||||
auto const unpack_reference_name =
|
||||
std::string(label) + " unpack read_bits loop";
|
||||
auto const unpack_bulk_name = std::string(label) + " unpack bitserial bulk";
|
||||
|
||||
record_bandwidth_benchmark(pack_reference_name, bytes_per_operation);
|
||||
BENCHMARK(std::string(pack_reference_name)) {
|
||||
BaqFixture<Bits>::pack_reference(f.src, f.packed_ref);
|
||||
return f.packed_ref.front();
|
||||
};
|
||||
|
||||
BENCHMARK(std::string(label) + " pack bitserial bulk") {
|
||||
record_bandwidth_benchmark(pack_bulk_name, bytes_per_operation);
|
||||
BENCHMARK(std::string(pack_bulk_name)) {
|
||||
bitserial::pack_bits<Bits>(f.src, f.packed_bulk);
|
||||
return f.packed_bulk.front();
|
||||
};
|
||||
|
||||
BENCHMARK(std::string(label) + " unpack read_bits loop") {
|
||||
record_bandwidth_benchmark(unpack_reference_name, bytes_per_operation);
|
||||
BENCHMARK(std::string(unpack_reference_name)) {
|
||||
BaqFixture<Bits>::unpack_reference(f.packed_ref, f.unpacked_ref);
|
||||
return f.unpacked_ref.front();
|
||||
};
|
||||
|
||||
BENCHMARK(std::string(label) + " unpack bitserial bulk") {
|
||||
record_bandwidth_benchmark(unpack_bulk_name, bytes_per_operation);
|
||||
BENCHMARK(std::string(unpack_bulk_name)) {
|
||||
bitserial::unpack_bits<Bits>(f.packed_ref, f.unpacked_bulk);
|
||||
return f.unpacked_bulk.front();
|
||||
};
|
||||
|
||||
@@ -535,12 +535,12 @@ TEST_CASE("comprehensive: all types combined stress test") {
|
||||
// =============================================================================
|
||||
|
||||
TEST_CASE("user-provided buffer: zero-allocation path") {
|
||||
AllPkt x{
|
||||
.u8 = 0x12,
|
||||
.s32 = -1,
|
||||
.f64 = 3.14,
|
||||
.inner = {.a = 100, .b = 0.5},
|
||||
};
|
||||
AllPkt x{};
|
||||
x.u8 = 0x12;
|
||||
x.s32 = -1;
|
||||
x.f64 = 3.14;
|
||||
x.inner = {.a = 100, .b = 0.5};
|
||||
|
||||
std::vector<std::uint8_t> buf(bitserial::byte_size_v<AllPkt>);
|
||||
bitserial::to_bytes(x, std::span<std::uint8_t>(buf));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user