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) override
53 {
54 }
55
57 countersJson() const override
58 {
59 return Json::Value();
60 }
61
63 currentJson() const override
64 {
65 return Json::Value();
66 }
67
68 void
69 resizeJobs(int const resize) override
70 {
71 }
72
73 void
74 rotate() override
75 {
76 }
77};
78
79} // namespace perf
80
81//------------------------------------------------------------------------------
82
84{
85public:
87 {
88 void
89 processTask(int instance) override
90 {
91 std::lock_guard const lk{mut};
92 if (--count == 0)
93 cv.notify_all();
94 }
95
98 int count = 0;
99 };
100
101 void
102 testThreads(int const tc1, int const tc2, int const tc3)
103 {
104 testcase(
105 "threadCounts: " + std::to_string(tc1) + " -> " + std::to_string(tc2) + " -> " +
106 std::to_string(tc3));
107
108 TestCallback cb;
110
111 Workers w(cb, perfLog.get(), "Test", tc1);
112 BEAST_EXPECT(w.getNumberOfThreads() == tc1);
113
114 auto testForThreadCount = [this, &cb, &w](int const threadCount) {
115 // Prepare the callback.
116 cb.count = threadCount;
117
118 // Execute the test.
119 w.setNumberOfThreads(threadCount);
120 BEAST_EXPECT(w.getNumberOfThreads() == threadCount);
121
122 for (int i = 0; i < threadCount; ++i)
123 w.addTask();
124
125 // 10 seconds should be enough to finish on any system
126 //
127 using namespace std::chrono_literals;
129 bool const signaled = cb.cv.wait_for(lk, 10s, [&cb] { return cb.count == 0; });
130 BEAST_EXPECT(signaled);
131 BEAST_EXPECT(cb.count == 0);
132 };
133 testForThreadCount(tc1);
134 testForThreadCount(tc2);
135 testForThreadCount(tc3);
136 w.stop();
137
138 // We had better finished all our work!
139 BEAST_EXPECT(cb.count == 0);
140 }
141
142 void
143 run() override
144 {
145 testThreads(0, 0, 0);
146 testThreads(1, 0, 1);
147 testThreads(2, 1, 2);
148 testThreads(4, 3, 5);
149 testThreads(16, 4, 15);
150 testThreads(64, 3, 65);
151 }
152};
153
154BEAST_DEFINE_TESTSUITE(Workers, core, xrpl);
155
156} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
A testsuite class.
Definition suite.h:51
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:150
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:41
void stop()
Pause all threads and wait until they are paused.
Definition Workers.cpp:92
void addTask()
Add a task to be performed.
Definition Workers.cpp:106
int getNumberOfThreads() const noexcept
Retrieve the desired number of threads.
Definition Workers.cpp:31
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:31
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:5
JobType
Definition Job.h:14
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)