Changeset View
Changeset View
Standalone View
Standalone View
src/bench/mempool_stress.cpp
Show All 23 Lines | |||||
struct Available { | struct Available { | ||||
CTransactionRef ref; | CTransactionRef ref; | ||||
size_t vin_left{0}; | size_t vin_left{0}; | ||||
size_t tx_count; | size_t tx_count; | ||||
Available(CTransactionRef &_ref, size_t _tx_count) | Available(CTransactionRef &_ref, size_t _tx_count) | ||||
: ref(_ref), tx_count(_tx_count) {} | : ref(_ref), tx_count(_tx_count) {} | ||||
}; | }; | ||||
static void ComplexMemPool(benchmark::State &state) { | static void ComplexMemPool(benchmark::Bench &bench) { | ||||
int childTxs = 800; | |||||
if (bench.complexityN() > 1) { | |||||
childTxs = static_cast<int>(bench.complexityN()); | |||||
} | |||||
FastRandomContext det_rand{true}; | FastRandomContext det_rand{true}; | ||||
std::vector<Available> available_coins; | std::vector<Available> available_coins; | ||||
std::vector<CTransactionRef> ordered_coins; | std::vector<CTransactionRef> ordered_coins; | ||||
// Create some base transactions | // Create some base transactions | ||||
size_t tx_counter = 1; | size_t tx_counter = 1; | ||||
for (auto x = 0; x < 100; ++x) { | for (auto x = 0; x < 100; ++x) { | ||||
CMutableTransaction tx = CMutableTransaction(); | CMutableTransaction tx = CMutableTransaction(); | ||||
tx.vin.resize(1); | tx.vin.resize(1); | ||||
tx.vin[0].scriptSig = CScript() << CScriptNum(tx_counter); | tx.vin[0].scriptSig = CScript() << CScriptNum(tx_counter); | ||||
tx.vout.resize(det_rand.randrange(10) + 2); | tx.vout.resize(det_rand.randrange(10) + 2); | ||||
for (auto &out : tx.vout) { | for (auto &out : tx.vout) { | ||||
out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL; | out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL; | ||||
out.nValue = 10 * COIN; | out.nValue = 10 * COIN; | ||||
} | } | ||||
ordered_coins.emplace_back(MakeTransactionRef(tx)); | ordered_coins.emplace_back(MakeTransactionRef(tx)); | ||||
available_coins.emplace_back(ordered_coins.back(), tx_counter++); | available_coins.emplace_back(ordered_coins.back(), tx_counter++); | ||||
} | } | ||||
for (auto x = 0; x < 800 && !available_coins.empty(); ++x) { | for (auto x = 0; x < childTxs && !available_coins.empty(); ++x) { | ||||
CMutableTransaction tx = CMutableTransaction(); | CMutableTransaction tx = CMutableTransaction(); | ||||
size_t n_ancestors = det_rand.randrange(10) + 1; | size_t n_ancestors = det_rand.randrange(10) + 1; | ||||
for (size_t ancestor = 0; | for (size_t ancestor = 0; | ||||
ancestor < n_ancestors && !available_coins.empty(); ++ancestor) { | ancestor < n_ancestors && !available_coins.empty(); ++ancestor) { | ||||
size_t idx = det_rand.randrange(available_coins.size()); | size_t idx = det_rand.randrange(available_coins.size()); | ||||
Available coin = available_coins[idx]; | Available coin = available_coins[idx]; | ||||
TxId txid = coin.ref->GetId(); | TxId txid = coin.ref->GetId(); | ||||
// biased towards taking just one ancestor, but maybe more | // biased towards taking just one ancestor, but maybe more | ||||
Show All 19 Lines | for (auto x = 0; x < childTxs && !available_coins.empty(); ++x) { | ||||
} | } | ||||
} | } | ||||
ordered_coins.emplace_back(MakeTransactionRef(tx)); | ordered_coins.emplace_back(MakeTransactionRef(tx)); | ||||
available_coins.emplace_back(ordered_coins.back(), tx_counter++); | available_coins.emplace_back(ordered_coins.back(), tx_counter++); | ||||
} | } | ||||
TestingSetup test_setup; | TestingSetup test_setup; | ||||
CTxMemPool pool; | CTxMemPool pool; | ||||
LOCK2(cs_main, pool.cs); | LOCK2(cs_main, pool.cs); | ||||
while (state.KeepRunning()) { | bench.run([&]() NO_THREAD_SAFETY_ANALYSIS { | ||||
for (auto &tx : ordered_coins) { | for (auto &tx : ordered_coins) { | ||||
AddTx(tx, pool); | AddTx(tx, pool); | ||||
} | } | ||||
pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4); | pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4); | ||||
pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front())); | pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front())); | ||||
} | }); | ||||
} | } | ||||
BENCHMARK(ComplexMemPool, 1); | BENCHMARK(ComplexMemPool); |