Skip to content

Commit d271cdc

Browse files
committed
chore: add readme and cleanup benches
1 parent b2a79ef commit d271cdc

File tree

14 files changed

+390
-132
lines changed

14 files changed

+390
-132
lines changed

.github/workflows/codspeed.yml

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@ jobs:
1818
steps:
1919
- uses: actions/checkout@v4
2020
with:
21-
token: ${{ secrets.PAT }}
2221
lfs: true
23-
- run: git config --global url."https://not-matthias:${{ secrets.PAT }}@github.com".insteadOf "https://github.com"
22+
- uses: extractions/setup-just@v2
2423

2524
- name: Cache opencv
2625
uses: actions/cache@v3
2726
with:
2827
path: build-opencv
2928
key: ${{ runner.os }}-build-opencv
3029

31-
- uses: extractions/setup-just@v2
32-
3330
# https://docs.opencv.org/4.x/d7/d9f/tutorial_linux_install.html
3431
- name: Build opencv
3532
run: |
@@ -49,12 +46,6 @@ jobs:
4946
ninja -j$(nproc)
5047
sudo ninja install
5148
52-
# - uses: Dovyski/[email protected]
53-
# with:
54-
# opencv-version: '4.0.0'
55-
# CMAKE_BUILD_TYPE: RelWithDebInfo
56-
# install-deps: false
57-
5849
- name: Build the benchmark target(s)
5950
run: |
6051
just build-instrumentation
@@ -75,15 +66,8 @@ jobs:
7566
steps:
7667
- uses: actions/checkout@v4
7768
with:
78-
token: ${{ secrets.PAT }}
7969
lfs: true
80-
- run: git config --global url."https://not-matthias:${{ secrets.PAT }}@github.com".insteadOf "https://github.com"
81-
8270
- uses: extractions/setup-just@v2
83-
# - uses: Dovyski/[email protected]
84-
# with:
85-
# opencv-version: '4.0.0'
86-
# install-deps: false
8771

8872
- run: sudo apt-get update && sudo apt install -y libopencv-dev
8973

@@ -92,7 +76,6 @@ jobs:
9276
just build-walltime
9377
just run
9478
95-
9679
- name: Run the benchmarks
9780
uses: CodSpeedHQ/action@v3
9881
with:

CMakeLists.txt

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,8 @@ cmake_minimum_required(VERSION 3.12)
22
project( opencv-benchmarks VERSION 0.0.0 LANGUAGES CXX)
33
find_package( OpenCV REQUIRED )
44
include_directories( ${OpenCV_INCLUDE_DIRS} )
5-
add_executable(
6-
cv-bench
7-
src/main.cpp
8-
src/classifier.cpp
9-
src/color_conversion.cpp
10-
src/edge_detection.cpp
11-
src/filter.cpp
12-
src/read_image.cpp
13-
src/transform.cpp
14-
)
15-
target_link_libraries( cv-bench ${OpenCV_LIBS} )
16-
175

6+
# Configure Google Benchmark
187
include(FetchContent)
198
set(BENCHMARK_DOWNLOAD_DEPENDENCIES ON)
209
set(BENCHMARK_ENABLE_TESTING OFF)
@@ -26,4 +15,30 @@ FetchContent_Declare(
2615
GIT_TAG origin/main
2716
)
2817
FetchContent_MakeAvailable(google_benchmark)
29-
target_link_libraries(cv-bench benchmark::benchmark)
18+
19+
# Configure the executables
20+
#
21+
set(BENCHMARKS classifier color filter image transform)
22+
foreach(benchmark IN LISTS BENCHMARKS)
23+
add_executable(${benchmark} src/${benchmark}.cpp)
24+
target_link_libraries(${benchmark}
25+
benchmark::benchmark
26+
${OpenCV_LIBS}
27+
)
28+
endforeach()
29+
30+
31+
add_custom_target(run_all_benchmarks
32+
COMMAND ${CMAKE_COMMAND} -E echo "Running all benchmarks..."
33+
)
34+
35+
# Make each benchmark depend on the run_all_benchmarks target
36+
foreach(benchmark IN LISTS BENCHMARKS)
37+
add_custom_command(
38+
TARGET run_all_benchmarks
39+
POST_BUILD
40+
COMMAND ${CMAKE_COMMAND} -E echo "Running ${benchmark}..."
41+
COMMAND $<TARGET_FILE:${benchmark}>
42+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
43+
)
44+
endforeach()

Justfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ clean:
22
rm -rf build
33

44
run:
5-
./build/cv-bench
5+
cp -r assets/ build/
6+
cmake --build ./build --target run_all_benchmarks
67

78
build:
89
mkdir -p build

README.md

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,76 @@
11
# opencv-benches
22

