xrpld
Loading...
Searching...
No Matches
PerfLog_test.cpp
1#include <test/jtx/Env.h>
2#include <test/jtx/envconfig.h>
3
4#include <xrpl/basics/StringUtilities.h>
5#include <xrpl/basics/random.h>
6#include <xrpl/beast/unit_test/suite.h>
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/core/Job.h>
9#include <xrpl/core/JobTypes.h>
10#include <xrpl/core/PerfLog.h>
11#include <xrpl/json/json_reader.h>
12#include <xrpl/json/json_value.h>
13#include <xrpl/protocol/ErrorCodes.h>
14#include <xrpl/protocol/jss.h>
15
16#include <algorithm>
17#include <array>
18#include <chrono>
19#include <cstdint>
20#include <filesystem>
21#include <fstream>
22#include <ios>
23#include <iterator>
24#include <limits>
25#include <memory>
26#include <ostream>
27#include <random>
28#include <ranges>
29#include <string>
30#include <string_view>
31#include <system_error>
32#include <thread>
33#include <utility>
34#include <vector>
35
36//------------------------------------------------------------------------------
37
38namespace xrpl {
39
41{
42 enum class WithFile : bool { No = false, Yes = true };
43
45
46 // The method names to count. PerfLog treats them as opaque keys, so these are
47 // made up rather than taken from the dispatch table: this test then needs no
48 // knowledge of the RPC layer, and does not change shape when a method is
49 // added or removed.
50 //
51 // String literals because PerfLog reads them back as C strings, which is what
52 // NullTerminatedView requires, and they must outlive the PerfLog. Sorted,
53 // because the counters are reported in sorted order.
54 static constexpr std::array kMethodNames{
55 NullTerminatedView{"method_a"},
56 NullTerminatedView{"method_b"},
57 NullTerminatedView{"method_c"},
58 NullTerminatedView{"method_d"},
59 NullTerminatedView{"method_e"},
60 };
61
62 // We're only using Env for its Journal. That Journal gives better
63 // coverage in unit tests.
65 beast::Journal j_{env_.app().getJournal("PerfLog_test")};
66
67 struct Fixture
68 {
71 bool stopSignaled{false};
72
74 {
75 // Clean up any stale state from a previous test run. On
76 // self-hosted CI runners the temp directory persists between
77 // runs, so the "nasty file" test may have left a regular file
78 // (or a non-empty directory) at the logDir path.
79 //
80 // The error code is intentionally ignored: if the path doesn't
81 // exist (the common case on a clean runner) remove_all returns
82 // an error, and that's fine — there's nothing to clean up.
83 using namespace std::filesystem;
85 remove_all(logDir(), ec);
86 }
87
89 {
90 using namespace std::filesystem;
91
92 auto const dir{logDir()};
93 auto const file{logFile()};
94 if (exists(file))
95 remove(file);
96
97 if (!exists(dir) || !is_directory(dir) || !is_empty(dir))
98 {
99 return;
100 }
101 remove(dir);
102 }
103
104 void
106 {
107 stopSignaled = true;
108 }
109
110 static Path
112 {
113 using namespace std::filesystem;
114 return temp_directory_path() / "perf_log_test_dir";
115 }
116
117 static Path
119 {
120 return logDir() / "perf_log.txt";
121 }
122
125 {
126 return std::chrono::milliseconds{10};
127 }
128
131 {
132 perf::PerfLog::Setup const setup{
133 .perfLog = withFile == WithFile::No ? "" : logFile(),
134 .logInterval = logInterval(),
135 };
136 return perf::makePerfLog(setup, app, kMethodNames, j, [this] {
137 signalStop();
138 return;
139 });
140 }
141
142 // Block until the log file has grown in size, indicating that the
143 // PerfLog has written new values to the file and _should_ have the
144 // latest update.
145 static void
147 {
148 using namespace std::filesystem;
149
150 auto const path = logFile();
151 if (!exists(path))
152 return;
153
154 // We wait for the file to change size twice. The first file size
155 // change may have been in process while we arrived.
156 std::uintmax_t const firstSize{file_size(path)};
157 std::uintmax_t secondSize{firstSize};
158 do
159 {
161 secondSize = file_size(path);
162 } while (firstSize >= secondSize);
163
164 do
165 {
167 } while (secondSize >= file_size(path));
168 }
169 };
170
171 // Return a uint64 from a JSON string.
172 static std::uint64_t
173 jsonToUInt64(json::Value const& jsonUIntAsString)
174 {
175 return std::stoull(jsonUIntAsString.asString());
176 }
177
178 // The PerfLog's current state is easier to sort by duration if the
179 // duration is converted from string to integer. The following struct
180 // is a way to think about the converted entry.
181 struct Cur
182 {
185
186 Cur(std::uint64_t d, std::string n) : dur(d), name(std::move(n))
187 {
188 }
189 };
190
191 // A convenience function to convert JSON to Cur and sort. The sort
192 // goes from longest to shortest duration. That way stuff that was started
193 // earlier goes to the front.
194 static std::vector<Cur>
195 getSortedCurrent(json::Value const& currentJson)
196 {
197 std::vector<Cur> currents;
198 currents.reserve(currentJson.size());
199 for (json::Value const& cur : currentJson)
200 {
201 currents.emplace_back(
202 jsonToUInt64(cur[jss::duration_us]),
203 cur.isMember(jss::job) ? cur[jss::job].asString() : cur[jss::method].asString());
204 }
205
206 // Note that the longest durations should be at the front of the
207 // vector since they were started first.
208 std::ranges::sort(currents, [](Cur const& lhs, Cur const& rhs) {
209 if (lhs.dur != rhs.dur)
210 return (rhs.dur < lhs.dur);
211 return (lhs.name < rhs.name);
212 });
213 return currents;
214 }
215
216public:
217 void
219 {
220 using namespace std::filesystem;
221
222 {
223 // Verify a PerfLog creates its file when constructed.
224 Fixture fixture{env_.app(), j_};
225 BEAST_EXPECT(!exists(fixture.logFile()));
226
227 auto const perfLog{fixture.perfLog(WithFile::Yes)};
228
229 BEAST_EXPECT(fixture.stopSignaled == false);
230 BEAST_EXPECT(exists(fixture.logFile()));
231 }
232 {
233 // Create a file where PerfLog wants to put its directory.
234 // Make sure that PerfLog tries to shutdown the server since it
235 // can't open its file.
236 Fixture fixture{env_.app(), j_};
237 if (!BEAST_EXPECT(!exists(fixture.logDir())))
238 return;
239
240 {
241 // Make a file that prevents PerfLog from creating its file.
242 std::ofstream nastyFile;
243 nastyFile.open(fixture.logDir().c_str(), std::ios::out | std::ios::app);
244 if (!BEAST_EXPECT(nastyFile))
245 return;
246 nastyFile.close();
247 }
248
249 // Now construct a PerfLog. The PerfLog should attempt to shut
250 // down the server because it can't open its file.
251 BEAST_EXPECT(fixture.stopSignaled == false);
252 auto perfLog{fixture.perfLog(WithFile::Yes)};
253 BEAST_EXPECT(fixture.stopSignaled == true);
254
255 // Start PerfLog and wait long enough for PerfLog::report()
256 // to not be able to write to its file. That should cause no
257 // problems.
258 perfLog->start();
260 perfLog->stop();
261
262 // Remove the file.
263 remove(fixture.logDir());
264 }
265 {
266 // Put a write protected file where PerfLog wants to write its
267 // file. Make sure that PerfLog tries to shutdown the server
268 // since it can't open its file.
270
271 Fixture fixture{env_.app(), j_};
272 if (!BEAST_EXPECT(!exists(fixture.logDir())))
273 return;
274
275 // Construct and write protect a file to prevent PerfLog
276 // from creating its file.
279 if (!BEAST_EXPECT(!ec))
280 return;
281
282 auto const fileWriteable = [](std::filesystem::path const& p) -> bool {
283 return std::ofstream{p, std::ios::out | std::ios::app}.is_open();
284 };
285
286 if (!BEAST_EXPECT(fileWriteable(fixture.logFile())))
287 return;
288
290 fixture.logFile(),
291 perms::owner_write | perms::others_write | perms::group_write,
292 std::filesystem::perm_options::remove);
293
294 // If the test is running as root, then the write protect may have
295 // no effect. Make sure write protect worked before proceeding.
296 if (fileWriteable(fixture.logFile()))
297 {
298 log << "Unable to write protect file. Test skipped." << std::endl;
299 return;
300 }
301
302 // Now construct a PerfLog. The PerfLog should attempt to shut
303 // down the server because it can't open its file.
304 BEAST_EXPECT(fixture.stopSignaled == false);
305 auto perfLog{fixture.perfLog(WithFile::Yes)};
306 BEAST_EXPECT(fixture.stopSignaled == true);
307
308 // Start PerfLog and wait long enough for PerfLog::report()
309 // to not be able to write to its file. That should cause no
310 // problems.
311 perfLog->start();
313 perfLog->stop();
314
315 // Fix file permissions so the file can be cleaned up.
317 fixture.logFile(),
318 perms::owner_write | perms::others_write | perms::group_write,
319 std::filesystem::perm_options::add);
320 }
321 }
322
323 void
325 {
326 // Exercise the rpc interfaces of PerfLog.
327 // Start up the PerfLog that we'll use for testing.
328 Fixture fixture{env_.app(), j_};
329 auto perfLog{fixture.perfLog(withFile)};
330 perfLog->start();
331
332 // The only labels the RPC interface accepts: those the PerfLog was
333 // constructed with, since rpcStart() reaches UNREACHABLE for any other.
334 // Copied into a vector because they are shuffled below, then paired
335 // positionally with the request ids.
336 auto labels = std::ranges::to<std::vector>(kMethodNames);
337 std::shuffle(labels.begin(), labels.end(), defaultPrng());
338
339 // Get two IDs to associate with each label. Errors tend to happen at
340 // boundaries, so we pick IDs starting from zero and ending at
341 // std::uint64_t>::max().
343 ids.reserve(labels.size() * 2);
346 labels.size(),
347 [i = std::numeric_limits<std::uint64_t>::min()] mutable { return i++; });
350 labels.size(),
351 [i = std::numeric_limits<std::uint64_t>::max()] mutable { return i--; });
352 std::shuffle(ids.begin(), ids.end(), defaultPrng());
353
354 // Start all of the RPC commands twice to show they can all be tracked
355 // simultaneously.
356 for (int labelIndex = 0; labelIndex < labels.size(); ++labelIndex)
357 {
358 for (int idIndex = 0; idIndex < 2; ++idIndex)
359 {
361 perfLog->rpcStart(labels[labelIndex], ids[(labelIndex * 2) + idIndex]);
362 }
363 }
364 {
365 // Examine current PerfLog::counterJson() values.
366 json::Value const countersJson{perfLog->countersJson()[jss::rpc]};
367 BEAST_EXPECT(countersJson.size() == labels.size() + 1);
368 for (auto const& label : labels)
369 {
370 // Expect every label in labels to have the same contents.
371 json::Value const& counter{countersJson[std::string{label}]};
372 BEAST_EXPECT(counter[jss::duration_us] == "0");
373 BEAST_EXPECT(counter[jss::errored] == "0");
374 BEAST_EXPECT(counter[jss::finished] == "0");
375 BEAST_EXPECT(counter[jss::started] == "2");
376 }
377 // Expect "total" to have a lot of "started"
378 json::Value const& total{countersJson[jss::total]};
379 BEAST_EXPECT(total[jss::duration_us] == "0");
380 BEAST_EXPECT(total[jss::errored] == "0");
381 BEAST_EXPECT(total[jss::finished] == "0");
382 BEAST_EXPECT(jsonToUInt64(total[jss::started]) == ids.size());
383 }
384 {
385 // Verify that every entry in labels appears twice in currents.
386 // If we sort by duration_us they should be in the order the
387 // rpcStart() call was made.
388 std::vector<Cur> const currents{getSortedCurrent(perfLog->currentJson()[jss::methods])};
389 BEAST_EXPECT(currents.size() == labels.size() * 2);
390
392 for (int i = 0; i < currents.size(); ++i)
393 {
394 BEAST_EXPECT(currents[i].name == labels[i / 2].view());
395 BEAST_EXPECT(prevDur > currents[i].dur);
396 prevDur = currents[i].dur;
397 }
398 }
399
400 // Finish all but the first RPC command in reverse order to show that
401 // the start and finish of the commands can interleave. Half of the
402 // commands finish correctly, the other half with errors.
403 for (int labelIndex = labels.size() - 1; labelIndex > 0; --labelIndex)
404 {
406 perfLog->rpcFinish(labels[labelIndex], ids[(labelIndex * 2) + 1]);
408 perfLog->rpcError(labels[labelIndex], ids[(labelIndex * 2) + 0]);
409 }
410 perfLog->rpcFinish(labels[0], ids[0 + 1]);
411 // Note that label[0] id[0] is intentionally left unfinished.
412
413 auto const validateFinalCounters = [this, &labels](json::Value const& countersJson) {
414 {
415 json::Value const& jobQueue = countersJson[jss::job_queue];
416 BEAST_EXPECT(jobQueue.isObject());
417 BEAST_EXPECT(jobQueue.size() == 0);
418 }
419
420 json::Value const& rpc = countersJson[jss::rpc];
421 BEAST_EXPECT(rpc.size() == labels.size() + 1);
422
423 // Verify that every entry in labels appears in rpc.
424 // If we access the entries by label we should be able to correlate
425 // their durations with the appropriate labels.
426 {
427 // The first label is special. It should have "errored" : "0".
428 json::Value const& first = rpc[std::string{labels[0]}];
429 BEAST_EXPECT(first[jss::duration_us] != "0");
430 BEAST_EXPECT(first[jss::errored] == "0");
431 BEAST_EXPECT(first[jss::finished] == "1");
432 BEAST_EXPECT(first[jss::started] == "2");
433 }
434
435 // Check the rest of the labels.
437 for (int i = 1; i < labels.size(); ++i)
438 {
439 json::Value const& counter{rpc[std::string{labels[i]}]};
440 std::uint64_t const dur{jsonToUInt64(counter[jss::duration_us])};
441 BEAST_EXPECT(dur != 0 && dur < prevDur);
442 prevDur = dur;
443 BEAST_EXPECT(counter[jss::errored] == "1");
444 BEAST_EXPECT(counter[jss::finished] == "1");
445 BEAST_EXPECT(counter[jss::started] == "2");
446 }
447
448 // Check "total"
449 json::Value const& total{rpc[jss::total]};
450 BEAST_EXPECT(total[jss::duration_us] != "0");
451 BEAST_EXPECT(jsonToUInt64(total[jss::errored]) == labels.size() - 1);
452 BEAST_EXPECT(jsonToUInt64(total[jss::finished]) == labels.size());
453 BEAST_EXPECT(jsonToUInt64(total[jss::started]) == labels.size() * 2);
454 };
455
456 auto const validateFinalCurrent = [this, &labels](json::Value const& currentJson) {
457 {
458 json::Value const& jobQueue = currentJson[jss::jobs];
459 BEAST_EXPECT(jobQueue.isArray());
460 BEAST_EXPECT(jobQueue.size() == 0);
461 }
462
463 json::Value const& methods = currentJson[jss::methods];
464 BEAST_EXPECT(methods.size() == 1);
465 BEAST_EXPECT(methods.isArray());
466
467 json::Value const& only = methods[0u];
468 BEAST_EXPECT(only.size() == 2);
469 BEAST_EXPECT(only.isObject());
470 BEAST_EXPECT(only[jss::duration_us] != "0");
471 BEAST_EXPECT(only[jss::method] == std::string{labels[0]});
472 };
473
474 // Validate the final state of the PerfLog.
475 validateFinalCounters(perfLog->countersJson());
476 validateFinalCurrent(perfLog->currentJson());
477
478 // Give the PerfLog enough time to flush it's state to the file.
479 fixture.wait();
480
481 // Politely stop the PerfLog.
482 perfLog->stop();
483
484 auto const fullPath = fixture.logFile();
485
486 if (withFile == WithFile::No)
487 {
488 BEAST_EXPECT(!exists(fullPath));
489 }
490 else
491 {
492 // The last line in the log file should contain the same
493 // information that countersJson() and currentJson() returned.
494 // Verify that.
495
496 // Get the last line of the log.
497 std::ifstream logStream(fullPath.c_str());
498 std::string lastLine;
499 for (std::string line; std::getline(logStream, line);)
500 {
501 if (!line.empty())
502 lastLine = std::move(line);
503 }
504
505 json::Value parsedLastLine;
506 json::Reader().parse(lastLine, parsedLastLine);
507 if (!BEAST_EXPECT(!rpc::containsError(parsedLastLine)))
508 {
509 // Avoid cascade of failures
510 return;
511 }
512
513 // Validate the contents of the last line of the log.
514 validateFinalCounters(parsedLastLine[jss::counters]);
515 validateFinalCurrent(parsedLastLine[jss::current_activities]);
516 }
517 }
518
519 void
521 {
522 using namespace std::chrono;
523
524 // Exercise the jobs interfaces of PerfLog.
525 // Start up the PerfLog that we'll use for testing.
526 Fixture fixture{env_.app(), j_};
527 auto perfLog{fixture.perfLog(withFile)};
528 perfLog->start();
529
530 // Get the all the JobTypes we can use to call the jobs interfaces
531 // without causing an assert.
532 struct JobName
533 {
534 JobType type;
535 std::string typeName;
536
537 JobName(JobType t, std::string name) : type(t), typeName(std::move(name))
538 {
539 }
540 };
541
543 {
544 auto const& jobTypes = JobTypes::instance();
545 jobs.reserve(jobTypes.size());
546 for (auto const& job : jobTypes)
547 {
548 jobs.emplace_back(job.first, job.second.name());
549 }
550 }
551 std::shuffle(jobs.begin(), jobs.end(), defaultPrng());
552
553 // Walk through all of the jobs, enqueuing every job once. Check
554 // the jobs data with every addition.
555 for (int i = 0; i < jobs.size(); ++i)
556 {
557 perfLog->jobQueue(jobs[i].type);
558 json::Value const jqCounters{perfLog->countersJson()[jss::job_queue]};
559
560 BEAST_EXPECT(jqCounters.size() == i + 2);
561 for (int j = 0; j <= i; ++j)
562 {
563 // Verify all expected counters are present and contain
564 // expected values.
565 json::Value const& counter{jqCounters[jobs[j].typeName]};
566 BEAST_EXPECT(counter.size() == 5);
567 BEAST_EXPECT(counter[jss::queued] == "1");
568 BEAST_EXPECT(counter[jss::started] == "0");
569 BEAST_EXPECT(counter[jss::finished] == "0");
570 BEAST_EXPECT(counter[jss::queued_duration_us] == "0");
571 BEAST_EXPECT(counter[jss::running_duration_us] == "0");
572 }
573
574 // Verify jss::total is present and has expected values.
575 json::Value const& total{jqCounters[jss::total]};
576 BEAST_EXPECT(total.size() == 5);
577 BEAST_EXPECT(jsonToUInt64(total[jss::queued]) == i + 1);
578 BEAST_EXPECT(total[jss::started] == "0");
579 BEAST_EXPECT(total[jss::finished] == "0");
580 BEAST_EXPECT(total[jss::queued_duration_us] == "0");
581 BEAST_EXPECT(total[jss::running_duration_us] == "0");
582 }
583
584 // Even with jobs queued, the perfLog should report nothing current.
585 {
586 json::Value current{perfLog->currentJson()};
587 BEAST_EXPECT(current.size() == 2);
588 BEAST_EXPECT(current.isMember(jss::jobs));
589 BEAST_EXPECT(current[jss::jobs].size() == 0);
590 BEAST_EXPECT(current.isMember(jss::methods));
591 BEAST_EXPECT(current[jss::methods].size() == 0);
592 }
593
594 // Current jobs are tracked by Worker ID. Even though it's not
595 // realistic, crank up the number of workers so we can have many
596 // jobs in process simultaneously without problems.
597 perfLog->resizeJobs(jobs.size() * 2);
598
599 // Start two instances of every job to show that the same job can run
600 // simultaneously (on different Worker threads). Admittedly, this
601 // will make the jss::queued count look a bit goofy since there will
602 // be half as many queued as started...
603 for (int i = 0; i < jobs.size(); ++i)
604 {
605 perfLog->jobStart(jobs[i].type, microseconds{i + 1}, steady_clock::now(), i * 2);
607
608 // Check each jobType counter entry.
609 json::Value const jqCounters{perfLog->countersJson()[jss::job_queue]};
610 for (int j = 0; j < jobs.size(); ++j)
611 {
612 json::Value const& counter{jqCounters[jobs[j].typeName]};
613 std::uint64_t const queuedDurUs{jsonToUInt64(counter[jss::queued_duration_us])};
614 if (j < i)
615 {
616 BEAST_EXPECT(counter[jss::started] == "2");
617 BEAST_EXPECT(queuedDurUs == j + 1);
618 }
619 else if (j == i)
620 {
621 BEAST_EXPECT(counter[jss::started] == "1");
622 BEAST_EXPECT(queuedDurUs == j + 1);
623 }
624 else
625 {
626 BEAST_EXPECT(counter[jss::started] == "0");
627 BEAST_EXPECT(queuedDurUs == 0);
628 }
629
630 BEAST_EXPECT(counter[jss::queued] == "1");
631 BEAST_EXPECT(counter[jss::finished] == "0");
632 BEAST_EXPECT(counter[jss::running_duration_us] == "0");
633 }
634 {
635 // Verify values in jss::total are what we expect.
636 json::Value const& total{jqCounters[jss::total]};
637 BEAST_EXPECT(jsonToUInt64(total[jss::queued]) == jobs.size());
638 BEAST_EXPECT(jsonToUInt64(total[jss::started]) == (i * 2) + 1);
639 BEAST_EXPECT(total[jss::finished] == "0");
640
641 // Total queued duration is triangle number of (i + 1).
642 BEAST_EXPECT(
643 jsonToUInt64(total[jss::queued_duration_us]) == (((i * i) + (3 * i) + 2) / 2));
644 BEAST_EXPECT(total[jss::running_duration_us] == "0");
645 }
646
647 perfLog->jobStart(jobs[i].type, microseconds{0}, steady_clock::now(), (i * 2) + 1);
649
650 // Verify that every entry in jobs appears twice in currents.
651 // If we sort by duration_us they should be in the order the
652 // rpcStart() call was made.
653 std::vector<Cur> const currents{getSortedCurrent(perfLog->currentJson()[jss::jobs])};
654 BEAST_EXPECT(currents.size() == (i + 1) * 2);
655
657 for (int j = 0; j <= i; ++j)
658 {
659 BEAST_EXPECT(currents[j * 2].name == jobs[j].typeName);
660 BEAST_EXPECT(prevDur > currents[j * 2].dur);
661 prevDur = currents[j * 2].dur;
662
663 BEAST_EXPECT(currents[(j * 2) + 1].name == jobs[j].typeName);
664 BEAST_EXPECT(prevDur > currents[(j * 2) + 1].dur);
665 prevDur = currents[(j * 2) + 1].dur;
666 }
667 }
668
669 // Finish every job we started. Finish them in reverse.
670 for (int i = jobs.size() - 1; i >= 0; --i)
671 {
672 // A number of the computations in this loop care about the
673 // number of jobs that have finished. Make that available.
674 int const finished = ((jobs.size() - i) * 2) - 1;
675 perfLog->jobFinish(jobs[i].type, microseconds(finished), (i * 2) + 1);
677
678 json::Value const jqCounters{perfLog->countersJson()[jss::job_queue]};
679 for (int j = 0; j < jobs.size(); ++j)
680 {
681 json::Value const& counter{jqCounters[jobs[j].typeName]};
682 std::uint64_t const runningDurUs{jsonToUInt64(counter[jss::running_duration_us])};
683 if (j < i)
684 {
685 BEAST_EXPECT(counter[jss::finished] == "0");
686 BEAST_EXPECT(runningDurUs == 0);
687 }
688 else if (j == i)
689 {
690 BEAST_EXPECT(counter[jss::finished] == "1");
691 BEAST_EXPECT(runningDurUs == ((jobs.size() - j) * 2) - 1);
692 }
693 else
694 {
695 BEAST_EXPECT(counter[jss::finished] == "2");
696 BEAST_EXPECT(runningDurUs == ((jobs.size() - j) * 4) - 1);
697 }
698
699 std::uint64_t const queuedDurUs{jsonToUInt64(counter[jss::queued_duration_us])};
700 BEAST_EXPECT(queuedDurUs == j + 1);
701 BEAST_EXPECT(counter[jss::queued] == "1");
702 BEAST_EXPECT(counter[jss::started] == "2");
703 }
704 {
705 // Verify values in jss::total are what we expect.
706 json::Value const& total{jqCounters[jss::total]};
707 BEAST_EXPECT(jsonToUInt64(total[jss::queued]) == jobs.size());
708 BEAST_EXPECT(jsonToUInt64(total[jss::started]) == jobs.size() * 2);
709 BEAST_EXPECT(jsonToUInt64(total[jss::finished]) == finished);
710
711 // Total queued duration should be triangle number of
712 // jobs.size().
713 int const queuedDur = ((jobs.size() * (jobs.size() + 1)) / 2);
714 BEAST_EXPECT(jsonToUInt64(total[jss::queued_duration_us]) == queuedDur);
715
716 // Total running duration should be triangle number of finished.
717 int const runningDur = ((finished * (finished + 1)) / 2);
718 BEAST_EXPECT(jsonToUInt64(total[jss::running_duration_us]) == runningDur);
719 }
720
721 perfLog->jobFinish(jobs[i].type, microseconds(finished + 1), (i * 2));
723
724 // Verify that the two jobs we just finished no longer appear in
725 // currents.
726 std::vector<Cur> const currents{getSortedCurrent(perfLog->currentJson()[jss::jobs])};
727 BEAST_EXPECT(currents.size() == i * 2);
728
730 for (int j = 0; j < i; ++j)
731 {
732 BEAST_EXPECT(currents[j * 2].name == jobs[j].typeName);
733 BEAST_EXPECT(prevDur > currents[j * 2].dur);
734 prevDur = currents[j * 2].dur;
735
736 BEAST_EXPECT(currents[(j * 2) + 1].name == jobs[j].typeName);
737 BEAST_EXPECT(prevDur > currents[(j * 2) + 1].dur);
738 prevDur = currents[(j * 2) + 1].dur;
739 }
740 }
741
742 // Validate the final results.
743 auto const validateFinalCounters = [this, &jobs](json::Value const& countersJson) {
744 {
745 json::Value const& rpc = countersJson[jss::rpc];
746 BEAST_EXPECT(rpc.isObject());
747 BEAST_EXPECT(rpc.size() == 0);
748 }
749
750 json::Value const& jobQueue = countersJson[jss::job_queue];
751 for (int i = jobs.size() - 1; i >= 0; --i)
752 {
753 json::Value const& counter{jobQueue[jobs[i].typeName]};
754 std::uint64_t const runningDurUs{jsonToUInt64(counter[jss::running_duration_us])};
755 BEAST_EXPECT(runningDurUs == ((jobs.size() - i) * 4) - 1);
756
757 std::uint64_t const queuedDurUs{jsonToUInt64(counter[jss::queued_duration_us])};
758 BEAST_EXPECT(queuedDurUs == i + 1);
759
760 BEAST_EXPECT(counter[jss::queued] == "1");
761 BEAST_EXPECT(counter[jss::started] == "2");
762 BEAST_EXPECT(counter[jss::finished] == "2");
763 }
764
765 // Verify values in jss::total are what we expect.
766 json::Value const& total{jobQueue[jss::total]};
767 int const finished = jobs.size() * 2;
768 BEAST_EXPECT(jsonToUInt64(total[jss::queued]) == jobs.size());
769 BEAST_EXPECT(jsonToUInt64(total[jss::started]) == finished);
770 BEAST_EXPECT(jsonToUInt64(total[jss::finished]) == finished);
771
772 // Total queued duration should be triangle number of
773 // jobs.size().
774 int const queuedDur = ((jobs.size() * (jobs.size() + 1)) / 2);
775 BEAST_EXPECT(jsonToUInt64(total[jss::queued_duration_us]) == queuedDur);
776
777 // Total running duration should be triangle number of finished.
778 int const runningDur = ((finished * (finished + 1)) / 2);
779 BEAST_EXPECT(jsonToUInt64(total[jss::running_duration_us]) == runningDur);
780 };
781
782 auto const validateFinalCurrent = [this](json::Value const& currentJson) {
783 {
784 json::Value const& j = currentJson[jss::jobs];
785 BEAST_EXPECT(j.isArray());
786 BEAST_EXPECT(j.size() == 0);
787 }
788
789 json::Value const& methods = currentJson[jss::methods];
790 BEAST_EXPECT(methods.size() == 0);
791 BEAST_EXPECT(methods.isArray());
792 };
793
794 // Validate the final state of the PerfLog.
795 validateFinalCounters(perfLog->countersJson());
796 validateFinalCurrent(perfLog->currentJson());
797
798 // Give the PerfLog enough time to flush it's state to the file.
799 fixture.wait();
800
801 // Politely stop the PerfLog.
802 perfLog->stop();
803
804 // Check file contents if that is appropriate.
805 auto const fullPath = fixture.logFile();
806
807 if (withFile == WithFile::No)
808 {
809 BEAST_EXPECT(!exists(fullPath));
810 }
811 else
812 {
813 // The last line in the log file should contain the same
814 // information that countersJson() and currentJson() returned.
815 // Verify that.
816
817 // Get the last line of the log.
818 std::ifstream logStream(fullPath.c_str());
819 std::string lastLine;
820 for (std::string line; std::getline(logStream, line);)
821 {
822 if (!line.empty())
823 lastLine = std::move(line);
824 }
825
826 json::Value parsedLastLine;
827 json::Reader().parse(lastLine, parsedLastLine);
828 if (!BEAST_EXPECT(!rpc::containsError(parsedLastLine)))
829 {
830 // Avoid cascade of failures
831 return;
832 }
833
834 // Validate the contents of the last line of the log.
835 validateFinalCounters(parsedLastLine[jss::counters]);
836 validateFinalCurrent(parsedLastLine[jss::current_activities]);
837 }
838 }
839
840 void
842 {
843 using namespace std::chrono;
844
845 // The Worker ID is used to identify jobs in progress. Show that
846 // the PerLog behaves as well as possible if an invalid ID is passed.
847
848 // Start up the PerfLog that we'll use for testing.
849 Fixture fixture{env_.app(), j_};
850 auto perfLog{fixture.perfLog(withFile)};
851 perfLog->start();
852
853 // Randomly select a job type and its name.
854 JobType jobType = JtInvalid;
855 std::string jobTypeName;
856 {
857 auto const& jobTypes = JobTypes::instance();
858
859 std::uniform_int_distribution<> dis(0, jobTypes.size() - 1);
860 auto iter{jobTypes.begin()};
861 std::advance(iter, dis(defaultPrng()));
862
863 jobType = iter->second.type();
864 jobTypeName = iter->second.name();
865 }
866
867 // Say there's one worker thread.
868 perfLog->resizeJobs(1);
869
870 // Lambda to validate countersJson for this test.
871 auto const verifyCounters = [this, jobTypeName](
872 json::Value const& countersJson,
873 int started,
874 int finished,
875 int queuedUs,
876 int runningUs) {
877 BEAST_EXPECT(countersJson.isObject());
878 BEAST_EXPECT(countersJson.size() == 2);
879
880 BEAST_EXPECT(countersJson.isMember(jss::rpc));
881 BEAST_EXPECT(countersJson[jss::rpc].isObject());
882 BEAST_EXPECT(countersJson[jss::rpc].size() == 0);
883
884 BEAST_EXPECT(countersJson.isMember(jss::job_queue));
885 BEAST_EXPECT(countersJson[jss::job_queue].isObject());
886 BEAST_EXPECT(countersJson[jss::job_queue].size() == 1);
887 {
888 json::Value const& job{countersJson[jss::job_queue][jobTypeName]};
889
890 BEAST_EXPECT(job.isObject());
891 BEAST_EXPECT(jsonToUInt64(job[jss::queued]) == 0);
892 BEAST_EXPECT(jsonToUInt64(job[jss::started]) == started);
893 BEAST_EXPECT(jsonToUInt64(job[jss::finished]) == finished);
894
895 BEAST_EXPECT(jsonToUInt64(job[jss::queued_duration_us]) == queuedUs);
896 BEAST_EXPECT(jsonToUInt64(job[jss::running_duration_us]) == runningUs);
897 }
898 };
899
900 // Lambda to validate currentJson (always empty) for this test.
901 auto const verifyEmptyCurrent = [this](json::Value const& currentJson) {
902 BEAST_EXPECT(currentJson.isObject());
903 BEAST_EXPECT(currentJson.size() == 2);
904
905 BEAST_EXPECT(currentJson.isMember(jss::jobs));
906 BEAST_EXPECT(currentJson[jss::jobs].isArray());
907 BEAST_EXPECT(currentJson[jss::jobs].size() == 0);
908
909 BEAST_EXPECT(currentJson.isMember(jss::methods));
910 BEAST_EXPECT(currentJson[jss::methods].isArray());
911 BEAST_EXPECT(currentJson[jss::methods].size() == 0);
912 };
913
914 // Start an ID that's too large.
915 perfLog->jobStart(jobType, microseconds{11}, steady_clock::now(), 2);
917 verifyCounters(perfLog->countersJson(), 1, 0, 11, 0);
918 verifyEmptyCurrent(perfLog->currentJson());
919
920 // Start a negative ID
921 perfLog->jobStart(jobType, microseconds{13}, steady_clock::now(), -1);
923 verifyCounters(perfLog->countersJson(), 2, 0, 24, 0);
924 verifyEmptyCurrent(perfLog->currentJson());
925
926 // Finish the too large ID
927 perfLog->jobFinish(jobType, microseconds{17}, 2);
929 verifyCounters(perfLog->countersJson(), 2, 1, 24, 17);
930 verifyEmptyCurrent(perfLog->currentJson());
931
932 // Finish the negative ID
933 perfLog->jobFinish(jobType, microseconds{19}, -1);
935 verifyCounters(perfLog->countersJson(), 2, 2, 24, 36);
936 verifyEmptyCurrent(perfLog->currentJson());
937
938 // Give the PerfLog enough time to flush it's state to the file.
939 fixture.wait();
940
941 // Politely stop the PerfLog.
942 perfLog->stop();
943
944 // Check file contents if that is appropriate.
945 auto const fullPath = fixture.logFile();
946
947 if (withFile == WithFile::No)
948 {
949 BEAST_EXPECT(!exists(fullPath));
950 }
951 else
952 {
953 // The last line in the log file should contain the same
954 // information that countersJson() and currentJson() returned.
955 // Verify that.
956
957 // Get the last line of the log.
958 std::ifstream logStream(fullPath.c_str());
959 std::string lastLine;
960 for (std::string line; std::getline(logStream, line);)
961 {
962 if (!line.empty())
963 lastLine = std::move(line);
964 }
965
966 json::Value parsedLastLine;
967 json::Reader().parse(lastLine, parsedLastLine);
968 if (!BEAST_EXPECT(!rpc::containsError(parsedLastLine)))
969 {
970 // Avoid cascade of failures
971 return;
972 }
973
974 // Validate the contents of the last line of the log.
975 verifyCounters(parsedLastLine[jss::counters], 2, 2, 24, 36);
976 verifyEmptyCurrent(parsedLastLine[jss::current_activities]);
977 }
978 }
979
980 void
982 {
983 // We can't fully test rotate because unit tests must run on Windows,
984 // and Windows doesn't (may not?) support rotate. But at least call
985 // the interface and see that it doesn't crash.
986 using namespace std::filesystem;
987
988 Fixture fixture{env_.app(), j_};
989 BEAST_EXPECT(!exists(fixture.logDir()));
990
991 auto perfLog{fixture.perfLog(withFile)};
992
993 BEAST_EXPECT(fixture.stopSignaled == false);
994 if (withFile == WithFile::No)
995 {
996 BEAST_EXPECT(!exists(fixture.logDir()));
997 }
998 else
999 {
1000 BEAST_EXPECT(exists(fixture.logFile()));
1001 BEAST_EXPECT(file_size(fixture.logFile()) == 0);
1002 }
1003
1004 // Start PerfLog and wait long enough for PerfLog::report()
1005 // to write to its file.
1006 perfLog->start();
1007 fixture.wait();
1008
1009 decltype(file_size(fixture.logFile())) firstFileSize{0};
1010 if (withFile == WithFile::No)
1011 {
1012 BEAST_EXPECT(!exists(fixture.logDir()));
1013 }
1014 else
1015 {
1016 firstFileSize = file_size(fixture.logFile());
1017 BEAST_EXPECT(firstFileSize > 0);
1018 }
1019
1020 // Rotate and then wait to make sure more stuff is written to the file.
1021 perfLog->rotate();
1022 fixture.wait();
1023
1024 perfLog->stop();
1025
1026 if (withFile == WithFile::No)
1027 {
1028 BEAST_EXPECT(!exists(fixture.logDir()));
1029 }
1030 else
1031 {
1032 BEAST_EXPECT(file_size(fixture.logFile()) > firstFileSize);
1033 }
1034 }
1035
1036 // makePerfLog() copies the range of names it is given, so only the names have
1037 // to outlive the PerfLog. Here the range does not: it is destroyed before the
1038 // counters are read. Retaining it instead is a use-after-free, which a
1039 // sanitizer build reports directly and which otherwise surfaces as a failed
1040 // assertion or a Debug-mode heap-corruption abort, not a silent pass.
1041 void
1043 {
1044 testcase("Caller's range need not outlive the PerfLog");
1045
1046 Fixture const fixture{env_.app(), j_};
1047
1049 {
1050 std::vector<NullTerminatedView> const names{kMethodNames.begin(), kMethodNames.end()};
1051 perf::PerfLog::Setup const setup{.perfLog = "", .logInterval = fixture.logInterval()};
1052 perfLog = perf::makePerfLog(setup, env_.app(), names, j_, [] {});
1053 }
1054
1055 perfLog->start();
1056 perfLog->rpcStart(kMethodNames[0], 1);
1057 perfLog->rpcFinish(kMethodNames[0], 1);
1058
1059 // Reads the retained names, which is where a dangling range would surface.
1060 json::Value const counters{perfLog->countersJson()[jss::rpc]};
1061 BEAST_EXPECT(counters.isMember(std::string{kMethodNames[0].view()}));
1062 perfLog->stop();
1063 }
1064
1065 void
1079};
1080
1082
1083} // namespace xrpl
T advance(T... args)
T back_inserter(T... args)
T begin(T... args)
A generic endpoint for log messages.
Definition Journal.h:44
A testsuite class.
Definition suite.h:52
LogOs< char > log
Logging output stream.
Definition suite.h:150
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Unserialize a JSON document into a Value.
Definition json_reader.h:20
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Represents a JSON value.
Definition json_value.h:121
bool isObject() const
bool isArray() const
UInt size() const
Number of values in array or object.
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
static JobTypes const & instance()
Definition JobTypes.h:106
A string that is known to reach its terminating null.
void testJobs(WithFile withFile)
static std::uint64_t jsonToUInt64(json::Value const &jsonUIntAsString)
static constexpr std::array kMethodNames
std::filesystem::path Path
void testRPC(WithFile withFile)
test::jtx::Env env_
static std::vector< Cur > getSortedCurrent(json::Value const &currentJson)
void testRotate(WithFile withFile)
void testCallerRangeNeedNotOutlive()
void run() override
Runs the suite.
void testInvalidID(WithFile withFile)
beast::Journal j_
A transaction testing environment.
Definition Env.h:161
T close(T... args)
T create_directories(T... args)
T emplace_back(T... args)
T end(T... args)
T endl(T... args)
T exists(T... args)
T file_size(T... args)
T generate_n(T... args)
T getline(T... args)
T is_directory(T... args)
T is_empty(T... args)
T is_open(T... args)
T max(T... args)
T min(T... args)
STL namespace.
std::unique_ptr< PerfLog > makePerfLog(PerfLog::Setup const &setup, Application &app, std::span< NullTerminatedView const > methodNames, beast::Journal journal, std::function< void()> &&signalStop)
The api_version numbers this server serves.
Definition ApiVersion.h:26
bool containsError(json::Value const &json)
Returns true if the json contains an rpc error specification.
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:38
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
beast::XorShiftEngine & defaultPrng()
Return the default random engine.
JobType
Definition Job.h:21
@ JtInvalid
Definition Job.h:23
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
T open(T... args)
T permissions(T... args)
T shuffle(T... args)
T remove_all(T... args)
T reserve(T... args)
T size(T... args)
T sleep_for(T... args)
T sort(T... args)
T stoull(T... args)
Cur(std::uint64_t d, std::string n)
std::unique_ptr< perf::PerfLog > perfLog(WithFile withFile)
Fixture(Application &app, beast::Journal j)
static std::chrono::milliseconds logInterval()
Configuration from [perf] section of xrpld.cfg.
Definition PerfLog.h:47
T temp_directory_path(T... args)