rippled
Loading...
Searching...
No Matches
submitters.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_SUBMITTERS_H_INCLUDED
20#define RIPPLE_TEST_CSF_SUBMITTERS_H_INCLUDED
21
22#include <test/csf/Peer.h>
23#include <test/csf/Scheduler.h>
24#include <test/csf/SimTime.h>
25#include <test/csf/Tx.h>
26#include <type_traits>
27namespace ripple {
28namespace test {
29namespace csf {
30
31// Submitters are classes for simulating submission of transactions to the
32// network
33
35struct Rate
36{
39
40 double
41 inv() const
42 {
43 return duration.count() / double(count);
44 }
45};
46
64template <class Distribution, class Generator, class Selector>
66{
67 Distribution dist_;
72 Generator& g_;
73
74 // Convert generated durations to SimDuration
75 static SimDuration
77 {
78 return d;
79 }
80
81 template <class T>
84 {
85 return SimDuration{static_cast<SimDuration::rep>(t)};
86 }
87
88 void
90 {
91 selector_()->submit(Tx{nextID_++});
92 if (scheduler_.now() < stop_)
93 {
94 scheduler_.in(asDuration(dist_(g_)), [&]() { submit(); });
95 }
96 }
97
98public:
100 Distribution dist,
101 SimTime start,
102 SimTime end,
103 Selector& selector,
104 Scheduler& s,
105 Generator& g)
106 : dist_{dist}, stop_{end}, selector_{selector}, scheduler_{s}, g_{g}
107 {
108 scheduler_.at(start, [&]() { submit(); });
109 }
110};
111
112template <class Distribution, class Generator, class Selector>
113Submitter<Distribution, Generator, Selector>
115 Distribution dist,
116 SimTime start,
117 SimTime end,
118 Selector& sel,
119 Scheduler& s,
120 Generator& g)
121{
123 dist, start, end, sel, s, g);
124}
125
126} // namespace csf
127} // namespace test
128} // namespace ripple
129
130#endif
Simulated discrete-event scheduler.
cancel_token at(time_point const &when, Function &&f)
Schedule an event at a specific time.
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.
Invocable that returns random samples from a range according to a discrete distribution.
Submits transactions to a specified peer.
Definition: submitters.h:66
static std::enable_if_t< std::is_arithmetic< T >::value, SimDuration > asDuration(T t)
Definition: submitters.h:83
Submitter(Distribution dist, SimTime start, SimTime end, Selector &selector, Scheduler &s, Generator &g)
Definition: submitters.h:99
static SimDuration asDuration(SimDuration d)
Definition: submitters.h:76
A single transaction.
Definition: Tx.h:38
Submitter< Distribution, Generator, Selector > makeSubmitter(Distribution dist, SimTime start, SimTime end, Selector &sel, Scheduler &s, Generator &g)
Definition: submitters.h:114
typename SimClock::duration SimDuration
Definition: SimTime.h:35
typename SimClock::time_point SimTime
Definition: SimTime.h:36
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
Represents rate as a count/duration.
Definition: submitters.h:36
SimDuration duration
Definition: submitters.h:38
double inv() const
Definition: submitters.h:41