rpc_mempool.cpp raw

   1  // Copyright (c) 2011-present The Bitcoin Core developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <bench/bench.h>
   6  #include <consensus/amount.h>
   7  #include <kernel/cs_main.h>
   8  #include <primitives/transaction.h>
   9  #include <rpc/mempool.h>
  10  #include <script/script.h>
  11  #include <sync.h>
  12  #include <test/util/setup_common.h>
  13  #include <test/util/txmempool.h>
  14  #include <txmempool.h>
  15  #include <univalue.h>
  16  #include <util/check.h>
  17  
  18  #include <memory>
  19  #include <vector>
  20  
  21  
  22  static void AddTx(const CTransactionRef& tx, const CAmount& fee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
  23  {
  24      LockPoints lp;
  25      TryAddToMempool(pool, CTxMemPoolEntry(tx, fee, /*time=*/0, /*entry_height=*/1, /*entry_sequence=*/0, /*spends_coinbase=*/false, /*sigops_cost=*/4, lp));
  26  }
  27  
  28  static void RpcMempool(benchmark::Bench& bench)
  29  {
  30      const auto testing_setup = MakeNoLogFileContext<const ChainTestingSetup>(ChainType::MAIN);
  31      CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
  32      LOCK2(cs_main, pool.cs);
  33  
  34      for (int i = 0; i < 1000; ++i) {
  35          CMutableTransaction tx = CMutableTransaction();
  36          tx.vin.resize(1);
  37          tx.vin[0].scriptSig = CScript() << OP_1;
  38          tx.vin[0].scriptWitness.stack.push_back({1});
  39          tx.vout.resize(1);
  40          tx.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
  41          tx.vout[0].nValue = i;
  42          const CTransactionRef tx_r{MakeTransactionRef(tx)};
  43          AddTx(tx_r, /*fee=*/i, pool);
  44      }
  45  
  46      bench.run([&] {
  47          (void)MempoolToJSON(pool, /*verbose=*/true);
  48      });
  49  }
  50  
  51  BENCHMARK(RpcMempool);
  52