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  int const N = 100; // Peers
40 
41  int const numUNLs = 15; // UNL lists
42  int const minUNLSize = N / 4, maxUNLSize = N / 2;
43 
44  ConsensusParms const parms{};
45  Sim sim;
46  PeerGroup network = sim.createGroup(N);
47 
48  // generate trust ranks
49  std::vector<double> const ranks =
50  sample(network.size(), PowerLawDistribution{1, 3}, sim.rng);
51 
52  // generate scale-free trust graph
53  randomRankedTrust(
54  network,
55  ranks,
56  numUNLs,
57  std::uniform_int_distribution<>{minUNLSize, maxUNLSize},
58  sim.rng);
59 
60  // nodes with a trust line in either direction are network-connected
61  network.connectFromTrust(
62  round<milliseconds>(0.2 * parms.ledgerGRANULARITY));
63 
64  // Initialize collectors to track statistics to report
65  TxCollector txCollector;
66  LedgerCollector ledgerCollector;
67  auto colls = makeCollectors(txCollector, ledgerCollector);
68  sim.collectors.add(colls);
69 
70  // Initial round to set prior state
71  sim.run(1);
72 
73  // Initialize timers
74  HeartbeatTimer heart(sim.scheduler, seconds(10s));
75 
76  // Run for 10 minues, submitting 100 tx/second
77  std::chrono::nanoseconds const simDuration = 10min;
78  std::chrono::nanoseconds const quiet = 10s;
79  Rate const rate{100, 1000ms};
80 
81  // txs, start/stop/step, target
82  auto peerSelector =
83  makeSelector(network.begin(), network.end(), ranks, sim.rng);
84  auto txSubmitter = makeSubmitter(
85  ConstantDistribution{rate.inv()},
86  sim.scheduler.now() + quiet,
87  sim.scheduler.now() + (simDuration - quiet),
88  peerSelector,
89  sim.scheduler,
90  sim.rng);
91 
92  // run simulation for given duration
93  heart.start();
94  sim.run(simDuration);
95 
96  BEAST_EXPECT(sim.branches() == 1);
97  BEAST_EXPECT(sim.synchronized());
98 
99  // TODO: Clean up this formatting mess!!
100 
101  log << "Peers: " << network.size() << std::endl;
102  log << "Simulated Duration: "
103  << duration_cast<milliseconds>(simDuration).count() << " ms"
104  << std::endl;
105  log << "Branches: " << sim.branches() << std::endl;
106  log << "Synchronized: " << (sim.synchronized() ? "Y" : "N")
107  << std::endl;
108  log << std::endl;
109 
110  txCollector.report(simDuration, log);
111  ledgerCollector.report(simDuration, log);
112  // Print summary?
113  // # forks? # of LCLs?
114  // # peers
115  // # tx submitted
116  // # ledgers/sec etc.?
117  }
118 };
119 
120 BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(ScaleFreeSim, consensus, ripple, 80);
121 
122 } // namespace test
123 } // 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