From 7c44d34feb7a49b3e815d3c39bde2148cfafce07 Mon Sep 17 00:00:00 2001 From: Weixie Cui Date: Sat, 11 Apr 2026 02:48:47 +0800 Subject: [PATCH] core/types, crypto, triedb/pathdb: add microbenchmarks --- .../tx_effective_gas_price_bench_test.go | 100 ++++++++++++++++++ crypto/createaddress2_bench_test.go | 71 +++++++++++++ triedb/pathdb/states_bench_test.go | 64 +++++++++++ 3 files changed, 235 insertions(+) create mode 100644 core/types/tx_effective_gas_price_bench_test.go create mode 100644 crypto/createaddress2_bench_test.go create mode 100644 triedb/pathdb/states_bench_test.go diff --git a/core/types/tx_effective_gas_price_bench_test.go b/core/types/tx_effective_gas_price_bench_test.go new file mode 100644 index 0000000000..2f5c22ba45 --- /dev/null +++ b/core/types/tx_effective_gas_price_bench_test.go @@ -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 . + +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) + } + }) +} diff --git a/crypto/createaddress2_bench_test.go b/crypto/createaddress2_bench_test.go new file mode 100644 index 0000000000..3631286afc --- /dev/null +++ b/crypto/createaddress2_bench_test.go @@ -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 . + +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) + } +} diff --git a/triedb/pathdb/states_bench_test.go b/triedb/pathdb/states_bench_test.go new file mode 100644 index 0000000000..8c523264a5 --- /dev/null +++ b/triedb/pathdb/states_bench_test.go @@ -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 . + +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) + } +}