github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/mempool/bench_test.go (about)

     1  package mempool
     2  
     3  import (
     4  	"encoding/binary"
     5  	"testing"
     6  
     7  	"github.com/line/ostracon/abci/example/kvstore"
     8  	"github.com/line/ostracon/proxy"
     9  )
    10  
    11  func BenchmarkReap(b *testing.B) {
    12  	app := kvstore.NewApplication()
    13  	cc := proxy.NewLocalClientCreator(app)
    14  	mempool, cleanup := newMempoolWithApp(cc)
    15  	defer cleanup()
    16  
    17  	size := 10000
    18  	for i := 0; i < size; i++ {
    19  		tx := make([]byte, 8)
    20  		binary.BigEndian.PutUint64(tx, uint64(i))
    21  		mempool.CheckTxSync(tx, TxInfo{}) // nolint: errcheck
    22  	}
    23  	b.ResetTimer()
    24  	for i := 0; i < b.N; i++ {
    25  		mempool.ReapMaxBytesMaxGas(100000000, 10000000)
    26  	}
    27  }
    28  
    29  func BenchmarkReapWithCheckTxAsync(b *testing.B) {
    30  	app := kvstore.NewApplication()
    31  	cc := proxy.NewLocalClientCreator(app)
    32  	mempool, cleanup := newMempoolWithApp(cc)
    33  	defer cleanup()
    34  
    35  	size := 10000
    36  	for i := 0; i < size; i++ {
    37  		tx := make([]byte, 8)
    38  		binary.BigEndian.PutUint64(tx, uint64(i))
    39  		mempool.CheckTxAsync(tx, TxInfo{}, nil, nil)
    40  	}
    41  	b.ResetTimer()
    42  	for i := 0; i < b.N; i++ {
    43  		mempool.ReapMaxBytesMaxGas(100000000, 10000000)
    44  	}
    45  }
    46  
    47  func BenchmarkCheckTxSync(b *testing.B) {
    48  	app := kvstore.NewApplication()
    49  	cc := proxy.NewLocalClientCreator(app)
    50  	mempool, cleanup := newMempoolWithApp(cc)
    51  	defer cleanup()
    52  
    53  	for i := 0; i < b.N; i++ {
    54  		tx := make([]byte, 8)
    55  		binary.BigEndian.PutUint64(tx, uint64(i))
    56  		mempool.CheckTxSync(tx, TxInfo{}) // nolint: errcheck
    57  	}
    58  }
    59  
    60  func BenchmarkCheckTxAsync(b *testing.B) {
    61  	app := kvstore.NewApplication()
    62  	cc := proxy.NewLocalClientCreator(app)
    63  	mempool, cleanup := newMempoolWithApp(cc)
    64  	defer cleanup()
    65  
    66  	for i := 0; i < b.N; i++ {
    67  		tx := make([]byte, 8)
    68  		binary.BigEndian.PutUint64(tx, uint64(i))
    69  		mempool.CheckTxAsync(tx, TxInfo{}, nil, nil)
    70  	}
    71  }
    72  
    73  func BenchmarkCacheInsertTime(b *testing.B) {
    74  	cache := newMapTxCache(b.N)
    75  	txs := make([][]byte, b.N)
    76  	for i := 0; i < b.N; i++ {
    77  		txs[i] = make([]byte, 8)
    78  		binary.BigEndian.PutUint64(txs[i], uint64(i))
    79  	}
    80  	b.ResetTimer()
    81  	for i := 0; i < b.N; i++ {
    82  		cache.Push(txs[i])
    83  	}
    84  }
    85  
    86  // This benchmark is probably skewed, since we actually will be removing
    87  // txs in parallel, which may cause some overhead due to mutex locking.
    88  func BenchmarkCacheRemoveTime(b *testing.B) {
    89  	cache := newMapTxCache(b.N)
    90  	txs := make([][]byte, b.N)
    91  	for i := 0; i < b.N; i++ {
    92  		txs[i] = make([]byte, 8)
    93  		binary.BigEndian.PutUint64(txs[i], uint64(i))
    94  		cache.Push(txs[i])
    95  	}
    96  	b.ResetTimer()
    97  	for i := 0; i < b.N; i++ {
    98  		cache.Remove(txs[i])
    99  	}
   100  }