rippled
Loading...
Searching...
No Matches
Workers_test.cpp
1#include <xrpl/beast/unit_test.h>
2#include <xrpl/core/PerfLog.h>
3#include <xrpl/core/detail/Workers.h>
4#include <xrpl/json/json_value.h>
5
6#include <chrono>
7#include <cstdint>
8#include <memory>
9#include <mutex>
10#include <string>
11
12namespace xrpl {
13
18namespace perf {
19
20class PerfLogTest : public PerfLog
21{
22 void
23 rpcStart(std::string const& method, std::uint64_t requestId) override
24 {
25 }
26
27 void
28 rpcFinish(std::string const& method, std::uint64_t requestId) override
29 {
30 }
31
32 void
33 rpcError(std::string const& method, std::uint64_t dur) override
34 {
35 }
36
37 void
38 jobQueue(JobType const type) override
39 {
40 }
41
42 void
44 JobType const type,
47 int instance) override
48 {
49 }
50
51 void
52 jobFinish(JobType const type, std::chrono::microseconds dur, int instance)
53 override
54 {
55 }
56
58 countersJson() const override
59 {
60 return Json::Value();
61 }
62
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 {
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) + " -> " +
107 std::to_string(tc2) + " -> " + std::to_string(tc3));
108
109 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 =
132 cb.cv.wait_for(lk, 10s, [&cb] { return cb.count == 0; });
133 BEAST_EXPECT(signaled);
134 BEAST_EXPECT(cb.count == 0);
135 };
136 testForThreadCount(tc1);
137 testForThreadCount(tc2);
138 testForThreadCount(tc3);
139 w.stop();
140
141 // We had better finished all our work!
142 BEAST_EXPECT(cb.count == 0);
143 }
144
145 void
146 run() override
147 {
148 testThreads(0, 0, 0);
149 testThreads(1, 0, 1);
150 testThreads(2, 1, 2);
151 testThreads(4, 3, 5);
152 testThreads(16, 4, 15);
153 testThreads(64, 3, 65);
154 }
155};
156
157BEAST_DEFINE_TESTSUITE(Workers, core, xrpl);
158
159} // namespace xrpl
Represents a JSON value.
Definition json_value.h:131
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:62
void setNumberOfThreads(int numberOfThreads)
Set the desired number of threads.
Definition Workers.cpp:44
void stop()
Pause all threads and wait until they are paused.
Definition Workers.cpp:95
void addTask()
Add a task to be performed.
Definition Workers.cpp:109
int getNumberOfThreads() const noexcept
Retrieve the desired number of threads.
Definition Workers.cpp:34
void rotate() override
Rotate perf log file.
Json::Value countersJson() const override
Render performance counters in Json.
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.
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:32
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:15
Called to perform tasks as needed.
Definition Workers.h:66
std::condition_variable cv
void processTask(int instance) override
Perform a task.
T to_string(T... args)