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 ripple::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{
72 static_cast<long>(std::sqrt((sum_squared / j) - mean_squared))},
73 j);
74 }
75
76 void
78 {
79 testcase("Handler lookup performance");
80
82 std::ranlux48 prng(dev());
83
86
88
89 std::size_t dummy = 0;
90 auto const [mean, stdev, n] = time(
91 1'000'000,
92 [&](std::size_t i) {
93 auto const d = RPC::getHandler(1, false, names[i]);
94 dummy = dummy + i + (int)d->role_;
95 },
96 [&]() -> std::size_t { return distr(prng); });
97
98 std::cout << "mean=" << mean << " stdev=" << stdev << " N=" << n
99 << '\n';
100
101 BEAST_EXPECT(dummy != 0);
102 }
103
104public:
105 void
106 run() override
107 {
109 }
110};
111
112BEAST_DEFINE_TESTSUITE_MANUAL(Handler, rpc, ripple);
113
114} // namespace ripple::test
T begin(T... args)
A testsuite class.
Definition suite.h:52
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:152
auto time(std::size_t n, auto f, auto prng) -> auto
void run() override
Runs the suite.
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition rpc.h:16
T end(T... args)
T make_tuple(T... args)
std::set< char const * > getHandlerNames()
Return names of all methods.
Definition Handler.cpp:300
Handler const * getHandler(unsigned version, bool betaEnabled, std::string const &name)
Definition Handler.cpp:294
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:6
static auto sum(TCollection const &col)
Definition BookStep.cpp:976
T size(T... args)
T sort(T... args)
T sqrt(T... args)