rippled
Loading...
Searching...
No Matches
Workers_test.cpp
1#include <xrpld/core/detail/Workers.h>
2#include <xrpld/perflog/PerfLog.h>
3
4#include <xrpl/beast/unit_test.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 ripple {
14
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)
54 override
55 {
56 }
57
59 countersJson() const override
60 {
61 return Json::Value();
62 }
63
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 {
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) + " -> " +
108 std::to_string(tc2) + " -> " + std::to_string(tc3));
109
110 TestCallback cb;
113
114 Workers w(cb, perfLog.get(), "Test", tc1);
115 BEAST_EXPECT(w.getNumberOfThreads() == tc1);
116
117 auto testForThreadCount = [this, &cb, &w](int const threadCount) {
118 // Prepare the callback.
119 cb.count = threadCount;
120
121 // Execute the test.
122 w.setNumberOfThreads(threadCount);
123 BEAST_EXPECT(w.getNumberOfThreads() == threadCount);
124
125 for (int i = 0; i < threadCount; ++i)
126 w.addTask();
127
128 // 10 seconds should be enough to finish on any system
129 //
130 using namespace std::chrono_literals;
132 bool const signaled =
133 cb.cv.wait_for(lk, 10s, [&cb] { return cb.count == 0; });
134 BEAST_EXPECT(signaled);
135 BEAST_EXPECT(cb.count == 0);
136 };
137 testForThreadCount(tc1);
138 testForThreadCount(tc2);
139 testForThreadCount(tc3);
140 w.stop();
141
142 // We had better finished all our work!
143 BEAST_EXPECT(cb.count == 0);
144 }
145
146 void
147 run() override
148 {
149 testThreads(0, 0, 0);
150 testThreads(1, 0, 1);
151 testThreads(2, 1, 2);
152 testThreads(4, 3, 5);
153 testThreads(16, 4, 15);
154 testThreads(64, 3, 65);
155 }
156};
157
158BEAST_DEFINE_TESTSUITE(Workers, core, ripple);
159
160} // namespace ripple
Represents a JSON value.
Definition json_value.h:130
A testsuite class.
Definition suite.h:52
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:152
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:63
int getNumberOfThreads() const noexcept
Retrieve the desired number of threads.
Definition Workers.cpp:35
void addTask()
Add a task to be performed.
Definition Workers.cpp:110
void stop()
Pause all threads and wait until they are paused.
Definition Workers.cpp:96
void setNumberOfThreads(int numberOfThreads)
Set the desired number of threads.
Definition Workers.cpp:45
void rpcError(std::string const &method, std::uint64_t dur) override
Log errored RPC call.
void rpcStart(std::string const &method, std::uint64_t requestId) override
Log start of RPC call.
void jobQueue(JobType const type) override
Log queued job.
void rotate() override
Rotate perf log file.
Json::Value currentJson() const override
Render currently executing jobs and RPC calls and durations in Json.
void rpcFinish(std::string const &method, std::uint64_t requestId) override
Log successful finish of RPC call.
void resizeJobs(int const resize) override
Ensure enough room to store each currently executing job.
Json::Value countersJson() const override
Render performance counters in Json.
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.
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
Definition PerfLog.h:33
T get(T... args)
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
JobType
Definition Job.h:16
Called to perform tasks as needed.
Definition Workers.h:67
void processTask(int instance) override
Perform a task.
T to_string(T... args)