rippled
Loading...
Searching...
No Matches
timers.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2017 Ripple Labs Inc
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_TEST_CSF_TIMERS_H_INCLUDED
21#define RIPPLE_TEST_CSF_TIMERS_H_INCLUDED
22
23#include <test/csf/Scheduler.h>
24#include <test/csf/SimTime.h>
25
26#include <chrono>
27#include <ostream>
28
29namespace ripple {
30namespace test {
31namespace csf {
32
33// Timers are classes that schedule repeated events and are mostly independent
34// of simulation-specific details.
35
39{
43
46
47public:
49 Scheduler& sched,
50 SimDuration interval = std::chrono::seconds{60},
52 : scheduler_{sched}
53 , interval_{interval}
54 , out_{out}
56 , startSimTime_{sched.now()}
57 {
58 }
59
60 void
62 {
63 scheduler_.in(interval_, [this]() { beat(scheduler_.now()); });
64 }
65
66 void
68 {
69 using namespace std::chrono;
70 RealTime realTime = RealClock::now();
71 SimTime simTime = when;
72
73 RealDuration realDuration = realTime - startRealTime_;
74 SimDuration simDuration = simTime - startSimTime_;
75 out_ << "Heartbeat. Time Elapsed: {sim: "
76 << duration_cast<seconds>(simDuration).count()
77 << "s | real: " << duration_cast<seconds>(realDuration).count()
78 << "s}\n"
79 << std::flush;
80
81 scheduler_.in(interval_, [this]() { beat(scheduler_.now()); });
82 }
83};
84
85} // namespace csf
86} // namespace test
87} // namespace ripple
88
89#endif
Gives heartbeat of simulation to signal simulation progression.
Definition timers.h:39
HeartbeatTimer(Scheduler &sched, SimDuration interval=std::chrono::seconds{60}, std::ostream &out=std::cerr)
Definition timers.h:48
void beat(SimTime when)
Definition timers.h:67
Simulated discrete-event scheduler.
time_point now() const
Return the current network time.
cancel_token in(duration const &delay, Function &&f)
Schedule an event after a specified duration passes.
T flush(T... args)
typename SimClock::duration SimDuration
Definition SimTime.h:36
RealClock::time_point RealTime
Definition SimTime.h:33
typename SimClock::time_point SimTime
Definition SimTime.h:37
RealClock::duration RealDuration
Definition SimTime.h:32
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25