xrpld
Loading...
Searching...
No Matches
timers.h
1#pragma once
2
3#include <test/csf/Scheduler.h>
4#include <test/csf/SimTime.h>
5
6#include <chrono>
7#include <ostream>
8
9namespace xrpl::test::csf {
10
11// Timers are classes that schedule repeated events and are mostly independent
12// of simulation-specific details.
13
17{
21
24
25public:
27 Scheduler& sched,
28 SimDuration interval = std::chrono::seconds{60},
30 : scheduler_{sched}
31 , interval_{interval}
32 , out_{out}
34 , startSimTime_{sched.now()}
35 {
36 }
37
38 void
40 {
41 scheduler_.in(interval_, [this]() { beat(scheduler_.now()); });
42 }
43
44 void
46 {
47 using namespace std::chrono;
48 RealTime const realTime = RealClock::now();
49 SimTime const simTime = when;
50
51 RealDuration const realDuration = realTime - startRealTime_;
52 SimDuration const simDuration = simTime - startSimTime_;
53 out_ << "Heartbeat. Time Elapsed: {sim: " << duration_cast<seconds>(simDuration).count()
54 << "s | real: " << duration_cast<seconds>(realDuration).count() << "s}\n"
55 << std::flush;
56
57 scheduler_.in(interval_, [this]() { beat(scheduler_.now()); });
58 }
59};
60
61} // namespace xrpl::test::csf
HeartbeatTimer(Scheduler &sched, SimDuration interval=std::chrono::seconds{60}, std::ostream &out=std::cerr)
Definition timers.h:26
void beat(SimTime when)
Definition timers.h:45
Simulated discrete-event scheduler.
T duration_cast(T... args)
T flush(T... args)
std::chrono::system_clock RealClock
Definition SimTime.h:9
SimClock::duration SimDuration
Definition SimTime.h:14
RealClock::duration RealDuration
Definition SimTime.h:10
SimClock::time_point SimTime
Definition SimTime.h:15
RealClock::time_point RealTime
Definition SimTime.h:11