Skip to content

Commit

Permalink
Update performance tests to use config from environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskohlhoff committed Nov 5, 2024
1 parent bdf9cfe commit e07d96a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 50 deletions.
15 changes: 9 additions & 6 deletions asio/src/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,16 @@ check_PROGRAMS = \
unit/write \
unit/write_at

if !STANDALONE
noinst_PROGRAMS = \
performance/client \
performance/server

if !STANDALONE
noinst_PROGRAMS += \
latency/tcp_client \
latency/tcp_server \
latency/udp_client \
latency/udp_server \
performance/client \
performance/server
latency/udp_server
endif

if HAVE_CXX11
Expand Down Expand Up @@ -455,13 +457,14 @@ noinst_HEADERS = \

AM_CXXFLAGS = -I$(srcdir)/../../include

performance_client_SOURCES = performance/client.cpp
performance_server_SOURCES = performance/server.cpp

if !STANDALONE
latency_tcp_client_SOURCES = latency/tcp_client.cpp
latency_tcp_server_SOURCES = latency/tcp_server.cpp
latency_udp_client_SOURCES = latency/udp_client.cpp
latency_udp_server_SOURCES = latency/udp_server.cpp
performance_client_SOURCES = performance/client.cpp
performance_server_SOURCES = performance/server.cpp
endif

unit_any_completion_executor_SOURCES = unit/any_completion_executor.cpp
Expand Down
36 changes: 17 additions & 19 deletions asio/src/tests/performance/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

#include "asio.hpp"
#include <algorithm>
#include <boost/bind/bind.hpp>
#include <boost/mem_fn.hpp>
#include <functional>
#include <iostream>
#include <list>
#include <string>
#include <thread>
#include "handler_allocator.hpp"

