diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2024-07-29 14:55:30 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2024-07-29 15:09:48 +0300 |
commit | 80adf9ab238b01ed48926dca38bf7eefd39c823d (patch) | |
tree | 0565154984fed73dc32aaf76c402f81c54ac46f6 /library/cpp/threading/future/benchmark/coroutine_traits.cpp | |
parent | 5d639bd83a0f3642a1c2b605bcac23a25a071920 (diff) | |
download | ydb-80adf9ab238b01ed48926dca38bf7eefd39c823d.tar.gz |
Intermediate changes
Diffstat (limited to 'library/cpp/threading/future/benchmark/coroutine_traits.cpp')
-rw-r--r-- | library/cpp/threading/future/benchmark/coroutine_traits.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/library/cpp/threading/future/benchmark/coroutine_traits.cpp b/library/cpp/threading/future/benchmark/coroutine_traits.cpp new file mode 100644 index 0000000000..93528bfac0 --- /dev/null +++ b/library/cpp/threading/future/benchmark/coroutine_traits.cpp @@ -0,0 +1,82 @@ +#include <library/cpp/threading/future/future.h> +#include <library/cpp/threading/future/core/coroutine_traits.h> + +#include <benchmark/benchmark.h> + +class TContext { +public: + TContext() + : NextInputPromise_(NThreading::NewPromise<bool>()) + {} + ~TContext() { + UpdateNextInput(false); + } + + NThreading::TFuture<bool> NextInput() { + return NextInputPromise_.GetFuture(); + } + + void UpdateNextInput(bool hasInput = true) { + auto prevNextInputPromise = NextInputPromise_; + NextInputPromise_ = NThreading::NewPromise<bool>(); + prevNextInputPromise.SetValue(hasInput); + } + +private: + NThreading::TPromise<bool> NextInputPromise_; +}; + +static void TestPureFutureChainSubscribe(benchmark::State& state) { + TContext context; + size_t cnt = 0; + std::function<void(const NThreading::TFuture<bool>&)> processInput = [&context, &cnt, &processInput](const NThreading::TFuture<bool>& hasInput) { + if (hasInput.GetValue()) { + benchmark::DoNotOptimize(++cnt); + context.NextInput().Subscribe(processInput); + } + }; + + processInput(NThreading::MakeFuture<bool>(true)); + for (auto _ : state) { + context.UpdateNextInput(); + } + context.UpdateNextInput(false); +} + +static void TestPureFutureChainApply(benchmark::State& state) { + TContext context; + size_t cnt = 0; + std::function<void(const NThreading::TFuture<bool>&)> processInput = [&context, &cnt, &processInput](const NThreading::TFuture<bool>& hasInput) { + if (hasInput.GetValue()) { + benchmark::DoNotOptimize(++cnt); + context.NextInput().Apply(processInput); + } + }; + + processInput(NThreading::MakeFuture<bool>(true)); + for (auto _ : state) { + context.UpdateNextInput(); + } + context.UpdateNextInput(false); +} + +static void TestCoroFutureChain(benchmark::State& state) { + TContext context; + size_t cnt = 0; + auto coroutine = [&context, &cnt]() -> NThreading::TFuture<void> { + while (co_await context.NextInput()) { + benchmark::DoNotOptimize(++cnt); + } + }; + + auto coroutineFuture = coroutine(); + for (auto _ : state) { + context.UpdateNextInput(); + } + context.UpdateNextInput(false); + coroutineFuture.GetValueSync(); +} + +BENCHMARK(TestPureFutureChainSubscribe); +BENCHMARK(TestPureFutureChainApply); +BENCHMARK(TestCoroFutureChain); |