blob: 93528bfac06d4940002ed0df6fb2d233ab7a490a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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);
|