Skip to content

Commit

Permalink
Update shp_bench.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
thbeu committed Aug 20, 2024
1 parent 8e95f3b commit 490c2c2
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions tests/shp_bench.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <filesystem>

#include <benchmark/benchmark.h>
#include <memory>
#include "shapefil.h"

namespace fs = std::filesystem;
Expand All @@ -9,14 +10,28 @@ namespace
{

static const auto kTestData = fs::path{"shape_eg_data"};
static uint64_t bytes{0};

template <int fastMode> 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<SAHooks>();
SASetupDefaultHooks(sHooks.get());
sHooks->FRead = read;
const auto handle =
SHPOpenLL(filename.string().c_str(), "rb", sHooks.get());
SHPSetFastModeReadObject(handle, static_cast<int>(state.range(0)));
int nEntities;
SHPGetInfo(handle, &nEntities, nullptr, nullptr, nullptr);
bytes = 0;
for (auto _ : state)
{
for (int i = 0; i < nEntities; ++i)
Expand All @@ -25,14 +40,16 @@ template <int fastMode> 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

Expand Down

0 comments on commit 490c2c2

Please sign in to comment.