benchmark_radix_tree.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright (c) 2018 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #if __cplusplus >= 201103L
  25. #include "radix_tree.hpp"
  26. #include "trie.hpp"
  27. #include <chrono>
  28. #include <cstddef>
  29. #include <cstdio>
  30. #include <random>
  31. #include <ratio>
  32. #include <vector>
  33. const std::size_t nkeys = 10000;
  34. const std::size_t nqueries = 1000000;
  35. const std::size_t warmup_runs = 10;
  36. const std::size_t samples = 10;
  37. const std::size_t key_length = 20;
  38. const char *chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  39. const int chars_len = 36;
  40. template <class T>
  41. void benchmark_lookup (T &subscriptions_,
  42. std::vector<unsigned char *> &queries_)
  43. {
  44. using namespace std::chrono;
  45. std::vector<duration<long, std::nano> > samples_vec;
  46. samples_vec.reserve (samples);
  47. for (std::size_t run = 0; run < warmup_runs; ++run) {
  48. for (auto &query : queries_)
  49. subscriptions_.check (query, key_length);
  50. }
  51. for (std::size_t run = 0; run < samples; ++run) {
  52. duration<long, std::nano> interval (0);
  53. for (auto &query : queries_) {
  54. auto start = steady_clock::now ();
  55. subscriptions_.check (query, key_length);
  56. auto end = steady_clock::now ();
  57. interval += end - start;
  58. }
  59. samples_vec.push_back (interval / queries_.size ());
  60. }
  61. std::size_t sum = 0;
  62. for (const auto &sample : samples_vec)
  63. sum += sample.count ();
  64. std::printf ("Average lookup time = %.1lf ns\n",
  65. static_cast<double> (sum) / samples);
  66. }
  67. int main ()
  68. {
  69. // Generate input set.
  70. std::minstd_rand rng (123456789);
  71. std::vector<unsigned char *> input_set;
  72. std::vector<unsigned char *> queries;
  73. input_set.reserve (nkeys);
  74. queries.reserve (nqueries);
  75. for (std::size_t i = 0; i < nkeys; ++i) {
  76. unsigned char *key = new unsigned char[key_length];
  77. for (std::size_t j = 0; j < key_length; j++)
  78. key[j] = static_cast<unsigned char> (chars[rng () % chars_len]);
  79. input_set.emplace_back (key);
  80. }
  81. for (std::size_t i = 0; i < nqueries; ++i)
  82. queries.push_back (input_set[rng () % nkeys]);
  83. // Initialize both data structures.
  84. //
  85. // Keeping initialization out of the benchmarking function helps
  86. // heaptrack detect peak memory consumption of the radix tree.
  87. zmq::trie_t trie;
  88. zmq::radix_tree_t radix_tree;
  89. for (auto &key : input_set) {
  90. trie.add (key, key_length);
  91. radix_tree.add (key, key_length);
  92. }
  93. // Create a benchmark.
  94. std::printf ("keys = %llu, queries = %llu, key size = %llu\n",
  95. static_cast<unsigned long long> (nkeys),
  96. static_cast<unsigned long long> (nqueries),
  97. static_cast<unsigned long long> (key_length));
  98. std::puts ("[trie]");
  99. benchmark_lookup (trie, queries);
  100. std::puts ("[radix_tree]");
  101. benchmark_lookup (radix_tree, queries);
  102. for (auto &op : input_set)
  103. delete[] op;
  104. }
  105. #else
  106. int main ()
  107. {
  108. }
  109. #endif