class stats
Expand Down Expand Up @@ -78,13 +78,13 @@ class session
{
asio::async_connect(socket_, endpoints,
asio::bind_executor(strand_,
boost::bind(&session::handle_connect, this,
std::bind(&session::handle_connect, this,
asio::placeholders::error)));
}

void stop()
{
asio::post(strand_, boost::bind(&session::close_socket, this));
asio::post(strand_, std::bind(&session::close_socket, this));
}

private:
Expand All @@ -101,13 +101,13 @@ class session
async_write(socket_, asio::buffer(write_data_, block_size_),
asio::bind_executor(strand_,
make_custom_alloc_handler(write_allocator_,
boost::bind(&session::handle_write, this,
std::bind(&session::handle_write, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
socket_.async_read_some(asio::buffer(read_data_, block_size_),
asio::bind_executor(strand_,
make_custom_alloc_handler(read_allocator_,
boost::bind(&session::handle_read, this,
std::bind(&session::handle_read, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
}
Expand All @@ -128,13 +128,13 @@ class session
async_write(socket_, asio::buffer(write_data_, read_data_length_),
asio::bind_executor(strand_,
make_custom_alloc_handler(write_allocator_,
boost::bind(&session::handle_write, this,
std::bind(&session::handle_write, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
socket_.async_read_some(asio::buffer(read_data_, block_size_),
asio::bind_executor(strand_,
make_custom_alloc_handler(read_allocator_,
boost::bind(&session::handle_read, this,
std::bind(&session::handle_read, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
}
Expand All @@ -154,13 +154,13 @@ class session
async_write(socket_, asio::buffer(write_data_, read_data_length_),
asio::bind_executor(strand_,
make_custom_alloc_handler(write_allocator_,
boost::bind(&session::handle_write, this,
std::bind(&session::handle_write, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
socket_.async_read_some(asio::buffer(read_data_, block_size_),
asio::bind_executor(strand_,
make_custom_alloc_handler(read_allocator_,
boost::bind(&session::handle_read, this,
std::bind(&session::handle_read, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
}
Expand Down Expand Up @@ -199,7 +199,7 @@ class client
stats_()
{
stop_timer_.expires_after(asio::chrono::seconds(timeout));
stop_timer_.async_wait(boost::bind(&client::handle_timeout, this));
stop_timer_.async_wait(std::bind(&client::handle_timeout, this));

for (size_t i = 0; i < session_count; ++i)
{
Expand All @@ -223,7 +223,7 @@ class client
void handle_timeout()
{
std::for_each(sessions_.begin(), sessions_.end(),
boost::mem_fn(&session::stop));
std::bind(&session::stop, std::placeholders::_1));
}

private:
Expand Down Expand Up @@ -252,28 +252,26 @@ int main(int argc, char* argv[])
size_t session_count = atoi(argv[5]);
int timeout = atoi(argv[6]);

asio::io_context ioc;
asio::io_context ioc{asio::config_from_env{}};

asio::ip::tcp::resolver r(ioc);
asio::ip::tcp::resolver::results_type endpoints =
r.resolve(host, port);

client c(ioc, endpoints, block_size, session_count, timeout);

std::list<asio::thread*> threads;
std::list<std::thread> threads;
while (--thread_count > 0)
{
asio::thread* new_thread = new asio::thread(
boost::bind(&asio::io_context::run, &ioc));
threads.push_back(new_thread);
std::thread new_thread(std::bind(&asio::io_context::run, &ioc));
threads.push_back(std::move(new_thread));
}

ioc.run();

while (!threads.empty())
{
threads.front()->join();
delete threads.front();
threads.front().join();
threads.pop_front();
}
}
Expand Down
14 changes: 7 additions & 7 deletions asio/src/tests/performance/handler_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,36 @@
#define HANDLER_ALLOCATOR_HPP

#include "asio.hpp"
#include <boost/aligned_storage.hpp>
#include <boost/noncopyable.hpp>

// Class to manage the memory to be used for handler-based custom allocation.
// It contains a single block of memory which may be returned for allocation
// requests. If the memory is in use when an allocation request is made, the
// allocator delegates allocation to the global heap.
class handler_allocator
: private boost::noncopyable
{
public:
handler_allocator()
: in_use_(false)
{
}

handler_allocator(const handler_allocator&) = delete;
handler_allocator& operator=(const handler_allocator&) = delete;

void* allocate(std::size_t size)
{
if (!in_use_ && size < storage_.size)
if (!in_use_ && size < sizeof(storage_))
{
in_use_ = true;
return storage_.address();
return storage_;
}

return ::operator new(size);
}

void deallocate(void* pointer)
{
if (pointer == storage_.address())
if (pointer == storage_)
{
in_use_ = false;
}
Expand All @@ -53,7 +53,7 @@ class handler_allocator

private:
// Storage space used for handler-based custom memory allocation.
boost::aligned_storage<1024> storage_;
alignas(std::max_align_t) unsigned char storage_[1024];

// Whether the handler-based custom allocation storage has been used.
bool in_use_;
Expand Down
34 changes: 16 additions & 18 deletions asio/src/tests/performance/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

#include "asio.hpp"
#include <algorithm>
#include <boost/bind/bind.hpp>
#include <functional>
#include <iostream>
#include <list>
#include <thread>
#include "handler_allocator.hpp"

class session
Expand Down Expand Up @@ -53,13 +54,13 @@ class session
socket_.async_read_some(asio::buffer(read_data_, block_size_),
asio::bind_executor(strand_,
make_custom_alloc_handler(read_allocator_,
boost::bind(&session::handle_read, this,
std::bind(&session::handle_read, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
}
else
{
asio::post(io_context_, boost::bind(&session::destroy, this));
asio::post(io_context_, std::bind(&session::destroy, this));
}
}

Expand All @@ -78,19 +79,19 @@ class session
async_write(socket_, asio::buffer(write_data_, read_data_length_),
asio::bind_executor(strand_,
make_custom_alloc_handler(write_allocator_,
boost::bind(&session::handle_write, this,
std::bind(&session::handle_write, this,
asio::placeholders::error))));
socket_.async_read_some(asio::buffer(read_data_, block_size_),
asio::bind_executor(strand_,
make_custom_alloc_handler(read_allocator_,
boost::bind(&session::handle_read, this,
std::bind(&session::handle_read, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
}
}

if (op_count_ == 0)
asio::post(io_context_, boost::bind(&session::destroy, this));
asio::post(io_context_, std::bind(&session::destroy, this));
}

void handle_write(const asio::error_code& err)
Expand All @@ -107,19 +108,19 @@ class session
async_write(socket_, asio::buffer(write_data_, read_data_length_),
asio::bind_executor(strand_,
make_custom_alloc_handler(write_allocator_,
boost::bind(&session::handle_write, this,
std::bind(&session::handle_write, this,
asio::placeholders::error))));
socket_.async_read_some(asio::buffer(read_data_, block_size_),
asio::bind_executor(strand_,
make_custom_alloc_handler(read_allocator_,
boost::bind(&session::handle_read, this,
std::bind(&session::handle_read, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
}
}

if (op_count_ == 0)
asio::post(io_context_, boost::bind(&session::destroy, this));
asio::post(io_context_, std::bind(&session::destroy, this));
}

static void destroy(session* s)
Expand Down Expand Up @@ -162,7 +163,7 @@ class server
{
session* new_session = new session(io_context_, block_size_);
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
std::bind(&server::handle_accept, this, new_session,
asio::placeholders::error));
}

Expand Down Expand Up @@ -202,25 +203,22 @@ int main(int argc, char* argv[])
int thread_count = atoi(argv[3]);
size_t block_size = atoi(argv[4]);

asio::io_context ioc;
asio::io_context ioc{asio::config_from_env{}};

server s(ioc, asio::ip::tcp::endpoint(address, port), block_size);

// Threads not currently supported in this test.
std::list<asio::thread*> threads;
std::list<std::thread> threads;
while (--thread_count > 0)
{
asio::thread* new_thread = new asio::thread(
boost::bind(&asio::io_context::run, &ioc));
threads.push_back(new_thread);
std::thread new_thread(std::bind(&asio::io_context::run, &ioc));
threads.push_back(std::move(new_thread));
}

ioc.run();

while (!threads.empty())
{
threads.front()->join();
delete threads.front();
threads.front().join();
threads.pop_front();
}
}
Expand Down

0 comments on commit e07d96a

Please sign in to comment.