- testing for current threading structures
This commit is contained in:
@@ -32,8 +32,10 @@
|
||||
#ifndef FENNEC_TEST_THREADING_H
|
||||
#define FENNEC_TEST_THREADING_H
|
||||
|
||||
#include <chrono>
|
||||
#include <fennec/threading/atomic.h>
|
||||
#include <fennec/threading/lock_guard.h>
|
||||
#include <fennec/threading/mpscq.h>
|
||||
#include <fennec/threading/mutex.h>
|
||||
#include <fennec/threading/thread.h>
|
||||
|
||||
@@ -81,15 +83,56 @@ inline void fennec_test_threading_run_test_mutex(array<thread, ThreadsV>& thread
|
||||
}
|
||||
|
||||
|
||||
|
||||
inline void fennec_test_threading_test_mpscq_producer(mpscq<size_t>* queue, size_t N = 1000) {
|
||||
for (size_t i = 0; i < N; ++i) {
|
||||
queue->emplace(1);
|
||||
}
|
||||
}
|
||||
|
||||
inline void fennec_test_threading_test_mpscq_consumer(mpscq<size_t>* queue, atomic<size_t>* res, atomic<bool>* done) {
|
||||
while (not done->load() or not queue->empty()) {
|
||||
unique_ptr<size_t> ptr = queue->pop();
|
||||
if (ptr) {
|
||||
*res += *ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<size_t ThreadsV>
|
||||
inline void fennec_test_threading_run_test_mpscq(array<thread, ThreadsV>& threads, size_t N = 1000) {
|
||||
mpscq<size_t> queue(N * ThreadsV);
|
||||
for (size_t i = 1; i < ThreadsV; ++i) {
|
||||
threads[i] = thread(fennec_test_threading_test_mpscq_producer, &queue, N);
|
||||
}
|
||||
atomic<size_t> test;
|
||||
atomic<bool> done;
|
||||
threads[0] = thread(fennec_test_threading_test_mpscq_consumer, &queue, &test, &done);
|
||||
for (size_t i = 1; i < ThreadsV; ++i) {
|
||||
threads[i].join();
|
||||
}
|
||||
done.store(true);
|
||||
threads[0].join();
|
||||
fennec_test_run(test.load(), N * (ThreadsV - 1));
|
||||
}
|
||||
|
||||
template<typename ReturnT, typename...ParamsT, typename...ArgsT>
|
||||
inline double fennec_test_threading_timed(ReturnT (func)(ParamsT...), ArgsT&&...args) {
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
func(fennec::forward<ArgsT>(args)...);
|
||||
return std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - start).count();
|
||||
}
|
||||
|
||||
|
||||
inline void fennec_test_threading() {
|
||||
|
||||
static constexpr size_t N = 1000, threads = 4;
|
||||
static constexpr size_t N = 100000, threads = 8;
|
||||
|
||||
array<thread, threads> arr;
|
||||
|
||||
fennec_test_threading_run_test_atomic(arr, N);
|
||||
fennec_test_threading_run_test_mutex(arr, N);
|
||||
|
||||
std::cout << fennec_test_threading_timed(fennec_test_threading_run_test_atomic<threads>, arr, N) << "s\n";
|
||||
std::cout << fennec_test_threading_timed(fennec_test_threading_run_test_mutex<threads>, arr, N) << "s\n";
|
||||
std::cout << fennec_test_threading_timed(fennec_test_threading_run_test_mpscq<threads>, arr, N) << "s\n";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user