|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +/* |
| 3 | + This file is part of clio: https://github.com/XRPLF/clio |
| 4 | + Copyright (c) 2025, the clio developers. |
| 5 | +
|
| 6 | + Permission to use, copy, modify, and 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 "rpc/WorkQueue.hpp" |
| 21 | +#include "util/Assert.hpp" |
| 22 | +#include "util/config/Array.hpp" |
| 23 | +#include "util/config/ConfigConstraints.hpp" |
| 24 | +#include "util/config/ConfigDefinition.hpp" |
| 25 | +#include "util/config/ConfigValue.hpp" |
| 26 | +#include "util/config/Types.hpp" |
| 27 | +#include "util/log/Logger.hpp" |
| 28 | +#include "util/prometheus/Prometheus.hpp" |
| 29 | + |
| 30 | +#include <benchmark/benchmark.h> |
| 31 | +#include <boost/asio.hpp> |
| 32 | +#include <boost/asio/spawn.hpp> |
| 33 | +#include <boost/asio/steady_timer.hpp> |
| 34 | +#include <boost/asio/thread_pool.hpp> |
| 35 | +#include <boost/json.hpp> |
| 36 | +#include <boost/json/object.hpp> |
| 37 | + |
| 38 | +#include <atomic> |
| 39 | +#include <cassert> |
| 40 | +#include <chrono> |
| 41 | +#include <cstddef> |
| 42 | +#include <cstdint> |
| 43 | +#include <mutex> |
| 44 | + |
| 45 | +using namespace rpc; |
| 46 | +using namespace util::config; |
| 47 | + |
| 48 | +namespace { |
| 49 | + |
| 50 | +auto const kCONFIG = ClioConfigDefinition{ |
| 51 | + {"prometheus.compress_reply", ConfigValue{ConfigType::Boolean}.defaultValue(true)}, |
| 52 | + {"prometheus.enabled", ConfigValue{ConfigType::Boolean}.defaultValue(true)}, |
| 53 | + {"log.channels.[].channel", Array{ConfigValue{ConfigType::String}}}, |
| 54 | + {"log.channels.[].level", Array{ConfigValue{ConfigType::String}}}, |
| 55 | + {"log.level", ConfigValue{ConfigType::String}.defaultValue("info")}, |
| 56 | + {"log.format", ConfigValue{ConfigType::String}.defaultValue(R"(%Y-%m-%d %H:%M:%S.%f %^%3!l:%n%$ - %v)")}, |
| 57 | + {"log.is_async", ConfigValue{ConfigType::Boolean}.defaultValue(false)}, |
| 58 | + {"log.enable_console", ConfigValue{ConfigType::Boolean}.defaultValue(false)}, |
| 59 | + {"log.directory", ConfigValue{ConfigType::String}.optional()}, |
| 60 | + {"log.rotation_size", ConfigValue{ConfigType::Integer}.defaultValue(2048).withConstraint(gValidateUint32)}, |
| 61 | + {"log.directory_max_files", ConfigValue{ConfigType::Integer}.defaultValue(25).withConstraint(gValidateUint32)}, |
| 62 | + {"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("none")}, |
| 63 | +}; |
| 64 | + |
| 65 | +// this should be a fixture but it did not work with Args very well |
| 66 | +void |
| 67 | +init() |
| 68 | +{ |
| 69 | + static std::once_flag kONCE; |
| 70 | + std::call_once(kONCE, [] { |
| 71 | + PrometheusService::init(kCONFIG); |
| 72 | + (void)util::LogService::init(kCONFIG); |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +} // namespace |
| 77 | + |
| 78 | +static void |
| 79 | +benchmarkWorkQueue(benchmark::State& state) |
| 80 | +{ |
| 81 | + init(); |
| 82 | + |
| 83 | + auto const total = static_cast<size_t>(state.range(0)); |
| 84 | + auto const numThreads = static_cast<uint32_t>(state.range(1)); |
| 85 | + auto const maxSize = static_cast<uint32_t>(state.range(2)); |
| 86 | + auto const delayMs = static_cast<uint32_t>(state.range(3)); |
| 87 | + |
| 88 | + for (auto _ : state) { |
| 89 | + std::atomic_size_t totalExecuted = 0uz; |
| 90 | + std::atomic_size_t totalQueued = 0uz; |
| 91 | + |
| 92 | + state.PauseTiming(); |
| 93 | + WorkQueue queue(numThreads, maxSize); |
| 94 | + state.ResumeTiming(); |
| 95 | + |
| 96 | + for (auto i = 0uz; i < total; ++i) { |
| 97 | + totalQueued += static_cast<std::size_t>(queue.postCoro( |
| 98 | + [&delayMs, &totalExecuted](auto yield) { |
| 99 | + ++totalExecuted; |
| 100 | + |
| 101 | + boost::asio::steady_timer timer(yield.get_executor(), std::chrono::milliseconds{delayMs}); |
| 102 | + timer.async_wait(yield); |
| 103 | + }, |
| 104 | + /* isWhiteListed = */ false |
| 105 | + )); |
| 106 | + } |
| 107 | + |
| 108 | + queue.stop(); |
| 109 | + |
| 110 | + ASSERT(totalExecuted == totalQueued, "Totals don't match"); |
| 111 | + ASSERT(totalQueued <= total, "Queued more than requested"); |
| 112 | + ASSERT(totalQueued >= maxSize, "Queued less than maxSize"); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// Usage example: |
| 117 | +/* |
| 118 | + ./clio_benchmark \ |
| 119 | + --benchmark_repetitions=10 \ |
| 120 | + --benchmark_display_aggregates_only=true \ |
| 121 | + --benchmark_min_time=1x \ |
| 122 | + --benchmark_filter="WorkQueue" |
| 123 | +*/ |
| 124 | +// TODO: figure out what happens on 1 thread |
| 125 | +BENCHMARK(benchmarkWorkQueue) |
| 126 | + ->ArgsProduct({{1'000, 10'000, 100'000}, {2, 4, 8}, {0, 5'000}, {10, 100, 250}}) |
| 127 | + ->Unit(benchmark::kMillisecond); |
0 commit comments