added benchmark test suite
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
CPMAddPackage("gh:catchorg/Catch2@3.14.0")
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
|
||||
|
||||
add_executable(Benchmarks
|
||||
sql/StatementCacheBenchmark.cpp
|
||||
../orm/backend/test_backend_service.cpp
|
||||
../orm/backend/test_backend_service.hpp
|
||||
../orm/backend/test_connection.cpp
|
||||
../orm/backend/test_connection.hpp
|
||||
../orm/backend/test_parameter_binder.cpp
|
||||
../orm/backend/test_parameter_binder.hpp
|
||||
../orm/backend/test_result_reader.cpp
|
||||
../orm/backend/test_result_reader.hpp
|
||||
../orm/backend/test_statement.cpp
|
||||
../orm/backend/test_statement.hpp
|
||||
../utils/MetricsObserver.hpp
|
||||
../utils/MetricsObserver.cpp
|
||||
../utils/RecordingObserver.hpp
|
||||
../utils/RecordingObserver.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(Benchmarks matador-orm matador-core Catch2::Catch2WithMain)
|
||||
|
||||
target_include_directories(Benchmarks
|
||||
PUBLIC
|
||||
PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>/test
|
||||
)
|
||||
|
||||
#if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
||||
# target_compile_options(CoreTests PRIVATE -coverage)
|
||||
# target_link_options(CoreTests PRIVATE -coverage)
|
||||
#endif ()
|
||||
|
||||
add_test(NAME Benchmarks COMMAND Benchmarks)
|
||||
@@ -0,0 +1,84 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/connection_pool.hpp"
|
||||
#include "matador/sql/interface/connection_impl.hpp"
|
||||
|
||||
#include "../test/orm/backend/test_backend_service.hpp"
|
||||
|
||||
#include "utils/MetricsObserver.hpp"
|
||||
#include "utils/RecordingObserver.hpp"
|
||||
|
||||
#include <random>
|
||||
|
||||
using namespace matador::test;
|
||||
using namespace matador::sql;
|
||||
using namespace matador::utils;
|
||||
|
||||
TEST_CASE("Multithreaded stress test", "[statement][cache][stress]") {
|
||||
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
||||
|
||||
constexpr int thread_count = 16;
|
||||
constexpr int iterations = 1000;
|
||||
constexpr int sql_pool_size = 10;
|
||||
|
||||
std::vector<query_context> contexts;
|
||||
for (int i = 0; i < sql_pool_size; ++i) {
|
||||
const auto sql = "SELECT " + std::to_string(i);
|
||||
contexts.push_back({sql, std::hash<std::string>{}(sql)});
|
||||
}
|
||||
|
||||
connection_pool pool("noop://noop.db", 4);
|
||||
message_bus bus;
|
||||
statement_cache cache(bus, pool, 5);
|
||||
RecordingObserver observer(bus);
|
||||
MetricsObserver metrics(bus);
|
||||
|
||||
auto start_time = std::chrono::steady_clock::now();
|
||||
|
||||
std::atomic_int lock_failed_count{0};
|
||||
std::atomic_int exec_failed_count{0};
|
||||
|
||||
auto worker = [&](const int tid) {
|
||||
std::mt19937 rng(tid);
|
||||
std::uniform_int_distribution dist(0, sql_pool_size - 1);
|
||||
|
||||
for (int i = 0; i < iterations; ++i) {
|
||||
const auto& ctx = contexts[dist(rng)];
|
||||
if (const auto result = cache.acquire(ctx); !result) {
|
||||
FAIL("Failed to acquire statement");
|
||||
} else {
|
||||
if (const auto exec_result = result->execute(); !exec_result) {
|
||||
if (exec_result.err().ec() == error_code::StatementLocked) {
|
||||
++lock_failed_count;
|
||||
} else {
|
||||
++exec_failed_count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<std::thread> threads;
|
||||
for (int i = 0; i < thread_count; ++i) {
|
||||
threads.emplace_back(worker, i);
|
||||
}
|
||||
for (auto& t : threads) {
|
||||
t.join();
|
||||
}
|
||||
const auto end_time = std::chrono::steady_clock::now();
|
||||
const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
|
||||
|
||||
std::cout << "Executed " << (thread_count * iterations) << " statements in " << duration.count() << " ms (lock failed: " << lock_failed_count << ", execute failed: " << exec_failed_count << ")\n";
|
||||
std::cout << "Average lock wait time: " << metrics.get_average_lock_wait_time().count() << "ms\n";
|
||||
std::cout << "Total lock wait time: " << metrics.get_total_lock_wait_time().count() << "ms\n";
|
||||
std::cout << "Average execution time: " << metrics.get_average_execution_time().count() << "ms\n";
|
||||
std::cout << "Total execution time: " << metrics.get_total_execution_time().count() << "ms\n";
|
||||
std::cout << "Number of lock failures: " << metrics.get_lock_failure_count() << "\n";
|
||||
|
||||
// Some events should be generated
|
||||
int accessed = 0;
|
||||
while (auto e = observer.poll()) {
|
||||
if (e->is<statement_accessed_event>()) accessed++;
|
||||
}
|
||||
REQUIRE(accessed > 0);
|
||||
}
|
||||
Reference in New Issue
Block a user