diff --git a/bench/Makefile b/bench/Makefile index 05f12e3c..7498d993 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -7,7 +7,7 @@ N=4 BENCH_DEF= RM=rm -rf -.PHONY: all pgo container queue mempool string plain bench-mlib bench-mlib-mempool bench-stl bench-qt bench-glib bench-klib bench-libdynamic bench-sparsepp bench-collectionc bench-tommyds bench-flathashmap bench-emilib bench-hopscotchmap bench-mlib-thread bench-liblfds bench-concurrentqueue bench-boost bench-mempool bench-string bench-plain +.PHONY: all pgo container queue mempool string plain bench-mlib bench-mlib-mempool bench-stl bench-qt bench-glib bench-klib bench-libdynamic bench-sparsepp bench-collectionc bench-tommyds bench-flathashmap bench-emilib bench-hopscotchmap bench-mlib-thread bench-liblfds bench-concurrentqueue bench-boost bench-mempool bench-string bench-plain bench-rigtorp-mpmc-queue all: container queue mempool string plain @@ -20,7 +20,7 @@ pgo: container: bench-mlib bench-mlib-mempool bench-stl bench-qt bench-glib bench-klib bench-libdynamic bench-sparsepp bench-densehashmap bench-collectionc bench-tommyds bench-flathashmap bench-emilib bench-hopscotchmap bench-uthash bench-qlibc bench-libsrt bench-nedtries bench-rigtorp-hashmap @echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" -queue: bench-mlib-thread bench-liblfds bench-concurrentqueue bench-boost +queue: bench-mlib-thread bench-liblfds bench-concurrentqueue bench-boost bench-rigtorp-mpmc-queue @echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" mempool: bench-mempool @@ -300,6 +300,13 @@ bench-rigtorp-hashmap0: @./bench-rigtorp-hashmap.exe 41 @./bench-rigtorp-hashmap.exe 43 +# RIGTORP_MPMC_QUEUE shall point to the directory where the headers are +bench-rigtorp-mpmc-queue: + @if test -n "$${RIGTORP_MPMC_QUEUE}" ; then $(MAKE) bench-rigtorp-mpmc-queue0 ; else echo "Nothing to be done for RIGTORP_MPMC_QUEUE." ; fi +bench-rigtorp-mpmc-queue0: + $(CXX) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I "$${RIGTORP_MPMC_QUEUE}" bench-rigtorp-mpmcqueue.cpp common.c -DMULTI_THREAD_MEASURE -o bench-mpmcqueue.exe -lpthread + @./bench-mpmcqueue.exe 60 + ############################################################################ bench-plain: diff --git a/bench/bench-rigtorp-mpmcqueue.cpp b/bench/bench-rigtorp-mpmcqueue.cpp new file mode 100644 index 00000000..a4e25dbb --- /dev/null +++ b/bench/bench-rigtorp-mpmcqueue.cpp @@ -0,0 +1,96 @@ +#define NDEBUG + +#include +#include +#include + +#include + +#include "common.h" + +/********************************************************************************************/ +rigtorp::MPMCQueue g_buff(4*64); +rigtorp::MPMCQueue g_final(4*64); + +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++) { + 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++) { + g_buff.pop(j); + s += j; + } + 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++) { + g_buff.push(r); + r = r * 31421U + 6927U; + } +} + +static void test_queue(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); + +} + + +/********************************************************************************************/ + +const config_func_t table[] = { + { 60, "Queue MPMC", 1000000, 0, test_queue, 0} +}; + +int main(int argc, const char *argv[]) +{ + test("RIGTORP-MPMC-QUEUE", numberof(table), table, argc, argv); + exit(0); +}