Skip to content

Commit

Permalink
Run benchmark on SHPSetFastModeReadObject
Browse files Browse the repository at this point in the history
  • Loading branch information
thbeu committed Aug 20, 2024
1 parent 8e23c95 commit 5565fdf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ EXTRA_DIST = makefile.vc CMakeLists.txt autogen.sh \
tests/CMakeLists.txt \
tests/dbf_test.cc \
tests/sbn_test.cc \
tests/shp_bench.cc \
tests/shp_test.cc \
tests/test1.sh tests/test2.sh tests/test3.sh \
tests/expect1.out tests/expect2.out tests/expect3.out \
Expand Down
23 changes: 20 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@

project(${CMAKE_PROJECT_NAME}Tests CXX)

# Set up GoogleTest
# Set up GoogleTest and Benchmark
include(FetchContent)

FetchContent_Declare(
benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG c19cfee61e136effb05a7fc8a037b0db3b13bd4c
GIT_SHALLOW TRUE
)

FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.15.2
GIT_SHALLOW TRUE
)

set(BENCHMARK_DOWNLOAD_DEPENDENCIES OFF CACHE BOOL "" FORCE)
set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE)
set(BENCHMARK_INSTALL_DOCS OFF CACHE BOOL "" FORCE)

FetchContent_MakeAvailable(benchmark)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

Expand All @@ -22,9 +37,11 @@ set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)

FetchContent_MakeAvailable(googletest)

foreach(executable dbf_test sbn_test shp_test)
set_target_properties(gtest gtest_main benchmark benchmark_main PROPERTIES FOLDER "tests/third-party")

foreach(executable dbf_test sbn_test shp_bench shp_test)
add_executable(${executable} ${PROJECT_SOURCE_DIR}/${executable}.cc)
target_link_libraries(${executable} PRIVATE ${PACKAGE} gtest)
target_link_libraries(${executable} PRIVATE ${PACKAGE} gtest benchmark)
add_test(
NAME ${executable}
COMMAND ${executable}
Expand Down
42 changes: 42 additions & 0 deletions tests/shp_bench.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <filesystem>

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

namespace fs = std::filesystem;

namespace
{

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

template <bool fastMode> static void Benchmark_ReadAll(benchmark::State &state)
{
for (auto _ : state)
{
const auto filename = kTestData / "mpatch3.shp";
const auto handle = SHPOpen(filename.string().c_str(), "rb");
if constexpr (fastMode)
{
SHPSetFastModeReadObject(handle, 1);
}
int nEntities;
SHPGetInfo(handle, &nEntities, nullptr, nullptr, nullptr);
for (int i = 0; i < nEntities; ++i)
{
auto obj = SHPReadObject(handle, i);
SHPDestroyObject(obj);
}
SHPClose(handle);
}
}

constexpr auto Benchmark_ReadAllSlow = Benchmark_ReadAll<false>;
constexpr auto Benchmark_ReadAllFast = Benchmark_ReadAll<true>;

BENCHMARK(Benchmark_ReadAllSlow);
BENCHMARK(Benchmark_ReadAllFast);

} // namespace

BENCHMARK_MAIN();

0 comments on commit 5565fdf

Please sign in to comment.