diff --git a/benchmarks/init.cpp b/benchmarks/init.cpp new file mode 100644 index 0000000..9b29413 --- /dev/null +++ b/benchmarks/init.cpp @@ -0,0 +1,98 @@ +#include +#include + +struct SomeComponent1 { + static constexpr utempl::ConstexprString kName = "1"; + constexpr SomeComponent1(auto, auto& context) { + context.template FindComponent(); + }; +}; + + +struct SomeComponent2 { + static constexpr utempl::ConstexprString kName = "2"; + constexpr SomeComponent2(auto, auto& context) { + context.template FindComponent(); + }; +}; + + +struct SomeComponent3 { + static constexpr utempl::ConstexprString kName = "3"; + constexpr SomeComponent3(auto, auto& context) { + context.template FindComponent(); + }; +}; + + +struct SomeComponent4 { + static constexpr utempl::ConstexprString kName = "4"; + constexpr SomeComponent4(auto, auto& context) { + context.template FindComponent(); + }; +}; + + +struct SomeComponent5 { + static constexpr utempl::ConstexprString kName = "5"; + constexpr SomeComponent5(auto, auto& context) { + context.template FindComponent(); + }; +}; + + +struct SomeComponent6 { + SomeComponent1& first; + SomeComponent2& second; + static constexpr utempl::ConstexprString kName = "6"; + constexpr SomeComponent6(auto, auto& context) : + first(context.template FindComponent<"1">()), + second(context.template FindComponent<"2">()) {}; +}; + +struct SomeComponent7 { + SomeComponent4& first; + SomeComponent6& second; + static constexpr utempl::ConstexprString kName = "6"; + constexpr SomeComponent7(auto, auto& context) : + first(context.template FindComponent<"4">()), + second(context.template FindComponent<"6">()) {}; +}; + + +struct SomeComponent8 { + SomeComponent3& first; + SomeComponent5& second; + static constexpr utempl::ConstexprString kName = "6"; + constexpr SomeComponent8(auto, auto& context) : + first(context.template FindComponent<"3">()), + second(context.template FindComponent<"5">()) {}; +}; + + +auto BMInitialise(benchmark::State& state) { + using Builder = decltype(cserver::ServiceContextBuilder{} + .AppendConfigParam<"threads", 8>() + .Append() + .Append() + .Append() + .Append() + .Append() + .Append() + .Append() + .Append() + .Sort()); + using ServiceContextType = decltype(Builder::GetServiceContext()); + alignas(ServiceContextType) std::byte context[sizeof(ServiceContextType)]; + for(auto _ : state) { + auto* ptr = new (context) ServiceContextType{}; + ptr->Run(); + state.PauseTiming(); + ptr->~ServiceContextType(); + state.ResumeTiming(); + }; +}; + +BENCHMARK(BMInitialise); + +BENCHMARK_MAIN(); diff --git a/include/cserver/engine/basic/task_processor.hpp b/include/cserver/engine/basic/task_processor.hpp index 3e7042a..d67d96d 100644 --- a/include/cserver/engine/basic/task_processor.hpp +++ b/include/cserver/engine/basic/task_processor.hpp @@ -15,6 +15,7 @@ struct TaskProcessor { }; inline constexpr ~TaskProcessor() { + this->ioContext.stop(); for(auto& thread : this->pool) { thread.join(); }; diff --git a/include/cserver/engine/components.hpp b/include/cserver/engine/components.hpp index 4e7ac3b..9bc3c9a 100644 --- a/include/cserver/engine/components.hpp +++ b/include/cserver/engine/components.hpp @@ -82,6 +82,7 @@ struct ServiceContext { }(std::index_sequence_for()) } {}; inline constexpr auto Run() { + this->taskProcessor.Run(); [&](std::index_sequence) { ([](auto& component){ if constexpr(requires{component.Run();}) { @@ -399,16 +400,19 @@ struct ServiceContextBuilder { }(std::index_sequence_for{}); }; + static constexpr auto GetServiceContext() { + return [] + (utempl::TypeList...>) { + return ServiceContext{}; + }(utempl::TypeList{}); + }; static constexpr auto Run() -> void { - [] - (utempl::TypeList...>) { - static ServiceContext context; - context.Run(); - for(;;) { - std::this_thread::sleep_for(std::chrono::minutes(1)); - }; - }(utempl::TypeList{}); + static auto context = GetServiceContext(); + context.Run(); + for(;;) { + std::this_thread::sleep_for(std::chrono::minutes(1)); + }; }; }; } // namespace cserver