|
| 1 | +# Benchmarks for geo-utils-cpp. |
| 2 | +# |
| 3 | +# Build with: |
| 4 | +# cmake -B build -DGEO_UTILS_CPP_BUILD_BENCHMARKS=ON |
| 5 | +# cmake --build build --target bench_all |
| 6 | +# |
| 7 | +# Google Benchmark is fetched automatically. S2, Boost.Geometry, and |
| 8 | +# GeographicLib are looked up via find_package — missing competitors are |
| 9 | +# skipped with a status message instead of causing a hard error. |
| 10 | + |
| 11 | +include(FetchContent) |
| 12 | + |
| 13 | +# --- Google Benchmark ------------------------------------------------------- |
| 14 | + |
| 15 | +set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE) |
| 16 | +set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "" FORCE) |
| 17 | +set(BENCHMARK_INSTALL_DOCS OFF CACHE BOOL "" FORCE) |
| 18 | +set(BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE BOOL "" FORCE) |
| 19 | + |
| 20 | +FetchContent_Declare( |
| 21 | + google_benchmark |
| 22 | + URL https://github.com/google/benchmark/archive/v1.8.4.tar.gz |
| 23 | + DOWNLOAD_EXTRACT_TIMESTAMP ON |
| 24 | +) |
| 25 | +FetchContent_MakeAvailable(google_benchmark) |
| 26 | + |
| 27 | +# --- Shared infrastructure -------------------------------------------------- |
| 28 | + |
| 29 | +add_library(geo_utils_cpp_bench_common INTERFACE) |
| 30 | +target_include_directories(geo_utils_cpp_bench_common INTERFACE |
| 31 | + ${CMAKE_CURRENT_SOURCE_DIR}/common |
| 32 | +) |
| 33 | +target_compile_features(geo_utils_cpp_bench_common INTERFACE cxx_std_17) |
| 34 | + |
| 35 | +# Aggregator target: depend on every bench that ends up being built. |
| 36 | +add_custom_target(bench_all) |
| 37 | + |
| 38 | +function(_geo_utils_cpp_add_bench name source) |
| 39 | + add_executable(bench_${name} ${source}) |
| 40 | + target_link_libraries(bench_${name} PRIVATE |
| 41 | + geo::utils |
| 42 | + geo_utils_cpp_bench_common |
| 43 | + benchmark::benchmark_main |
| 44 | + ${ARGN} |
| 45 | + ) |
| 46 | + add_dependencies(bench_all bench_${name}) |
| 47 | +endfunction() |
| 48 | + |
| 49 | +# --- geo-utils-cpp itself + naive haversine baseline ------------------------ |
| 50 | + |
| 51 | +_geo_utils_cpp_add_bench(geo_utils speed/bench_geo_utils.cpp) |
| 52 | +_geo_utils_cpp_add_bench(naive speed/bench_naive.cpp) |
| 53 | + |
| 54 | +# --- Optional: S2 Geometry -------------------------------------------------- |
| 55 | +# |
| 56 | +# vcpkg port name: `s2geometry`. Homebrew formula: `s2geometry`. The CMake |
| 57 | +# package is conventionally exported as `s2`. |
| 58 | +find_package(s2 QUIET) |
| 59 | +if(s2_FOUND) |
| 60 | + _geo_utils_cpp_add_bench(s2 speed/bench_s2.cpp s2::s2) |
| 61 | + message(STATUS "geo-utils-cpp benchmarks: S2 found — bench_s2 enabled.") |
| 62 | +else() |
| 63 | + message(STATUS "geo-utils-cpp benchmarks: S2 not found — bench_s2 skipped. " |
| 64 | + "Install via 'vcpkg install s2geometry' or 'brew install s2geometry'.") |
| 65 | +endif() |
| 66 | + |
| 67 | +# --- Optional: Boost.Geometry ----------------------------------------------- |
| 68 | +# |
| 69 | +# Boost.Geometry is header-only. CMake 3.30 deprecates the FindBoost module in |
| 70 | +# favour of Boost's exported BoostConfig.cmake; we prefer CONFIG and fall back |
| 71 | +# to the legacy module so this works across CMake 3.14+. |
| 72 | +if(POLICY CMP0167) |
| 73 | + cmake_policy(SET CMP0167 NEW) |
| 74 | +endif() |
| 75 | + |
| 76 | +find_package(Boost 1.70 QUIET CONFIG) |
| 77 | +if(NOT Boost_FOUND) |
| 78 | + find_package(Boost 1.70 QUIET) |
| 79 | +endif() |
| 80 | + |
| 81 | +if(Boost_FOUND) |
| 82 | + # Different Boost versions expose the header set as Boost::headers (>=1.71), |
| 83 | + # Boost::boost (older), or only via Boost_INCLUDE_DIRS. |
| 84 | + if(TARGET Boost::headers) |
| 85 | + _geo_utils_cpp_add_bench(boost speed/bench_boost.cpp Boost::headers) |
| 86 | + elseif(TARGET Boost::boost) |
| 87 | + _geo_utils_cpp_add_bench(boost speed/bench_boost.cpp Boost::boost) |
| 88 | + else() |
| 89 | + _geo_utils_cpp_add_bench(boost speed/bench_boost.cpp) |
| 90 | + target_include_directories(bench_boost PRIVATE ${Boost_INCLUDE_DIRS}) |
| 91 | + endif() |
| 92 | + message(STATUS "geo-utils-cpp benchmarks: Boost.Geometry found — bench_boost enabled.") |
| 93 | +else() |
| 94 | + message(STATUS "geo-utils-cpp benchmarks: Boost not found — bench_boost skipped. " |
| 95 | + "Install via 'vcpkg install boost-geometry' or 'brew install boost'.") |
| 96 | +endif() |
| 97 | + |
| 98 | +# --- Optional: GeographicLib ------------------------------------------------ |
| 99 | +# |
| 100 | +# Recent GeographicLib versions export the target `GeographicLib::GeographicLib`. |
| 101 | +# Older versions exposed `${GeographicLib_LIBRARIES}` instead — we accept both. |
| 102 | +find_package(GeographicLib QUIET) |
| 103 | +if(GeographicLib_FOUND) |
| 104 | + if(TARGET GeographicLib::GeographicLib) |
| 105 | + _geo_utils_cpp_add_bench(geographiclib speed/bench_geographiclib.cpp |
| 106 | + GeographicLib::GeographicLib) |
| 107 | + else() |
| 108 | + _geo_utils_cpp_add_bench(geographiclib speed/bench_geographiclib.cpp |
| 109 | + ${GeographicLib_LIBRARIES}) |
| 110 | + target_include_directories(bench_geographiclib PRIVATE ${GeographicLib_INCLUDE_DIRS}) |
| 111 | + endif() |
| 112 | + message(STATUS "geo-utils-cpp benchmarks: GeographicLib found — bench_geographiclib enabled.") |
| 113 | +else() |
| 114 | + message(STATUS "geo-utils-cpp benchmarks: GeographicLib not found — bench_geographiclib skipped. " |
| 115 | + "Install via 'vcpkg install geographiclib' or 'brew install geographiclib'.") |
| 116 | +endif() |
0 commit comments