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