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