Benchmark ThreadPool::Submit
| 121 | |
| 122 | // Benchmark ThreadPool::Submit |
| 123 | static void ThreadPoolSubmit(benchmark::State& state) { // NOLINT non-const reference |
| 124 | const auto nthreads = static_cast<int>(state.range(0)); |
| 125 | const auto workload_size = static_cast<int32_t>(state.range(1)); |
| 126 | |
| 127 | Workload workload(workload_size); |
| 128 | |
| 129 | const int32_t nspawns = 10000000 / workload_size + 1; |
| 130 | |
| 131 | for (auto _ : state) { |
| 132 | state.PauseTiming(); |
| 133 | auto pool = *ThreadPool::Make(nthreads); |
| 134 | std::atomic<int32_t> n_finished{0}; |
| 135 | state.ResumeTiming(); |
| 136 | |
| 137 | for (int32_t i = 0; i < nspawns; ++i) { |
| 138 | // Pass the task by reference to avoid copying it around |
| 139 | (void)DeferNotOk(pool->Submit(std::ref(workload))).Then([&]() { |
| 140 | n_finished.fetch_add(1); |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | // Wait for all tasks to finish |
| 145 | ABORT_NOT_OK(pool->Shutdown(true /* wait */)); |
| 146 | ASSERT_EQ(n_finished.load(), nspawns); |
| 147 | state.PauseTiming(); |
| 148 | pool.reset(); |
| 149 | state.ResumeTiming(); |
| 150 | } |
| 151 | state.SetItemsProcessed(state.iterations() * nspawns); |
| 152 | } |
| 153 | |
| 154 | // Benchmark serial TaskGroup |
| 155 | static void SerialTaskGroup(benchmark::State& state) { // NOLINT non-const reference |