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#include <xrpld/rpc/detail/Handler.h>
22#include <xrpl/beast/unit_test.h>
23
24#include <chrono>
25#include <iostream>
26#include <limits>
27#include <random>
28
29namespace ripple::test {
30
31// NOTE: there should be no need for this function;
32// `std::cout << some_duration` should just work if built with a compliant
33// C++20 compiler. Sadly, we are not using one, as of today
34// TODO: remove this operator<< overload when we bump compiler version
37{
38 return (os << ns.count() << "ns");
39}
40
41// NOTE This is a rather naive effort at a microbenchmark. Ideally we want
42// Google Benchmark, or something similar. Also, this actually does not belong
43// to unit tests, as it makes little sense to run it in conditions very
44// dissimilar to how rippled will normally work.
45// TODO as https://github.com/XRPLF/rippled/issues/4765
46
48{
49 auto
50 time(std::size_t n, auto f, auto prng) -> auto
51 {
52 using clock = std::chrono::steady_clock;
53 assert(n > 0);
54 double sum = 0;
55 double sum_squared = 0;
56 std::size_t j = 0;
57 while (j < n)
58 {
59 // Generate 100 inputs upfront, separated from the inner loop
60 std::array<decltype(prng()), 100> inputs = {};
61 for (auto& i : inputs)
62 {
63 i = prng();
64 }
65
66 // Take 100 samples, then sort and throw away 35 from each end,
67 // using only middle 30. This helps to reduce measurement noise.
68 std::array<long, 100> samples = {};
69 for (std::size_t k = 0; k < 100; ++k)
70 {
71 auto start = std::chrono::steady_clock::now();
72 f(inputs[k]);
73 samples[k] = (std::chrono::steady_clock::now() - start).count();
74 }
75
76 std::sort(samples.begin(), samples.end());
77 for (std::size_t k = 35; k < 65; ++k)
78 {
79 j += 1;
80 sum += samples[k];
81 sum_squared += (samples[k] * samples[k]);
82 }
83 }
84
85 const double mean_squared = (sum * sum) / (j * j);
86 return std::make_tuple(
87 clock::duration{static_cast<long>(sum / j)},
88 clock::duration{
89 static_cast<long>(std::sqrt((sum_squared / j) - mean_squared))},
90 j);
91 }
92
93 void
95 {
96 testcase("Handler lookup performance");
97
99 std::ranlux48 prng(dev());
100
103
105
106 std::size_t dummy = 0;
107 auto const [mean, stdev, n] = time(
108 1'000'000,
109 [&](std::size_t i) {
110 auto const d = RPC::getHandler(1, false, names[i]);
111 dummy = dummy + i + (int)d->role_;
112 },
113 [&]() -> std::size_t { return distr(prng); });
114
115 std::cout << "mean=" << mean << " stdev=" << stdev << " N=" << n
116 << '\n';
117
118 BEAST_EXPECT(dummy != 0);
119 }
120
121public:
122 void
123 run() override
124 {
126 }
127};
128
129BEAST_DEFINE_TESTSUITE_MANUAL(Handler, test, ripple);
130
131} // 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:316
Handler const * getHandler(unsigned version, bool betaEnabled, std::string const &name)
Definition: Handler.cpp:310
auto make_vector(Input const &input)
Definition: TestHelpers.h:43
std::ostream & operator<<(std::ostream &os, PrettyAmount const &amount)
Definition: amount.cpp:72
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:999
T size(T... args)
T sort(T... args)
T sqrt(T... args)