rippled
Loading...
Searching...
No Matches
Workers_test.cpp
1//------------------------------------------------------------------------------
2/*
3This file is part of rippled: https://github.com/ripple/rippled
4Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6Permission to use, copy, modify, and/or distribute this software for any
7purpose with or without fee is hereby granted, provided that the above
8copyright notice and this permission notice appear in all copies.
9
10THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/core/detail/Workers.h>
21#include <xrpld/perflog/PerfLog.h>
22
23#include <xrpl/beast/unit_test.h>
24#include <xrpl/json/json_value.h>
25
26#include <chrono>
27#include <cstdint>
28#include <memory>
29#include <mutex>
30#include <string>
31
32namespace ripple {
33
38namespace perf {
39
40class PerfLogTest : public PerfLog
41{
42 void
43 rpcStart(std::string const& method, std::uint64_t requestId) override
44 {
45 }
46
47 void
48 rpcFinish(std::string const& method, std::uint64_t requestId) override
49 {
50 }
51
52 void
53 rpcError(std::string const& method, std::uint64_t dur) override
54 {
55 }
56
57 void
58 jobQueue(JobType const type) override
59 {
60 }
61
62 void
64 JobType const type,
67 int instance) override
68 {
69 }
70
71 void
72 jobFinish(JobType const type, std::chrono::microseconds dur, int instance)
73 override
74 {
75 }
76
78 countersJson() const override
79 {
80 return Json::Value();
81 }
82
84 currentJson() const override
85 {
86 return Json::Value();
87 }
88
89 void
90 resizeJobs(int const resize) override
91 {
92 }
93
94 void
95 rotate() override
96 {
97 }
98};
99
100} // namespace perf
101
102//------------------------------------------------------------------------------
103
105{
106public:
108 {
109 void
110 processTask(int instance) override
111 {
113 if (--count == 0)
114 cv.notify_all();
115 }
116
119 int count = 0;
120 };
121
122 void
123 testThreads(int const tc1, int const tc2, int const tc3)
124 {
125 testcase(
126 "threadCounts: " + std::to_string(tc1) + " -> " +
127 std::to_string(tc2) + " -> " + std::to_string(tc3));
128
129 TestCallback cb;
132
133 Workers w(cb, perfLog.get(), "Test", tc1);
134 BEAST_EXPECT(w.getNumberOfThreads() == tc1);
135
136 auto testForThreadCount = [this, &cb, &w](int const threadCount) {
137 // Prepare the callback.
138 cb.count = threadCount;
139
140 // Execute the test.
141 w.setNumberOfThreads(threadCount);
142 BEAST_EXPECT(w.getNumberOfThreads() == threadCount);
143
144 for (int i = 0; i < threadCount; ++i)
145 w.addTask();
146
147 // 10 seconds should be enough to finish on any system
148 //
149 using namespace std::chrono_literals;
151 bool const signaled =
152 cb.cv.wait_for(lk, 10s, [&cb] { return cb.count == 0; });
153 BEAST_EXPECT(signaled);
154 BEAST_EXPECT(cb.count == 0);
155 };
156 testForThreadCount(tc1);
157 testForThreadCount(tc2);
158 testForThreadCount(tc3);
159 w.stop();
160
161 // We had better finished all our work!
162 BEAST_EXPECT(cb.count == 0);
163 }
164
165 void
166 run() override
167 {
168 testThreads(0, 0, 0);
169 testThreads(1, 0, 1);
170 testThreads(2, 1, 2);
171 testThreads(4, 3, 5);
172 testThreads(16, 4, 15);
173 testThreads(64, 3, 65);
174 }
175};
176
177BEAST_DEFINE_TESTSUITE(Workers, core, ripple);
178
179} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
A testsuite class.
Definition suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:155
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:82
int getNumberOfThreads() const noexcept
Retrieve the desired number of threads.
Definition Workers.cpp:54
void addTask()
Add a task to be performed.
Definition Workers.cpp:129
void stop()
Pause all threads and wait until they are paused.
Definition Workers.cpp:115
void setNumberOfThreads(int numberOfThreads)
Set the desired number of threads.
Definition Workers.cpp:64
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:52
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:25
JobType
Definition Job.h:35
Called to perform tasks as needed.
Definition Workers.h:86
void processTask(int instance) override
Perform a task.
T to_string(T... args)