diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dd42b2b553..4f4a5183fa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,7 +36,15 @@ jobs: steps: - name: Install deps ⛓️ run: | - apt update && apt install -y --no-install-recommends ca-certificates cmake build-essential git clang llvm pkg-config autoconf automake libtool libelf-dev wget libc-ares-dev libcurl4-openssl-dev libssl-dev libtbb-dev libjq-dev libjsoncpp-dev libgrpc++-dev protobuf-compiler-grpc libgtest-dev libprotobuf-dev linux-headers-${{ matrix.arch }} + apt update && apt install -y --no-install-recommends curl ca-certificates build-essential git clang llvm pkg-config autoconf automake libtool libelf-dev wget libc-ares-dev libcurl4-openssl-dev libssl-dev libtbb-dev libjq-dev libjsoncpp-dev libgrpc++-dev protobuf-compiler-grpc libgtest-dev libprotobuf-dev linux-headers-${{ matrix.arch }} + + - name: Install a recent version of CMake ⛓️ + run: | + curl -L -o /tmp/cmake.tar.gz https://github.com/Kitware/CMake/releases/download/v3.22.5/cmake-3.22.5-linux-$(uname -m).tar.gz + gzip -d /tmp/cmake.tar.gz + tar -xpf /tmp/cmake.tar --directory=/tmp + cp -R /tmp/cmake-3.22.5-linux-$(uname -m)/* /usr + rm -rf /tmp/cmake-3.22.5-linux-$(uname -m)/ - name: Checkout Libs ⤵️ uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 @@ -56,7 +64,7 @@ jobs: UBSAN_OPTIONS: print_stacktrace=1 run: | mkdir -p build - cd build && cmake ${{ matrix.cmake_opts }} ../ + cd build && cmake ${{ matrix.cmake_opts }} -DENABLE_BENCHMARKS=ON ../ KERNELDIR=/lib/modules/$(ls /lib/modules)/build make -j4 make run-unit-tests @@ -82,9 +90,15 @@ jobs: - name: Build and test 🏗️🧪 run: | mkdir -p build - cd build && cmake -DBUILD_BPF=On -DBUILD_DRIVER=Off -DUSE_BUNDLED_DEPS=On -DUSE_BUNDLED_LIBELF=Off -DUSE_SHARED_LIBELF=Off -DBUILD_LIBSCAP_MODERN_BPF=ON -DMUSL_OPTIMIZED_BUILD=On ../ + cd build && cmake -DBUILD_BPF=On -DBUILD_DRIVER=Off -DUSE_BUNDLED_DEPS=On -DUSE_BUNDLED_LIBELF=Off -DUSE_SHARED_LIBELF=Off -DBUILD_LIBSCAP_MODERN_BPF=ON -DMUSL_OPTIMIZED_BUILD=On -DENABLE_BENCHMARKS=ON ../ make run-unit-tests -j4 + - name: Run benchmarks + run: | + cd build + make bench + ./benchmark/bench + build-shared-libs-linux-amd64: name: build-shared-libs-linux-amd64 🧐 runs-on: ubuntu-latest @@ -107,7 +121,7 @@ jobs: - name: Build and test 🏗️🧪 run: | mkdir -p build - cd build && cmake -DBUILD_SHARED_LIBS=True -DUSE_BUNDLED_DEPS=False -DMINIMAL_BUILD=True -DCMAKE_INSTALL_PREFIX=/tmp/libs-test ../ + cd build && cmake -DBUILD_SHARED_LIBS=True -DUSE_BUNDLED_DEPS=False -DMINIMAL_BUILD=True -DCMAKE_INSTALL_PREFIX=/tmp/libs-test -DENABLE_BENCHMARKS=ON ../ make -j4 make run-unit-tests @@ -147,7 +161,7 @@ jobs: - name: Build and test 🏗️🧪 run: | mkdir -p build - cd build && cmake -DUSE_BUNDLED_DEPS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_MSVC_RUNTIME_LIBRARY=${{ matrix.crt }} -DCREATE_TEST_TARGETS=ON -DMINIMAL_BUILD=ON .. + cd build && cmake -DUSE_BUNDLED_DEPS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_MSVC_RUNTIME_LIBRARY=${{ matrix.crt }} -DCREATE_TEST_TARGETS=ON -DMINIMAL_BUILD=ON -DENABLE_BENCHMARKS=ON .. cmake --build . --config Release --parallel 4 && make run-unit-tests || libsinsp\test\Release\unit-test-libsinsp.exe build-shared-libs-macos-amd64: diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c32f04db5..2e7b29f0cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,3 +151,8 @@ if(CREATE_TEST_TARGETS) endif() endif() + +option(ENABLE_BENCHMARKS "Enable Benchmarks" OFF) +if(ENABLE_BENCHMARKS) + add_subdirectory(benchmark) +endif() \ No newline at end of file diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt new file mode 100644 index 0000000000..37ce498970 --- /dev/null +++ b/benchmark/CMakeLists.txt @@ -0,0 +1,35 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (C) 2024 The Falco 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. +# + +message(STATUS "Benchmarks enabled") + +include(googleBenchmark) + +set(BENCHMARK_SOURCES ./main.cpp) +set(BENCHMARK_DEPENDENCIES sinsp) +set(BENCHMARK_LIBRARIES benchmark::benchmark sinsp) +set(BENCHMARK_INCLUDE PRIVATE "${LIBSINSP_INCLUDE_DIRS}") + +file(GLOB_RECURSE SINSP_SUITE "${CMAKE_CURRENT_SOURCE_DIR}/libsinsp/*.cpp") +list(APPEND BENCHMARK_SOURCES ${SINSP_SUITE}) + +add_compile_options(${FALCOSECURITY_LIBS_USERSPACE_COMPILE_FLAGS}) +add_link_options(${FALCOSECURITY_LIBS_USERSPACE_LINK_FLAGS}) +add_executable(bench ${BENCHMARK_SOURCES}) +target_link_libraries(bench ${BENCHMARK_LIBRARIES}) +target_include_directories(bench ${BENCHMARK_INCLUDE}) +add_dependencies(bench ${BENCHMARK_DEPENDENCIES}) diff --git a/benchmark/README.md b/benchmark/README.md new file mode 100644 index 0000000000..c2b4f9e3fd --- /dev/null +++ b/benchmark/README.md @@ -0,0 +1,14 @@ +# Benchmarks + +## Build + +```bash +cmake -DENABLE_BENCHMARKS=ON .. +make bench +``` + +## Run + +```bash +sudo ./benchmark/bench +``` diff --git a/benchmark/libsinsp/utils.cpp b/benchmark/libsinsp/utils.cpp new file mode 100644 index 0000000000..1a5dfa768f --- /dev/null +++ b/benchmark/libsinsp/utils.cpp @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: Apache-2.0 +/* +Copyright (C) 2024 The Falco 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 +#include + +static void BM_sinsp_split(benchmark::State& state) +{ + std::string str = "hello,world,"; + for(auto _ : state) + { + sinsp_split(str, ','); + } +} +BENCHMARK(BM_sinsp_split)->Repetitions(2); diff --git a/benchmark/main.cpp b/benchmark/main.cpp new file mode 100644 index 0000000000..8660d26f29 --- /dev/null +++ b/benchmark/main.cpp @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: Apache-2.0 +/* +Copyright (C) 2024 The Falco 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 + +BENCHMARK_MAIN(); \ No newline at end of file diff --git a/cmake/modules/googleBenchmark.cmake b/cmake/modules/googleBenchmark.cmake new file mode 100644 index 0000000000..d2fb9a0a70 --- /dev/null +++ b/cmake/modules/googleBenchmark.cmake @@ -0,0 +1,26 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (C) 2024 The Falco 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. +# + +# Disable the Google Benchmark requirement on Google Test +set(BENCHMARK_ENABLE_TESTING OFF) + +include(FetchContent) + +FetchContent_Declare( + googlebenchmark + GIT_REPOSITORY https://github.com/google/benchmark.git + GIT_TAG v1.9.0 +) + +FetchContent_MakeAvailable(googlebenchmark) \ No newline at end of file