core/types, crypto, triedb/pathdb: add microbenchmarks

This commit is contained in:
Weixie Cui 2026-04-11 02:48:47 +08:00
parent cc1078dda6
commit 7c44d34feb
3 changed files with 235 additions and 0 deletions

View file

@ -0,0 +1,100 @@
// Copyright 2024 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package types
import (
"math/big"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
)
func BenchmarkBlobTxEffectiveGasPrice(b *testing.B) {
gasTipCap := uint256.NewInt(2000000000) // 2 gwei
gasFeeCap := uint256.NewInt(3000000000) // 3 gwei
baseFee := big.NewInt(1000000000) // 1 gwei
tx := &BlobTx{
ChainID: uint256.NewInt(1),
Nonce: 0,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Gas: 21000,
To: common.Address{},
Value: uint256.NewInt(0),
Data: nil,
}
b.Run("WithBaseFee", func(b *testing.B) {
b.ReportAllocs()
dst := new(big.Int)
for b.Loop() {
tx.effectiveGasPrice(dst, baseFee)
}
})
}
func BenchmarkDynamicFeeTxEffectiveGasPrice(b *testing.B) {
gasTipCap := big.NewInt(2000000000) // 2 gwei
gasFeeCap := big.NewInt(3000000000) // 3 gwei
baseFee := big.NewInt(1000000000) // 1 gwei
tx := &DynamicFeeTx{
ChainID: big.NewInt(1),
Nonce: 0,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Gas: 21000,
To: &common.Address{},
Value: big.NewInt(0),
Data: nil,
}
b.Run("WithBaseFee", func(b *testing.B) {
b.ReportAllocs()
dst := new(big.Int)
for b.Loop() {
tx.effectiveGasPrice(dst, baseFee)
}
})
}
func BenchmarkSetCodeTxEffectiveGasPrice(b *testing.B) {
gasTipCap := uint256.NewInt(2000000000) // 2 gwei
gasFeeCap := uint256.NewInt(3000000000) // 3 gwei
baseFee := big.NewInt(1000000000) // 1 gwei
tx := &SetCodeTx{
ChainID: uint256.NewInt(1),
Nonce: 0,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Gas: 21000,
To: common.Address{},
Value: uint256.NewInt(0),
Data: nil,
}
b.Run("WithBaseFee", func(b *testing.B) {
b.ReportAllocs()
dst := new(big.Int)
for b.Loop() {
tx.effectiveGasPrice(dst, baseFee)
}
})
}

View file

@ -0,0 +1,71 @@
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package crypto
import (
"crypto/rand"
"testing"
"github.com/ethereum/go-ethereum/common"
)
var (
benchAddr = common.HexToAddress("0x970e8128ab834e8eac17ab8e3812f010678cf791")
benchSalt = [32]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}
benchInitHash = Keccak256([]byte("test init code hash"))
)
// BenchmarkCreateAddress2 benchmarks CreateAddress2 with fixed inputs
func BenchmarkCreateAddress2(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = CreateAddress2(benchAddr, benchSalt, benchInitHash)
}
}
// BenchmarkCreateAddress2_ZeroSalt benchmarks CreateAddress2 with zero salt
func BenchmarkCreateAddress2_ZeroSalt(b *testing.B) {
var zeroSalt [32]byte
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = CreateAddress2(benchAddr, zeroSalt, benchInitHash)
}
}
// BenchmarkCreateAddress2_RandomSalt benchmarks CreateAddress2 with random salt each iteration
func BenchmarkCreateAddress2_RandomSalt(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
var salt [32]byte
rand.Read(salt[:])
_ = CreateAddress2(benchAddr, salt, benchInitHash)
}
}
// BenchmarkCreateAddress2_RandomInputs benchmarks CreateAddress2 with all random inputs
func BenchmarkCreateAddress2_RandomInputs(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
var addr common.Address
var salt [32]byte
rand.Read(addr[:])
rand.Read(salt[:])
initHash := make([]byte, 32)
rand.Read(initHash)
_ = CreateAddress2(addr, salt, initHash)
}
}

View file

@ -0,0 +1,64 @@
// Copyright 2024 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package pathdb
import (
"crypto/rand"
"testing"
"github.com/ethereum/go-ethereum/common"
)
// BenchmarkStateSetAccount benchmarks the account lookup performance.
func BenchmarkStateSetAccount(b *testing.B) {
// Create a stateSet with a reasonable number of accounts
const numAccounts = 1000
accountData := make(map[common.Hash][]byte, numAccounts)
// Generate random account hashes and data
hashes := make([]common.Hash, numAccounts)
for i := 0; i < numAccounts; i++ {
var hash common.Hash
rand.Read(hash[:])
hashes[i] = hash
accountData[hash] = make([]byte, 32)
rand.Read(accountData[hash])
}
s := newStates(accountData, nil, false)
// Prepare test hashes: mix of existing and non-existing
testHashes := make([]common.Hash, 100)
for i := 0; i < 50; i++ {
// Use existing hashes
testHashes[i] = hashes[i*10]
}
for i := 50; i < 100; i++ {
// Use non-existing hashes
var hash common.Hash
rand.Read(hash[:])
testHashes[i] = hash
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
hash := testHashes[i%len(testHashes)]
_, _ = s.account(hash)
}
}