rippled
Loading...
Searching...
No Matches
DistributedValidatorsSim_test.cpp
1#include <test/csf.h>
2
3#include <xrpl/beast/unit_test.h>
4
5#include <boost/algorithm/string/classification.hpp>
6#include <boost/algorithm/string/split.hpp>
7
8#include <algorithm>
9#include <fstream>
10#include <sstream>
11#include <string>
12#include <utility>
13
14namespace xrpl {
15namespace test {
16
20{
21 void
23 std::size_t numPeers,
25 bool printHeaders = false)
26 {
27 using namespace csf;
28 using namespace std::chrono;
29
30 // Initialize persistent collector logs specific to this method
31 std::string const prefix =
32 "DistributedValidators_"
33 "completeTrustCompleteConnectFixedDelay";
34 std::fstream txLog(prefix + "_tx.csv", std::ofstream::app),
35 ledgerLog(prefix + "_ledger.csv", std::ofstream::app);
36
37 // title
38 log << prefix << "(" << numPeers << "," << delay.count() << ")" << std::endl;
39
40 // number of peers, UNLs, connections
41 BEAST_EXPECT(numPeers >= 1);
42
43 Sim sim;
44 PeerGroup peers = sim.createGroup(numPeers);
45
46 // complete trust graph
47 peers.trust(peers);
48
49 // complete connect graph with fixed delay
50 peers.connect(peers, delay);
51
52 // Initialize collectors to track statistics to report
53 TxCollector txCollector;
54 LedgerCollector ledgerCollector;
55 auto colls = makeCollectors(txCollector, ledgerCollector);
56 sim.collectors.add(colls);
57
58 // Initial round to set prior state
59 sim.run(1);
60
61 // Run for 10 minutes, submitting 100 tx/second
62 std::chrono::nanoseconds const simDuration = 10min;
63 std::chrono::nanoseconds const quiet = 10s;
64 Rate const rate{100, 1000ms};
65
66 // Initialize timers
67 HeartbeatTimer heart(sim.scheduler);
68
69 // txs, start/stop/step, target
70 auto peerSelector =
71 makeSelector(peers.begin(), peers.end(), std::vector<double>(numPeers, 1.), sim.rng);
72 auto txSubmitter = makeSubmitter(
73 ConstantDistribution{rate.inv()},
74 sim.scheduler.now() + quiet,
75 sim.scheduler.now() + simDuration - quiet,
76 peerSelector,
77 sim.scheduler,
78 sim.rng);
79
80 // run simulation for given duration
81 heart.start();
82 sim.run(simDuration);
83
84 // BEAST_EXPECT(sim.branches() == 1);
85 // BEAST_EXPECT(sim.synchronized());
86
87 log << std::right;
88 log << "| Peers: " << std::setw(2) << peers.size();
89 log << " | Duration: " << std::setw(6) << duration_cast<milliseconds>(simDuration).count()
90 << " ms";
91 log << " | Branches: " << std::setw(1) << sim.branches();
92 log << " | Synchronized: " << std::setw(1) << (sim.synchronized() ? "Y" : "N");
93 log << " |" << std::endl;
94
95 txCollector.report(simDuration, log, true);
96 ledgerCollector.report(simDuration, log, false);
97
98 std::string const tag = std::to_string(numPeers);
99 txCollector.csv(simDuration, txLog, tag, printHeaders);
100 ledgerCollector.csv(simDuration, ledgerLog, tag, printHeaders);
101
102 log << std::endl;
103 }
104
105 void
107 std::size_t numPeers,
109 bool printHeaders = false)
110 {
111 using namespace csf;
112 using namespace std::chrono;
113
114 // Initialize persistent collector logs specific to this method
115 std::string const prefix =
116 "DistributedValidators__"
117 "completeTrustScaleFreeConnectFixedDelay";
118 std::fstream txLog(prefix + "_tx.csv", std::ofstream::app),
119 ledgerLog(prefix + "_ledger.csv", std::ofstream::app);
120
121 // title
122 log << prefix << "(" << numPeers << "," << delay.count() << ")" << std::endl;
123
124 // number of peers, UNLs, connections
125 int const numCNLs = std::max(int(1.00 * numPeers), 1);
126 int const minCNLSize = std::max(int(0.25 * numCNLs), 1);
127 int const maxCNLSize = std::max(int(0.50 * numCNLs), 1);
128 BEAST_EXPECT(numPeers >= 1);
129 BEAST_EXPECT(numCNLs >= 1);
130 BEAST_EXPECT(1 <= minCNLSize && minCNLSize <= maxCNLSize && maxCNLSize <= numPeers);
131
132 Sim sim;
133 PeerGroup peers = sim.createGroup(numPeers);
134
135 // complete trust graph
136 peers.trust(peers);
137
138 // scale-free connect graph with fixed delay
139 std::vector<double> const ranks = sample(peers.size(), PowerLawDistribution{1, 3}, sim.rng);
140 randomRankedConnect(
141 peers,
142 ranks,
143 numCNLs,
144 std::uniform_int_distribution<>{minCNLSize, maxCNLSize},
145 sim.rng,
146 delay);
147
148 // Initialize collectors to track statistics to report
149 TxCollector txCollector;
150 LedgerCollector ledgerCollector;
151 auto colls = makeCollectors(txCollector, ledgerCollector);
152 sim.collectors.add(colls);
153
154 // Initial round to set prior state
155 sim.run(1);
156
157 // Run for 10 minutes, submitting 100 tx/second
158 std::chrono::nanoseconds const simDuration = 10min;
159 std::chrono::nanoseconds const quiet = 10s;
160 Rate const rate{100, 1000ms};
161
162 // Initialize timers
163 HeartbeatTimer heart(sim.scheduler);
164
165 // txs, start/stop/step, target
166 auto peerSelector =
167 makeSelector(peers.begin(), peers.end(), std::vector<double>(numPeers, 1.), sim.rng);
168 auto txSubmitter = makeSubmitter(
169 ConstantDistribution{rate.inv()},
170 sim.scheduler.now() + quiet,
171 sim.scheduler.now() + simDuration - quiet,
172 peerSelector,
173 sim.scheduler,
174 sim.rng);
175
176 // run simulation for given duration
177 heart.start();
178 sim.run(simDuration);
179
180 // BEAST_EXPECT(sim.branches() == 1);
181 // BEAST_EXPECT(sim.synchronized());
182
183 log << std::right;
184 log << "| Peers: " << std::setw(2) << peers.size();
185 log << " | Duration: " << std::setw(6) << duration_cast<milliseconds>(simDuration).count()
186 << " ms";
187 log << " | Branches: " << std::setw(1) << sim.branches();
188 log << " | Synchronized: " << std::setw(1) << (sim.synchronized() ? "Y" : "N");
189 log << " |" << std::endl;
190
191 txCollector.report(simDuration, log, true);
192 ledgerCollector.report(simDuration, log, false);
193
194 std::string const tag = std::to_string(numPeers);
195 txCollector.csv(simDuration, txLog, tag, printHeaders);
196 ledgerCollector.csv(simDuration, ledgerLog, tag, printHeaders);
197
198 log << std::endl;
199 }
200
201 void
202 run() override
203 {
204 std::string const defaultArgs = "5 200";
205 std::string const args = arg().empty() ? defaultArgs : arg();
206 std::stringstream argStream(args);
207
208 int maxNumValidators = 0;
209 int delayCount(200);
210 argStream >> maxNumValidators;
211 argStream >> delayCount;
212
213 std::chrono::milliseconds const delay(delayCount);
214
215 log << "DistributedValidators: 1 to " << maxNumValidators << " Peers" << std::endl;
216
224 for (int i = 2; i <= maxNumValidators; i++)
225 {
227 }
228
236 for (int i = 2; i <= maxNumValidators; i++)
237 {
239 }
240 }
241};
242
243BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(DistributedValidators, consensus, xrpl, 2);
244
245} // namespace test
246} // namespace xrpl
A testsuite class.
Definition suite.h:51
log_os< char > log
Logging output stream.
Definition suite.h:147
std::string const & arg() const
Return the argument associated with the runner.
Definition suite.h:279
In progress simulations for diversifying and distributing validators.
void completeTrustCompleteConnectFixedDelay(std::size_t numPeers, std::chrono::milliseconds delay=std::chrono::milliseconds(200), bool printHeaders=false)
void completeTrustScaleFreeConnectFixedDelay(std::size_t numPeers, std::chrono::milliseconds delay=std::chrono::milliseconds(200), bool printHeaders=false)
T empty(T... args)
T endl(T... args)
T right(T... args)
T max(T... args)
Json::Value rate(Account const &account, double multiplier)
Set a transfer rate.
Definition rate.cpp:13
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
T setw(T... args)
Represents a transfer rate.
Definition Rate.h:20
T to_string(T... args)