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 {
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("threadCounts: " + std::to_string(tc1) + " -> " + std::to_string(tc2) + " -> " + std::to_string(tc3));
105
106 TestCallback cb;
108
109 Workers w(cb, perfLog.get(), "Test", tc1);
110 BEAST_EXPECT(w.getNumberOfThreads() == tc1);
111
112 auto testForThreadCount = [this, &cb, &w](int const threadCount) {
113 // Prepare the callback.
114 cb.count = threadCount;
115
116 // Execute the test.
117 w.setNumberOfThreads(threadCount);
118 BEAST_EXPECT(w.getNumberOfThreads() == threadCount);
119
120 for (int i = 0; i < threadCount; ++i)
121 w.addTask();
122
123 // 10 seconds should be enough to finish on any system
124 //
125 using namespace std::chrono_literals;
127 bool const signaled = cb.cv.wait_for(lk, 10s, [&cb] { return cb.count == 0; });
128 BEAST_EXPECT(signaled);
129 BEAST_EXPECT(cb.count == 0);
130 };
131 testForThreadCount(tc1);
132 testForThreadCount(tc2);
133 testForThreadCount(tc3);
134 w.stop();
135
136 // We had better finished all our work!
137 BEAST_EXPECT(cb.count == 0);
138 }
139
140 void
141 run() override
142 {
143 testThreads(0, 0, 0);
144 testThreads(1, 0, 1);
145 testThreads(2, 1, 2);
146 testThreads(4, 3, 5);
147 testThreads(16, 4, 15);
148 testThreads(64, 3, 65);
149 }
150};
151
152BEAST_DEFINE_TESTSUITE(Workers, core, xrpl);
153
154} // 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:147
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:40
void stop()
Pause all threads and wait until they are paused.
Definition Workers.cpp:91
void addTask()
Add a task to be performed.
Definition Workers.cpp:103
int getNumberOfThreads() const noexcept
Retrieve the desired number of threads.
Definition Workers.cpp:30
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)