Bitcoin Core  22.99.0
P2P Digital Currency
bench.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * Copyright (c) 2018 Pieter Wuille, Greg Maxwell, Gleb Naumenko *
3  * Distributed under the MIT software license, see the accompanying *
4  * file LICENSE or http://www.opensource.org/licenses/mit-license.php.*
5  **********************************************************************/
6 
7 #include "../include/minisketch.h"
8 #include <string.h>
9 #include <memory>
10 #include <vector>
11 #include <chrono>
12 #include <random>
13 #include <set>
14 #include <algorithm>
15 
16 int main(int argc, char** argv) {
17  if (argc < 1 || argc > 4) {
18  printf("Usage: %s [syndromes=150] [errors=syndromes] [iters=10]\n", argv[0]);
19  return 1;
20  }
21  int syndromes = argc > 1 ? strtoul(argv[1], NULL, 10) : 150;
22  int errors = argc > 2 ? strtoul(argv[2], NULL, 10) : syndromes;
23  int iters = argc > 3 ? strtoul(argv[3], NULL, 10) : 10;
24  if (syndromes < 0 || syndromes > 1000000) {
25  printf("Number of syndromes (%i) out of range 0..1000000\n", syndromes);
26  return 1;
27  }
28  if (errors < 0) {
29  printf("Number of errors (%i) is negative(%i)\n", errors, syndromes);
30  return 1;
31  }
32  if (iters < 0 || iters > 1000000000) {
33  printf("Number of iterations (%i) out of range 0..1000000000\n", iters);
34  return 1;
35  }
36  uint32_t max_impl = minisketch_implementation_max();
37  for (int bits = 2; bits <= 64; ++bits) {
38  if (errors > pow(2.0, bits - 1)) continue;
39  if (!minisketch_bits_supported(bits)) continue;
40  printf("recover[ms]\t% 3i\t", bits);
41  for (uint32_t impl = 0; impl <= max_impl; ++impl) {
42  std::vector<minisketch*> states;
43  std::vector<uint64_t> roots(2 * syndromes);
44  std::random_device rng;
45  std::uniform_int_distribution<uint64_t> dist(1, (uint64_t(1) << bits) - 1);
46  states.resize(iters);
47  std::vector<double> benches;
48  benches.reserve(iters);
49  for (int i = 0; i < iters; ++i) {
50  states[i] = minisketch_create(bits, impl, syndromes);
51  if (!states[i]) break;
52  std::set<uint64_t> done;
53  for (int j = 0; j < errors; ++j) {
54  uint64_t r;
55  do {
56  r = dist(rng);
57  } while (done.count(r));
58  done.insert(r);
59  minisketch_add_uint64(states[i], r);
60  }
61  }
62  if (!states[0]) {
63  printf(" -\t");
64  } else {
65  double total = 0.0;
66  for (auto& state : states) {
67  auto start = std::chrono::steady_clock::now();
68  minisketch_decode(state, 2 * syndromes, roots.data());
69  auto stop = std::chrono::steady_clock::now();
70  std::chrono::duration<double> dur(stop - start);
71  total += dur.count();
72  benches.push_back(dur.count());
73  }
74  std::sort(benches.begin(), benches.end());
75  printf("% 10.5f\t", benches[0] * 1000.0);
76  }
77  for (auto& state : states) {
78  minisketch_destroy(state);
79  }
80  }
81  printf("\n");
82  printf("create[ns]\t% 3i\t", bits);
83  for (uint32_t impl = 0; impl <= max_impl; ++impl) {
84  std::vector<minisketch*> states;
85  std::random_device rng;
86  std::uniform_int_distribution<uint64_t> dist;
87  std::vector<uint64_t> data;
88  data.resize(errors * 10);
89  states.resize(iters);
90  std::vector<double> benches;
91  benches.reserve(iters);
92  for (int i = 0; i < iters; ++i) {
93  states[i] = minisketch_create(bits, impl, syndromes);
94  }
95  for (size_t i = 0; i < data.size(); ++i) {
96  data[i] = dist(rng);
97  }
98  if (!states[0]) {
99  printf(" -\t");
100  } else {
101  double total = 0.0;
102  for (auto& state : states) {
103  auto start = std::chrono::steady_clock::now();
104  for (auto val : data) {
105  minisketch_add_uint64(state, val);
106  }
107  auto stop = std::chrono::steady_clock::now();
108  std::chrono::duration<double> dur(stop - start);
109  total += dur.count();
110  benches.push_back(dur.count());
111  }
112  std::sort(benches.begin(), benches.end());
113  printf("% 10.5f\t", benches[0] * 1000000000.0 / data.size() / syndromes);
114  }
115  for (auto& state : states) {
116  minisketch_destroy(state);
117  }
118  }
119  printf("\n");
120  }
121  return 0;
122 }
minisketch_create
MINISKETCH_API minisketch * minisketch_create(uint32_t bits, uint32_t implementation, size_t capacity)
Construct a sketch for a given element size, implementation and capacity.
Definition: minisketch.cpp:380
string.h
tinyformat::printf
void printf(const char *fmt, const Args &... args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1079
minisketch_bits_supported
MINISKETCH_API int minisketch_bits_supported(uint32_t bits)
Determine whether support for elements of bits bits was compiled in.
Definition: minisketch.cpp:165
minisketch_destroy
MINISKETCH_API void minisketch_destroy(minisketch *sketch)
Destroy a sketch.
Definition: minisketch.cpp:426
minisketch_decode
MINISKETCH_API ssize_t minisketch_decode(const minisketch *sketch, size_t max_elements, uint64_t *output)
Decode a sketch.
Definition: minisketch.cpp:470
main
int main(int argc, char **argv)
Definition: bench.cpp:16
minisketch_implementation_max
MINISKETCH_API uint32_t minisketch_implementation_max(void)
Determine the maximum number of implementations available.
Definition: minisketch.cpp:358
stop
static RPCHelpMan stop()
Definition: server.cpp:161
minisketch_add_uint64
MINISKETCH_API void minisketch_add_uint64(minisketch *sketch, uint64_t element)
Add an element to a sketch.
Definition: minisketch.cpp:454