3+
A benchmarking suite for comparing performance of various opencv library components using Google Benchmark.
4+
5+
## Requirements
6+
7+
- CMake 3.14+
8+
- C++14 compiler
9+
- OpenCV 4.x
10+
- [Just](https://github.com/casey/just) command runner
11+
- Ninja
12+
13+
## Build
14+
15+
Using the just command runner:
16+
```bash
17+
just clean # Make sure that there's no 'build/' dir
18+
just build
19+
just run
20+
```
21+
22+
Or by doing it manually:
23+
```bash
24+
mkdir build && cd build
25+
26+
# Without codspeed
27+
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
28+
29+
# With Codspeed instrumentation
30+
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCODSPEED_MODE=instrumentation ..
31+
32+
# With regular timing based benchmarks
33+
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCODSPEED_MODE=walltime ..
34+
35+
make
36+
```
37+
38+
## Run
39+
40+
```bash
41+
cp -r assets/ build/
42+
43+
# Run individual component benchmarks
44+
./build/classifier
45+
./build/color
46+
./build/filter
47+
./build/image
48+
./build/transform
49+
50+
# Run all benchmarks with a single command
51+
cmake --build ./build --target run_all_benchmarks
52+
53+
# Pass arguments to all benchmarks
54+
cmake --build . --target run_all_benchmarks -- --benchmark_filter=BM_Boost
55+
56+
# Run with regex filter
57+
./build/transform --benchmark_filter="BM_Rotate"
58+
59+
# List available benchmarks
60+
./build/transform --benchmark_list_tests
61+
```
62+
63+
64+
## Benchmark Categories
65+
66+
- **Classifier**: Detects objects in images.
67+
- **Color**: Converts colors between different color spaces.
68+
- **Image**: Everything related to image processing
69+
- **Filter**: Applies various image filters (e.g. blur, sharpen, edge detection).
70+
- **Transform**: Performs image transformations (e.g. rotation, scaling, cropping, ...)
371

472
## References
573

6-
https://docs.opencv.org/4.x/db/df5/tutorial_linux_gcc_cmake.html
7-
https://docs.opencv.org/4.x/d7/d9f/tutorial_linux_install.html
74+
- https://docs.opencv.org/4.x/db/df5/tutorial_linux_gcc_cmake.html
75+
- https://docs.opencv.org/4.x/d7/d9f/tutorial_linux_install.html
76+
- https://docs.opencv.org/4.x/index.html

src/classifier.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ BENCHMARK_DEFINE_F(ClassifierFixture, BM_ClassifyCat)(benchmark::State& state) {
6161
}
6262
}
6363
BENCHMARK_REGISTER_F(ClassifierFixture, BM_ClassifyCat);
64+
65+
BENCHMARK_MAIN();

src/color.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <benchmark/benchmark.h>
2+
#include <opencv2/opencv.hpp>
3+
#include "fixtures.hpp"
4+
5+
BENCHMARK_DEFINE_F(ImageFixture, BM_ColorConversion)
6+
(benchmark::State& state) {
7+
// Get the color conversion code from the argument
8+
int conversionCode = state.range(0);
9+
10+
for (auto _ : state) {
11+
cv::Mat output;
12+
cv::cvtColor(img, output, conversionCode);
13+
benchmark::DoNotOptimize(output.data);
14+
}
15+
}
16+
17+
BENCHMARK_DEFINE_F(ImageFixture, BM_ColorConversion_MultiStep)
18+
(benchmark::State& state) {
19+
for (auto _ : state) {
20+
cv::Mat temp, output;
21+
cv::cvtColor(img, temp, cv::COLOR_BGR2HSV);
22+
cv::cvtColor(temp, output, cv::COLOR_HSV2RGB);
23+
benchmark::DoNotOptimize(output.data);
24+
}
25+
}
26+
27+
BENCHMARK_DEFINE_F(ImageFixture, BM_ChannelOperations)
28+
(benchmark::State& state) {
29+
for (auto _ : state) {
30+
std::vector<cv::Mat> channels;
31+
cv::split(img, channels);
32+
cv::Mat merged;
33+
cv::merge(channels, merged);
34+
benchmark::DoNotOptimize(merged.data);
35+
}
36+
}
37+
38+
BENCHMARK_DEFINE_F(ImageFixture, BM_ColorConversion_BitDepth)
39+
(benchmark::State& state) {
40+
for (auto _ : state) {
41+
cv::Mat float_img, output;
42+
img.convertTo(float_img, CV_32F, 1.0 / 255.0);
43+
cv::cvtColor(float_img, output, cv::COLOR_BGR2HSV);
44+
benchmark::DoNotOptimize(output.data);
45+
}
46+
}
47+
48+
BENCHMARK_REGISTER_F(ImageFixture, BM_ColorConversion)
49+
->Arg(cv::COLOR_BGR2GRAY) // Test BGR to Grayscale
50+
->Arg(cv::COLOR_BGR2HSV) // Test BGR to HSV
51+
->Arg(cv::COLOR_BGR2Lab) // Test BGR to Lab
52+
->Arg(cv::COLOR_BGR2RGB); // Test BGR to RGB
53+
BENCHMARK_REGISTER_F(ImageFixture, BM_ColorConversion_MultiStep);
54+
BENCHMARK_REGISTER_F(ImageFixture, BM_ChannelOperations);
55+
BENCHMARK_REGISTER_F(ImageFixture, BM_ColorConversion_BitDepth);
56+
57+
BENCHMARK_MAIN();

src/color_conversion.cpp

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/edge_detection.cpp

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)