github.com/aakash4dev/cometbft@v0.38.2/mempool/bench_test.go (about) 1 package mempool 2 3 import ( 4 "fmt" 5 "testing" 6 7 "sync/atomic" 8 9 "github.com/aakash4dev/cometbft/abci/example/kvstore" 10 abciserver "github.com/aakash4dev/cometbft/abci/server" 11 "github.com/aakash4dev/cometbft/internal/test" 12 "github.com/aakash4dev/cometbft/libs/log" 13 cmtrand "github.com/aakash4dev/cometbft/libs/rand" 14 "github.com/aakash4dev/cometbft/proxy" 15 "github.com/stretchr/testify/assert" 16 "github.com/stretchr/testify/require" 17 ) 18 19 func BenchmarkReap(b *testing.B) { 20 app := kvstore.NewInMemoryApplication() 21 cc := proxy.NewLocalClientCreator(app) 22 mp, cleanup := newMempoolWithApp(cc) 23 defer cleanup() 24 25 mp.config.Size = 100_000_000 // so that the nmempool never saturates 26 27 size := 10000 28 for i := 0; i < size; i++ { 29 tx := kvstore.NewTxFromID(i) 30 if _, err := mp.CheckTx(tx); err != nil { 31 b.Fatal(err) 32 } 33 } 34 b.ResetTimer() 35 for i := 0; i < b.N; i++ { 36 mp.ReapMaxBytesMaxGas(100000000, 10000000) 37 } 38 } 39 40 func BenchmarkCheckTx(b *testing.B) { 41 app := kvstore.NewInMemoryApplication() 42 cc := proxy.NewLocalClientCreator(app) 43 mp, cleanup := newMempoolWithApp(cc) 44 defer cleanup() 45 46 mp.config.Size = 100_000_000 47 48 b.ResetTimer() 49 for i := 0; i < b.N; i++ { 50 b.StopTimer() 51 tx := kvstore.NewTxFromID(i) 52 b.StartTimer() 53 54 if _, err := mp.CheckTx(tx); err != nil { 55 b.Fatal(err) 56 } 57 } 58 } 59 60 func BenchmarkParallelCheckTx(b *testing.B) { 61 app := kvstore.NewInMemoryApplication() 62 cc := proxy.NewLocalClientCreator(app) 63 mp, cleanup := newMempoolWithApp(cc) 64 defer cleanup() 65 66 mp.config.Size = 100_000_000 67 68 var txcnt uint64 69 next := func() uint64 { 70 return atomic.AddUint64(&txcnt, 1) 71 } 72 73 b.ResetTimer() 74 b.RunParallel(func(pb *testing.PB) { 75 for pb.Next() { 76 tx := kvstore.NewTxFromID(int(next())) 77 if _, err := mp.CheckTx(tx); err != nil { 78 b.Fatal(err) 79 } 80 } 81 }) 82 } 83 84 func BenchmarkCheckDuplicateTx(b *testing.B) { 85 app := kvstore.NewInMemoryApplication() 86 cc := proxy.NewLocalClientCreator(app) 87 mp, cleanup := newMempoolWithApp(cc) 88 defer cleanup() 89 90 mp.config.Size = 2 91 92 tx := kvstore.NewTxFromID(1) 93 if _, err := mp.CheckTx(tx); err != nil { 94 b.Fatal(err) 95 } 96 e := mp.FlushAppConn() 97 require.True(b, e == nil) 98 99 b.ResetTimer() 100 for i := 0; i < b.N; i++ { 101 if _, err := mp.CheckTx(tx); err == nil { 102 b.Fatal("tx should be duplicate") 103 } 104 } 105 106 } 107 108 func BenchmarkUpdateRemoteClient(b *testing.B) { 109 sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", cmtrand.Str(6)) 110 app := kvstore.NewInMemoryApplication() 111 112 // Start server 113 server := abciserver.NewSocketServer(sockPath, app) 114 server.SetLogger(log.TestingLogger().With("module", "abci-server")) 115 if err := server.Start(); err != nil { 116 b.Fatalf("Error starting socket server: %v", err.Error()) 117 } 118 119 b.Cleanup(func() { 120 if err := server.Stop(); err != nil { 121 b.Error(err) 122 } 123 }) 124 cfg := test.ResetTestRoot("mempool_test") 125 mp, cleanup := newMempoolWithAppAndConfig(proxy.NewRemoteClientCreator(sockPath, "socket", true), cfg) 126 defer cleanup() 127 128 b.ResetTimer() 129 for i := 1; i <= b.N; i++ { 130 131 tx := kvstore.NewTxFromID(i) 132 133 _, e := mp.CheckTx(tx) 134 require.True(b, e == nil) 135 136 e = mp.FlushAppConn() 137 require.True(b, e == nil) 138 139 require.True(b, mp.Size() == 1) 140 141 var txs = mp.ReapMaxTxs(mp.Size()) 142 doCommit(b, mp, app, txs, int64(i)) 143 assert.True(b, true) 144 } 145 146 }