Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark 'add' and 'rm' of generic_mtrie #4574

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,13 @@ if(BUILD_SHARED)
if(ZMQ_HAVE_WINDOWS_UWP)
set_target_properties(benchmark_radix_tree PROPERTIES LINK_FLAGS_DEBUG "/OPT:NOICF /OPT:NOREF")
endif()

add_executable(benchmark_generic_mtrie perf/benchmark_generic_mtrie.cpp)
target_link_libraries(benchmark_gneric_mtrie libzmq-static)
target_include_directories(benchmark_generic_mtrie PUBLIC "${CMAKE_CURRENT_LIST_DIR}/src")
if(ZMQ_HAVE_WINDOWS_UWP)
set_target_properties(benchmark_generic_mtrie PROPERTIES LINK_FLAGS_DEBUG "/OPT:NOICF /OPT:NOREF")
endif()
endif()
elseif(WITH_PERF_TOOL)
message(FATAL_ERROR "Shared library disabled - perf-tools unavailable.")
Expand Down
9 changes: 8 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,20 @@ perf_proxy_thr_SOURCES = perf/proxy_thr.cpp

if ENABLE_STATIC
noinst_PROGRAMS += \
perf/benchmark_radix_tree
perf/benchmark_radix_tree \
perf/benchmark_generic_mtrie

perf_benchmark_radix_tree_DEPENDENCIES = src/libzmq.la
perf_benchmark_radix_tree_CPPFLAGS = -I$(top_srcdir)/src
perf_benchmark_radix_tree_LDADD = $(top_builddir)/src/.libs/libzmq.a \
${src_libzmq_la_LIBADD}
perf_benchmark_radix_tree_SOURCES = perf/benchmark_radix_tree.cpp

perf_benchmark_generic_mtrie_DEPENDENCIES = src/libzmq.la
perf_benchmark_generic_mtrie_CPPFLAGS = -I$(top_srcdir)/src
perf_benchmark_generic_mtrie_LDADD = $(top_builddir)/src/.libs/libzmq.a \
${src_libzmq_la_LIBADD}
perf_benchmark_generic_mtrie_SOURCES = perf/benchmark_generic_mtrie.cpp
endif
endif

Expand Down
141 changes: 141 additions & 0 deletions perf/benchmark_generic_mtrie.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
Copyright (c) 2023 Contributors as noted in the AUTHORS file

This file is part of libzmq, the ZeroMQ core engine in C++.

libzmq is free software; you can redistribute it and/or modify it under
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The license has changed, please check other files and update the header to match

the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

As a special exception, the Contributors give you permission to link
this library with independent modules to produce an executable,
regardless of the license terms of these independent modules, and to
copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the
terms and conditions of the license of that module. An independent
module is a module which is not derived from or based on this library.
If you modify this library, you must extend this exception to your
version of the library.

libzmq is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#if __cplusplus >= 201103L
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this necessary?


#include "generic_mtrie_impl.hpp"

#include <chrono>
#include <cstdio>
#include <random>
#include <ratio>
#include <vector>
#include <string>

const std::size_t nprefix = 1000000;
const std::size_t samples = 10;

template <class T>
void benchmark_add (
T &subscriptions_,
std::vector<std::pair<zmq::generic_mtrie_t<uint64_t>::prefix_t, size_t> >
&prefixes_,
uint64_t *pipes_)
{
using namespace std::chrono;
std::vector<duration<long, std::nano> > samples_vec;
samples_vec.reserve (samples);

for (std::size_t run = 0; run < samples; ++run) {
duration<long, std::nano> interval (0);
for (std::size_t i = 0; i < prefixes_.size (); i++) {
auto start = steady_clock::now ();
subscriptions_.add (prefixes_[i].first, prefixes_[i].second,
&pipes_[i]);
auto end = steady_clock::now ();
interval += end - start;
}
samples_vec.push_back (interval / prefixes_.size ());
}

std::size_t sum = 0;
for (const auto &sample : samples_vec)
sum += sample.count ();
std::printf ("Average operation time = %.1lf ns\n",
static_cast<double> (sum) / samples);
}

template <class T>
void benchmark_rm (
T &subscriptions_,
std::vector<std::pair<zmq::generic_mtrie_t<uint64_t>::prefix_t, size_t> >
&prefixes_,
uint64_t *pipes_)
{
using namespace std::chrono;
std::vector<duration<long, std::nano> > samples_vec;
samples_vec.reserve (samples);

for (std::size_t run = 0; run < samples; ++run) {
duration<long, std::nano> interval (0);
for (std::size_t i = 0; i < prefixes_.size (); i++) {
auto start = steady_clock::now ();
subscriptions_.rm (prefixes_[i].first, prefixes_[i].second,
&pipes_[i]);
auto end = steady_clock::now ();
interval += end - start;
}
samples_vec.push_back (interval / prefixes_.size ());
}

std::size_t sum = 0;
for (const auto &sample : samples_vec)
sum += sample.count ();
std::printf ("Average operation time = %.1lf ns\n",
static_cast<double> (sum) / samples);
}

int main ()
{
// Generate input set.
uint64_t pipes[nprefix];
std::vector<std::pair<zmq::generic_mtrie_t<uint64_t>::prefix_t, size_t> >
prefixes;
prefixes.reserve (nprefix);
for (std::size_t i = 0; i < nprefix; ++i) {
char prefix[100];
size_t len = sprintf (prefix, "prefix_%lu", i);
prefixes.push_back (std::make_pair (
reinterpret_cast<zmq::generic_mtrie_t<uint64_t>::prefix_t> (prefix),
len));
}

// Initialize data structures.
//
// Keeping initialization out of the benchmarking function helps
// heaptrack detect peak memory consumption of the radix tree.
zmq::generic_mtrie_t<uint64_t> mtrie;

// Create a benchmark.
std::printf ("prefixes = %llu\n",
static_cast<unsigned long long> (nprefix));
std::puts ("[generic_mtrie::add]");
benchmark_add (mtrie, prefixes, pipes);

std::puts ("[generic_mtrie::rm]");
benchmark_rm (mtrie, prefixes, pipes);
}

#else

int main ()
{
}

#endif