Skip to content

Commit a3efc57

Browse files
committed
Add benchmark
1 parent 0c1a893 commit a3efc57

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

.github/workflows/main.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
- uses: actions/checkout@v3
4848
- name: Install dependencies
4949
run: |
50-
sudo apt install ninja-build
50+
sudo apt install ninja-build libbenchmark-dev
5151
if [ "${{ matrix.cxxlib }}" = "libc++" ]; then
5252
sudo apt remove -y 'libc++*'
5353
apt search libunwind
@@ -71,7 +71,7 @@ jobs:
7171
export EMBEDDED_BLOCKS_RUNTIME=ON
7272
fi
7373
ls -lahR /usr/lib/llvm-${{ matrix.llvm-version }}/lib/
74-
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{matrix.build-type}} -G Ninja -DTESTS=ON -DEMBEDDED_BLOCKS_RUNTIME=$EMBEDDED_BLOCKS_RUNTIME -DCMAKE_C_COMPILER=clang-${{matrix.llvm-version}} -DCMAKE_OBJC_COMPILER=clang-${{matrix.llvm-version}} -DCMAKE_ASM_COMPILER=clang-${{matrix.llvm-version}} -DCMAKE_CXX_COMPILER=clang++-${{matrix.llvm-version}} -DCMAKE_OBJCXX_COMPILER=clang++-${{matrix.llvm-version}} -DCMAKE_CXX_FLAGS="-stdlib=${{matrix.cxxlib}}"
74+
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{matrix.build-type}} -G Ninja -DTESTS=ON -DBENCHMARKS=ON -DEMBEDDED_BLOCKS_RUNTIME=$EMBEDDED_BLOCKS_RUNTIME -DCMAKE_C_COMPILER=clang-${{matrix.llvm-version}} -DCMAKE_OBJC_COMPILER=clang-${{matrix.llvm-version}} -DCMAKE_ASM_COMPILER=clang-${{matrix.llvm-version}} -DCMAKE_CXX_COMPILER=clang++-${{matrix.llvm-version}} -DCMAKE_OBJCXX_COMPILER=clang++-${{matrix.llvm-version}} -DCMAKE_CXX_FLAGS="-stdlib=${{matrix.cxxlib}}"
7575
# Build with a nice ninja status line
7676
- name: Build
7777
working-directory: ${{github.workspace}}/build
@@ -81,6 +81,10 @@ jobs:
8181
working-directory: ${{github.workspace}}/build
8282
run: |
8383
ctest --output-on-failure -j 4
84+
- name: Benchmark
85+
working-directory: ${{github.workspace}}/build
86+
run: |
87+
./benchmark/benchmark_objc_msgSend
8488
8589
qemu-crossbuild:
8690
strategy:
@@ -237,12 +241,12 @@ jobs:
237241
with:
238242
msystem: ${{ matrix.msystem }}
239243
update: true
240-
install: git mingw-w64-${{ matrix.package-prefix }}-clang mingw-w64-${{ matrix.package-prefix }}-lld mingw-w64-${{ matrix.package-prefix }}-cmake
244+
install: git mingw-w64-${{ matrix.package-prefix }}-clang mingw-w64-${{ matrix.package-prefix }}-lld mingw-w64-${{ matrix.package-prefix }}-cmake mingw-w64-${{ matrix.package-prefix }}-benchmark
241245
- name: Configure CMake
242246
run: |
243247
mkdir build
244248
cd build
245-
${{ matrix.cmake-flags }} cmake .. -DTESTS=ON -DCMAKE_C_COMPILER="clang" -DCMAKE_CXX_COMPILER="clang++" -DCMAKE_BUILD_TYPE=${{ matrix.build-type }} -DSTRICT_APPLE_COMPATIBILITY=${{ matrix.strict-apple-compatibility }}
249+
${{ matrix.cmake-flags }} cmake .. -DTESTS=ON -DBENCHMARKS=ON -DCMAKE_C_COMPILER="clang" -DCMAKE_CXX_COMPILER="clang++" -DCMAKE_BUILD_TYPE=${{ matrix.build-type }} -DSTRICT_APPLE_COMPATIBILITY=${{ matrix.strict-apple-compatibility }}
246250
- name: Build
247251
working-directory: build
248252
run: |
@@ -251,6 +255,10 @@ jobs:
251255
working-directory: build
252256
run: |
253257
ctest -j 4 --output-on-failure -T test ${{ matrix.ctest-flags }}
258+
- name: Benchmark
259+
working-directory: build
260+
run: |
261+
./benchmarks/benchmark_objc_msgSend.exe
254262
- name: Install
255263
working-directory: build
256264
run: |

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ option(DEBUG_ARC_COMPAT
140140
"Log warnings for classes that don't hit ARC fast paths" OFF)
141141
option(ENABLE_OBJCXX "Enable support for Objective-C++" ON)
142142
option(TESTS "Enable building the tests")
143+
option(BENCHMARKS "Enable building the benchmarks" OFF)
143144
option(EMBEDDED_BLOCKS_RUNTIME "Include an embedded blocks runtime, rather than relying on libBlocksRuntime to supply it" ON)
144145
option(STRICT_APPLE_COMPATIBILITY "Use strict Apple compatibility, always defining BOOL as signed char" OFF)
145146

@@ -448,6 +449,10 @@ if (TESTS)
448449
add_subdirectory(Test)
449450
endif (TESTS)
450451

452+
if (BENCHMARKS)
453+
add_subdirectory(benchmarks)
454+
endif()
455+
451456
CHECK_CXX_SOURCE_COMPILES("
452457
#include <stdlib.h>
453458
extern \"C\" {

benchmarks/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
find_package(benchmark REQUIRED)
2+
3+
add_executable(benchmark_objc_msgSend objc_msgSend.cpp TestClass.m)
4+
5+
target_compile_options(benchmark_objc_msgSend PRIVATE "-fobjc-runtime=gnustep-2.0")
6+
target_link_libraries(benchmark_objc_msgSend benchmark::benchmark objc)

benchmarks/TestClass.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@interface TestClass
2+
- (int)answer;
3+
@end
4+
5+
@implementation TestClass
6+
- (int)answer
7+
{
8+
return 42;
9+
}
10+
@end

benchmarks/objc_msgSend.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <benchmark/benchmark.h>
2+
#include "../objc/runtime.h"
3+
#include <assert.h>
4+
5+
static id testClass;
6+
static SEL answerSel;
7+
8+
static void objc_msgSendSetup(const benchmark::State& state) {
9+
testClass = objc_getClass("TestClass");
10+
answerSel = sel_registerName("answer");
11+
}
12+
13+
static void objc_msgSend(benchmark::State& state) {
14+
for (auto _ : state)
15+
{
16+
id a = objc_msgSend(testClass, answerSel);
17+
assert((uintptr_t)a == 42);
18+
}
19+
}
20+
// Register the function as a benchmark
21+
BENCHMARK(objc_msgSend)->Setup(objc_msgSendSetup)->Repetitions(25);
22+
23+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)