Skip to content

Commit 3f9e5c8

Browse files
moawnallahyyforyongyu
authored andcommitted
wallet: benchmark DifferentAddressTypes
1 parent bbb6cb5 commit 3f9e5c8

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

wallet/signer_benchmark_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,3 +1359,102 @@ func BenchmarkComputeUnlockingScriptWithTweaker(b *testing.B) {
13591359
})
13601360
}
13611361
}
1362+
1363+
// BenchmarkDifferentAddressTypes compares signing performance across
1364+
// different address types (P2PKH, P2WKH, P2TR).
1365+
func BenchmarkDifferentAddressTypes(b *testing.B) {
1366+
const (
1367+
numAccounts = 5
1368+
numAddresses = 10
1369+
)
1370+
1371+
testCases := []struct {
1372+
name string
1373+
scope waddrmgr.KeyScope
1374+
hashType txscript.SigHashType
1375+
}{
1376+
{
1377+
name: "P2PKH-Legacy",
1378+
scope: waddrmgr.KeyScopeBIP0044,
1379+
hashType: txscript.SigHashAll,
1380+
},
1381+
{
1382+
name: "P2WKH-SegWit",
1383+
scope: waddrmgr.KeyScopeBIP0084,
1384+
hashType: txscript.SigHashAll,
1385+
},
1386+
{
1387+
name: "P2TR-Taproot",
1388+
scope: waddrmgr.KeyScopeBIP0086,
1389+
hashType: txscript.SigHashDefault,
1390+
},
1391+
}
1392+
1393+
for _, tc := range testCases {
1394+
b.Run(tc.name, func(b *testing.B) {
1395+
scopes := []waddrmgr.KeyScope{tc.scope}
1396+
bw := setupBenchmarkWallet(
1397+
b, benchmarkWalletConfig{
1398+
scopes: scopes,
1399+
numAccounts: numAccounts,
1400+
numAddresses: numAddresses,
1401+
numWalletTxs: 0,
1402+
},
1403+
)
1404+
1405+
// Get a test address for this scope.
1406+
testAddr, err := bw.CurrentAddress(0, tc.scope)
1407+
if err != nil {
1408+
// Fallback to getting any address.
1409+
testAddr = getTestAddress(
1410+
b, bw.Wallet, numAccounts,
1411+
)
1412+
}
1413+
1414+
pkScript, err := txscript.PayToAddrScript(testAddr)
1415+
require.NoError(b, err)
1416+
1417+
prevOut := &wire.TxOut{
1418+
Value: 100000,
1419+
PkScript: pkScript,
1420+
}
1421+
1422+
// Create a spending transaction.
1423+
tx := wire.NewMsgTx(2)
1424+
tx.AddTxIn(&wire.TxIn{
1425+
PreviousOutPoint: wire.OutPoint{
1426+
Hash: chainhash.Hash{},
1427+
Index: 0,
1428+
},
1429+
})
1430+
tx.AddTxOut(&wire.TxOut{
1431+
Value: 50000,
1432+
PkScript: pkScript,
1433+
})
1434+
1435+
fetcher := txscript.NewCannedPrevOutputFetcher(
1436+
prevOut.PkScript, prevOut.Value,
1437+
)
1438+
sigHashes := txscript.NewTxSigHashes(tx, fetcher)
1439+
1440+
params := &UnlockingScriptParams{
1441+
Tx: tx,
1442+
InputIndex: 0,
1443+
Output: prevOut,
1444+
SigHashes: sigHashes,
1445+
HashType: tc.hashType,
1446+
}
1447+
1448+
b.ReportAllocs()
1449+
b.ResetTimer()
1450+
1451+
for b.Loop() {
1452+
unlockScript, err := bw.ComputeUnlockingScript(
1453+
b.Context(), params,
1454+
)
1455+
require.NoError(b, err)
1456+
require.NotNil(b, unlockScript)
1457+
}
1458+
})
1459+
}
1460+
}

0 commit comments

Comments
 (0)