#include "catch_amalgamated.hpp" #include #include #include #include #include #include #include #include #include #include #include namespace { constexpr std::size_t kElementCount = 1024 * 1024; template struct BaqSchemaPacket { std::uint8_t data[kElementCount]; }; } // namespace namespace bitserial { template struct schema_of> { using BitserialSelf = BaqSchemaPacket; static constexpr auto fields = std::make_tuple( ::bitserial::ArrayField<&BitserialSelf::data, Bits>{}, ::bitserial::detail::tuple_end{} ); }; } // namespace bitserial namespace { 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 = static_cast((1u << Bits) - 1u); std::vector src; std::vector packed_ref; std::vector packed_bulk; std::vector packed_schema; std::vector unpacked_ref; std::vector unpacked_bulk; std::unique_ptr> schema_src; explicit BaqFixture(std::size_t n = kElementCount) : src(n), packed_ref((n * Bits + 7) / 8, 0), packed_bulk((n * Bits + 7) / 8, 0), packed_schema(bitserial::byte_size_v>, 0), unpacked_ref(n), unpacked_bulk(n), schema_src(std::make_unique>()) { std::mt19937 gen(0xB17u + static_cast(Bits)); std::uniform_int_distribution dist(0, max_value); for (std::uint8_t &x : src) { x = static_cast(dist(gen)); } std::ranges::copy(src, schema_src->data); pack_reference(src, packed_ref); bitserial::pack_bits(src, packed_bulk); REQUIRE(packed_bulk == packed_ref); unpack_reference(packed_ref, unpacked_ref); bitserial::unpack_bits(packed_ref, unpacked_bulk); REQUIRE(unpacked_bulk == unpacked_ref); if (n == kElementCount) { bitserial::to_bytes(*schema_src, packed_schema); REQUIRE(packed_schema == packed_ref); auto schema_unpacked = bitserial::from_bytes>(packed_ref); REQUIRE(std::ranges::equal(unpacked_ref, schema_unpacked.data)); } } static void pack_reference( std::span in, std::span out ) noexcept { std::size_t bit_pos = 0; for (const std::uint8_t x : in) { bitserial::write_bits(out, bit_pos, Bits, x & max_value); bit_pos += Bits; } } static void unpack_reference( std::span in, std::span out ) noexcept { std::size_t bit_pos = 0; for (std::uint8_t &x : out) { x = static_cast(bitserial::read_bits(in, bit_pos, Bits)); bit_pos += Bits; } } }; template void check_edges() { for (std::size_t n = 0; n <= 257; ++n) { BaqFixture f(n); CAPTURE(Bits, n); CHECK(f.packed_bulk == f.packed_ref); CHECK(f.unpacked_bulk == f.unpacked_ref); } } 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(); 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 pack_schema_name = std::string(label) + " pack schema to_bytes"; auto const unpack_reference_name = std::string(label) + " unpack read_bits loop"; auto const unpack_bulk_name = std::string(label) + " unpack bitserial bulk"; auto const unpack_schema_name = std::string(label) + " unpack schema from_bytes"; 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(); }; 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(); }; record_bandwidth_benchmark(pack_schema_name, bytes_per_operation); BENCHMARK(std::string(pack_schema_name)) { bitserial::to_bytes(*f.schema_src, f.packed_schema); return f.packed_schema.front(); }; 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(); }; 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(); }; record_bandwidth_benchmark(unpack_schema_name, bytes_per_operation); BENCHMARK(std::string(unpack_schema_name)) { auto packet = bitserial::from_bytes>(f.packed_ref); return packet.data[0]; }; } } // namespace TEST_CASE("BAQ bulk pack/unpack edge cases", "[baq][bulk]") { check_edges<3>(); check_edges<4>(); check_edges<5>(); } TEST_CASE("BAQ bulk benchmarks", "[!benchmark][baq][bulk]") { run_benchmarks<3>("3:8"); run_benchmarks<4>("4:8"); run_benchmarks<5>("5:8"); }