| 25 | #include <vector> |
| 26 | |
| 27 | void SpinLock_diff_spin_count_bench(benchmark::State& state) { |
| 28 | async_simple::executors::SimpleExecutor executor(50); |
| 29 | auto spin_count = state.range(0); |
| 30 | async_simple::coro::SpinLock spinlock(spin_count); |
| 31 | |
| 32 | int value = 0; |
| 33 | int loop_num{200}; |
| 34 | std::vector<async_simple::coro::RescheduleLazy<void>> input; |
| 35 | input.reserve(loop_num); |
| 36 | |
| 37 | auto test = [&]() -> async_simple::coro::Lazy<void> { |
| 38 | auto writer = [&]() -> async_simple::coro::Lazy<void> { |
| 39 | co_await spinlock.coLock(); |
| 40 | value++; |
| 41 | spinlock.unlock(); |
| 42 | co_return; |
| 43 | }; |
| 44 | for (int i = 0; i < loop_num; i++) |
| 45 | input.emplace_back(writer().via(&executor)); |
| 46 | auto combined_lazy = collectAll(std::move(input)); |
| 47 | auto out = co_await std::move(combined_lazy); |
| 48 | input.clear(); |
| 49 | }; |
| 50 | for ([[maybe_unused]] const auto& _ : state) |
| 51 | async_simple::coro::syncAwait(test()); |
| 52 | } |
| 53 | |
| 54 | void SpinLock_thread_lock_bench(benchmark::State& state) { |
| 55 | async_simple::executors::SimpleExecutor executor(50); |