Commit bcb9817f authored by neverlord's avatar neverlord

performance tests

parent 920472e6
include ../Makefile.rules
CXX = /usr/bin/g++-4.6
CXXFLAGS = -std=c++0x -pedantic -Wall -Wextra -O2
#CXX = /opt/local/bin/g++-mp-4.5
#CXX = /opt/local/bin/g++-mp-4.6
#CXXFLAGS = -std=c++0x -pedantic -Wall -Wextra -g -O0 -I/opt/local/include/
#CXXFLAGS = -std=c++0x -pedantic -Wall -Wextra -O2 -I/opt/local/include/
#LIBS = -L/opt/local/lib -lboost_thread-mt -L../ -lcppa
INCLUDES = -I./ -I../
INCLUDES = -I./
LIBS = -pthread
FLAGS = -DCACHE_LINE_SIZE=64
EXECUTABLE = ../queue_test
EXECUTABLE = queue_test
HEADERS = sutter_list.hpp
HEADERS = blocking_cached_stack2.hpp blocking_cached_stack.hpp blocking_sutter_list.hpp cached_stack.hpp defines.hpp intrusive_sutter_list.hpp lockfree_list.hpp sutter_list.hpp
SOURCES = main.cpp
OBJECTS = $(SOURCES:.cpp=.o)
%.o : %.cpp $(HEADERS) $(HEADERS)
%.o : %.cpp $(HEADERS)
$(CXX) $(CXXFLAGS) $(INCLUDES) $(FLAGS) -c $< -o $@
$(EXECUTABLE) : $(OBJECTS) $(HEADERS)
$(CXX) $(LIBS) -L../ -lcppa $(OBJECTS) -o $(EXECUTABLE)
$(CXX) $(LIBS) $(OBJECTS) -o $(EXECUTABLE)
all : $(EXECUTABLE)
......
#ifndef BLOCKING_CACHED_STACK_HPP
#define BLOCKING_CACHED_STACK_HPP
#include <thread>
#include <atomic>
#include <boost/thread.hpp>
#include "defines.hpp"
......@@ -20,10 +20,10 @@ class blocking_cached_stack
char m_pad2[CACHE_LINE_SIZE - sizeof(std::atomic<T*>)];
// locked on enqueue/dequeue operations to/from an empty list
boost::mutex m_mtx;
boost::condition_variable m_cv;
std::mutex m_mtx;
std::condition_variable m_cv;
typedef boost::unique_lock<boost::mutex> lock_type;
typedef std::unique_lock<std::mutex> lock_type;
// read all elements of m_stack, convert them to FIFO order and store
// them in m_head
......
......@@ -23,10 +23,10 @@ class blocking_cached_stack2
char m_pad3[CACHE_LINE_SIZE - sizeof(T)];
// locked on enqueue/dequeue operations to/from an empty list
boost::mutex m_mtx;
boost::condition_variable m_cv;
std::mutex m_mtx;
std::condition_variable m_cv;
typedef boost::unique_lock<boost::mutex> lock_type;
typedef std::unique_lock<std::mutex> lock_type;
// read all elements of m_stack, convert them to FIFO order and store
// them in m_head
......
......@@ -36,10 +36,10 @@ class blocking_sutter_list
char m_pad3[CACHE_LINE_SIZE - sizeof(std::atomic<bool>)];
// locked on enqueue/dequeue operations to/from an empty list
boost::mutex m_mtx;
boost::condition_variable m_cv;
std::mutex m_mtx;
std::condition_variable m_cv;
typedef boost::unique_lock<boost::mutex> lock_type;
typedef std::unique_lock<std::mutex> lock_type;
public:
......@@ -67,7 +67,7 @@ class blocking_sutter_list
// acquire exclusivity
while (m_producer_lock.exchange(true))
{
boost::this_thread::yield();
std::this_thread::yield();
}
// do we have to wakeup a sleeping consumer?
// this is a sufficient condition because m_last->value is 0
......
......@@ -95,7 +95,7 @@ class cached_stack
T* result = try_pop();
while (!result)
{
boost::this_thread::yield();
std::this_thread::yield();
result = try_pop();
}
return result;
......
......@@ -62,7 +62,7 @@ class intrusive_sutter_list
// acquire exclusivity
while (m_producer_lock.exchange(true))
{
boost::this_thread::yield();
std::this_thread::yield();
}
// publish & swing last forward
m_last->next = tmp;
......@@ -97,7 +97,7 @@ class intrusive_sutter_list
T result;
while (!try_pop(result))
{
boost::this_thread::yield();
std::this_thread::yield();
}
return result;
}
......
......@@ -90,7 +90,7 @@ class lockfree_list
T result;
while (!try_pop(result))
{
boost::this_thread::yield();
std::this_thread::yield();
}
return result;
}
......
#include <thread>
#include <vector>
#include <cstddef>
#include <sstream>
#include <iostream>
#include <stdexcept>
#include <boost/thread.hpp>
//#include <boost/thread.hpp>
#include <boost/progress.hpp>
#include <boost/lexical_cast.hpp>
......@@ -79,11 +80,12 @@ void usage()
<< " - lockfree_list" << endl
<< endl
<< " possible format string variables: " << endl
<< " - $MESSAGES" << endl
<< " - $PRODUCERS" << endl
<< " - $TIME" << endl
<< " - MESSAGES" << endl
<< " - MSG_IN_MILLION" << endl
<< " - PRODUCERS" << endl
<< " - TIME" << endl
<< endl
<< "example: ./queue_test 10000 10 cached_list \"$MESSAGES $TIME\""
<< "example: ./queue_test 10000 10 cached_stack \"MESSAGES TIME\""
<< endl;
}
......@@ -96,12 +98,12 @@ double run_test(size_t num_messages, size_t num_producers,
boost::timer t0;
// locals
Queue list;
std::vector<boost::thread*> producer_threads(num_producers);
std::vector<std::thread*> producer_threads(num_producers);
for (size_t i = 0; i < num_producers; ++i)
{
producer_threads[i] = new boost::thread(producer<Queue, Allocator>,
boost::ref(list),
boost::ref(element_allocator),
producer_threads[i] = new std::thread(producer<Queue, Allocator>,
std::ref(list),
std::ref(element_allocator),
i * num_messages_per_producer,
(i+1) * num_messages_per_producer);
}
......@@ -256,9 +258,10 @@ int main(int argc, char** argv)
}
// build output message
std::vector<std::pair<std::string, std::string>> replacements = {
{ "$MESSAGES", argv[1] },
{ "$PRODUCERS", argv[2] },
{ "$TIME", boost::lexical_cast<std::string>(elapsed_time) }
{ "MESSAGES", argv[1] },
{ "PRODUCERS", argv[2] },
{ "MSG_IN_MILLION", boost::lexical_cast<std::string>(num_messages / 1000000.0) },
{ "TIME", boost::lexical_cast<std::string>(elapsed_time) }
};
for (auto i = replacements.begin(); i != replacements.end(); ++i)
{
......
......@@ -59,7 +59,7 @@ class sutter_list
// acquire exclusivity
while (m_producer_lock.exchange(true))
{
boost::this_thread::yield();
std::this_thread::yield();
}
// publish & swing last forward
m_last->next = tmp;
......@@ -96,7 +96,7 @@ class sutter_list
T* result = try_pop();
while (!result)
{
boost::this_thread::yield();
std::this_thread::yield();
result = try_pop();
}
return result;
......
......@@ -9,6 +9,19 @@
#include "cppa/util/single_reader_queue.hpp"
//#define DEBUG_RESULTS
// "config"
namespace {
const size_t slave_messages = 1000000;
const size_t trials = 10;
} // namespace <anonymous>
using cppa::util::single_reader_queue;
using std::cout;
......@@ -107,7 +120,7 @@ class locked_queue
}
}
void push(element_type* new_element)
void push_back(element_type* new_element)
{
lock_type guard(m_mtx);
if (m_pub.empty())
......@@ -150,43 +163,75 @@ void slave(Queue& q, size_t from, size_t to)
}
template<typename Queue, size_t num_slaves, size_t num_slave_msgs>
void master(Queue& q)
void master()
{
static const size_t num_msgs = (num_slaves) * (num_slave_msgs);
static const size_t calc_result = ((num_msgs)*(num_msgs + 1)) / 2;
//cout << num_slaves << " workers; running test";
//cout.flush();
double elapsed[trials];
for (size_t i = 0; i < trials; ++i)
{
//cout << " ... " << (i + 1);
//cout.flush();
Queue q;
boost::timer t0;
for (size_t i = 0; i < num_slaves; ++i)
for (size_t j = 0; j < num_slaves; ++j)
{
size_t from = (i * num_slave_msgs) + 1;
size_t from = (j * num_slave_msgs) + 1;
size_t to = from + num_slave_msgs;
boost::thread(slave<Queue>, boost::ref(q), from, to).detach();
}
size_t result = 0;
# ifdef DEBUG_RESULTS
size_t min_val = calc_result;
size_t max_val = 0;
for (size_t i = 0; i < num_msgs; ++i)
# endif
for (size_t j = 0; j < num_msgs; ++j)
{
queue_element* e = q.pop();
result += e->value;
# ifdef DEBUG_RESULTS
min_val = std::min(min_val, e->value);
max_val = std::max(max_val, e->value);
# endif
delete e;
}
if (result != calc_result)
{
cerr << "ERROR: result = " << result
<< " (should be: " << calc_result << ")" << endl
<< "min: " << min_val << endl
<< "max: " << max_val << endl;
<< " (should be: " << calc_result << ")"
# ifdef DEBUG_RESULTS
<< endl << "min: " << min_val
<< endl << "max: " << max_val
# endif
<< endl;
}
elapsed[i] = t0.elapsed();
//cout << t0.elapsed() << " " << num_slaves << endl;
}
cout << t0.elapsed() << " " << num_slaves << endl;
//cout << endl;
double sum = 0;
//cout << "runtimes = { ";
for (size_t i = 0; i < trials; ++i)
{
//cout << (i == 0 ? "" : ", ") << elapsed[i];
sum += elapsed[i];
}
//cout << " }" << endl;
//cout << "AVG = " << (sum / trials) << endl;
cout << (sum / trials) << " " << num_slaves << endl;
}
namespace { const size_t slave_messages = 1000000; }
template<size_t Pos, size_t Max, size_t Step,
template<size_t> class Stmt>
struct static_for
......@@ -222,8 +267,7 @@ struct test_step
template<typename QueueToken>
static inline void _(QueueToken)
{
typename QueueToken::type q;
boost::thread t0(master<typename QueueToken::type, NumThreads, slave_messages>, boost::ref(q));
boost::thread t0(master<typename QueueToken::type, NumThreads, slave_messages>);
t0.join();
}
};
......@@ -237,8 +281,13 @@ void test_q_impl()
void test__queue_performance()
{
cout << "Format: "
"(average value of 10 runs) "
// "(standard deviation) "
"(number of worker threads)"
<< endl;
cout << "locked_queue:" << endl;
// test_q_impl<locked_queue<queue_element>>();
test_q_impl<locked_queue<queue_element>>();
cout << endl;
cout << "single_reader_queue:" << endl;
test_q_impl<single_reader_queue<queue_element>>();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment