From faf1fc56d0e790529329641d3a122573d0aaff1e Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Thu, 31 Aug 2017 23:12:51 +0200 Subject: [PATCH] Add bench of boost / locked free queue --- bench/Makefile | 8 ++++ bench/bench-boost.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 bench/bench-boost.cpp diff --git a/bench/Makefile b/bench/Makefile index d14ef61b..b3914b1a 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -192,6 +192,7 @@ bench-libsrt0: @./bench-libsrt.exe 43 @./bench-libsrt.exe 50 + # LIBLFDS shall point to the directory where the bin/build/inc/obj/src directories are # The library shall be built. bench-liblfds: @@ -201,6 +202,7 @@ bench-liblfds0: $(CC) $(CFLAGS) $(CPPFLAGS) -I "$${LIBLFDS}/inc" bench-liblfds.c -o bench-liblfds.exe $${LIBLFDS}/bin/liblfds711.so -lpthread @LD_LIBRARY_PATH="$${LIBLFDS}/bin:$${LD_LIBRARY_PATH}" ./bench-liblfds.exe 60 + # CONCURRENTQUEUE shall point to the directory where the bin/build/inc/obj/src directories are # The library shall be built. bench-concurrentqueue: @@ -210,6 +212,12 @@ bench-concurrentqueue0: $(CXX) $(CFLAGS) $(CPPFLAGS) -I "$${CONCURRENTQUEUE}/" bench-concurrentqueue.cpp -o bench-concurrentqueue.exe -lpthread @./bench-concurrentqueue.exe 60 + +# BOOST is assumed to be installed in the system +bench-boost: + $(CXX) $(CFLAGS) $(CPPFLAGS) bench-boost.cpp -o bench-boost.exe -lpthread + @./bench-boost.exe 60 + ############################################################################ bench-plain: diff --git a/bench/bench-boost.cpp b/bench/bench-boost.cpp new file mode 100644 index 00000000..8d6ea7d6 --- /dev/null +++ b/bench/bench-boost.cpp @@ -0,0 +1,98 @@ +#define NDEBUG + +#include +#include +#include + +#include + +#include "m-mutex.h" +#define MULTI_THREAD_MEASURE +#include "common.h" + + +/********************************************************************************************/ +boost::lockfree::queue> g_buff; +boost::lockfree::queue> g_final; + +static void final(void *arg) +{ + size_t *p_n = (size_t*) arg; + size_t n = *p_n; + unsigned long long j, s = 0; + for(unsigned int i = 0; i < n;i++) { + while (!g_final.pop(j)); + s += j; + } + g_result = s; +} + +static void conso(void *arg) +{ + unsigned int j; + size_t *p_n = (size_t*)arg; + size_t n = *p_n; + unsigned long long s = 0; + for(unsigned int i = 0; i < n;i++) { + while (!g_buff.pop(j)); + s += j; + } + while (!g_final.push(s)); +} + +static void prod(void *arg) +{ + size_t *p_n = (size_t*)arg; + size_t n = *p_n; + size_t r = n; + for(unsigned int i = 0; i < n;i++) { + while (!g_buff.push(r)); + r = r * 31421U + 6927U; + } +} + +static void test_buffer(size_t n) +{ + const int cpu_count = get_cpu_count(); + const int prod_count = cpu_count/2; + const int conso_count = cpu_count - prod_count; + if (cpu_count < 2) { + fprintf(stderr, "WARNING: Can not measure Buffer performance.\n"); + return; + } + rand_get(); + // Create thread + m_thread_t idx_p[prod_count]; + m_thread_t idx_c[conso_count]; + m_thread_t idx_final; + for(int i = 0; i < prod_count; i++) { + m_thread_create (idx_p[i], prod, &n); + } + for(int i = 0; i < conso_count; i++) { + m_thread_create (idx_c[i], conso, &n); + } + size_t n2 = conso_count; + m_thread_create(idx_final, final, &n2); + + // Wait for jobs to be done. + for(int i = 0; i < prod_count; i++) { + m_thread_join(idx_p[i]); + } + for(int i = 0; i < conso_count; i++) { + m_thread_join(idx_c[i]); + } + m_thread_join(idx_final); + +} + + +/********************************************************************************************/ + +int main(int argc, const char *argv[]) +{ + int n = (argc > 1) ? atoi(argv[1]) : 0; + if (n == 60) + test_function("Buffer time", 1000000, test_buffer); + exit(0); +} +