2023-06-05 00:16:05 +01:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2018-11-09 05:49:40 -05:00
|
|
|
|
|
|
|
#if __cplusplus >= 201103L
|
|
|
|
|
|
|
|
#include "radix_tree.hpp"
|
|
|
|
#include "trie.hpp"
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <random>
|
2019-02-13 13:47:39 -05:00
|
|
|
#include <ratio>
|
2018-11-09 05:49:40 -05:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
const std::size_t nkeys = 10000;
|
2019-02-13 13:47:39 -05:00
|
|
|
const std::size_t nqueries = 1000000;
|
2018-11-09 05:49:40 -05:00
|
|
|
const std::size_t warmup_runs = 10;
|
|
|
|
const std::size_t samples = 10;
|
|
|
|
const std::size_t key_length = 20;
|
|
|
|
const char *chars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
|
const int chars_len = 36;
|
|
|
|
|
|
|
|
template <class T>
|
2019-06-28 11:57:45 -04:00
|
|
|
void benchmark_lookup (T &subscriptions_,
|
|
|
|
std::vector<unsigned char *> &queries_)
|
2018-11-09 05:49:40 -05:00
|
|
|
{
|
2019-02-13 13:47:39 -05:00
|
|
|
using namespace std::chrono;
|
2019-07-11 13:13:52 +02:00
|
|
|
std::vector<duration<long, std::nano> > samples_vec;
|
2019-02-13 13:47:39 -05:00
|
|
|
samples_vec.reserve (samples);
|
|
|
|
|
2018-11-09 05:49:40 -05:00
|
|
|
for (std::size_t run = 0; run < warmup_runs; ++run) {
|
2019-06-28 11:57:45 -04:00
|
|
|
for (auto &query : queries_)
|
|
|
|
subscriptions_.check (query, key_length);
|
2018-11-09 05:49:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::size_t run = 0; run < samples; ++run) {
|
2019-02-13 13:47:39 -05:00
|
|
|
duration<long, std::nano> interval (0);
|
2019-06-28 11:57:45 -04:00
|
|
|
for (auto &query : queries_) {
|
2019-02-13 13:47:39 -05:00
|
|
|
auto start = steady_clock::now ();
|
2019-06-28 11:57:45 -04:00
|
|
|
subscriptions_.check (query, key_length);
|
2019-02-13 13:47:39 -05:00
|
|
|
auto end = steady_clock::now ();
|
|
|
|
interval += end - start;
|
|
|
|
}
|
2019-06-28 11:57:45 -04:00
|
|
|
samples_vec.push_back (interval / queries_.size ());
|
2018-11-09 05:49:40 -05:00
|
|
|
}
|
|
|
|
|
2019-02-13 13:47:39 -05:00
|
|
|
std::size_t sum = 0;
|
2018-11-09 05:49:40 -05:00
|
|
|
for (const auto &sample : samples_vec)
|
2019-02-13 13:47:39 -05:00
|
|
|
sum += sample.count ();
|
|
|
|
std::printf ("Average lookup time = %.1lf ns\n",
|
|
|
|
static_cast<double> (sum) / samples);
|
2018-11-09 05:49:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
// Generate input set.
|
|
|
|
std::minstd_rand rng (123456789);
|
|
|
|
std::vector<unsigned char *> input_set;
|
|
|
|
std::vector<unsigned char *> queries;
|
|
|
|
input_set.reserve (nkeys);
|
|
|
|
queries.reserve (nqueries);
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < nkeys; ++i) {
|
|
|
|
unsigned char *key = new unsigned char[key_length];
|
|
|
|
for (std::size_t j = 0; j < key_length; j++)
|
|
|
|
key[j] = static_cast<unsigned char> (chars[rng () % chars_len]);
|
|
|
|
input_set.emplace_back (key);
|
|
|
|
}
|
|
|
|
for (std::size_t i = 0; i < nqueries; ++i)
|
|
|
|
queries.push_back (input_set[rng () % nkeys]);
|
|
|
|
|
|
|
|
// Initialize both data structures.
|
|
|
|
//
|
|
|
|
// Keeping initialization out of the benchmarking function helps
|
|
|
|
// heaptrack detect peak memory consumption of the radix tree.
|
|
|
|
zmq::trie_t trie;
|
2019-06-28 11:57:45 -04:00
|
|
|
zmq::radix_tree_t radix_tree;
|
2018-11-09 05:49:40 -05:00
|
|
|
for (auto &key : input_set) {
|
|
|
|
trie.add (key, key_length);
|
|
|
|
radix_tree.add (key, key_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a benchmark.
|
2019-02-13 13:47:39 -05:00
|
|
|
std::printf ("keys = %llu, queries = %llu, key size = %llu\n",
|
|
|
|
static_cast<unsigned long long> (nkeys),
|
|
|
|
static_cast<unsigned long long> (nqueries),
|
|
|
|
static_cast<unsigned long long> (key_length));
|
2018-11-09 05:49:40 -05:00
|
|
|
std::puts ("[trie]");
|
2019-07-12 11:29:14 -04:00
|
|
|
benchmark_lookup (trie, queries);
|
2018-11-09 05:49:40 -05:00
|
|
|
|
|
|
|
std::puts ("[radix_tree]");
|
2019-07-12 11:29:14 -04:00
|
|
|
benchmark_lookup (radix_tree, queries);
|
2018-11-09 05:49:40 -05:00
|
|
|
|
|
|
|
for (auto &op : input_set)
|
|
|
|
delete[] op;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|