xrpld
Loading...
Searching...
No Matches
Workers_test.cpp
1#include <xrpl/beast/unit_test/suite.h>
2#include <xrpl/core/Job.h>
3#include <xrpl/core/PerfLog.h>
4#include <xrpl/core/detail/Workers.h>
5#include <xrpl/json/json_value.h>
6
7#include <chrono>
8#include <cstdint>
9#include <memory>
10#include <mutex>
11#include <string>
12
13namespace xrpl {
14
18
19namespace perf {
20
21class PerfLogTest : public PerfLog
22{
23 void
24 rpcStart(std::string const& method, std::uint64_t requestId) override
25 {
26 }
27
28 void
29 rpcFinish(std::string const& method, std::uint64_t requestId) override
30 {
31 }
32
33 void
34 rpcError(std::string const& method, std::uint64_t dur) override
35 {
36 }
37
38 void
39 jobQueue(JobType const type) override
40 {
41 }
42
43 void
45 JobType const type,
48 int instance) override
49 {
50 }
51
52 void
53 jobFinish(JobType const type, std::chrono::microseconds dur, int instance) override
54 {
55 }
56
57 [[nodiscard]] json::Value
58 countersJson() const override
59 {
60 return json::Value();
61 }
62
63 [[nodiscard]] json::Value
64 currentJson() const override
65 {
66 return json::Value();
67 }
68
69 void
70 resizeJobs(int const resize) override
71 {
72 }
73
74 void
75 rotate() override
76 {
77 }
78};
79
80} // namespace perf
81
82//------------------------------------------------------------------------------
83
85{
86public:
88 {
89 void
90 processTask(int instance) override
91 {
92 std::scoped_lock const lk{mut};
93 if (--count == 0)
94 cv.notify_all();
95 }
96
99 int count = 0;
100 };
101
102 void
103 testThreads(int const tc1, int const tc2, int const tc3)
104 {
105 testcase(
106 "threadCounts: " + std::to_string(tc1) + " -> " + std::to_string(tc2) + " -> " +
107 std::to_string(tc3));
108
109 TestCallback cb;
111
112 Workers w(cb, perfLog.get(), "Test", tc1);
113 BEAST_EXPECT(w.getNumberOfThreads() == tc1);
114
115 auto testForThreadCount = [this, &cb, &w](int const threadCount) {
116 // Prepare the callback.
117 cb.count = threadCount;
118
119 // Execute the test.
120 w.setNumberOfThreads(threadCount);
121 BEAST_EXPECT(w.getNumberOfThreads() == threadCount);
122
123 for (int i = 0; i < threadCount; ++i)
124 w.addTask();
125
126 // 10 seconds should be enough to finish on any system
127 //
128 using namespace std::chrono_literals;
130 bool const signaled = cb.cv.wait_for(lk, 10s, [&cb] { return cb.count == 0; });
131 BEAST_EXPECT(signaled);
132 BEAST_EXPECT(cb.count == 0);
133 };
134 testForThreadCount(tc1);
135 testForThreadCount(tc2);
136 testForThreadCount(tc3);
137 w.stop();
138
139 // We had better finished all our work!
140 BEAST_EXPECT(cb.count == 0);
141 }
142
143 void
144 run() override
145 {
146 testThreads(0, 0, 0);
147 testThreads(1, 0, 1);
148 testThreads(2, 1, 2);
149 testThreads(4, 3, 5);
150 testThreads(16, 4, 15);
151 testThreads(64, 3, 65);
152 }
153};
154
156
157} // namespace xrpl
A testsuite class.
Definition suite.h:50
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:149
Represents a JSON value.
Definition json_value.h:130
void run() override
Runs the suite.
void testThreads(int const tc1, int const tc2, int const tc3)
Workers is effectively a thread pool.
Definition Workers.h:61
void setNumberOfThreads(int numberOfThreads)
Set the desired number of threads.
Definition Workers.cpp:47
void stop()
Pause all threads and wait until they are paused.
Definition Workers.cpp:98
void addTask()
Add a task to be performed.
Definition Workers.cpp:112
int getNumberOfThreads() const noexcept
Retrieve the desired number of threads.
Definition Workers.cpp:37
void rotate() override
Rotate perf log file.
void rpcStart(std::string const &method, std::uint64_t requestId) override
Log start of RPC call.
void resizeJobs(int const resize) override
Ensure enough room to store each currently executing job.
void jobStart(JobType const type, std::chrono::microseconds dur, std::chrono::time_point< std::chrono::steady_clock > startTime, int instance) override
void rpcError(std::string const &method, std::uint64_t dur) override
Log errored RPC call.
void jobFinish(JobType const type, std::chrono::microseconds dur, int instance) override
Log job finishing.
json::Value currentJson() const override
Render currently executing jobs and RPC calls and durations in Json.
json::Value countersJson() const override
Render performance counters in Json.
void rpcFinish(std::string const &method, std::uint64_t requestId) override
Log successful finish of RPC call.
void jobQueue(JobType const type) override
Log queued job.
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
Definition PerfLog.h:31
T get(T... args)
T make_unique(T... args)
Dummy class for unit tests.
Definition Workers.h:14
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
JobType
Definition Job.h:16
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
Called to perform tasks as needed.
Definition Workers.h:65
std::condition_variable cv
void processTask(int instance) override
Perform a task.
T to_string(T... args)