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#include <string_view>
13
14namespace xrpl {
15
19
20namespace perf {
21
22class PerfLogTest : public PerfLog
23{
24 void
25 rpcStart(std::string_view method, std::uint64_t requestId) override
26 {
27 }
28
29 void
30 rpcFinish(std::string_view method, std::uint64_t requestId) override
31 {
32 }
33
34 void
35 rpcError(std::string_view method, std::uint64_t requestId) override
36 {
37 }
38
39 void
40 jobQueue(JobType const type) override
41 {
42 }
43
44 void
46 JobType const type,
49 int instance) override
50 {
51 }
52
53 void
54 jobFinish(JobType const type, std::chrono::microseconds dur, int instance) override
55 {
56 }
57
58 [[nodiscard]] json::Value
59 countersJson() const override
60 {
61 return json::Value();
62 }
63
64 [[nodiscard]] json::Value
65 currentJson() const override
66 {
67 return json::Value();
68 }
69
70 void
71 resizeJobs(int const resize) override
72 {
73 }
74
75 void
76 rotate() override
77 {
78 }
79};
80
81} // namespace perf
82
83//------------------------------------------------------------------------------
84
86{
87public:
89 {
90 void
91 processTask(int instance) override
92 {
93 std::scoped_lock const lk{mut};
94 if (--count == 0)
95 cv.notify_all();
96 }
97
100 int count = 0;
101 };
102
103 void
104 testThreads(int const tc1, int const tc2, int const tc3)
105 {
106 testcase(
107 "threadCounts: " + std::to_string(tc1) + " -> " + std::to_string(tc2) + " -> " +
108 std::to_string(tc3));
109
110 TestCallback cb;
112
113 Workers w(cb, perfLog.get(), "Test", tc1);
114 BEAST_EXPECT(w.getNumberOfThreads() == tc1);
115
116 auto testForThreadCount = [this, &cb, &w](int const threadCount) {
117 // Prepare the callback.
118 cb.count = threadCount;
119
120 // Execute the test.
121 w.setNumberOfThreads(threadCount);
122 BEAST_EXPECT(w.getNumberOfThreads() == threadCount);
123
124 for (int i = 0; i < threadCount; ++i)
125 w.addTask();
126
127 // 10 seconds should be enough to finish on any system
128 //
129 using namespace std::chrono_literals;
131 bool const signaled = cb.cv.wait_for(lk, 10s, [&cb] { return cb.count == 0; });
132 BEAST_EXPECT(signaled);
133 BEAST_EXPECT(cb.count == 0);
134 };
135 testForThreadCount(tc1);
136 testForThreadCount(tc2);
137 testForThreadCount(tc3);
138 w.stop();
139
140 // We had better finished all our work!
141 BEAST_EXPECT(cb.count == 0);
142 }
143
144 void
145 run() override
146 {
147 testThreads(0, 0, 0);
148 testThreads(1, 0, 1);
149 testThreads(2, 1, 2);
150 testThreads(4, 3, 5);
151 testThreads(16, 4, 15);
152 testThreads(64, 3, 65);
153 }
154};
155
157
158} // namespace xrpl
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:117
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 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 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_view method, std::uint64_t requestId) override
Log successful finish of RPC call.
void rpcError(std::string_view method, std::uint64_t requestId) override
Log errored RPC call.
void rpcStart(std::string_view method, std::uint64_t requestId) override
Log start 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:33
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:21
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
Called to perform tasks as needed.
Definition Workers.h:67
std::condition_variable cv
void processTask(int instance) override
Perform a task.
T to_string(T... args)