From b81ad6aaf711f3c3fc220e75d1500f1581da7a53 Mon Sep 17 00:00:00 2001 From: Nikita Taranov Date: Thu, 8 Dec 2022 13:38:08 +0100 Subject: [PATCH] Add google benchmark to contrib (#43779) * add google benchmark to contrib * rework integer_hash_tables_and_hashes * update readme * keep benchmarks near the benchmarked code * fix fasttests build * rm old target * fix --- .gitmodules | 3 + CMakeLists.txt | 2 + contrib/CMakeLists.txt | 2 + contrib/google-benchmark | 1 + contrib/google-benchmark-cmake/CMakeLists.txt | 34 ++++ src/Common/CMakeLists.txt | 4 + src/Common/benchmarks/CMakeLists.txt | 9 + .../integer_hash_tables_and_hashes.cpp | 159 +++++++++--------- src/Common/examples/CMakeLists.txt | 3 - 9 files changed, 138 insertions(+), 79 deletions(-) create mode 160000 contrib/google-benchmark create mode 100644 contrib/google-benchmark-cmake/CMakeLists.txt create mode 100644 src/Common/benchmarks/CMakeLists.txt rename src/Common/{examples => benchmarks}/integer_hash_tables_and_hashes.cpp (94%) diff --git a/.gitmodules b/.gitmodules index ebeef312ae8..a4cfcc91485 100644 --- a/.gitmodules +++ b/.gitmodules @@ -287,3 +287,6 @@ [submodule "contrib/xxHash"] path = contrib/xxHash url = https://github.com/Cyan4973/xxHash.git +[submodule "contrib/google-benchmark"] + path = contrib/google-benchmark + url = https://github.com/google/benchmark.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 06e6f943fd3..e121559d4e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,6 +111,7 @@ if (ENABLE_FUZZING) set (ENABLE_JEMALLOC 0) set (ENABLE_CHECK_HEAVY_BUILDS 1) set (GLIBC_COMPATIBILITY OFF) + set (ENABLE_BENCHMARKS 0) # For codegen_select_fuzzer set (ENABLE_PROTOBUF 1) @@ -168,6 +169,7 @@ endif () option(ENABLE_TESTS "Provide unit_test_dbms target with Google.Test unit tests" ON) option(ENABLE_EXAMPLES "Build all example programs in 'examples' subdirectories" OFF) +option(ENABLE_BENCHMARKS "Build all benchmark programs in 'benchmarks' subdirectories" OFF) if (OS_LINUX AND (ARCH_AMD64 OR ARCH_AARCH64) AND USE_STATIC_LIBRARIES AND NOT SPLIT_SHARED_LIBRARIES AND NOT USE_MUSL) # Only for Linux, x86_64 or aarch64. diff --git a/contrib/CMakeLists.txt b/contrib/CMakeLists.txt index ec7382846c2..c7419d74aac 100644 --- a/contrib/CMakeLists.txt +++ b/contrib/CMakeLists.txt @@ -171,6 +171,8 @@ add_contrib (annoy-cmake annoy) add_contrib (xxHash-cmake xxHash) +add_contrib (google-benchmark-cmake google-benchmark) + # Put all targets defined here and in subdirectories under "contrib/" folders in GUI-based IDEs. # Some of third-party projects may override CMAKE_FOLDER or FOLDER property of their targets, so they would not appear # in "contrib/..." as originally planned, so we workaround this by fixing FOLDER properties of all targets manually, diff --git a/contrib/google-benchmark b/contrib/google-benchmark new file mode 160000 index 00000000000..2257fa4d6af --- /dev/null +++ b/contrib/google-benchmark @@ -0,0 +1 @@ +Subproject commit 2257fa4d6afb8e5a2ccd510a70f38fe7fcdf1edf diff --git a/contrib/google-benchmark-cmake/CMakeLists.txt b/contrib/google-benchmark-cmake/CMakeLists.txt new file mode 100644 index 00000000000..5d8fa7b838b --- /dev/null +++ b/contrib/google-benchmark-cmake/CMakeLists.txt @@ -0,0 +1,34 @@ +set (SRC_DIR "${ClickHouse_SOURCE_DIR}/contrib/google-benchmark/src") + +set (SRCS + "${SRC_DIR}/benchmark.cc" + "${SRC_DIR}/benchmark_api_internal.cc" + "${SRC_DIR}/benchmark_name.cc" + "${SRC_DIR}/benchmark_register.cc" + "${SRC_DIR}/benchmark_runner.cc" + "${SRC_DIR}/check.cc" + "${SRC_DIR}/colorprint.cc" + "${SRC_DIR}/commandlineflags.cc" + "${SRC_DIR}/complexity.cc" + "${SRC_DIR}/console_reporter.cc" + "${SRC_DIR}/counter.cc" + "${SRC_DIR}/csv_reporter.cc" + "${SRC_DIR}/json_reporter.cc" + "${SRC_DIR}/perf_counters.cc" + "${SRC_DIR}/reporter.cc" + "${SRC_DIR}/sleep.cc" + "${SRC_DIR}/statistics.cc" + "${SRC_DIR}/string_util.cc" + "${SRC_DIR}/sysinfo.cc" + "${SRC_DIR}/timers.cc") + +add_library(google_benchmark "${SRCS}") +target_include_directories(google_benchmark SYSTEM PUBLIC "${SRC_DIR}/../include") + +add_library(google_benchmark_main "${SRC_DIR}/benchmark_main.cc") +target_link_libraries(google_benchmark_main PUBLIC google_benchmark) + +add_library(google_benchmark_all INTERFACE) +target_link_libraries(google_benchmark_all INTERFACE google_benchmark google_benchmark_main) + +add_library(ch_contrib::gbenchmark_all ALIAS google_benchmark_all) diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt index 490628a2180..e527b3dec43 100644 --- a/src/Common/CMakeLists.txt +++ b/src/Common/CMakeLists.txt @@ -1,5 +1,9 @@ add_subdirectory(StringUtils) +if (ENABLE_BENCHMARKS) + add_subdirectory(benchmarks) +endif() + if (ENABLE_EXAMPLES) add_subdirectory(examples) endif() diff --git a/src/Common/benchmarks/CMakeLists.txt b/src/Common/benchmarks/CMakeLists.txt new file mode 100644 index 00000000000..57ed837db8b --- /dev/null +++ b/src/Common/benchmarks/CMakeLists.txt @@ -0,0 +1,9 @@ +clickhouse_add_executable(integer_hash_tables_and_hashes integer_hash_tables_and_hashes.cpp) +target_link_libraries (integer_hash_tables_and_hashes PRIVATE + ch_contrib::gbenchmark_all + dbms + ch_contrib::abseil_swiss_tables + ch_contrib::sparsehash + ch_contrib::wyhash + ch_contrib::farmhash + ch_contrib::xxHash) diff --git a/src/Common/examples/integer_hash_tables_and_hashes.cpp b/src/Common/benchmarks/integer_hash_tables_and_hashes.cpp similarity index 94% rename from src/Common/examples/integer_hash_tables_and_hashes.cpp rename to src/Common/benchmarks/integer_hash_tables_and_hashes.cpp index 0e9390ab3ac..c245fc471cc 100644 --- a/src/Common/examples/integer_hash_tables_and_hashes.cpp +++ b/src/Common/benchmarks/integer_hash_tables_and_hashes.cpp @@ -1,5 +1,8 @@ -#include +#include + #include +#include +#include #include #include @@ -13,12 +16,23 @@ //#define DBMS_HASH_MAP_COUNT_COLLISIONS //#define DBMS_HASH_MAP_DEBUG_RESIZES -#include -#include +#include +#include #include +#include +#include #include #include +#include +#include + +#ifdef __clang__ +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wused-but-marked-unused" +#endif +#include + using Key = UInt64; using Value = UInt64; @@ -282,98 +296,91 @@ namespace Hashes return res; } }; + + struct FarmHash + { + size_t operator()(Key x) const { return NAMESPACE_FOR_HASH_FUNCTIONS::Hash64(reinterpret_cast(&x), sizeof(x)); } + }; + + struct WyHash + { + size_t operator()(Key x) const { return wyhash(reinterpret_cast(&x), sizeof(x), 0, _wyp); } + }; + + struct XXH3Hash + { + size_t operator()(Key x) const { return XXH_INLINE_XXH3_64bits(reinterpret_cast(&x), sizeof(x)); } + }; } template