xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
core
Workers_test.cpp
1
#include <xrpl/beast/unit_test/suite.h>
2
#include <xrpl/core/Job.h>
3
#include <xrpl/core/PerfLog.h>
4
#include <xrpl/core/detail/Workers.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
#include <
string_view
>
13
14
namespace
xrpl
{
15
19
20
namespace
perf
{
21
22
class
PerfLogTest
:
public
PerfLog
23
{
24
void
25
rpcStart
(
std::string_view
method,
std::uint64_t
requestId)
override
26
{
27
}
28
29
void
30
rpcFinish
(
std::string_view
method,
std::uint64_t
requestId)
override
31
{
32
}
33
34
void
35
rpcError
(
std::string_view
method,
std::uint64_t
requestId)
override
36
{
37
}
38
39
void
40
jobQueue
(
JobType
const
type)
override
41
{
42
}
43
44
void
45
jobStart
(
46
JobType
const
type,
47
std::chrono::microseconds
dur,
48
std::chrono::time_point<std::chrono::steady_clock>
startTime,
49
int
instance)
override
50
{
51
}
52
53
void
54
jobFinish
(
JobType
const
type,
std::chrono::microseconds
dur,
int
instance)
override
55
{
56
}
57
58
[[nodiscard]]
json::Value
59
countersJson
()
const override
60
{
61
return
json::Value
();
62
}
63
64
[[nodiscard]]
json::Value
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
85
class
Workers_test
:
public
beast::unit_test::Suite
86
{
87
public
:
88
struct
TestCallback
:
Workers::Callback
89
{
90
void
91
processTask
(
int
instance)
override
92
{
93
std::scoped_lock
const
lk{
mut
};
94
if
(--
count
== 0)
95
cv
.notify_all();
96
}
97
98
std::condition_variable
cv
;
99
std::mutex
mut
;
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) +
" -> "
+
std::to_string
(tc2) +
" -> "
+
108
std::to_string
(tc3));
109
110
TestCallback
cb;
111
std::unique_ptr<perf::PerfLog>
const
perfLog =
std::make_unique<perf::PerfLogTest>
();
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;
130
std::unique_lock<std::mutex>
lk{cb.
mut
};
131
bool
const
signaled = cb.
cv
.
wait_for
(lk, 10s, [&cb] {
return
cb.
count
== 0; });
132
BEAST_EXPECT(signaled);
133
BEAST_EXPECT(cb.
count
== 0);
134
};
135
testForThreadCount(tc1);
136
testForThreadCount(tc2);
137
testForThreadCount(tc3);
138
w.
stop
();
139
140
// We had better finished all our work!
141
BEAST_EXPECT(cb.
count
== 0);
142
}
143
144
void
145
run
()
override
146
{
147
testThreads
(0, 0, 0);
148
testThreads
(1, 0, 1);
149
testThreads
(2, 1, 2);
150
testThreads
(4, 3, 5);
151
testThreads
(16, 4, 15);
152
testThreads
(64, 3, 65);
153
}
154
};
155
156
BEAST_DEFINE_TESTSUITE
(
Workers
, core,
xrpl
);
157
158
}
// namespace xrpl
std::string_view
chrono
beast::unit_test::Suite
A testsuite class.
Definition
suite.h:52
beast::unit_test::Suite::testcase
TestcaseT testcase
Memberspace for declaring test cases.
Definition
suite.h:155
json::Value
Represents a JSON value.
Definition
json_value.h:117
xrpl::Workers_test
Definition
Workers_test.cpp:86
xrpl::Workers_test::run
void run() override
Runs the suite.
Definition
Workers_test.cpp:145
xrpl::Workers_test::testThreads
void testThreads(int const tc1, int const tc2, int const tc3)
Definition
Workers_test.cpp:104
xrpl::Workers
Workers is effectively a thread pool.
Definition
Workers.h:61
xrpl::Workers::setNumberOfThreads
void setNumberOfThreads(int numberOfThreads)
Set the desired number of threads.
Definition
Workers.cpp:47
xrpl::Workers::stop
void stop()
Pause all threads and wait until they are paused.
Definition
Workers.cpp:98
xrpl::Workers::addTask
void addTask()
Add a task to be performed.
Definition
Workers.cpp:112
xrpl::Workers::getNumberOfThreads
int getNumberOfThreads() const noexcept
Retrieve the desired number of threads.
Definition
Workers.cpp:37
xrpl::perf::PerfLogTest
Definition
Workers_test.cpp:23
xrpl::perf::PerfLogTest::rotate
void rotate() override
Rotate perf log file.
Definition
Workers_test.cpp:76
xrpl::perf::PerfLogTest::resizeJobs
void resizeJobs(int const resize) override
Ensure enough room to store each currently executing job.
Definition
Workers_test.cpp:71
xrpl::perf::PerfLogTest::jobStart
void jobStart(JobType const type, std::chrono::microseconds dur, std::chrono::time_point< std::chrono::steady_clock > startTime, int instance) override
Definition
Workers_test.cpp:45
xrpl::perf::PerfLogTest::jobFinish
void jobFinish(JobType const type, std::chrono::microseconds dur, int instance) override
Log job finishing.
Definition
Workers_test.cpp:54
xrpl::perf::PerfLogTest::currentJson
json::Value currentJson() const override
Render currently executing jobs and RPC calls and durations in Json.
Definition
Workers_test.cpp:65
xrpl::perf::PerfLogTest::countersJson
json::Value countersJson() const override
Render performance counters in Json.
Definition
Workers_test.cpp:59
xrpl::perf::PerfLogTest::rpcFinish
void rpcFinish(std::string_view method, std::uint64_t requestId) override
Log successful finish of RPC call.
Definition
Workers_test.cpp:30
xrpl::perf::PerfLogTest::rpcError
void rpcError(std::string_view method, std::uint64_t requestId) override
Log errored RPC call.
Definition
Workers_test.cpp:35
xrpl::perf::PerfLogTest::rpcStart
void rpcStart(std::string_view method, std::uint64_t requestId) override
Log start of RPC call.
Definition
Workers_test.cpp:25
xrpl::perf::PerfLogTest::jobQueue
void jobQueue(JobType const type) override
Log queued job.
Definition
Workers_test.cpp:40
xrpl::perf::PerfLog
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
Definition
PerfLog.h:33
std::condition_variable
cstdint
std::chrono::microseconds
std::unique_ptr::get
T get(T... args)
std::uint64_t
std::make_unique
T make_unique(T... args)
memory
mutex
xrpl::perf
Dummy class for unit tests.
Definition
Workers.h:14
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::JobType
JobType
Definition
Job.h:21
xrpl::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
std::scoped_lock
string
string_view
xrpl::Workers::Callback
Called to perform tasks as needed.
Definition
Workers.h:67
xrpl::Workers_test::TestCallback
Definition
Workers_test.cpp:89
xrpl::Workers_test::TestCallback::cv
std::condition_variable cv
Definition
Workers_test.cpp:98
xrpl::Workers_test::TestCallback::processTask
void processTask(int instance) override
Perform a task.
Definition
Workers_test.cpp:91
xrpl::Workers_test::TestCallback::mut
std::mutex mut
Definition
Workers_test.cpp:99
xrpl::Workers_test::TestCallback::count
int count
Definition
Workers_test.cpp:100
std::chrono::time_point
std::to_string
T to_string(T... args)
std::unique_lock
std::unique_ptr
std::condition_variable::wait_for
T wait_for(T... args)
Generated by
1.17.0