|
| 1 | +#include <benchmark/benchmark.h> |
| 2 | +#include "threadsafe_pool_allocator.h" |
| 3 | +#include <vector> |
| 4 | +#include <thread> |
| 5 | + |
| 6 | +using namespace fast_alloc; |
| 7 | + |
| 8 | +static void BM_ThreadSafePoolAllocator_SingleThread(benchmark::State& state) |
| 9 | +{ |
| 10 | + constexpr std::size_t block_size = 64; |
| 11 | + constexpr std::size_t block_count = 10000; |
| 12 | + ThreadSafePoolAllocator pool(block_size, block_count); |
| 13 | + |
| 14 | + for (auto _ : state) |
| 15 | + { |
| 16 | + void* ptr = pool.allocate(); |
| 17 | + benchmark::DoNotOptimize(ptr); |
| 18 | + pool.deallocate(ptr); |
| 19 | + } |
| 20 | + |
| 21 | + state.SetItemsProcessed(state.iterations()); |
| 22 | +} |
| 23 | + |
| 24 | +BENCHMARK(BM_ThreadSafePoolAllocator_SingleThread); |
| 25 | + |
| 26 | +static void BM_ThreadSafePoolAllocator_MultiThread(benchmark::State& state) |
| 27 | +{ |
| 28 | + constexpr std::size_t block_size = 64; |
| 29 | + constexpr std::size_t block_count = 10000; |
| 30 | + ThreadSafePoolAllocator pool(block_size, block_count); |
| 31 | + |
| 32 | + const int num_threads = state.range(0); |
| 33 | + |
| 34 | + for (auto _ : state) |
| 35 | + { |
| 36 | + std::vector<std::thread> threads; |
| 37 | + threads.reserve(num_threads); |
| 38 | + |
| 39 | + for (int i = 0; i < num_threads; ++i) |
| 40 | + { |
| 41 | + threads.emplace_back([&pool]() |
| 42 | + { |
| 43 | + void* ptr = pool.allocate(); |
| 44 | + benchmark::DoNotOptimize(ptr); |
| 45 | + if (ptr) pool.deallocate(ptr); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + for (auto& thread : threads) |
| 50 | + { |
| 51 | + thread.join(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + state.SetItemsProcessed(state.iterations() * num_threads); |
| 56 | +} |
| 57 | + |
| 58 | +BENCHMARK(BM_ThreadSafePoolAllocator_MultiThread)->Arg(2)->Arg(4)->Arg(8)->UseRealTime(); |
| 59 | + |
| 60 | +static void BM_ThreadSafePoolAllocator_Contention(benchmark::State& state) |
| 61 | +{ |
| 62 | + constexpr std::size_t block_size = 64; |
| 63 | + constexpr std::size_t block_count = 1000; |
| 64 | + ThreadSafePoolAllocator pool(block_size, block_count); |
| 65 | + |
| 66 | + const int num_threads = state.range(0); |
| 67 | + |
| 68 | + for (auto _ : state) |
| 69 | + { |
| 70 | + std::vector<std::thread> threads; |
| 71 | + threads.reserve(num_threads); |
| 72 | + |
| 73 | + for (int i = 0; i < num_threads; ++i) |
| 74 | + { |
| 75 | + threads.emplace_back([&pool]() |
| 76 | + { |
| 77 | + constexpr int operations = 100; |
| 78 | + for (int j = 0; j < operations; ++j) |
| 79 | + { |
| 80 | + if (void* ptr = pool.allocate()) pool.deallocate(ptr); |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + for (auto& thread : threads) |
| 86 | + { |
| 87 | + thread.join(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + state.SetItemsProcessed(state.iterations() * num_threads * 100); |
| 92 | +} |
| 93 | + |
| 94 | +BENCHMARK(BM_ThreadSafePoolAllocator_Contention)->Arg(2)->Arg(4)->Arg(8)->UseRealTime(); |
| 95 | + |
| 96 | +static void BM_NewDelete_MultiThread(benchmark::State& state) |
| 97 | +{ |
| 98 | + const int num_threads = state.range(0); |
| 99 | + |
| 100 | + for (auto _ : state) |
| 101 | + { |
| 102 | + std::vector<std::thread> threads; |
| 103 | + threads.reserve(num_threads); |
| 104 | + |
| 105 | + for (int i = 0; i < num_threads; ++i) |
| 106 | + { |
| 107 | + threads.emplace_back([]() |
| 108 | + { |
| 109 | + constexpr std::size_t block_size = 64; |
| 110 | + void* ptr = operator new(block_size); |
| 111 | + benchmark::DoNotOptimize(ptr); |
| 112 | + operator delete(ptr); |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + for (auto& thread : threads) |
| 117 | + { |
| 118 | + thread.join(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + state.SetItemsProcessed(state.iterations() * num_threads); |
| 123 | +} |
| 124 | + |
| 125 | +BENCHMARK(BM_NewDelete_MultiThread)->Arg(2)->Arg(4)->Arg(8)->UseRealTime(); |
| 126 | + |
| 127 | +static void BM_ThreadSafePoolAllocator_BulkOperations(benchmark::State& state) |
| 128 | +{ |
| 129 | + constexpr std::size_t block_size = 64; |
| 130 | + constexpr std::size_t block_count = 10000; |
| 131 | + ThreadSafePoolAllocator pool(block_size, block_count); |
| 132 | + |
| 133 | + const std::size_t operations_per_thread = state.range(0); |
| 134 | + constexpr int num_threads = 4; |
| 135 | + |
| 136 | + for (auto _ : state) |
| 137 | + { |
| 138 | + std::vector<std::thread> threads; |
| 139 | + threads.reserve(num_threads); |
| 140 | + |
| 141 | + for (int i = 0; i < num_threads; ++i) |
| 142 | + { |
| 143 | + threads.emplace_back([&pool, operations_per_thread]() |
| 144 | + { |
| 145 | + std::vector<void*> ptrs; |
| 146 | + ptrs.reserve(operations_per_thread); |
| 147 | + |
| 148 | + for (std::size_t j = 0; j < operations_per_thread; ++j) |
| 149 | + { |
| 150 | + void* ptr = pool.allocate(); |
| 151 | + if (ptr) ptrs.push_back(ptr); |
| 152 | + } |
| 153 | + |
| 154 | + for (void* ptr : ptrs) |
| 155 | + { |
| 156 | + pool.deallocate(ptr); |
| 157 | + } |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + for (auto& thread : threads) |
| 162 | + { |
| 163 | + thread.join(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + state.SetItemsProcessed(state.iterations() * num_threads * operations_per_thread); |
| 168 | +} |
| 169 | + |
| 170 | +BENCHMARK(BM_ThreadSafePoolAllocator_BulkOperations)->Arg(100)->Arg(500)->Arg(1000)->UseRealTime(); |
0 commit comments