Skip to content

Commit e3fec3c

Browse files
committed
Merge #13419: [tests] Speed up knapsack_solver_test by not recreating wallet 100 times.
a679109 Speed up knapsack_solver_test by not recreating wallet 100 times. ([email protected]) Pull request description: Optimization of `knapsack_solver_test`by moving an expensive wallet creation to outside a 100x for loop. On my (slow) machine: ``` before: 9.8s after: 6.2s -------------------- saved: 3.6s (36%) ``` This PR was split from #13050. Also see #10026. Tree-SHA512: bde1a856b5f076a5845e14d1a924855c8c91742c3139b47903081289b21d01fef6f2d1fd8947058728a57de56f877bab3866af8cd1d25ba2daa44411752cdb2f
2 parents bcffd87 + a679109 commit e3fec3c

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

src/wallet/test/coinselector_tests.cpp

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -462,14 +462,19 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
462462
BOOST_CHECK(testWallet.SelectCoinsMinConf(MIN_CHANGE * 9990 / 100, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
463463
BOOST_CHECK_EQUAL(nValueRet, 101 * MIN_CHANGE);
464464
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
465+
}
465466

466-
// test with many inputs
467-
for (CAmount amt=1500; amt < COIN; amt*=10) {
468-
empty_wallet();
469-
// Create 676 inputs (= (old MAX_STANDARD_TX_SIZE == 100000) / 148 bytes per input)
470-
for (uint16_t j = 0; j < 676; j++)
471-
add_coin(amt);
467+
// test with many inputs
468+
for (CAmount amt=1500; amt < COIN; amt*=10) {
469+
empty_wallet();
470+
// Create 676 inputs (= (old MAX_STANDARD_TX_SIZE == 100000) / 148 bytes per input)
471+
for (uint16_t j = 0; j < 676; j++)
472+
add_coin(amt);
473+
474+
// We only create the wallet once to save time, but we still run the coin selection RUN_TESTS times.
475+
for (int i = 0; i < RUN_TESTS; i++) {
472476
BOOST_CHECK(testWallet.SelectCoinsMinConf(2000, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
477+
473478
if (amt - 2000 < MIN_CHANGE) {
474479
// needs more than one input:
475480
uint16_t returnSize = std::ceil((2000.0 + MIN_CHANGE)/amt);
@@ -481,14 +486,17 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
481486
BOOST_CHECK_EQUAL(nValueRet, amt);
482487
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
483488
}
484-
}
489+
}
490+
}
485491

486-
// test randomness
487-
{
488-
empty_wallet();
489-
for (int i2 = 0; i2 < 100; i2++)
490-
add_coin(COIN);
492+
// test randomness
493+
{
494+
empty_wallet();
495+
for (int i2 = 0; i2 < 100; i2++)
496+
add_coin(COIN);
491497

498+
// Again, we only create the wallet once to save time, but we still run the coin selection RUN_TESTS times.
499+
for (int i = 0; i < RUN_TESTS; i++) {
492500
// picking 50 from 100 coins doesn't depend on the shuffle,
493501
// but does depend on randomness in the stochastic approximation code
494502
BOOST_CHECK(testWallet.SelectCoinsMinConf(50 * COIN, filter_standard, GroupCoins(vCoins), setCoinsRet , nValueRet, coin_selection_params, bnb_used));
@@ -506,17 +514,19 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
506514
fails++;
507515
}
508516
BOOST_CHECK_NE(fails, RANDOM_REPEATS);
509-
510-
// add 75 cents in small change. not enough to make 90 cents,
511-
// then try making 90 cents. there are multiple competing "smallest bigger" coins,
512-
// one of which should be picked at random
513-
add_coin(5 * CENT);
514-
add_coin(10 * CENT);
515-
add_coin(15 * CENT);
516-
add_coin(20 * CENT);
517-
add_coin(25 * CENT);
518-
519-
fails = 0;
517+
}
518+
519+
// add 75 cents in small change. not enough to make 90 cents,
520+
// then try making 90 cents. there are multiple competing "smallest bigger" coins,
521+
// one of which should be picked at random
522+
add_coin(5 * CENT);
523+
add_coin(10 * CENT);
524+
add_coin(15 * CENT);
525+
add_coin(20 * CENT);
526+
add_coin(25 * CENT);
527+
528+
for (int i = 0; i < RUN_TESTS; i++) {
529+
int fails = 0;
520530
for (int j = 0; j < RANDOM_REPEATS; j++)
521531
{
522532
// selecting 1 from 100 identical coins depends on the shuffle; this test will fail 1% of the time
@@ -527,8 +537,9 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
527537
fails++;
528538
}
529539
BOOST_CHECK_NE(fails, RANDOM_REPEATS);
530-
}
531-
}
540+
}
541+
}
542+
532543
empty_wallet();
533544
}
534545

0 commit comments

Comments
 (0)