Skip to content

Commit 295fbae

Browse files
committed
add benchmarks
1 parent 2a07743 commit 295fbae

File tree

14 files changed

+710
-8
lines changed

14 files changed

+710
-8
lines changed

.github/workflows/ci.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ jobs:
5353
- name: Run demo
5454
run: |
5555
./scripts/run-demo.sh
56+
57+
- name: Run formatting benchmark
58+
run: |
59+
CMAKE_ARGS="-DBUILD_BENCHMARK=ON" ./scripts/build.sh
60+
./build/bin/Release/format_benchmark --benchmark_min_time=0.5s
61+
62+
- name: Run database insert benchmark
63+
run: |
64+
./build/bin/Release/db_insert_benchmark --benchmark_min_time=0.5s
5665
5766
- name: Stop InfluxDB
5867
if: always()
@@ -120,6 +129,15 @@ jobs:
120129
run: |
121130
./scripts/run-demo.sh
122131
132+
- name: Run formatting benchmark
133+
run: |
134+
CMAKE_ARGS="-DBUILD_BENCHMARK=ON" ./scripts/build.sh
135+
./build/bin/Release/format_benchmark --benchmark_min_time=0.5s
136+
137+
- name: Run database insert benchmark
138+
run: |
139+
./build/bin/Release/db_insert_benchmark --benchmark_min_time=0.5s
140+
123141
- name: Stop InfluxDB
124142
if: always()
125143
run: |
@@ -182,6 +200,15 @@ jobs:
182200
- name: Run demo
183201
run: |
184202
scripts\run-demo.bat
203+
204+
- name: Run formatting benchmark
205+
run: |
206+
scripts\build.bat Release -DBUILD_BENCHMARK=ON
207+
build\bin\Release\format_benchmark.exe --benchmark_min_time=0.5s
208+
209+
- name: Run database insert benchmark
210+
run: |
211+
build\bin\Release\db_insert_benchmark.exe --benchmark_min_time=0.5s
185212
186213
- name: Stop InfluxDB
187214
if: always()

CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ endif()
2323
# Build options
2424
option(BUILD_TESTING "Build tests" ON)
2525
option(BUILD_DEMO "Build demo application" ON)
26+
option(BUILD_BENCHMARK "Build benchmark application" OFF)
2627
option(USE_CONAN "Use Conan for dependency management" ON)
2728

2829
# Include directories
@@ -445,6 +446,33 @@ if(BUILD_DEMO)
445446
endif()
446447
endif()
447448

449+
# Benchmark applications
450+
if(BUILD_BENCHMARK)
451+
add_subdirectory(src/benchmark)
452+
453+
# Benchmark output directory - match test executables location
454+
if(CMAKE_CONFIGURATION_TYPES)
455+
# Multi-config generator (Visual Studio, Xcode)
456+
set_target_properties(format_benchmark db_insert_benchmark
457+
PROPERTIES
458+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/$<CONFIG>"
459+
)
460+
else()
461+
# Single-config generator
462+
if(CMAKE_BUILD_TYPE)
463+
set_target_properties(format_benchmark db_insert_benchmark
464+
PROPERTIES
465+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}"
466+
)
467+
else()
468+
set_target_properties(format_benchmark db_insert_benchmark
469+
PROPERTIES
470+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
471+
)
472+
endif()
473+
endif()
474+
endif()
475+
448476
# Installation
449477
install(TARGETS influxdb-cpp-rest influx-c-rest
450478
LIBRARY DESTINATION lib

docs/benchmarking.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Formatting Benchmark
2+
3+
This benchmark measures the performance of `std::format` operations used in the library.
4+
5+
## Building
6+
7+
### Using build scripts (recommended)
8+
9+
```bash
10+
# Linux/macOS
11+
CMAKE_ARGS="-DBUILD_BENCHMARK=ON" ./scripts/build.sh
12+
13+
# Or with explicit build type
14+
BUILD_TYPE=Release CMAKE_ARGS="-DBUILD_BENCHMARK=ON" ./scripts/build.sh
15+
16+
# Windows
17+
set CMAKE_ARGS=-DBUILD_BENCHMARK=ON
18+
scripts\build.bat
19+
```
20+
21+
### Manual build
22+
23+
```bash
24+
# Build with benchmark enabled
25+
cmake -B build -DBUILD_BENCHMARK=ON
26+
cmake --build build --config Release
27+
```
28+
29+
## Running
30+
31+
```bash
32+
# Linux/macOS
33+
./build/bin/Release/format_benchmark
34+
35+
# Windows
36+
build\bin\Release\format_benchmark.exe
37+
```
38+
39+
**Note:** By default, each benchmark runs for at least 0.5 seconds (or until it reaches a stable measurement). To run faster benchmarks in CI, use:
40+
```bash
41+
./build/bin/Release/format_benchmark --benchmark_min_time=0.5
42+
```
43+
44+
## Results
45+
46+
The benchmark measures:
47+
- `BM_FormatKVP`: Formatting key-value pairs (e.g., `"tag1=value1"`)
48+
- `BM_FormatLine`: Formatting complete InfluxDB line protocol lines
49+
- `BM_FormatCombined`: Combined formatting operations
50+
51+
Results show time per iteration, CPU time, and iterations per second.

scripts/build.bat

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,35 @@ if not exist "conan_toolchain.cmake" if not exist "generators\conan_toolchain.cm
2222
)
2323
)
2424

