From 490c2c26d1e2528d1de41f69698ce02174d386ec Mon Sep 17 00:00:00 2001 From: Thomas Beutlich Date: Tue, 20 Aug 2024 23:33:22 +0200 Subject: [PATCH] Update shp_bench.cc --- tests/shp_bench.cc | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/tests/shp_bench.cc b/tests/shp_bench.cc index c12a2d2..27ef348 100644 --- a/tests/shp_bench.cc +++ b/tests/shp_bench.cc @@ -1,6 +1,7 @@ #include #include +#include #include "shapefil.h" namespace fs = std::filesystem; @@ -9,14 +10,28 @@ namespace { static const auto kTestData = fs::path{"shape_eg_data"}; +static uint64_t bytes{0}; -template static void Benchmark_ReadAll(benchmark::State &state) +static SAOffset read(void *p, SAOffset size, SAOffset nmemb, SAFile file) +{ + const auto bytes_read = + (SAOffset)fread(p, (size_t)size, (size_t)nmemb, (FILE *)file); + bytes += bytes_read; + return bytes_read; +} + +static void Benchmark_ReadAll(benchmark::State &state) { const auto filename = kTestData / "mpatch3.shp"; - const auto handle = SHPOpen(filename.string().c_str(), "rb"); - SHPSetFastModeReadObject(handle, fastMode); + const auto sHooks = std::make_unique(); + SASetupDefaultHooks(sHooks.get()); + sHooks->FRead = read; + const auto handle = + SHPOpenLL(filename.string().c_str(), "rb", sHooks.get()); + SHPSetFastModeReadObject(handle, static_cast(state.range(0))); int nEntities; SHPGetInfo(handle, &nEntities, nullptr, nullptr, nullptr); + bytes = 0; for (auto _ : state) { for (int i = 0; i < nEntities; ++i) @@ -25,14 +40,16 @@ template static void Benchmark_ReadAll(benchmark::State &state) SHPDestroyObject(obj); } } + state.SetBytesProcessed(bytes); SHPClose(handle); } -constexpr auto Benchmark_ReadAllSlow = Benchmark_ReadAll<0>; -constexpr auto Benchmark_ReadAllFast = Benchmark_ReadAll<1>; - -BENCHMARK(Benchmark_ReadAllSlow); -BENCHMARK(Benchmark_ReadAllFast); +BENCHMARK(Benchmark_ReadAll) + ->Arg(0) + ->Arg(1) + ->ArgNames({"fastMode"}) + ->Repetitions(5) + ->Unit(benchmark::kMicrosecond); } // namespace