Merge pull request #1 from linuxnyasha/initialisation_benchmarks

Add Benchmarks
This commit is contained in:
sha512sum 2024-04-09 13:11:46 +00:00 committed by GitHub
commit 8589e0ffcc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 111 additions and 8 deletions

98
benchmarks/init.cpp Normal file
View file

@ -0,0 +1,98 @@
#include <benchmark/benchmark.h>
#include <cserver/engine/components.hpp>
struct SomeComponent1 {
static constexpr utempl::ConstexprString kName = "1";
constexpr SomeComponent1(auto, auto& context) {
context.template FindComponent<cserver::kBasicTaskProcessorName>();
};
};
struct SomeComponent2 {
static constexpr utempl::ConstexprString kName = "2";
constexpr SomeComponent2(auto, auto& context) {
context.template FindComponent<cserver::kBasicTaskProcessorName>();
};
};
struct SomeComponent3 {
static constexpr utempl::ConstexprString kName = "3";
constexpr SomeComponent3(auto, auto& context) {
context.template FindComponent<cserver::kBasicTaskProcessorName>();
};
};
struct SomeComponent4 {
static constexpr utempl::ConstexprString kName = "4";
constexpr SomeComponent4(auto, auto& context) {
context.template FindComponent<cserver::kBasicTaskProcessorName>();
};
};
struct SomeComponent5 {
static constexpr utempl::ConstexprString kName = "5";
constexpr SomeComponent5(auto, auto& context) {
context.template FindComponent<cserver::kBasicTaskProcessorName>();
};
};
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<SomeComponent1>()
.Append<SomeComponent2>()
.Append<SomeComponent3>()
.Append<SomeComponent4>()
.Append<SomeComponent5>()
.Append<SomeComponent6>()
.Append<SomeComponent7>()
.Append<SomeComponent8>()
.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();

View file

@ -15,6 +15,7 @@ struct TaskProcessor {
}; };
inline constexpr ~TaskProcessor() { inline constexpr ~TaskProcessor() {
this->ioContext.stop();
for(auto& thread : this->pool) { for(auto& thread : this->pool) {
thread.join(); thread.join();
}; };

View file

@ -82,6 +82,7 @@ struct ServiceContext {
}(std::index_sequence_for<Ts...>()) }(std::index_sequence_for<Ts...>())
} {}; } {};
inline constexpr auto Run() { inline constexpr auto Run() {
this->taskProcessor.Run();
[&]<auto... Is>(std::index_sequence<Is...>) { [&]<auto... Is>(std::index_sequence<Is...>) {
([](auto& component){ ([](auto& component){
if constexpr(requires{component.Run();}) { if constexpr(requires{component.Run();}) {
@ -399,16 +400,19 @@ struct ServiceContextBuilder {
}(std::index_sequence_for<ComponentConfigs...>{}); }(std::index_sequence_for<ComponentConfigs...>{});
}; };
static constexpr auto GetServiceContext() {
return []<utempl::ConstexprString... names, typename... TTs, Options... Options>
(utempl::TypeList<ComponentConfig<names, TTs, Options>...>) {
return ServiceContext<config, utempl::Tuple{names...}, utempl::Tuple{Options...}, TTs...>{};
}(utempl::TypeList<ComponentConfigs...>{});
};
static constexpr auto Run() -> void { static constexpr auto Run() -> void {
[]<utempl::ConstexprString... names, typename... TTs, Options... Options> static auto context = GetServiceContext();
(utempl::TypeList<ComponentConfig<names, TTs, Options>...>) {
static ServiceContext<config, utempl::Tuple{names...}, utempl::Tuple{Options...}, TTs...> context;
context.Run(); context.Run();
for(;;) { for(;;) {
std::this_thread::sleep_for(std::chrono::minutes(1)); std::this_thread::sleep_for(std::chrono::minutes(1));
}; };
}(utempl::TypeList<ComponentConfigs...>{});
}; };
}; };
} // namespace cserver } // namespace cserver