rippled
Loading...
Searching...
No Matches
Handler_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2023 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/jtx.h>
21
22#include <xrpld/rpc/detail/Handler.h>
23
24#include <xrpl/beast/unit_test.h>
25
26#include <chrono>
27#include <iostream>
28#include <limits>
29#include <random>
30
31namespace ripple::test {
32
33// NOTE: there should be no need for this function;
34// `std::cout << some_duration` should just work if built with a compliant
35// C++20 compiler. Sadly, we are not using one, as of today
36// TODO: remove this operator<< overload when we bump compiler version
39{
40 return (os << ns.count() << "ns");
41}
42
43// NOTE This is a rather naive effort at a microbenchmark. Ideally we want
44// Google Benchmark, or something similar. Also, this actually does not belong
45// to unit tests, as it makes little sense to run it in conditions very
46// dissimilar to how rippled will normally work.
47// TODO as https://github.com/XRPLF/rippled/issues/4765
48
50{
51 auto
52 time(std::size_t n, auto f, auto prng) -> auto
53 {
54 using clock = std::chrono::steady_clock;
55 assert(n > 0);
56 double sum = 0;
57 double sum_squared = 0;
58 std::size_t j = 0;
59 while (j < n)
60 {
61 // Generate 100 inputs upfront, separated from the inner loop
62 std::array<decltype(prng()), 100> inputs = {};
63 for (auto& i : inputs)
64 {
65 i = prng();
66 }
67
68 // Take 100 samples, then sort and throw away 35 from each end,
69 // using only middle 30. This helps to reduce measurement noise.
70 std::array<long, 100> samples = {};
71 for (std::size_t k = 0; k < 100; ++k)
72 {
73 auto start = std::chrono::steady_clock::now();
74 f(inputs[k]);
75 samples[k] = (std::chrono::steady_clock::now() - start).count();
76 }
77
78 std::sort(samples.begin(), samples.end());
79 for (std::size_t k = 35; k < 65; ++k)
80 {
81 j += 1;
82 sum += samples[k];
83 sum_squared += (samples[k] * samples[k]);
84 }
85 }
86
87 double const mean_squared = (sum * sum) / (j * j);
88 return std::make_tuple(
89 clock::duration{static_cast<long>(sum / j)},
90 clock::duration{
91 static_cast<long>(std::sqrt((sum_squared / j) - mean_squared))},
92 j);
93 }
94
95 void
97 {
98 testcase("Handler lookup performance");
99
101 std::ranlux48 prng(dev());
102
105
107
108 std::size_t dummy = 0;
109 auto const [mean, stdev, n] = time(
110 1'000'000,
111 [&](std::size_t i) {
112 auto const d = RPC::getHandler(1, false, names[i]);
113 dummy = dummy + i + (int)d->role_;
114 },
115 [&]() -> std::size_t { return distr(prng); });
116
117 std::cout << "mean=" << mean << " stdev=" << stdev << " N=" << n
118 << '\n';
119
120 BEAST_EXPECT(dummy != 0);
121 }
122
123public:
124 void
125 run() override
126 {
128 }
129};
130
131BEAST_DEFINE_TESTSUITE_MANUAL(Handler, test, ripple);
132
133} // namespace ripple::test
T begin(T... args)
A testsuite class.
Definition: suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition: suite.h:155
auto time(std::size_t n, auto f, auto prng) -> auto
void run() override
Runs the suite.
T end(T... args)
T make_tuple(T... args)
std::set< char const * > getHandlerNames()
Return names of all methods.
Definition: Handler.cpp:318
Handler const * getHandler(unsigned version, bool betaEnabled, std::string const &name)
Definition: Handler.cpp:312
auto make_vector(Input const &input)
Definition: TestHelpers.h:44
std::ostream & operator<<(std::ostream &os, PrettyAmount const &amount)
Definition: amount.cpp:73
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
static auto sum(TCollection const &col)
Definition: BookStep.cpp:1001
T size(T... args)
T sort(T... args)
T sqrt(T... args)