From 60f0b46ba3b7e93943effb3d1f1cfac020e11916 Mon Sep 17 00:00:00 2001 From: battlmonstr Date: Mon, 12 Feb 2024 09:42:12 +0100 Subject: [PATCH] snapshots seg: library target (#1807) --- cmake/common/targets.cmake | 1 + cmd/test/CMakeLists.txt | 5 -- silkworm/{node => core}/common/bit_count.hpp | 0 silkworm/node/CMakeLists.txt | 26 +++------ .../snapshots/rec_split/common/common.hpp | 2 +- silkworm/node/snapshots/seg/CMakeLists.txt | 26 +++++++++ .../seg/compressor/patricia_tree.cpp | 2 +- .../seg/compressor/patricia_tree_test.cpp | 1 + .../seg/compressor/pattern_extractor.hpp | 2 - silkworm/node/snapshots/seg/decompressor.hpp | 2 - ...est.cpp => snapshot_decompressor_test.cpp} | 10 ++-- tests/unit/README.md | 18 +++---- tests/unit/run_unit_test_loop.py | 53 ++++--------------- 13 files changed, 60 insertions(+), 88 deletions(-) rename silkworm/{node => core}/common/bit_count.hpp (100%) create mode 100644 silkworm/node/snapshots/seg/CMakeLists.txt rename silkworm/node/snapshots/{seg/decompressor_test.cpp => snapshot_decompressor_test.cpp} (99%) diff --git a/cmake/common/targets.cmake b/cmake/common/targets.cmake index 3ac18aaf9b..3b34e84056 100644 --- a/cmake/common/targets.cmake +++ b/cmake/common/targets.cmake @@ -34,6 +34,7 @@ function(silkworm_library TARGET) file(GLOB_RECURSE SRC CONFIGURE_DEPENDS "*.cpp" "*.hpp") list(FILTER SRC EXCLUDE REGEX "_test\\.cpp$") + list(FILTER SRC EXCLUDE REGEX "_benchmark\\.cpp$") list_filter(SRC ARG_EXCLUDE_REGEX) add_library(${TARGET} ${SRC}) diff --git a/cmd/test/CMakeLists.txt b/cmd/test/CMakeLists.txt index d013021380..24b6c47436 100644 --- a/cmd/test/CMakeLists.txt +++ b/cmd/test/CMakeLists.txt @@ -32,11 +32,6 @@ endif() if(NOT SILKWORM_CORE_ONLY) find_package(GTest REQUIRED) - # Silkworm Node Tests - file(GLOB_RECURSE SILKWORM_NODE_TESTS CONFIGURE_DEPENDS "${SILKWORM_MAIN_SRC_DIR}/node/*_test.cpp") - add_executable(node_test unit_test.cpp ${SILKWORM_NODE_TESTS}) - target_link_libraries(node_test silkworm_node Catch2::Catch2) - # Silkworm RpcDaemon Tests file(GLOB_RECURSE SILKWORM_RPCDAEMON_TESTS CONFIGURE_DEPENDS "${SILKWORM_MAIN_SRC_DIR}/rpc/*_test.cpp") add_executable(rpcdaemon_test unit_test.cpp ${SILKWORM_RPCDAEMON_TESTS}) diff --git a/silkworm/node/common/bit_count.hpp b/silkworm/core/common/bit_count.hpp similarity index 100% rename from silkworm/node/common/bit_count.hpp rename to silkworm/core/common/bit_count.hpp diff --git a/silkworm/node/CMakeLists.txt b/silkworm/node/CMakeLists.txt index 3431da0986..080b84034f 100644 --- a/silkworm/node/CMakeLists.txt +++ b/silkworm/node/CMakeLists.txt @@ -14,6 +14,10 @@ limitations under the License. ]] +include("${SILKWORM_MAIN_DIR}/cmake/common/targets.cmake") + +add_subdirectory(snapshots/seg) + find_package(absl REQUIRED) find_package(asio-grpc REQUIRED) find_package(Boost REQUIRED headers container) @@ -23,23 +27,6 @@ find_package(magic_enum REQUIRED) find_package(Protobuf REQUIRED) find_package(roaring REQUIRED) -file( - GLOB_RECURSE - SILKWORM_NODE_SRC - CONFIGURE_DEPENDS - "*.cpp" - "*.hpp" - "*.c" - "*.h" - "*.cc" -) -list(FILTER SILKWORM_NODE_SRC EXCLUDE REGEX "_test\\.cpp$") -list(FILTER SILKWORM_NODE_SRC EXCLUDE REGEX "_benchmark\\.cpp$") - -add_library(silkworm_node "${SILKWORM_NODE_SRC}") - -target_include_directories(silkworm_node PUBLIC "${SILKWORM_MAIN_DIR}") - set(SILKWORM_NODE_PUBLIC_LIBS silkworm_core silkworm_infra @@ -63,13 +50,14 @@ set(SILKWORM_NODE_PRIVATE_LIBS cborcpp evmone magic_enum::magic_enum - sais_lite silkworm_interfaces + silkworm_snapshots_seg ) # cmake-format: on -target_link_libraries( +silkworm_library( silkworm_node PUBLIC ${SILKWORM_NODE_PUBLIC_LIBS} PRIVATE ${SILKWORM_NODE_PRIVATE_LIBS} + EXCLUDE_REGEX "snapshots/seg" ) diff --git a/silkworm/node/snapshots/rec_split/common/common.hpp b/silkworm/node/snapshots/rec_split/common/common.hpp index 1f4897b9d4..8854efb4b9 100644 --- a/silkworm/node/snapshots/rec_split/common/common.hpp +++ b/silkworm/node/snapshots/rec_split/common/common.hpp @@ -56,7 +56,7 @@ #include #include -#include +#include // Explicit branch predictions #define likely(x) __builtin_expect(!!(x), 1) diff --git a/silkworm/node/snapshots/seg/CMakeLists.txt b/silkworm/node/snapshots/seg/CMakeLists.txt new file mode 100644 index 0000000000..7089ab4d4c --- /dev/null +++ b/silkworm/node/snapshots/seg/CMakeLists.txt @@ -0,0 +1,26 @@ +#[[ + Copyright 2024 The Silkworm Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +]] + +include("${SILKWORM_MAIN_DIR}/cmake/common/targets.cmake") + +# circular_buffer +find_package(Boost REQUIRED headers) + +silkworm_library( + silkworm_snapshots_seg + PUBLIC silkworm_core silkworm_infra + PRIVATE Boost::headers sais_lite +) diff --git a/silkworm/node/snapshots/seg/compressor/patricia_tree.cpp b/silkworm/node/snapshots/seg/compressor/patricia_tree.cpp index b68dd1778c..2dc025efa1 100644 --- a/silkworm/node/snapshots/seg/compressor/patricia_tree.cpp +++ b/silkworm/node/snapshots/seg/compressor/patricia_tree.cpp @@ -26,8 +26,8 @@ #include #include +#include #include -#include #include "lcp_kasai.hpp" diff --git a/silkworm/node/snapshots/seg/compressor/patricia_tree_test.cpp b/silkworm/node/snapshots/seg/compressor/patricia_tree_test.cpp index 7ef88fd1d6..8a2c443d0a 100644 --- a/silkworm/node/snapshots/seg/compressor/patricia_tree_test.cpp +++ b/silkworm/node/snapshots/seg/compressor/patricia_tree_test.cpp @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ + #include "patricia_tree.hpp" #include diff --git a/silkworm/node/snapshots/seg/compressor/pattern_extractor.hpp b/silkworm/node/snapshots/seg/compressor/pattern_extractor.hpp index 71a8de0aa1..e3b1569f98 100644 --- a/silkworm/node/snapshots/seg/compressor/pattern_extractor.hpp +++ b/silkworm/node/snapshots/seg/compressor/pattern_extractor.hpp @@ -23,8 +23,6 @@ #include -#include "intx/intx.hpp" - namespace silkworm::snapshots::seg { /** diff --git a/silkworm/node/snapshots/seg/decompressor.hpp b/silkworm/node/snapshots/seg/decompressor.hpp index 0ebf28dc8c..e99de84e62 100644 --- a/silkworm/node/snapshots/seg/decompressor.hpp +++ b/silkworm/node/snapshots/seg/decompressor.hpp @@ -27,9 +27,7 @@ #include -#include #include -#include #include namespace silkworm::snapshots::seg { diff --git a/silkworm/node/snapshots/seg/decompressor_test.cpp b/silkworm/node/snapshots/snapshot_decompressor_test.cpp similarity index 99% rename from silkworm/node/snapshots/seg/decompressor_test.cpp rename to silkworm/node/snapshots/snapshot_decompressor_test.cpp index ae01d077ae..c9e17022d7 100644 --- a/silkworm/node/snapshots/seg/decompressor_test.cpp +++ b/silkworm/node/snapshots/snapshot_decompressor_test.cpp @@ -14,8 +14,6 @@ limitations under the License. */ -#include "decompressor.hpp" - #include #include #include @@ -34,14 +32,16 @@ #include #include -using Catch::Matchers::Message; +#include "seg/decompressor.hpp" -namespace silkworm::snapshots::seg { +namespace silkworm::snapshots { +using Catch::Matchers::Message; namespace test = test_util; using silkworm::test_util::null_stream; using silkworm::test_util::SetLogVerbosityGuard; using silkworm::test_util::TemporaryFile; +using namespace snapshots::seg; //! DecodingTable exposed for white-box testing class DecodingTable_ForTest : public DecodingTable { @@ -547,4 +547,4 @@ TEST_CASE("Decompressor: lorem ipsum has_prefix", "[silkworm][node][seg][decompr CHECK(test_function(it)); } -} // namespace silkworm::snapshots::seg +} // namespace silkworm::snapshots diff --git a/tests/unit/README.md b/tests/unit/README.md index 526f331afa..e02200cf6f 100644 --- a/tests/unit/README.md +++ b/tests/unit/README.md @@ -8,18 +8,16 @@ It works with multiple compilers and build configurations. $cd tests/unit $./run_unit_test_loop.py -Usage: ./run_unit_test_loop.py [-h] [-i iterations] [-m modules] [-t test] builddir +Usage: ./run_unit_test_loop.py [-h] [-i iterations] [-t test] modules Launch an automated unit test sequence on target build configuration -builddir - the path of the target build folder +modules + comma-separated list of unit test executables to launch -h print this help -i iterations the number of iterations for each configuration (default: 1000) --m modules - the list of unit test modules to launch (default: ['core_test', 'node_test', 'rpcdaemon_test', 'sentry_test', 'sync_test']) -o options the Catch2 options to pass to the launcher enclosed in string (default: "" i.e. none) -t test @@ -29,11 +27,9 @@ builddir ## Examples ``` -$cd tests/unit - -$./run_unit_test_loop.py -i 100 -m node_test ../../cmake-build-clang-release +$./run_unit_test_loop.py -i 100 build/silkworm/node/silkworm_node_test -$./run_unit_test_loop.py -i 100 -m node_test -o "-d yes" ../../cmake-build-clang-release +$./run_unit_test_loop.py -i 100 -o "-d yes" build/silkworm/node/silkworm_node_test -$./run_unit_test_loop.py -i 100 -m node_test -t "MemoryMutationCursor: to_next" ../../cmake-build-clang-release -``` \ No newline at end of file +$./run_unit_test_loop.py -i 100 -t "MemoryMutationCursor: to_next" build/silkworm/node/silkworm_node_test +``` diff --git a/tests/unit/run_unit_test_loop.py b/tests/unit/run_unit_test_loop.py index 034e56afd0..93f544e00a 100755 --- a/tests/unit/run_unit_test_loop.py +++ b/tests/unit/run_unit_test_loop.py @@ -9,69 +9,46 @@ from typing import List -class UnitTestModule(str, Enum): - """ The unit test modules""" - core_test = 'core_test' - node_test = 'node_test' - rpcdaemon_test = 'rpcdaemon_test' - sentry_test = 'sentry_test' - sync_test = 'sync_test' - - @classmethod - def item_names(cls) -> List[str]: - """ Return the list of enumeration item names """ - return [member_name for member_name in UnitTestModule.__members__.keys()] - - @classmethod - def has_item(cls, name: str) -> bool: - """ Return true if name is a valid enumeration item, false otherwise """ - return name in cls.item_names() - - class UnitTest: """ The unit test executable """ - def __init__(self, module: UnitTestModule, build_dir: str): + def __init__(self, module: str): """ Create a new unit test execution """ self.module = module - self.build_dir = build_dir def execute(self, num_iterations: int, test_name: str = None, test_options: str = "") -> None: """ Execute the unit tests `num_iterations` times """ - cmd = self.build_dir + "/cmd/test/" + self.module.name.lower() + cmd = self.module if test_name is not None and test_name != '': cmd = cmd + " \"" + test_name + "\"" cmd = cmd + " " + test_options print("Unit test runner: " + cmd + "\n") - print("Unit test stress for " + self.module.name.lower() + " STARTED") + print("Unit test stress for " + self.module + " STARTED") for i in range(num_iterations): - print("Unit test stress for " + self.module.name.lower() + " RUN [i=" + str(i) + "]") + print("Unit test stress for " + self.module + " RUN [i=" + str(i) + "]") status = os.system(cmd) if status != 0: - print("Unit test stress for " + self.module.name.lower() + " FAILED [i=" + str(i) + "]") + print("Unit test stress for " + self.module + " FAILED [i=" + str(i) + "]") sys.exit(-1) - print("Unit test stress for " + self.module.name.lower() + " COMPLETED [" + str(num_iterations) + "]") + print("Unit test stress for " + self.module + " COMPLETED [" + str(num_iterations) + "]") DEFAULT_NUM_ITERATIONS: int = 1000 -DEFAULT_MODULES: List[str] = UnitTestModule.item_names() def usage(argv): """ Print usage """ - print("Usage: " + argv[0] + " [-h] [-i iterations] [-m modules] [-t test] builddir") + print("Usage: " + argv[0] + " [-h] [-i iterations] [-t test] modules") print("") print("Launch an automated unit test sequence on target build configuration") print("") - print("builddir") - print(" \tthe path of the target build folder") + print("modules") + print(" \tcomma-separated list of unit test executables to launch") print("") print("-h\tprint this help") print("-i\titerations") print(" \tthe number of iterations for each configuration (default: " + str(DEFAULT_NUM_ITERATIONS) + ")") - print("-m\tmodules") - print(" \tthe list of unit test modules to launch (default: " + str(DEFAULT_MODULES) + ")") print("-o\toptions") print(" \tthe Catch2 options to pass to the launcher enclosed in string (default: \"\" i.e. none)") print("-t\ttest") @@ -87,19 +64,16 @@ def main(argv) -> int: usage(argv) return 1 - build_dir = args[0] + modules = args[0].split(",") test_name = None test_options = "" iterations = DEFAULT_NUM_ITERATIONS - modules = DEFAULT_MODULES for option, option_arg in opts: if option in ("-h", "--help"): usage(argv) elif option == "-i": iterations = int(option_arg) - elif option == "-m": - modules = str(option_arg).split(",") elif option == "-o": test_options = option_arg print("test_options=" + test_options) @@ -107,12 +81,7 @@ def main(argv) -> int: test_name = option_arg for module_name in modules: - module_name = module_name.lower() - if not UnitTestModule.has_item(module_name): - print("Invalid test module name [" + module_name + "], ignored") - continue - unit_test_module = UnitTestModule(module_name) - unit_test = UnitTest(unit_test_module, build_dir) + unit_test = UnitTest(module_name) unit_test.execute(iterations, test_name, test_options) return 0