rippled
Loading...
Searching...
No Matches
Handler_test.cpp
1#include <test/jtx.h>
2
3#include <xrpld/rpc/detail/Handler.h>
4
5#include <xrpl/beast/unit_test.h>
6
7#include <chrono>
8#include <iostream>
9#include <limits>
10#include <random>
11
12namespace xrpl::test {
13
14// NOTE: there should be no need for this function;
15// `std::cout << some_duration` should just work if built with a compliant
16// C++20 compiler. Sadly, we are not using one, as of today
17// TODO: remove this operator<< overload when we bump compiler version
20{
21 return (os << ns.count() << "ns");
22}
23
24// NOTE This is a rather naive effort at a microbenchmark. Ideally we want
25// Google Benchmark, or something similar. Also, this actually does not belong
26// to unit tests, as it makes little sense to run it in conditions very
27// dissimilar to how rippled will normally work.
28// TODO as https://github.com/XRPLF/rippled/issues/4765
29
31{
32 auto
33 time(std::size_t n, auto f, auto prng) -> auto
34 {
35 using clock = std::chrono::steady_clock;
36 assert(n > 0);
37 double sum = 0;
38 double sum_squared = 0;
39 std::size_t j = 0;
40 while (j < n)
41 {
42 // Generate 100 inputs upfront, separated from the inner loop
43 std::array<decltype(prng()), 100> inputs = {};
44 for (auto& i : inputs)
45 {
46 i = prng();
47 }
48
49 // Take 100 samples, then sort and throw away 35 from each end,
50 // using only middle 30. This helps to reduce measurement noise.
51 std::array<long, 100> samples = {};
52 for (std::size_t k = 0; k < 100; ++k)
53 {
54 auto start = std::chrono::steady_clock::now();
55 f(inputs[k]);
56 samples[k] = (std::chrono::steady_clock::now() - start).count();
57 }
58
59 std::sort(samples.begin(), samples.end());
60 for (std::size_t k = 35; k < 65; ++k)
61 {
62 j += 1;
63 sum += samples[k];
64 sum_squared += (samples[k] * samples[k]);
65 }
66 }
67
68 double const mean_squared = (sum * sum) / (j * j);
69 return std::make_tuple(
70 clock::duration{static_cast<long>(sum / j)},
71 clock::duration{static_cast<long>(std::sqrt((sum_squared / j) - mean_squared))},
72 j);
73 }
74
75 void
77 {
78 testcase("Handler lookup performance");
79
81 std::ranlux48 prng(dev());
82
84
86
87 std::size_t dummy = 0;
88 auto const [mean, stdev, n] = time(
89 1'000'000,
90 [&](std::size_t i) {
91 auto const d = RPC::getHandler(1, false, names[i]);
92 dummy = dummy + i + (int)d->role_;
93 },
94 [&]() -> std::size_t { return distr(prng); });
95
96 std::cout << "mean=" << mean << " stdev=" << stdev << " N=" << n << '\n';
97
98 BEAST_EXPECT(dummy != 0);
99 }
100
101public:
102 void
103 run() override
104 {
106 }
107};
108
109BEAST_DEFINE_TESTSUITE_MANUAL(Handler, rpc, xrpl);
110
111} // namespace xrpl::test
T begin(T... args)
A testsuite class.
Definition suite.h:51
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:147
void run() override
Runs the suite.
auto time(std::size_t n, auto f, auto prng) -> auto
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition rpc.h:15
T end(T... args)
T make_tuple(T... args)
std::set< char const * > getHandlerNames()
Return names of all methods.
Definition Handler.cpp:242
Handler const * getHandler(unsigned version, bool betaEnabled, std::string const &name)
Definition Handler.cpp:236
auto make_vector(Input const &input)
std::ostream & operator<<(std::ostream &os, PrettyAmount const &amount)
Definition amount.cpp:54
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
static auto sum(TCollection const &col)
Definition BookStep.cpp:872
T size(T... args)
T sort(T... args)
T sqrt(T... args)