From 56f8fbb70160adfcb7ded285aa79a5fa52e8a604 Mon Sep 17 00:00:00 2001 From: DawnMagnet Date: Mon, 4 May 2026 23:55:54 +0800 Subject: [PATCH] Add bandwidth benchmarking and reporting for BAQ bulk operations --- benchmarks/baq_bulk_benchmarks.cpp | 104 +++++++++++++++++++++++++++-- tests/bitserial_tests.cpp | 12 ++-- 2 files changed, 106 insertions(+), 10 deletions(-) diff --git a/benchmarks/baq_bulk_benchmarks.cpp b/benchmarks/baq_bulk_benchmarks.cpp index 2fcaaf3..2c983a5 100644 --- a/benchmarks/baq_bulk_benchmarks.cpp +++ b/benchmarks/baq_bulk_benchmarks.cpp @@ -5,14 +5,98 @@ #include #include #include +#include +#include #include #include +#include #include namespace { constexpr std::size_t kElementCount = 1024 * 1024; +struct BandwidthBenchmark { + std::string name; + std::size_t bytes_per_operation; +}; + +std::vector &bandwidth_benchmarks() { + static std::vector 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(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 struct BaqFixture { static constexpr std::uint8_t max_value = @@ -82,23 +166,35 @@ template void run_benchmarks(char const *label) { BaqFixture 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::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(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::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(f.packed_ref, f.unpacked_bulk); return f.unpacked_bulk.front(); }; diff --git a/tests/bitserial_tests.cpp b/tests/bitserial_tests.cpp index b95459a..2335b97 100644 --- a/tests/bitserial_tests.cpp +++ b/tests/bitserial_tests.cpp @@ -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 buf(bitserial::byte_size_v); bitserial::to_bytes(x, std::span(buf));