-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from petiaccja/benchmark
benchmark, thread_pool rewrite again
- Loading branch information
Showing
19 changed files
with
770 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
add_executable(benchmark) | ||
|
||
target_sources(benchmark | ||
PRIVATE | ||
main.cpp | ||
benchmark_task_spawn.cpp | ||
benchmark_thread_pool.cpp | ||
benchmark_atomic.cpp | ||
) | ||
|
||
|
||
find_package(Celero REQUIRED) | ||
target_link_libraries(benchmark celero) | ||
target_link_libraries(benchmark asyncpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#include <asyncpp/threading/cache.hpp> | ||
|
||
#include <array> | ||
#include <atomic> | ||
#include <thread> | ||
|
||
#include <celero/Celero.h> | ||
|
||
|
||
// This file benchmarks atomic operations themselves, not the library. | ||
// The measurements can be used as a baseline or target as to what is | ||
// achievable and reasonable on the hardware. | ||
|
||
|
||
using namespace asyncpp; | ||
|
||
|
||
static constexpr size_t base_reps = 4'000'000; | ||
|
||
|
||
BASELINE(atomic_rmw, x1_thread, 30, 1) { | ||
alignas(avoid_false_sharing) std::atomic_size_t counter = 0; | ||
static constexpr size_t reps = base_reps; | ||
|
||
static const auto func = [&counter] { | ||
for (size_t rep = 0; rep < reps; ++rep) { | ||
counter.fetch_add(1, std::memory_order_relaxed); | ||
} | ||
}; | ||
|
||
std::array<std::jthread, 1> threads; | ||
std::ranges::generate(threads, [&] { return std::jthread(func); }); | ||
} | ||
|
||
|
||
BENCHMARK(atomic_rmw, x2_thread, 30, 1) { | ||
alignas(avoid_false_sharing) std::atomic_size_t counter = 0; | ||
static constexpr size_t reps = base_reps / 2; | ||
|
||
static const auto func = [&counter] { | ||
for (size_t rep = 0; rep < reps; ++rep) { | ||
counter.fetch_add(1, std::memory_order_relaxed); | ||
} | ||
}; | ||
|
||
std::array<std::jthread, 2> threads; | ||
std::ranges::generate(threads, [&] { return std::jthread(func); }); | ||
} | ||
|
||
|
||
BENCHMARK(atomic_rmw, x4_thread, 30, 1) { | ||
alignas(avoid_false_sharing) std::atomic_size_t counter = 0; | ||
static constexpr size_t reps = base_reps / 4; | ||
|
||
static const auto func = [&counter] { | ||
for (size_t rep = 0; rep < reps; ++rep) { | ||
counter.fetch_add(1, std::memory_order_relaxed); | ||
} | ||
}; | ||
|
||
std::array<std::jthread, 4> threads; | ||
std::ranges::generate(threads, [&] { return std::jthread(func); }); | ||
} | ||
|
||
|
||
BENCHMARK(atomic_rmw, x8_thread, 30, 1) { | ||
alignas(avoid_false_sharing) std::atomic_size_t counter = 0; | ||
static constexpr size_t reps = base_reps / 8; | ||
|
||
static const auto func = [&counter] { | ||
for (size_t rep = 0; rep < reps; ++rep) { | ||
counter.fetch_add(1, std::memory_order_relaxed); | ||
} | ||
}; | ||
|
||
std::array<std::jthread, 8> threads; | ||
std::ranges::generate(threads, [&] { return std::jthread(func); }); | ||
} | ||
|
||
|
||
BASELINE(atomic_read, x1_thread, 30, 1) { | ||
alignas(avoid_false_sharing) std::atomic_size_t counter = 0; | ||
static constexpr size_t reps = base_reps; | ||
|
||
static const auto func = [&counter] { | ||
for (size_t rep = 0; rep < reps; ++rep) { | ||
static_cast<void>(counter.load(std::memory_order_relaxed)); | ||
} | ||
}; | ||
|
||
std::array<std::jthread, 1> threads; | ||
std::ranges::generate(threads, [&] { return std::jthread(func); }); | ||
} | ||
|
||
|
||
BENCHMARK(atomic_read, x2_thread, 30, 1) { | ||
alignas(avoid_false_sharing) std::atomic_size_t counter = 0; | ||
static constexpr size_t reps = base_reps / 2; | ||
|
||
static const auto func = [&counter] { | ||
for (size_t rep = 0; rep < reps; ++rep) { | ||
static_cast<void>(counter.load(std::memory_order_relaxed)); | ||
} | ||
}; | ||
|
||
std::array<std::jthread, 2> threads; | ||
std::ranges::generate(threads, [&] { return std::jthread(func); }); | ||
} | ||
|
||
|
||
BENCHMARK(atomic_read, x4_thread, 30, 1) { | ||
alignas(avoid_false_sharing) std::atomic_size_t counter = 0; | ||
static constexpr size_t reps = base_reps / 4; | ||
|
||
static const auto func = [&counter] { | ||
for (size_t rep = 0; rep < reps; ++rep) { | ||
static_cast<void>(counter.load(std::memory_order_relaxed)); | ||
} | ||
}; | ||
|
||
std::array<std::jthread, 4> threads; | ||
std::ranges::generate(threads, [&] { return std::jthread(func); }); | ||
} | ||
|
||
|
||
BENCHMARK(atomic_read, x8_thread, 30, 1) { | ||
alignas(avoid_false_sharing) std::atomic_size_t counter = 0; | ||
static constexpr size_t reps = base_reps / 8; | ||
|
||
static const auto func = [&counter] { | ||
for (size_t rep = 0; rep < reps; ++rep) { | ||
static_cast<void>(counter.load(std::memory_order_relaxed)); | ||
} | ||
}; | ||
|
||
std::array<std::jthread, 8> threads; | ||
std::ranges::generate(threads, [&] { return std::jthread(func); }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
#include <asyncpp/join.hpp> | ||
#include <asyncpp/task.hpp> | ||
#include <asyncpp/threading/cache.hpp> | ||
|
||
#include <array> | ||
#include <memory_resource> | ||
|
||
#include <celero/Celero.h> | ||
|
||
|
||
using namespace asyncpp; | ||
|
||
|
||
task<int> plain() { | ||
co_return 1; | ||
} | ||
|
||
|
||
task<int> allocator_backed(std::allocator_arg_t, std::pmr::polymorphic_allocator<> alloc) { | ||
co_return 1; | ||
} | ||
|
||
|
||
struct FixtureNewDelete : celero::TestFixture { | ||
inline std::pmr::polymorphic_allocator<>& getAlloc() { | ||
return alloc; | ||
} | ||
|
||
private: | ||
std::pmr::polymorphic_allocator<> alloc = { std::pmr::new_delete_resource() }; | ||
}; | ||
|
||
|
||
struct FixturePool : celero::TestFixture { | ||
inline std::pmr::polymorphic_allocator<>& getAlloc() { | ||
return alloc; | ||
} | ||
|
||
private: | ||
std::pmr::unsynchronized_pool_resource resource; | ||
std::pmr::polymorphic_allocator<> alloc = { &resource }; | ||
}; | ||
|
||
|
||
struct FixtureStack : celero::TestFixture { | ||
void setUp(const ExperimentValue* x) override { | ||
alloc.~polymorphic_allocator(); | ||
resource.~monotonic_buffer_resource(); | ||
new (&resource) std::pmr::monotonic_buffer_resource(buffer.get(), size, std::pmr::new_delete_resource()); | ||
new (&alloc) std::pmr::polymorphic_allocator<>(&resource); | ||
} | ||
|
||
inline std::pmr::polymorphic_allocator<>& getAlloc() { | ||
return alloc; | ||
} | ||
|
||
private: | ||
static constexpr inline size_t size = 10485760; | ||
struct block { | ||
alignas(avoid_false_sharing) std::byte content[avoid_false_sharing]; | ||
}; | ||
std::unique_ptr<block[]> buffer = std::make_unique_for_overwrite<block[]>(size / sizeof(block)); | ||
std::pmr::monotonic_buffer_resource resource; | ||
std::pmr::polymorphic_allocator<> alloc = { &resource }; | ||
}; | ||
|
||
|
||
constexpr int numSamples = 1000; | ||
constexpr int numIterations = 5000; | ||
|
||
|
||
BASELINE(task_spawn, unoptimized, numSamples, numIterations) { | ||
bool ready = false; | ||
{ | ||
auto task = plain(); | ||
volatile auto ptr = &task; | ||
ptr->launch(); | ||
ready = ptr->ready(); | ||
} | ||
assert(ready); | ||
celero::DoNotOptimizeAway(ready); | ||
} | ||
|
||
|
||
BENCHMARK(task_spawn, HALO, numSamples, numIterations) { | ||
bool ready = false; | ||
{ | ||
auto task = plain(); | ||
task.launch(); | ||
ready = task.ready(); | ||
} | ||
assert(ready); | ||
celero::DoNotOptimizeAway(ready); | ||
} | ||
|
||
|
||
BENCHMARK_F(task_spawn, PMR_new_delete, FixtureNewDelete, numSamples, numIterations) { | ||
bool ready = false; | ||
auto& alloc = getAlloc(); | ||
{ | ||
auto task = allocator_backed(std::allocator_arg, alloc); | ||
volatile auto ptr = &task; | ||
ptr->launch(); | ||
ready = ptr->ready(); | ||
} | ||
assert(ready); | ||
celero::DoNotOptimizeAway(ready); | ||
} | ||
|
||
|
||
BENCHMARK_F(task_spawn, PMR_unsync_pool, FixturePool, numSamples, numIterations) { | ||
bool ready = false; | ||
auto& alloc = getAlloc(); | ||
{ | ||
auto task = allocator_backed(std::allocator_arg, alloc); | ||
volatile auto ptr = &task; | ||
ptr->launch(); | ||
ready = ptr->ready(); | ||
} | ||
assert(ready); | ||
celero::DoNotOptimizeAway(ready); | ||
} | ||
|
||
|
||
BENCHMARK_F(task_spawn, PMR_stack, FixtureStack, numSamples, numIterations) { | ||
bool ready = false; | ||
auto& alloc = getAlloc(); | ||
{ | ||
auto task = allocator_backed(std::allocator_arg, alloc); | ||
volatile auto ptr = &task; | ||
ptr->launch(); | ||
ready = ptr->ready(); | ||
} | ||
assert(ready); | ||
celero::DoNotOptimizeAway(ready); | ||
} |
Oops, something went wrong.