Update init benchmark code

This commit is contained in:
sha512sum 2024-04-10 16:45:29 +00:00 committed by sha512sum
parent 9f48938386
commit 16f66321a8
2 changed files with 13 additions and 5 deletions

View file

@ -87,6 +87,7 @@ auto BMInitialise(benchmark::State& state) {
for(auto _ : state) { for(auto _ : state) {
auto* ptr = new (context) ServiceContextType{}; auto* ptr = new (context) ServiceContextType{};
ptr->Run(); ptr->Run();
ptr->FindComponent<cserver::kBasicTaskProcessorName>().ioContext.run();
state.PauseTiming(); state.PauseTiming();
ptr->~ServiceContextType(); ptr->~ServiceContextType();
state.ResumeTiming(); state.ResumeTiming();

View file

@ -1,13 +1,14 @@
#pragma once #pragma once
#include <utempl/constexpr_string.hpp> #include <utempl/constexpr_string.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <thread>
namespace cserver::engine::basic { namespace cserver::engine::basic {
template <std::size_t Size = 0> template <std::size_t Size = 0>
struct TaskProcessor { struct TaskProcessor {
boost::asio::io_context ioContext; boost::asio::io_context ioContext;
std::array<std::thread, Size> pool; std::array<std::optional<std::thread>, Size> pool;
static constexpr utempl::ConstexprString kName = "basicTaskProcessor"; static constexpr utempl::ConstexprString kName = "basicTaskProcessor";
inline constexpr TaskProcessor(auto, auto&) : inline constexpr TaskProcessor(auto, auto&) :
ioContext{}, ioContext{},
@ -15,16 +16,22 @@ struct TaskProcessor {
}; };
inline constexpr ~TaskProcessor() { inline constexpr ~TaskProcessor() {
this->ioContext.stop();
for(auto& thread : this->pool) { for(auto& thread : this->pool) {
thread.join(); if(thread && thread->joinable()) {
thread->join();
};
}; };
}; };
inline auto Run() { inline auto Run() {
for(auto& thread : this->pool) { for(auto& thread : this->pool) {
thread = std::thread([&]{ if(this->ioContext.stopped()) {
boost::asio::executor_work_guard<decltype(this->ioContext.get_executor())> guard{this->ioContext.get_executor()}; return;
};
thread = std::jthread([&]{
if(this->ioContext.stopped()) {
return;
};
this->ioContext.run(); this->ioContext.run();
}); });
}; };