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
20#ifndef RIPPLE_TEST_CSF_SUBMITTERS_H_INCLUDED
21#define RIPPLE_TEST_CSF_SUBMITTERS_H_INCLUDED
22
23#include <test/csf/Peer.h>
24#include <test/csf/Scheduler.h>
25#include <test/csf/SimTime.h>
26#include <test/csf/Tx.h>
27
28#include <type_traits>
29
30namespace ripple {
31namespace test {
32namespace csf {
33
34// Submitters are classes for simulating submission of transactions to the
35// network
36
38struct Rate
39{
42
43 double
44 inv() const
45 {
46 return duration.count() / double(count);
47 }
48};
49
67template <class Distribution, class Generator, class Selector>
69{
70 Distribution dist_;
75 Generator& g_;
76
77 // Convert generated durations to SimDuration
78 static SimDuration
80 {
81 return d;
82 }
83
84 template <class T>
87 {
88 return SimDuration{static_cast<SimDuration::rep>(t)};
89 }
90
91 void
93 {
94 selector_()->submit(Tx{nextID_++});
95 if (scheduler_.now() < stop_)
96 {
97 scheduler_.in(asDuration(dist_(g_)), [&]() { submit(); });
98 }
99 }
100
101public:
103 Distribution dist,
104 SimTime start,
105 SimTime end,
106 Selector& selector,
107 Scheduler& s,
108 Generator& g)
109 : dist_{dist}, stop_{end}, selector_{selector}, scheduler_{s}, g_{g}
110 {
111 scheduler_.at(start, [&]() { submit(); });
112 }
113};
114
115template <class Distribution, class Generator, class Selector>
116Submitter<Distribution, Generator, Selector>
118 Distribution dist,
119 SimTime start,
120 SimTime end,
121 Selector& sel,
122 Scheduler& s,
123 Generator& g)
124{
126 dist, start, end, sel, s, g);
127}
128
129} // namespace csf
130} // namespace test
131} // namespace ripple
132
133#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:69
static std::enable_if_t< std::is_arithmetic< T >::value, SimDuration > asDuration(T t)
Definition submitters.h:86
Submitter(Distribution dist, SimTime start, SimTime end, Selector &selector, Scheduler &s, Generator &g)
Definition submitters.h:102
static SimDuration asDuration(SimDuration d)
Definition submitters.h:79
A single transaction.
Definition Tx.h:42
Submitter< Distribution, Generator, Selector > makeSubmitter(Distribution dist, SimTime start, SimTime end, Selector &sel, Scheduler &s, Generator &g)
Definition submitters.h:117
typename SimClock::duration SimDuration
Definition SimTime.h:36
typename SimClock::time_point SimTime
Definition SimTime.h:37
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Represents rate as a count/duration.
Definition submitters.h:39
double inv() const
Definition submitters.h:44