rippled
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 <ripple/beast/unit_test.h>
21 #include <ripple/rpc/impl/Handler.h>
22 #include <test/jtx.h>
23 
24 #include <chrono>
25 #include <iostream>
26 #include <limits>
27 #include <numeric>
28 #include <random>
29 
30 namespace 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 
48 class Handler_test : public beast::unit_test::suite
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 
105  std::uniform_int_distribution<std::size_t> distr{0, names.size() - 1};
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 
122 public:
123  void
124  run() override
125  {
127  }
128 };
129 
130 BEAST_DEFINE_TESTSUITE_MANUAL(Handler, test, ripple);
131 
132 } // namespace ripple::test
std::chrono::steady_clock
std::make_tuple
T make_tuple(T... args)
ripple::test::Handler_test::run
void run() override
Definition: Handler_test.cpp:124
std::ranlux48
std::uniform_int_distribution
ripple::test::Handler_test::reportLookupPerformance
void reportLookupPerformance()
Definition: Handler_test.cpp:95
ripple::RPC::getHandler
Handler const * getHandler(unsigned version, bool betaEnabled, std::string const &name)
Definition: Handler.cpp:307
ripple::RPC::getHandlerNames
std::set< char const * > getHandlerNames()
Return names of all methods.
Definition: Handler.cpp:313
std::vector
STL class.
std::vector::size
T size(T... args)
std::chrono::nanoseconds
random
iostream
std::sort
T sort(T... args)
std::sqrt
T sqrt(T... args)
std::cout
std::random_device
std::ostream
STL class.
chrono
std::array
STL class.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test
Definition: LedgerDeltaAcquire.h:35
ripple::test::Handler_test::time
auto time(std::size_t n, auto f, auto prng) -> auto
Definition: Handler_test.cpp:51
limits
std::array::begin
T begin(T... args)
std::chrono::nanoseconds::count
T count(T... args)
ripple::test::jtx::make_vector
auto make_vector(Input const &input) requires requires(Input &v)
Definition: TestHelpers.h:42
ripple::test::operator<<
std::ostream & operator<<(std::ostream &os, std::chrono::nanoseconds ns)
Definition: Handler_test.cpp:37
std::size_t
std::array::end
T end(T... args)
ripple::test::BEAST_DEFINE_TESTSUITE_MANUAL
BEAST_DEFINE_TESTSUITE_MANUAL(AMMCalc, app, ripple)
numeric
ripple::sum
static auto sum(TCollection const &col)
Definition: BookStep.cpp:884
ripple::test::Handler_test
Definition: Handler_test.cpp:48
std::chrono::steady_clock::now
T now(T... args)