|
| 1 | +#include <bind/arangodb/future.hpp> |
| 2 | +#include <semaphore.hpp> |
| 3 | + |
| 4 | +#include <future> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +#include <arangodb/futures/Future.h> |
| 8 | +#include <arangodb/futures/Promise.h> |
| 9 | +#include <arangodb/futures/Try.h> |
| 10 | +#include <arangodb/futures/Utilities.h> |
| 11 | +#include <benchmark/benchmark.h> |
| 12 | + |
| 13 | +namespace bench { |
| 14 | +namespace { |
| 15 | + |
| 16 | +template <typename T> |
| 17 | +T Incr(arangodb::futures::Try<T>&& t) { |
| 18 | + return std::move(t).get() + 1; |
| 19 | +} |
| 20 | + |
| 21 | +arangodb::futures::Future<int> Thens(arangodb::futures::Future<int> f, std::size_t n, |
| 22 | + detail::adb::TestExecutor* executor) { |
| 23 | + for (std::size_t i = 0; i != n; ++i) { |
| 24 | + if (executor != nullptr) { |
| 25 | + arangodb::futures::Promise<int> outer_p; |
| 26 | + auto outer_f = outer_p.getFuture(); |
| 27 | + std::move(f).then([executor, outer_p = std::move(outer_p)](arangodb::futures::Try<int>&& t) mutable { |
| 28 | + executor->add([outer_p = std::move(outer_p), t = std::move(t)]() mutable { |
| 29 | + std::move(outer_p).setValue(Incr<int>(std::move(t))); |
| 30 | + // TODO possible exception ignored now |
| 31 | + }); |
| 32 | + }) /*implicit detach*/; |
| 33 | + f = std::move(outer_f); |
| 34 | + } else { |
| 35 | + f = std::move(f).then /*inline*/ (Incr<int>); |
| 36 | + } |
| 37 | + } |
| 38 | + return f; |
| 39 | +} |
| 40 | + |
| 41 | +} // namespace |
| 42 | +namespace detail::adb { |
| 43 | + |
| 44 | +TestExecutor::TestExecutor(std::size_t num_threads) { |
| 45 | + num_threads = std::max(std::size_t{1}, num_threads); |
| 46 | + _workers.reserve(num_threads); |
| 47 | + for (std::size_t i = 0; i != num_threads; ++i) { |
| 48 | + _workers.emplace_back([this] { |
| 49 | + std::unique_lock lock{_m}; |
| 50 | + while (true) { |
| 51 | + while (!_jobs.empty()) { |
| 52 | + auto work = std::move(_jobs.front()); |
| 53 | + _jobs.pop(); |
| 54 | + lock.unlock(); |
| 55 | + work(); |
| 56 | + lock.lock(); |
| 57 | + } |
| 58 | + if (_stop) { |
| 59 | + return; |
| 60 | + } |
| 61 | + _cv.wait(lock); |
| 62 | + } |
| 63 | + }); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +void TestExecutor::Restart() { |
| 68 | + std::lock_guard lock{_m}; |
| 69 | + _jobs = {}; |
| 70 | +} |
| 71 | + |
| 72 | +void TestExecutor::Join() { |
| 73 | + { |
| 74 | + std::lock_guard lock{_m}; |
| 75 | + _stop = true; |
| 76 | + } |
| 77 | + _cv.notify_all(); |
| 78 | + for (auto& worker : _workers) { |
| 79 | + if (worker.joinable()) { |
| 80 | + worker.join(); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +TestExecutor::~TestExecutor() { |
| 86 | + Join(); |
| 87 | +} |
| 88 | + |
| 89 | +void TestExecutor::add(Job job) { |
| 90 | + { |
| 91 | + std::lock_guard lock{_m}; |
| 92 | + _jobs.push(std::move(job)); |
| 93 | + } |
| 94 | + _cv.notify_one(); |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace detail::adb |
| 98 | + |
| 99 | +void ArangoDB::CreateFuture() { |
| 100 | + std::ignore = arangodb::futures::makeFuture<int>(42); |
| 101 | +} |
| 102 | + |
| 103 | +void ArangoDB::PromiseAndFuture() { |
| 104 | + arangodb::futures::Promise<int> p; |
| 105 | + arangodb::futures::Future<int> f = p.getFuture(); |
| 106 | + std::move(p).setValue(42); |
| 107 | + std::ignore = std::move(f).get(); |
| 108 | +} |
| 109 | + |
| 110 | +detail::adb::TestExecutor* ArangoDB::AcquireExecutor(std::size_t threads) { |
| 111 | + if (threads != 0) { |
| 112 | + return new detail::adb::TestExecutor{threads}; |
| 113 | + } |
| 114 | + return nullptr; |
| 115 | +} |
| 116 | + |
| 117 | +void ArangoDB::ReleaseExecutor(std::size_t threads, detail::adb::TestExecutor* e) { |
| 118 | + if (threads != 0) { |
| 119 | + delete e; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +void ArangoDB::SomeThens(detail::adb::TestExecutor* executor, size_t n, bool no_inline) { |
| 124 | + const bool is_executor = executor != nullptr; |
| 125 | + auto f = arangodb::futures::makeFuture(42); |
| 126 | + f = Thens(std::move(f), n, (is_executor && no_inline ? executor : nullptr)); |
| 127 | + f = Thens(std::move(f), 1, (is_executor ? executor : nullptr)); |
| 128 | + f = Thens(std::move(f), n, (is_executor && no_inline ? executor : nullptr)); |
| 129 | + f.wait(); |
| 130 | +} |
| 131 | + |
| 132 | +void ArangoDB::NoContention(benchmark::State& state) { |
| 133 | + state.PauseTiming(); |
| 134 | + |
| 135 | + std::vector<arangodb::futures::Promise<int>> promises(kContentionIteration); |
| 136 | + std::vector<arangodb::futures::Future<int>> futures; |
| 137 | + futures.reserve(kContentionIteration); |
| 138 | + |
| 139 | + std::promise<void> p_producer; |
| 140 | + auto f_producer = p_producer.get_future(); |
| 141 | + |
| 142 | + for (auto& p : promises) { |
| 143 | + futures.push_back(p.getFuture().then(Incr<int>)); |
| 144 | + } |
| 145 | + |
| 146 | + std::thread producer{[&] { |
| 147 | + p_producer.set_value(); |
| 148 | + for (auto& p : promises) { |
| 149 | + std::move(p).setValue(42); |
| 150 | + } |
| 151 | + }}; |
| 152 | + |
| 153 | + f_producer.wait(); |
| 154 | + |
| 155 | + state.ResumeTiming(); |
| 156 | + |
| 157 | + producer.join(); |
| 158 | +} |
| 159 | + |
| 160 | +void ArangoDB::Contention(benchmark::State& state) { |
| 161 | + state.PauseTiming(); |
| 162 | + |
| 163 | + std::vector<arangodb::futures::Promise<int>> promises(kContentionIteration); |
| 164 | + std::vector<arangodb::futures::Future<int>> futures; |
| 165 | + futures.reserve(kContentionIteration); |
| 166 | + |
| 167 | + for (auto& p : promises) { |
| 168 | + futures.push_back(p.getFuture()); |
| 169 | + } |
| 170 | + |
| 171 | + BusySemaphoreSPSC semaphore; |
| 172 | + std::promise<void> p_consumer; |
| 173 | + auto f_consumer = p_consumer.get_future(); |
| 174 | + std::promise<void> p_producer; |
| 175 | + auto f_producer = p_producer.get_future(); |
| 176 | + |
| 177 | + auto producer = std::thread([&] { |
| 178 | + p_producer.set_value(); |
| 179 | + for (auto& p : promises) { |
| 180 | + semaphore.Release(); |
| 181 | + std::move(p).setValue(42); |
| 182 | + } |
| 183 | + }); |
| 184 | + auto consumer = std::thread([&] { |
| 185 | + p_consumer.set_value(); |
| 186 | + for (auto& f : futures) { |
| 187 | + semaphore.Acquire(); |
| 188 | + f = std::move(f).then(Incr<int>); |
| 189 | + } |
| 190 | + }); |
| 191 | + |
| 192 | + f_consumer.wait(); |
| 193 | + f_producer.wait(); |
| 194 | + |
| 195 | + state.ResumeTiming(); |
| 196 | + |
| 197 | + producer.join(); |
| 198 | + consumer.join(); |
| 199 | +} |
| 200 | + |
| 201 | +} // namespace bench |
0 commit comments