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