25-
REM Configure
26-
cmake .. -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DCMAKE_CXX_STANDARD=20
25+
REM Configure (pass any additional arguments)
26+
REM Find Conan toolchain file
27+
set CONAN_TOOLCHAIN=
28+
if exist "generators\conan_toolchain.cmake" (
29+
set CONAN_TOOLCHAIN=-DCMAKE_TOOLCHAIN_FILE=generators\conan_toolchain.cmake
30+
) else if exist "conan_toolchain.cmake" (
31+
set CONAN_TOOLCHAIN=-DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
32+
) else if exist "build\generators\conan_toolchain.cmake" (
33+
set CONAN_TOOLCHAIN=-DCMAKE_TOOLCHAIN_FILE=build\generators\conan_toolchain.cmake
34+
) else if exist "build\build\generators\conan_toolchain.cmake" (
35+
set CONAN_TOOLCHAIN=-DCMAKE_TOOLCHAIN_FILE=build\build\generators\conan_toolchain.cmake
36+
)
37+
38+
set CMAKE_ARGS=%*
39+
set CMAKE_ARGS=!CMAKE_ARGS:%BUILD_TYPE%=!
40+
if not "!CMAKE_ARGS!"=="" (
41+
echo Additional CMake args: !CMAKE_ARGS!
42+
if not "!CONAN_TOOLCHAIN!"=="" (
43+
cmake .. -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DCMAKE_CXX_STANDARD=20 !CONAN_TOOLCHAIN! !CMAKE_ARGS!
44+
) else (
45+
cmake .. -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DCMAKE_CXX_STANDARD=20 !CMAKE_ARGS!
46+
)
47+
) else (
48+
if not "!CONAN_TOOLCHAIN!"=="" (
49+
cmake .. -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DCMAKE_CXX_STANDARD=20 !CONAN_TOOLCHAIN!
50+
) else (
51+
cmake .. -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DCMAKE_CXX_STANDARD=20
52+
)
53+
)
2754
if errorlevel 1 (
2855
echo ERROR: CMake configuration failed
2956
exit /b 1

scripts/build.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

4-
BUILD_TYPE="${1:-Release}"
54
BUILD_DIR="${BUILD_DIR:-build}"
5+
BUILD_TYPE="${BUILD_TYPE:-Release}"
6+
CMAKE_ARGS="${CMAKE_ARGS:-}"
67

78
echo "Building with CMake (${BUILD_TYPE})..."
89

@@ -26,11 +27,18 @@ if [ ! -f "conan_toolchain.cmake" ] && [ ! -f "generators/conan_toolchain.cmake"
2627
fi
2728
fi
2829

29-
# Configure
30-
cmake .. -DCMAKE_BUILD_TYPE="${BUILD_TYPE}" -DCMAKE_CXX_STANDARD=20
30+
# Configure with additional CMake arguments
31+
cmake .. -DCMAKE_BUILD_TYPE="${BUILD_TYPE}" -DCMAKE_CXX_STANDARD=20 ${CMAKE_ARGS}
3132

3233
# Build
33-
cmake --build . --config "${BUILD_TYPE}"
34+
# Only use --config for multi-config generators (Visual Studio, Xcode)
35+
if command -v ninja &> /dev/null || [ -f "Makefile" ]; then
36+
# Single-config generator (Unix Makefiles, Ninja)
37+
cmake --build .
38+
else
39+
# Multi-config generator (Visual Studio, Xcode)
40+
cmake --build . --config "${BUILD_TYPE}"
41+
fi
3442

3543
echo "Build complete! Binaries are in ${BUILD_DIR}/bin"
3644

scripts/publish-conan-version.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ class InfluxdbCppRestConan(ConanFile):
178178
deps.generate()
179179
tc = CMakeToolchain(self)
180180
# Disable tests and demo for packaging
181-
tc.variables["BUILD_TESTING"] = False
182-
tc.variables["BUILD_DEMO"] = False
181+
tc.cache_variables["BUILD_TESTING"] = False
182+
tc.cache_variables["BUILD_DEMO"] = False
183183
tc.generate()
184184
185185
def build(self):
@@ -214,6 +214,21 @@ sources:
214214
sha256: "${SHA256}"
215215
EOF
216216

217+
# Create test_package directory
218+
TEST_PACKAGE_DIR="${RECIPE_DIR}/test_package"
219+
mkdir -p "${TEST_PACKAGE_DIR}"
220+
221+
# Copy test_package files from template
222+
TEMPLATE_DIR="${SCRIPT_DIR}/test_package_template"
223+
if [ -d "${TEMPLATE_DIR}" ]; then
224+
cp "${TEMPLATE_DIR}/test_package.cpp" "${TEST_PACKAGE_DIR}/"
225+
cp "${TEMPLATE_DIR}/CMakeLists.txt" "${TEST_PACKAGE_DIR}/"
226+
cp "${TEMPLATE_DIR}/conanfile.py" "${TEST_PACKAGE_DIR}/"
227+
echo "Created test_package files in ${TEST_PACKAGE_DIR}/"
228+
else
229+
echo "Warning: test_package template directory not found at ${TEMPLATE_DIR}"
230+
fi
231+
217232
echo "Created recipe files in ${RECIPE_DIR}/"
218233

219234
if [ "${DRY_RUN}" = true ]; then

scripts/run-db-insert-benchmark.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
# Download and extract InfluxDB binary
3+
# Usage: download-influxdb.sh <version> <architecture>
4+
# Example: download-influxdb.sh 1.8.10 darwin_amd64
5+
6+
set -euo pipefail
7+
8+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9+
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
10+
11+
cd "${PROJECT_ROOT}"
12+
13+
CMAKE_ARGS="-DBUILD_BENCHMARK=ON" ./scripts/build.sh
14+
15+
./build/bin/Release/db_insert_benchmark
16+

scripts/run-format-benchmark.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
# Download and extract InfluxDB binary
3+
# Usage: download-influxdb.sh <version> <architecture>
4+
# Example: download-influxdb.sh 1.8.10 darwin_amd64
5+
6+
set -euo pipefail
7+
8+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9+
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
10+
11+
cd "${PROJECT_ROOT}"
12+
13+
CMAKE_ARGS="-DBUILD_BENCHMARK=ON" ./scripts/build.sh
14+
15+
./build/bin/Release/db_insert_benchmark
16+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(test_package LANGUAGES CXX)
3+
4+
find_package(influxdb-cpp-rest REQUIRED CONFIG)
5+
6+
add_executable(${PROJECT_NAME} test_package.cpp)
7+
target_link_libraries(${PROJECT_NAME} PRIVATE influxdb-cpp-rest::influxdb-cpp-rest)
8+
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
9+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from conan import ConanFile
2+
from conan.tools.build import can_run
3+
from conan.tools.cmake import cmake_layout, CMake
4+
import os
5+
6+
7+
class TestPackageConan(ConanFile):
8+
settings = "os", "arch", "compiler", "build_type"
9+
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
10+
test_type = "explicit"
11+
12+
def requirements(self):
13+
self.requires(self.tested_reference_str)
14+
15+
def layout(self):
16+
cmake_layout(self)
17+
18+
def build(self):
19+
cmake = CMake(self)
20+
cmake.configure()
21+
cmake.build()
22+
23+
def test(self):
24+
if can_run(self):
25+
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
26+
self.run(bin_path, env="conanrun")
27+

0 commit comments

Comments
 (0)