85 lines
2.9 KiB
C++
85 lines
2.9 KiB
C++
#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);
|
|
}
|