Skip to content

Commit 5b5a76e

Browse files
laanwjMunkybooty
authored andcommitted
Merge bitcoin#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. (lucash.dev@gmail.com) 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 bitcoin#13050. Also see bitcoin#10026. Tree-SHA512: bde1a856b5f076a5845e14d1a924855c8c91742c3139b47903081289b21d01fef6f2d1fd8947058728a57de56f877bab3866af8cd1d25ba2daa44411752cdb2f
1 parent fbd2f3d commit 5b5a76e

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

src/wallet/test/coinselector_tests.cpp

+36-25
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,19 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
463463
BOOST_CHECK(testWallet.SelectCoinsMinConf(MIN_CHANGE * 9990 / 100, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
464464
BOOST_CHECK_EQUAL(nValueRet, 101 * MIN_CHANGE);
465465
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
466+
}
466467

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);
468+
// test with many inputs
469+
for (CAmount amt=1500; amt < COIN; amt*=10) {
470+
empty_wallet();
471+
// Create 676 inputs (= (old MAX_STANDARD_TX_SIZE == 100000) / 148 bytes per input)
472+
for (uint16_t j = 0; j < 676; j++)
473+
add_coin(amt);
474+
475+
// We only create the wallet once to save time, but we still run the coin selection RUN_TESTS times.
476+
for (int i = 0; i < RUN_TESTS; i++) {
473477
BOOST_CHECK(testWallet.SelectCoinsMinConf(2000, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
478+
474479
if (amt - 2000 < MIN_CHANGE) {
475480
// needs more than one input:
476481
uint16_t returnSize = std::ceil((2000.0 + MIN_CHANGE)/amt);
@@ -482,14 +487,17 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
482487
BOOST_CHECK_EQUAL(nValueRet, amt);
483488
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
484489
}
485-
}
490+
}
491+
}
486492

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

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

0 commit comments

Comments
 (0)