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