Skip to content

Instantly share code, notes, and snippets.

@eao197
Created March 21, 2023 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eao197/f2db1094b9c396e077a89614abef15e4 to your computer and use it in GitHub Desktop.
Save eao197/f2db1094b9c396e077a89614abef15e4 to your computer and use it in GitHub Desktop.
Бенчмарк для проверки производительности двух разных вариантов реализации multi_sink_bindings в SO-5.8
#include <iostream>
#include <chrono>
#include <vector>
#include <so_5/all.hpp>
using hires_clock = std::chrono::high_resolution_clock;
struct msg_01 final : public so_5::signal_t {};
struct msg_02 final : public so_5::signal_t {};
struct msg_03 final : public so_5::signal_t {};
struct msg_04 final : public so_5::signal_t {};
struct msg_05 final : public so_5::signal_t {};
struct msg_06 final : public so_5::signal_t {};
struct msg_07 final : public so_5::signal_t {};
struct msg_08 final : public so_5::signal_t {};
struct msg_09 final : public so_5::signal_t {};
struct msg_10 final : public so_5::signal_t {};
struct msg_11 final : public so_5::signal_t {};
struct msg_12 final : public so_5::signal_t {};
struct msg_13 final : public so_5::signal_t {};
struct msg_14 final : public so_5::signal_t {};
struct msg_15 final : public so_5::signal_t {};
struct msg_16 final : public so_5::signal_t {};
struct msg_17 final : public so_5::signal_t {};
struct msg_18 final : public so_5::signal_t {};
struct msg_19 final : public so_5::signal_t {};
struct msg_20 final : public so_5::signal_t {};
struct msg_21 final : public so_5::signal_t {};
struct msg_22 final : public so_5::signal_t {};
struct msg_23 final : public so_5::signal_t {};
struct msg_24 final : public so_5::signal_t {};
using mboxes_container_t = std::vector< so_5::mbox_t >;
using msinks_container_t = std::vector< so_5::msink_t >;
[[nodiscard]]
mboxes_container_t
make_mboxes( so_5::environment_t & env, std::size_t count )
{
mboxes_container_t result;
result.reserve( count );
for( std::size_t i = 0u; i < count; ++i )
result.push_back( env.create_mbox() );
return result;
}
[[nodiscard]]
msinks_container_t
make_msinks( const so_5::mbox_t & actual_dest, std::size_t count )
{
msinks_container_t result;
result.reserve( count );
for( std::size_t i = 0u; i < count; ++i )
result.push_back( so_5::wrap_to_msink( actual_dest ) );
return result;
}
template< typename Bindings >
class benchmarker_t
{
const mboxes_container_t & m_mboxes;
msinks_container_t m_msinks;
Bindings m_bindings;
public:
benchmarker_t(
const mboxes_container_t & mboxes,
const so_5::mbox_t & actual_dest,
std::size_t msinks_count )
: m_mboxes{ mboxes }
, m_msinks{ make_msinks( actual_dest, msinks_count ) }
{}
void
benchmark_bind()
{
const auto started_at = hires_clock::now();
for( const auto & sink : m_msinks )
{
for( const auto & box : m_mboxes )
{
m_bindings.template bind< msg_01 >( box, sink );
m_bindings.template bind< msg_02 >( box, sink );
m_bindings.template bind< msg_03 >( box, sink );
m_bindings.template bind< msg_04 >( box, sink );
m_bindings.template bind< msg_05 >( box, sink );
m_bindings.template bind< msg_06 >( box, sink );
m_bindings.template bind< msg_07 >( box, sink );
m_bindings.template bind< msg_08 >( box, sink );
m_bindings.template bind< msg_09 >( box, sink );
m_bindings.template bind< msg_10 >( box, sink );
m_bindings.template bind< msg_11 >( box, sink );
m_bindings.template bind< msg_12 >( box, sink );
m_bindings.template bind< msg_13 >( box, sink );
m_bindings.template bind< msg_15 >( box, sink );
m_bindings.template bind< msg_16 >( box, sink );
m_bindings.template bind< msg_17 >( box, sink );
m_bindings.template bind< msg_18 >( box, sink );
m_bindings.template bind< msg_19 >( box, sink );
m_bindings.template bind< msg_20 >( box, sink );
m_bindings.template bind< msg_21 >( box, sink );
m_bindings.template bind< msg_22 >( box, sink );
m_bindings.template bind< msg_23 >( box, sink );
m_bindings.template bind< msg_24 >( box, sink );
}
}
const auto finished_at = hires_clock::now();
std::cout << "bind: " << std::chrono::duration_cast<
std::chrono::milliseconds >( finished_at - started_at ).count()
<< "ms" << std::endl;
}
void
benchmark_unbind()
{
const auto started_at = hires_clock::now();
for( const auto & sink : m_msinks )
{
for( const auto & box : m_mboxes )
{
m_bindings.template unbind< msg_01 >( box, sink );
m_bindings.template unbind< msg_02 >( box, sink );
m_bindings.template unbind< msg_03 >( box, sink );
m_bindings.template unbind< msg_04 >( box, sink );
m_bindings.template unbind< msg_05 >( box, sink );
m_bindings.template unbind< msg_06 >( box, sink );
m_bindings.template unbind< msg_07 >( box, sink );
m_bindings.template unbind< msg_08 >( box, sink );
m_bindings.template unbind< msg_09 >( box, sink );
m_bindings.template unbind< msg_10 >( box, sink );
m_bindings.template unbind< msg_11 >( box, sink );
m_bindings.template unbind< msg_12 >( box, sink );
m_bindings.template unbind< msg_13 >( box, sink );
m_bindings.template unbind< msg_15 >( box, sink );
m_bindings.template unbind< msg_16 >( box, sink );
m_bindings.template unbind< msg_17 >( box, sink );
m_bindings.template unbind< msg_18 >( box, sink );
m_bindings.template unbind< msg_19 >( box, sink );
m_bindings.template unbind< msg_20 >( box, sink );
m_bindings.template unbind< msg_21 >( box, sink );
m_bindings.template unbind< msg_22 >( box, sink );
m_bindings.template unbind< msg_23 >( box, sink );
m_bindings.template unbind< msg_24 >( box, sink );
}
}
const auto finished_at = hires_clock::now();
std::cout << "unbind: " << std::chrono::duration_cast<
std::chrono::milliseconds >( finished_at - started_at ).count()
<< "ms" << std::endl;
}
void
benchmark_unbind_all()
{
const auto started_at = hires_clock::now();
for( const auto & sink : m_msinks )
{
for( const auto & box : m_mboxes )
{
m_bindings.unbind_all( box, sink );
}
}
const auto finished_at = hires_clock::now();
std::cout << "unbind_all: " << std::chrono::duration_cast<
std::chrono::milliseconds >( finished_at - started_at ).count()
<< "ms" << std::endl;
}
};
void
do_benchmarks(
so_5::environment_t & env,
std::size_t mboxes_count,
std::size_t msinks_count )
{
std::cout << "=== mboxes: " << mboxes_count << ", msinks: " << msinks_count << std::endl;
const auto actual_dest = env.create_mbox();
const auto mboxes = make_mboxes( env, mboxes_count );
const auto do_bench = []( auto & benchmarker ) {
benchmarker.benchmark_bind();
benchmarker.benchmark_unbind_all();
benchmarker.benchmark_bind();
benchmarker.benchmark_unbind();
};
{
std::cout << "*** several nested maps ***" << std::endl;
benchmarker_t< so_5::multi_sink_v1::multi_sink_binding_t<> > benchmarker{
mboxes,
actual_dest,
msinks_count
};
do_bench( benchmarker );
}
{
std::cout << "*** just one map ***" << std::endl;
benchmarker_t< so_5::multi_sink_v2::multi_sink_binding_t<> > benchmarker{
mboxes,
actual_dest,
msinks_count
};
do_bench( benchmarker );
}
}
int
main()
{
so_5::launch( []( so_5::environment_t & env ) {
do_benchmarks( env, 20000, 10 );
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment