rippled
Loading...
Searching...
No Matches
ScaleFreeSim_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2016 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#include <test/csf.h>
21#include <test/csf/random.h>
22#include <xrpl/beast/unit_test.h>
23
24#include <utility>
25
26namespace ripple {
27namespace test {
28
30{
31 void
32 run() override
33 {
34 using namespace std::chrono;
35 using namespace csf;
36
37 // Generate a quasi-random scale free network and simulate consensus
38 // as we vary transaction submission rates
39
40 int const N = 100; // Peers
41
42 int const numUNLs = 15; // UNL lists
43 int const minUNLSize = N / 4, maxUNLSize = N / 2;
44
45 ConsensusParms const parms{};
46 Sim sim;
47 PeerGroup network = sim.createGroup(N);
48
49 // generate trust ranks
50 std::vector<double> const ranks =
51 sample(network.size(), PowerLawDistribution{1, 3}, sim.rng);
52
53 // generate scale-free trust graph
54 randomRankedTrust(
55 network,
56 ranks,
57 numUNLs,
58 std::uniform_int_distribution<>{minUNLSize, maxUNLSize},
59 sim.rng);
60
61 // nodes with a trust line in either direction are network-connected
62 network.connectFromTrust(
63 round<milliseconds>(0.2 * parms.ledgerGRANULARITY));
64
65 // Initialize collectors to track statistics to report
66 TxCollector txCollector;
67 LedgerCollector ledgerCollector;
68 auto colls = makeCollectors(txCollector, ledgerCollector);
69 sim.collectors.add(colls);
70
71 // Initial round to set prior state
72 sim.run(1);
73
74 // Initialize timers
75 HeartbeatTimer heart(sim.scheduler, seconds(10s));
76
77 // Run for 10 minues, submitting 100 tx/second
78 std::chrono::nanoseconds const simDuration = 10min;
79 std::chrono::nanoseconds const quiet = 10s;
80 Rate const rate{100, 1000ms};
81
82 // txs, start/stop/step, target
83 auto peerSelector =
84 makeSelector(network.begin(), network.end(), ranks, sim.rng);
85 auto txSubmitter = makeSubmitter(
86 ConstantDistribution{rate.inv()},
87 sim.scheduler.now() + quiet,
88 sim.scheduler.now() + (simDuration - quiet),
89 peerSelector,
90 sim.scheduler,
91 sim.rng);
92
93 // run simulation for given duration
94 heart.start();
95 sim.run(simDuration);
96
97 BEAST_EXPECT(sim.branches() == 1);
98 BEAST_EXPECT(sim.synchronized());
99
100 // TODO: Clean up this formatting mess!!
101
102 log << "Peers: " << network.size() << std::endl;
103 log << "Simulated Duration: "
104 << duration_cast<milliseconds>(simDuration).count() << " ms"
105 << std::endl;
106 log << "Branches: " << sim.branches() << std::endl;
107 log << "Synchronized: " << (sim.synchronized() ? "Y" : "N")
108 << std::endl;
109 log << std::endl;
110
111 txCollector.report(simDuration, log);
112 ledgerCollector.report(simDuration, log);
113 // Print summary?
114 // # forks? # of LCLs?
115 // # peers
116 // # tx submitted
117 // # ledgers/sec etc.?
118 }
119};
120
121BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(ScaleFreeSim, consensus, ripple, 80);
122
123} // namespace test
124} // namespace ripple
A testsuite class.
Definition: suite.h:55
log_os< char > log
Logging output stream.
Definition: suite.h:152
void run() override
Runs the suite.
T endl(T... args)
Json::Value rate(Account const &account, double multiplier)
Set a transfer rate.
Definition: rate.cpp:31
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
Consensus algorithm parameters.
Represents a transfer rate.
Definition: Rate.h:40