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#ifndef RIPPLE_TEST_CSF_TIMERS_H_INCLUDED
20#define RIPPLE_TEST_CSF_TIMERS_H_INCLUDED
21
22#include <test/csf/Scheduler.h>
23#include <test/csf/SimTime.h>
24#include <chrono>
25#include <ostream>
26
27namespace ripple {
28namespace test {
29namespace csf {
30
31// Timers are classes that schedule repeated events and are mostly independent
32// of simulation-specific details.
33
37{
41
44
45public:
47 Scheduler& sched,
48 SimDuration interval = std::chrono::seconds{60},
50 : scheduler_{sched}
51 , interval_{interval}
52 , out_{out}
54 , startSimTime_{sched.now()}
55 {
56 }
57
58 void
60 {
61 scheduler_.in(interval_, [this]() { beat(scheduler_.now()); });
62 }
63
64 void
66 {
67 using namespace std::chrono;
68 RealTime realTime = RealClock::now();
69 SimTime simTime = when;
70
71 RealDuration realDuration = realTime - startRealTime_;
72 SimDuration simDuration = simTime - startSimTime_;
73 out_ << "Heartbeat. Time Elapsed: {sim: "
74 << duration_cast<seconds>(simDuration).count()
75 << "s | real: " << duration_cast<seconds>(realDuration).count()
76 << "s}\n"
77 << std::flush;
78
79 scheduler_.in(interval_, [this]() { beat(scheduler_.now()); });
80 }
81};
82
83} // namespace csf
84} // namespace test
85} // namespace ripple
86
87#endif
Gives heartbeat of simulation to signal simulation progression.
Definition: timers.h:37
HeartbeatTimer(Scheduler &sched, SimDuration interval=std::chrono::seconds{60}, std::ostream &out=std::cerr)
Definition: timers.h:46
void beat(SimTime when)
Definition: timers.h:65
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:35
RealClock::time_point RealTime
Definition: SimTime.h:32
typename SimClock::time_point SimTime
Definition: SimTime.h:36
RealClock::duration RealDuration
Definition: SimTime.h:31
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26