github.com/bcnmy/go-ethereum@v1.10.27/core/types/types_test.go (about)

     1  // Copyright 2021 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package types
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/crypto"
    25  	"github.com/ethereum/go-ethereum/rlp"
    26  )
    27  
    28  type devnull struct{ len int }
    29  
    30  func (d *devnull) Write(p []byte) (int, error) {
    31  	d.len += len(p)
    32  	return len(p), nil
    33  }
    34  
    35  func BenchmarkEncodeRLP(b *testing.B) {
    36  	benchRLP(b, true)
    37  }
    38  
    39  func BenchmarkDecodeRLP(b *testing.B) {
    40  	benchRLP(b, false)
    41  }
    42  
    43  func benchRLP(b *testing.B, encode bool) {
    44  	key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    45  	to := common.HexToAddress("0x00000000000000000000000000000000deadbeef")
    46  	signer := NewLondonSigner(big.NewInt(1337))
    47  	for _, tc := range []struct {
    48  		name string
    49  		obj  interface{}
    50  	}{
    51  		{
    52  			"legacy-header",
    53  			&Header{
    54  				Difficulty: big.NewInt(10000000000),
    55  				Number:     big.NewInt(1000),
    56  				GasLimit:   8_000_000,
    57  				GasUsed:    8_000_000,
    58  				Time:       555,
    59  				Extra:      make([]byte, 32),
    60  			},
    61  		},
    62  		{
    63  			"london-header",
    64  			&Header{
    65  				Difficulty: big.NewInt(10000000000),
    66  				Number:     big.NewInt(1000),
    67  				GasLimit:   8_000_000,
    68  				GasUsed:    8_000_000,
    69  				Time:       555,
    70  				Extra:      make([]byte, 32),
    71  				BaseFee:    big.NewInt(10000000000),
    72  			},
    73  		},
    74  		{
    75  			"receipt-for-storage",
    76  			&ReceiptForStorage{
    77  				Status:            ReceiptStatusSuccessful,
    78  				CumulativeGasUsed: 0x888888888,
    79  				Logs:              make([]*Log, 0),
    80  			},
    81  		},
    82  		{
    83  			"receipt-full",
    84  			&Receipt{
    85  				Status:            ReceiptStatusSuccessful,
    86  				CumulativeGasUsed: 0x888888888,
    87  				Logs:              make([]*Log, 0),
    88  			},
    89  		},
    90  		{
    91  			"legacy-transaction",
    92  			MustSignNewTx(key, signer,
    93  				&LegacyTx{
    94  					Nonce:    1,
    95  					GasPrice: big.NewInt(500),
    96  					Gas:      1000000,
    97  					To:       &to,
    98  					Value:    big.NewInt(1),
    99  				}),
   100  		},
   101  		{
   102  			"access-transaction",
   103  			MustSignNewTx(key, signer,
   104  				&AccessListTx{
   105  					Nonce:    1,
   106  					GasPrice: big.NewInt(500),
   107  					Gas:      1000000,
   108  					To:       &to,
   109  					Value:    big.NewInt(1),
   110  				}),
   111  		},
   112  		{
   113  			"1559-transaction",
   114  			MustSignNewTx(key, signer,
   115  				&DynamicFeeTx{
   116  					Nonce:     1,
   117  					Gas:       1000000,
   118  					To:        &to,
   119  					Value:     big.NewInt(1),
   120  					GasTipCap: big.NewInt(500),
   121  					GasFeeCap: big.NewInt(500),
   122  				}),
   123  		},
   124  	} {
   125  		if encode {
   126  			b.Run(tc.name, func(b *testing.B) {
   127  				b.ReportAllocs()
   128  				var null = &devnull{}
   129  				for i := 0; i < b.N; i++ {
   130  					rlp.Encode(null, tc.obj)
   131  				}
   132  				b.SetBytes(int64(null.len / b.N))
   133  			})
   134  		} else {
   135  			data, _ := rlp.EncodeToBytes(tc.obj)
   136  			// Test decoding
   137  			b.Run(tc.name, func(b *testing.B) {
   138  				b.ReportAllocs()
   139  				for i := 0; i < b.N; i++ {
   140  					if err := rlp.DecodeBytes(data, tc.obj); err != nil {
   141  						b.Fatal(err)
   142  					}
   143  				}
   144  				b.SetBytes(int64(len(data)))
   145  			})
   146  		}
   147  	}
   148  }