Skip to content

Commit b626e4c

Browse files
committed
Add simple runtime benchmarks
For now we add just two binaries, one with assertions taking the fast path, one with assertions taking the slow path, and the ability to run 1 of `REQUIRE(true)`, `REQUIRE_NOTHROW`, `REQUIRE_THROWS` in a loop. I also split off a CMake preset which enables more tests than the basic `simple-tests` preset, but does not enable the most expensive tests which force recompilation of Catch2 multiple times.
1 parent 434bf55 commit b626e4c

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ cmake_dependent_option(CATCH_BUILD_TESTING "Build the SelfTest project" ON "CATC
2020
cmake_dependent_option(CATCH_BUILD_EXAMPLES "Build code examples" OFF "CATCH_DEVELOPMENT_BUILD" OFF)
2121
cmake_dependent_option(CATCH_BUILD_EXTRA_TESTS "Build extra tests" OFF "CATCH_DEVELOPMENT_BUILD" OFF)
2222
cmake_dependent_option(CATCH_BUILD_FUZZERS "Build fuzzers" OFF "CATCH_DEVELOPMENT_BUILD" OFF)
23+
cmake_dependent_option(CATCH_BUILD_BENCHMARKS "Build the benchmarks" OFF "CATCH_DEVELOPMENT_BUILD" OFF)
2324
cmake_dependent_option(CATCH_ENABLE_COVERAGE "Generate coverage for codecov.io" OFF "CATCH_DEVELOPMENT_BUILD" OFF)
2425
cmake_dependent_option(CATCH_ENABLE_WERROR "Enables Werror during build" ON "CATCH_DEVELOPMENT_BUILD" OFF)
2526
cmake_dependent_option(CATCH_BUILD_SURROGATES "Enable generating and building surrogate TUs for the main headers" OFF "CATCH_DEVELOPMENT_BUILD" OFF)
@@ -77,6 +78,11 @@ set(SELF_TEST_DIR ${CATCH_DIR}/tests/SelfTest)
7778
# We need to bring-in the variables defined there to this scope
7879
add_subdirectory(src)
7980

81+
if (CATCH_BUILD_BENCHMARKS)
82+
set(CMAKE_FOLDER "benchmarks")
83+
add_subdirectory(benchmarks)
84+
endif()
85+
8086
# Build tests only if requested
8187
if(BUILD_TESTING AND CATCH_BUILD_TESTING AND NOT_SUBPROJECT)
8288
find_package(Python3 REQUIRED COMPONENTS Interpreter)

CMakePresets.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,23 @@
1515
}
1616
},
1717
{
18-
"name": "all-tests",
18+
"name": "most-tests",
1919
"inherits": "basic-tests",
2020
"displayName": "Full development build",
21-
"description": "Enables development build with examples and ALL tests",
21+
"description": "Enables development build with extended set of tests (still relatively cheap to build)",
2222
"cacheVariables": {
2323
"CATCH_BUILD_EXAMPLES": "ON",
2424
"CATCH_BUILD_EXTRA_TESTS": "ON",
2525
"CATCH_BUILD_SURROGATES": "ON",
26+
"CATCH_BUILD_BENCHMARKS": "ON"
27+
}
28+
},
29+
{
30+
"name": "all-tests",
31+
"inherits": "most-tests",
32+
"displayName": "Full development build",
33+
"description": "Enables development build with examples and ALL tests",
34+
"cacheVariables": {
2635
"CATCH_ENABLE_CONFIGURE_TESTS": "ON",
2736
"CATCH_ENABLE_CMAKE_HELPER_TESTS": "ON"
2837
}

benchmarks/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
include(CatchMiscFunctions)
2+
3+
add_executable(AssertionsFastPath
4+
runtime_assertion_benches.cpp
5+
)
6+
7+
add_executable(AssertionsSlowPath
8+
runtime_assertion_benches.cpp
9+
assertion_listener.cpp
10+
)
11+
12+
target_link_libraries(AssertionsFastPath PRIVATE Catch2::Catch2WithMain)
13+
target_link_libraries(AssertionsSlowPath PRIVATE Catch2::Catch2WithMain)
14+
15+
list(APPEND CATCH_TEST_TARGETS AssertionsFastPath AssertionsSlowPath)
16+
set(CATCH_TEST_TARGETS ${CATCH_TEST_TARGETS} PARENT_SCOPE)

benchmarks/assertion_listener.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
// Copyright Catch2 Authors
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE.txt or copy at
5+
// https://www.boost.org/LICENSE_1_0.txt)
6+
7+
// SPDX-License-Identifier: BSL-1.0
8+
9+
#include <catch2/reporters/catch_reporter_event_listener.hpp>
10+
#include <catch2/reporters/catch_reporter_registrars.hpp>
11+
12+
/**
13+
* Event listener that listens to all assertions, forcing assertion slow path
14+
*/
15+
class AssertionSlowPathListener : public Catch::EventListenerBase {
16+
public:
17+
static std::string getDescription() {
18+
return "Validates ordering of Catch2's listener events";
19+
}
20+
21+
AssertionSlowPathListener(Catch::IConfig const* config) :
22+
EventListenerBase(config) {
23+
m_preferences.shouldReportAllAssertions = true;
24+
m_preferences.shouldReportAllAssertionStarts = true;
25+
}
26+
};
27+
28+
CATCH_REGISTER_LISTENER( AssertionSlowPathListener )
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
// Copyright Catch2 Authors
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE.txt or copy at
5+
// https://www.boost.org/LICENSE_1_0.txt)
6+
7+
// SPDX-License-Identifier: BSL-1.0
8+
9+
#include <catch2/catch_test_macros.hpp>
10+
11+
TEST_CASE("Simple REQUIRE - 10M") {
12+
for (size_t i = 0; i < 10'000'000; ++i) {
13+
REQUIRE(true);
14+
}
15+
}
16+
17+
TEST_CASE("Simple NOTHROW - 10M") {
18+
for (size_t i = 0; i < 10'000'000; ++i) {
19+
REQUIRE_NOTHROW([](){}());
20+
}
21+
}
22+
23+
TEST_CASE("Simple THROWS - 10M") {
24+
for (size_t i = 0; i < 10'000'000; ++i) {
25+
REQUIRE_THROWS([]() { throw 1; }());
26+
}
27+
}

0 commit comments

Comments
 (0)