xrpld
Loading...
Searching...
No Matches
collectors.h
1#pragma once
2
3#include <xrpl/basics/UnorderedContainers.h>
4
5#include <csf/Histogram.h>
6#include <csf/SimTime.h>
7#include <csf/Tx.h>
8#include <csf/Validation.h>
9#include <csf/events.h>
10
11#include <algorithm>
12#include <cassert>
13#include <chrono>
14#include <cstddef>
15#include <iomanip>
16#include <ios>
17#include <map>
18#include <optional>
19#include <ostream>
20#include <tuple>
21#include <utility>
22#include <vector>
23
24namespace xrpl::test::csf {
25
26// A collector is any class that implements
27//
28// on(NodeID, SimTime, Event)
29//
30// for all events emitted by a Peer.
31//
32// This file contains helper functions for composing different collectors
33// and also defines several standard collectors available for simulations.
34
43template <class... Cs>
45{
46 std::tuple<Cs&...> cs_;
47
48 template <class C, class E>
49 static void
50 apply(C& c, PeerID who, SimTime when, E e)
51 {
52 c.on(who, when, e);
53 }
54
55 template <std::size_t... Is, class E>
56 static void
58 {
59 (..., apply(std::get<Is>(cs), who, when, e));
60 }
61
62public:
68 Collectors(Cs&... cs) : cs_(std::tie(cs...))
69 {
70 }
71
72 template <class E>
73 void
74 on(PeerID who, SimTime when, E e)
75 {
77 }
78};
79
83template <class... Cs>
84Collectors<Cs...>
86{
87 return Collectors<Cs...>(cs...);
88}
89
99template <class CollectorType>
101{
103
104 CollectorType&
106 {
107 return byNode[who];
108 }
109
110 CollectorType const&
112 {
113 return byNode[who];
114 }
115 template <class E>
116 void
117 on(PeerID who, SimTime when, E const& e)
118 {
119 byNode[who].on(who, when, e);
120 }
121};
122
127{
128 template <class E>
129 void
130 on(PeerID, SimTime, E const& e)
131 {
132 }
133};
134
139{
140 bool init = false;
143
144 template <class E>
145 void
146 on(PeerID, SimTime when, E const& e)
147 {
148 if (!init)
149 {
150 start = when;
151 init = true;
152 }
153 else
154 {
155 stop = when;
156 }
157 }
158};
159
171{
172 // Counts
176
188
190
194
195 // Ignore most events by default
196 template <class E>
197 void
198 on(PeerID, SimTime when, E const& e)
199 {
200 }
201
202 void
203 on(PeerID who, SimTime when, SubmitTx const& e)
204 {
205 // save first time it was seen
206 if (txs.emplace(e.tx.id(), Tracker{e.tx, when}).second)
207 {
208 submitted++;
209 }
210 }
211
212 void
213 on(PeerID who, SimTime when, AcceptLedger const& e)
214 {
215 for (auto const& tx : e.ledger.txs())
216 {
217 auto const it = txs.find(tx.id());
218 if (it != txs.end() && !it->second.accepted)
219 {
220 Tracker& tracker = it->second;
221 tracker.accepted = when;
222 accepted++;
223
224 submitToAccept.insert(*tracker.accepted - tracker.submitted);
225 }
226 }
227 }
228
229 void
231 {
232 for (auto const& tx : e.ledger.txs())
233 {
234 auto const it = txs.find(tx.id());
235 if (it != txs.end() && !it->second.validated)
236 {
237 Tracker& tracker = it->second;
238 // Should only validated a previously accepted Tx
239 assert(tracker.accepted);
240
241 tracker.validated = when;
242 validated++;
243 submitToValidate.insert(*tracker.validated - tracker.submitted);
244 }
245 }
246 }
247
248 // Returns the number of txs which were never accepted
249 [[nodiscard]] std::size_t
250 orphaned() const
251 {
252 return std::count_if(
253 txs.begin(), txs.end(), [](auto const& it) { return !it.second.accepted; });
254 }
255
256 // Returns the number of txs which were never validated
257 [[nodiscard]] std::size_t
259 {
260 return std::count_if(
261 txs.begin(), txs.end(), [](auto const& it) { return !it.second.validated; });
262 }
263
264 template <class T>
265 void
266 report(SimDuration simDuration, T& log, bool printBreakline = false)
267 {
268 using namespace std::chrono;
269 auto const perSec = [&simDuration](std::size_t count) {
270 return double(count) / duration_cast<seconds>(simDuration).count();
271 };
272
273 auto const fmtS = [](SimDuration dur) {
275 };
276
277 if (printBreakline)
278 {
279 log << std::setw(11) << std::setfill('-') << "-" << "-" << std::setw(7)
280 << std::setfill('-') << "-" << "-" << std::setw(7) << std::setfill('-') << "-"
281 << "-" << std::setw(36) << std::setfill('-') << "-" << std::endl;
282 log << std::setfill(' ');
283 }
284
285 log << std::left << std::setw(11) << "TxStats" << "|" << std::setw(7) << "Count" << "|"
286 << std::setw(7) << "Per Sec" << "|" << std::setw(15) << "Latency (sec)" << std::right
287 << std::setw(7) << "10-ile" << std::setw(7) << "50-ile" << std::setw(7) << "90-ile"
288 << std::left << std::endl;
289
290 log << std::setw(11) << std::setfill('-') << "-" << "|" << std::setw(7) << std::setfill('-')
291 << "-" << "|" << std::setw(7) << std::setfill('-') << "-" << "|" << std::setw(36)
292 << std::setfill('-') << "-" << std::endl;
293 log << std::setfill(' ');
294
295 log << std::left << std::setw(11) << "Submit " << "|" << std::right << std::setw(7)
296 << submitted << "|" << std::setw(7) << std::setprecision(2) << perSec(submitted) << "|"
297 << std::setw(36) << "" << std::endl;
298
299 log << std::left << std::setw(11) << "Accept " << "|" << std::right << std::setw(7)
300 << accepted << "|" << std::setw(7) << std::setprecision(2) << perSec(accepted) << "|"
301 << std::setw(15) << std::left << "From Submit" << std::right << std::setw(7)
302 << std::setprecision(2) << fmtS(submitToAccept.percentile(0.1f)) << std::setw(7)
303 << std::setprecision(2) << fmtS(submitToAccept.percentile(0.5f)) << std::setw(7)
304 << std::setprecision(2) << fmtS(submitToAccept.percentile(0.9f)) << std::endl;
305
306 log << std::left << std::setw(11) << "Validate " << "|" << std::right << std::setw(7)
307 << validated << "|" << std::setw(7) << std::setprecision(2) << perSec(validated) << "|"
308 << std::setw(15) << std::left << "From Submit" << std::right << std::setw(7)
309 << std::setprecision(2) << fmtS(submitToValidate.percentile(0.1f)) << std::setw(7)
310 << std::setprecision(2) << fmtS(submitToValidate.percentile(0.5f)) << std::setw(7)
311 << std::setprecision(2) << fmtS(submitToValidate.percentile(0.9f)) << std::endl;
312
313 log << std::left << std::setw(11) << "Orphan" << "|" << std::right << std::setw(7)
314 << orphaned() << "|" << std::setw(7) << "" << "|" << std::setw(36) << std::endl;
315
316 log << std::left << std::setw(11) << "Unvalidated" << "|" << std::right << std::setw(7)
317 << unvalidated() << "|" << std::setw(7) << "" << "|" << std::setw(43) << std::endl;
318
319 log << std::setw(11) << std::setfill('-') << "-" << "-" << std::setw(7) << std::setfill('-')
320 << "-" << "-" << std::setw(7) << std::setfill('-') << "-" << "-" << std::setw(36)
321 << std::setfill('-') << "-" << std::endl;
322 log << std::setfill(' ');
323 }
324
325 template <class T, class Tag>
326 void
327 csv(SimDuration simDuration, T& log, Tag const& tag, bool printHeaders = false)
328 {
329 using namespace std::chrono;
330 auto const perSec = [&simDuration](std::size_t count) {
331 return double(count) / duration_cast<seconds>(simDuration).count();
332 };
333
334 auto const fmtS = [](SimDuration dur) {
336 };
337
338 if (printHeaders)
339 {
340 log << "tag" << "," << "txNumSubmitted" << "," << "txNumAccepted"
341 << "," << "txNumValidated" << "," << "txNumOrphaned" << ","
342 << "txUnvalidated" << "," << "txRateSumbitted" << ","
343 << "txRateAccepted" << "," << "txRateValidated" << ","
344 << "txLatencySubmitToAccept10Pctl" << ","
345 << "txLatencySubmitToAccept50Pctl" << ","
346 << "txLatencySubmitToAccept90Pctl" << ","
347 << "txLatencySubmitToValidatet10Pctl" << ","
348 << "txLatencySubmitToValidatet50Pctl" << ","
349 << "txLatencySubmitToValidatet90Pctl" << std::endl;
350 }
351
352 log << tag
353 << ","
354 // txNumSubmitted
355 << submitted
356 << ","
357 // txNumAccepted
358 << accepted
359 << ","
360 // txNumValidated
361 << validated
362 << ","
363 // txNumOrphaned
364 << orphaned()
365 << ","
366 // txNumUnvalidated
367 << unvalidated()
368 << ","
369 // txRateSubmitted
370 << std::setprecision(2) << perSec(submitted)
371 << ","
372 // txRateAccepted
373 << std::setprecision(2) << perSec(accepted)
374 << ","
375 // txRateValidated
376 << std::setprecision(2) << perSec(validated)
377 << ","
378 // txLatencySubmitToAccept10Pctl
379 << std::setprecision(2) << fmtS(submitToAccept.percentile(0.1f))
380 << ","
381 // txLatencySubmitToAccept50Pctl
382 << std::setprecision(2) << fmtS(submitToAccept.percentile(0.5f))
383 << ","
384 // txLatencySubmitToAccept90Pctl
385 << std::setprecision(2) << fmtS(submitToAccept.percentile(0.9f))
386 << ","
387 // txLatencySubmitToValidate10Pctl
388 << std::setprecision(2) << fmtS(submitToValidate.percentile(0.1f))
389 << ","
390 // txLatencySubmitToValidate50Pctl
391 << std::setprecision(2) << fmtS(submitToValidate.percentile(0.5f))
392 << ","
393 // txLatencySubmitToValidate90Pctl
394 << std::setprecision(2) << fmtS(submitToValidate.percentile(0.9f)) << "," << std::endl;
395 }
396};
397
405{
408
418
420
425
426 // Ignore most events by default
427 template <class E>
428 void
429 on(PeerID, SimTime, E const& e)
430 {
431 }
432
433 void
434 on(PeerID who, SimTime when, AcceptLedger const& e)
435 {
436 // First time this ledger accepted
437 if (ledgers.emplace(e.ledger.id(), Tracker{when}).second)
438 {
439 ++accepted;
440 // ignore jumps?
441 if (e.prior.id() == e.ledger.parentID())
442 {
443 auto const it = ledgers.find(e.ledger.parentID());
444 if (it != ledgers.end())
445 {
446 acceptToAccept.insert(when - it->second.accepted);
447 }
448 }
449 }
450 }
451
452 void
454 {
455 // ignore jumps
456 if (e.prior.id() == e.ledger.parentID())
457 {
458 auto const it = ledgers.find(e.ledger.id());
459 assert(it != ledgers.end());
460 auto& tracker = it->second;
461 // first time fully validated
462 if (!tracker.fullyValidated)
463 {
465 tracker.fullyValidated = when;
466 acceptToFullyValid.insert(when - tracker.accepted);
467
468 auto const parentIt = ledgers.find(e.ledger.parentID());
469 if (parentIt != ledgers.end())
470 {
471 auto& parentTracker = parentIt->second;
472 if (parentTracker.fullyValidated)
473 {
474 fullyValidToFullyValid.insert(when - *parentTracker.fullyValidated);
475 }
476 }
477 }
478 }
479 }
480
481 [[nodiscard]] std::size_t
483 {
484 return std::count_if(ledgers.begin(), ledgers.end(), [](auto const& it) {
485 return !it.second.fullyValidated;
486 });
487 }
488
489 template <class T>
490 void
491 report(SimDuration simDuration, T& log, bool printBreakline = false)
492 {
493 using namespace std::chrono;
494 auto const perSec = [&simDuration](std::size_t count) {
495 return double(count) / duration_cast<seconds>(simDuration).count();
496 };
497
498 auto const fmtS = [](SimDuration dur) {
500 };
501
502 if (printBreakline)
503 {
504 log << std::setw(11) << std::setfill('-') << "-" << "-" << std::setw(7)
505 << std::setfill('-') << "-" << "-" << std::setw(7) << std::setfill('-') << "-"
506 << "-" << std::setw(36) << std::setfill('-') << "-" << std::endl;
507 log << std::setfill(' ');
508 }
509
510 log << std::left << std::setw(11) << "LedgerStats" << "|" << std::setw(7) << "Count" << "|"
511 << std::setw(7) << "Per Sec"
512 << "|" << std::setw(15) << "Latency (sec)" << std::right << std::setw(7) << "10-ile"
513 << std::setw(7) << "50-ile" << std::setw(7) << "90-ile" << std::left << std::endl;
514
515 log << std::setw(11) << std::setfill('-') << "-" << "|" << std::setw(7) << std::setfill('-')
516 << "-" << "|" << std::setw(7) << std::setfill('-') << "-" << "|" << std::setw(36)
517 << std::setfill('-') << "-" << std::endl;
518 log << std::setfill(' ');
519
520 log << std::left << std::setw(11) << "Accept " << "|" << std::right << std::setw(7)
521 << accepted << "|" << std::setw(7) << std::setprecision(2) << perSec(accepted) << "|"
522 << std::setw(15) << std::left << "From Accept" << std::right << std::setw(7)
523 << std::setprecision(2) << fmtS(acceptToAccept.percentile(0.1f)) << std::setw(7)
524 << std::setprecision(2) << fmtS(acceptToAccept.percentile(0.5f)) << std::setw(7)
525 << std::setprecision(2) << fmtS(acceptToAccept.percentile(0.9f)) << std::endl;
526
527 log << std::left << std::setw(11) << "Validate " << "|" << std::right << std::setw(7)
528 << fullyValidated << "|" << std::setw(7) << std::setprecision(2)
529 << perSec(fullyValidated) << "|" << std::setw(15) << std::left << "From Validate "
531 << fmtS(fullyValidToFullyValid.percentile(0.1f)) << std::setw(7) << std::setprecision(2)
532 << fmtS(fullyValidToFullyValid.percentile(0.5f)) << std::setw(7) << std::setprecision(2)
533 << fmtS(fullyValidToFullyValid.percentile(0.9f)) << std::endl;
534
535 log << std::setw(11) << std::setfill('-') << "-" << "-" << std::setw(7) << std::setfill('-')
536 << "-" << "-" << std::setw(7) << std::setfill('-') << "-" << "-" << std::setw(36)
537 << std::setfill('-') << "-" << std::endl;
538 log << std::setfill(' ');
539 }
540
541 template <class T, class Tag>
542 void
543 csv(SimDuration simDuration, T& log, Tag const& tag, bool printHeaders = false)
544 {
545 using namespace std::chrono;
546 auto const perSec = [&simDuration](std::size_t count) {
547 return double(count) / duration_cast<seconds>(simDuration).count();
548 };
549
550 auto const fmtS = [](SimDuration dur) {
552 };
553
554 if (printHeaders)
555 {
556 log << "tag" << "," << "ledgerNumAccepted" << ","
557 << "ledgerNumFullyValidated" << "," << "ledgerRateAccepted"
558 << "," << "ledgerRateFullyValidated" << ","
559 << "ledgerLatencyAcceptToAccept10Pctl" << ","
560 << "ledgerLatencyAcceptToAccept50Pctl" << ","
561 << "ledgerLatencyAcceptToAccept90Pctl" << ","
562 << "ledgerLatencyFullyValidToFullyValid10Pctl" << ","
563 << "ledgerLatencyFullyValidToFullyValid50Pctl" << ","
564 << "ledgerLatencyFullyValidToFullyValid90Pctl" << std::endl;
565 }
566
567 log << tag
568 << ","
569 // ledgerNumAccepted
570 << accepted
571 << ","
572 // ledgerNumFullyValidated
574 << ","
575 // ledgerRateAccepted
576 << std::setprecision(2) << perSec(accepted)
577 << ","
578 // ledgerRateFullyValidated
579 << std::setprecision(2) << perSec(fullyValidated)
580 << ","
581 // ledgerLatencyAcceptToAccept10Pctl
582 << std::setprecision(2) << fmtS(acceptToAccept.percentile(0.1f))
583 << ","
584 // ledgerLatencyAcceptToAccept50Pctl
585 << std::setprecision(2) << fmtS(acceptToAccept.percentile(0.5f))
586 << ","
587 // ledgerLatencyAcceptToAccept90Pctl
588 << std::setprecision(2) << fmtS(acceptToAccept.percentile(0.9f))
589 << ","
590 // ledgerLatencyFullyValidToFullyValid10Pctl
591 << std::setprecision(2) << fmtS(fullyValidToFullyValid.percentile(0.1f))
592 << ","
593 // ledgerLatencyFullyValidToFullyValid50Pctl
594 << std::setprecision(2) << fmtS(fullyValidToFullyValid.percentile(0.5f))
595 << ","
596 // ledgerLatencyFullyValidToFullyValid90Pctl
597 << std::setprecision(2) << fmtS(fullyValidToFullyValid.percentile(0.9f)) << std::endl;
598 }
599};
600
608{
610
611 // Ignore most events by default
612 template <class E>
613 void
614 on(PeerID, SimTime, E const& e)
615 {
616 }
617
618 void
619 on(PeerID who, SimTime when, AcceptLedger const& e)
620 {
621 out << when.time_since_epoch().count() << ": Node " << who << " accepted " << "L"
622 << e.ledger.id() << " " << e.ledger.txs() << "\n";
623 }
624
625 void
627 {
628 out << when.time_since_epoch().count() << ": Node " << who << " fully-validated " << "L"
629 << e.ledger.id() << " " << e.ledger.txs() << "\n";
630 }
631};
632
640{
648
651
652 // Ignore most events by default
653 template <class E>
654 void
655 on(PeerID, SimTime, E const& e)
656 {
657 }
658
659 void
660 on(PeerID who, SimTime when, AcceptLedger const& e)
661 {
662 // Not a direct child -> parent switch
663 if (e.ledger.parentID() != e.prior.id())
664 closeJumps.emplace_back(Jump{.id = who, .when = when, .from = e.prior, .to = e.ledger});
665 }
666
667 void
669 {
670 // Not a direct child -> parent switch
671 if (e.ledger.parentID() != e.prior.id())
672 {
673 fullyValidatedJumps.emplace_back(
674 Jump{.id = who, .when = when, .from = e.prior, .to = e.ledger});
675 }
676 }
677};
678
679} // namespace xrpl::test::csf
Group of collectors.
Definition collectors.h:45
static void apply(std::tuple< Cs &... > &cs, PeerID who, SimTime when, E e, std::index_sequence< Is... >)
Definition collectors.h:57
void on(PeerID who, SimTime when, E e)
Definition collectors.h:74
std::tuple< Cs &... > cs_
Definition collectors.h:46
Collectors(Cs &... cs)
Constructor.
Definition collectors.h:68
static void apply(C &c, PeerID who, SimTime when, E e)
Definition collectors.h:50
Basic histogram.
Definition Histogram.h:23
A ledger is a set of observed transactions and a sequence number identifying the ledger.
Definition ledgers.h:48
TxSetType const & txs() const
Definition ledgers.h:208
A single transaction.
Definition Tx.h:24
ID const & id() const
Definition Tx.h:40
T count_if(T... args)
T duration_cast(T... args)
T endl(T... args)
T left(T... args)
T log(T... args)
STL namespace.
SimClock::duration SimDuration
Definition SimTime.h:14
TaggedInteger< std::uint32_t, PeerIDTag > PeerID
Definition Validation.h:17
SimClock::time_point SimTime
Definition SimTime.h:15
Collectors< Cs... > makeCollectors(Cs &... cs)
Create an instance of Collectors<Cs...>.
Definition collectors.h:85
std::unordered_map< Key, Value, Hash, Pred, Allocator > HashMap
T setfill(T... args)
T setprecision(T... args)
T setw(T... args)
Peer accepted consensus results.
Definition events.h:121
Maintain an instance of a Collector per peer.
Definition collectors.h:101
CollectorType const & operator[](PeerID who) const
Definition collectors.h:111
void on(PeerID who, SimTime when, E const &e)
Definition collectors.h:117
std::map< PeerID, CollectorType > byNode
Definition collectors.h:102
CollectorType & operator[](PeerID who)
Definition collectors.h:105
Peer fully validated a new ledger.
Definition events.h:144
Ledger prior
The prior fully validated ledger This is a jump if prior.id() != ledger.parentID().
Definition events.h:154
Ledger ledger
The new fully validated ledger.
Definition events.h:148
Saves information about Jumps for closed and fully validated ledgers.
Definition collectors.h:640
std::vector< Jump > fullyValidatedJumps
Definition collectors.h:650
void on(PeerID, SimTime, E const &e)
Definition collectors.h:655
void on(PeerID who, SimTime when, FullyValidateLedger const &e)
Definition collectors.h:668
void on(PeerID who, SimTime when, AcceptLedger const &e)
Definition collectors.h:660
std::vector< Jump > closeJumps
Definition collectors.h:649
std::optional< SimTime > fullyValidated
Definition collectors.h:412
Tracks the accepted -> validated evolution of ledgers.
Definition collectors.h:405
void on(PeerID who, SimTime when, AcceptLedger const &e)
Definition collectors.h:434
HashMap< Ledger::ID, Tracker > ledgers
Definition collectors.h:419
void csv(SimDuration simDuration, T &log, Tag const &tag, bool printHeaders=false)
Definition collectors.h:543
void on(PeerID, SimTime, E const &e)
Definition collectors.h:429
void on(PeerID who, SimTime when, FullyValidateLedger const &e)
Definition collectors.h:453
void report(SimDuration simDuration, T &log, bool printBreakline=false)
Definition collectors.h:491
Histogram< SimTime::duration > Hist
Definition collectors.h:421
std::size_t unvalidated() const
Definition collectors.h:482
Collector which ignores all events.
Definition collectors.h:127
void on(PeerID, SimTime, E const &e)
Definition collectors.h:130
Tracks the overall duration of a simulation.
Definition collectors.h:139
void on(PeerID, SimTime when, E const &e)
Definition collectors.h:146
Write out stream of ledger activity.
Definition collectors.h:608
void on(PeerID, SimTime, E const &e)
Definition collectors.h:614
void on(PeerID who, SimTime when, FullyValidateLedger const &e)
Definition collectors.h:626
void on(PeerID who, SimTime when, AcceptLedger const &e)
Definition collectors.h:619
A transaction submitted to a peer.
Definition events.h:82
Tx tx
The submitted transaction.
Definition events.h:86
std::optional< SimTime > accepted
Definition collectors.h:181
std::optional< SimTime > validated
Definition collectors.h:182
Tracker(Tx tx, SimTime submitted)
Definition collectors.h:184
Tracks the submission -> accepted -> validated evolution of transactions.
Definition collectors.h:171
void report(SimDuration simDuration, T &log, bool printBreakline=false)
Definition collectors.h:266
void on(PeerID who, SimTime when, AcceptLedger const &e)
Definition collectors.h:213
std::size_t orphaned() const
Definition collectors.h:250
void on(PeerID who, SimTime when, FullyValidateLedger const &e)
Definition collectors.h:230
std::size_t unvalidated() const
Definition collectors.h:258
void on(PeerID, SimTime when, E const &e)
Definition collectors.h:198
Histogram< SimTime::duration > Hist
Definition collectors.h:191
void on(PeerID who, SimTime when, SubmitTx const &e)
Definition collectors.h:203
HashMap< Tx::ID, Tracker > txs
Definition collectors.h:189
void csv(SimDuration simDuration, T &log, Tag const &tag, bool printHeaders=false)
Definition collectors.h:327