Benchmark ThreadPool::Spawn
| 75 | |
| 76 | // Benchmark ThreadPool::Spawn |
| 77 | static void ThreadPoolSpawn(benchmark::State& state) { // NOLINT non-const reference |
| 78 | const auto nthreads = static_cast<int>(state.range(0)); |
| 79 | const auto workload_size = static_cast<int32_t>(state.range(1)); |
| 80 | |
| 81 | Workload workload(workload_size); |
| 82 | |
| 83 | // Spawn enough tasks to make the pool start up overhead negligible |
| 84 | const int32_t nspawns = 200000000 / workload_size + 1; |
| 85 | |
| 86 | for (auto _ : state) { |
| 87 | state.PauseTiming(); |
| 88 | std::shared_ptr<ThreadPool> pool; |
| 89 | pool = *ThreadPool::Make(nthreads); |
| 90 | state.ResumeTiming(); |
| 91 | |
| 92 | for (int32_t i = 0; i < nspawns; ++i) { |
| 93 | // Pass the task by reference to avoid copying it around |
| 94 | ABORT_NOT_OK(pool->Spawn(std::ref(workload))); |
| 95 | } |
| 96 | |
| 97 | // Wait for all tasks to finish |
| 98 | ABORT_NOT_OK(pool->Shutdown(true /* wait */)); |
| 99 | state.PauseTiming(); |
| 100 | pool.reset(); |
| 101 | state.ResumeTiming(); |
| 102 | } |
| 103 | state.SetItemsProcessed(state.iterations() * nspawns); |
| 104 | } |
| 105 | |
| 106 | // Benchmark SerialExecutor::RunInSerialExecutor |
| 107 | static void RunInSerialExecutor(benchmark::State& state) { // NOLINT non-const reference |