Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c0a5715

Browse files
otracklasarojc
andauthoredJun 19, 2023
mempool: Fix the benchmarks (celestiaorg#934)
* fix mempool benchmarks * add a benchmark for `mempool:update` --------- Co-authored-by: Lasaro <[email protected]>
1 parent f695d99 commit c0a5715

File tree

5 files changed

+91
-22
lines changed

5 files changed

+91
-22
lines changed
 

‎mempool/bench_test.go

+66-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package mempool
22

33
import (
4+
"fmt"
5+
"testing"
6+
47
"encoding/binary"
58
"sync/atomic"
6-
"testing"
79

810
"github.com/cometbft/cometbft/abci/example/kvstore"
11+
abciserver "github.com/cometbft/cometbft/abci/server"
12+
"github.com/cometbft/cometbft/internal/test"
13+
"github.com/cometbft/cometbft/libs/log"
14+
cmtrand "github.com/cometbft/cometbft/libs/rand"
915
"github.com/cometbft/cometbft/proxy"
16+
"github.com/stretchr/testify/assert"
17+
"github.com/stretchr/testify/require"
1018
)
1119

1220
func BenchmarkReap(b *testing.B) {
@@ -15,11 +23,11 @@ func BenchmarkReap(b *testing.B) {
1523
mp, cleanup := newMempoolWithApp(cc)
1624
defer cleanup()
1725

18-
mp.config.Size = 100000
26+
mp.config.Size = 100_000_000 // so that the nmempool never saturates
1927

2028
size := 10000
2129
for i := 0; i < size; i++ {
22-
tx := make([]byte, 8)
30+
tx := kvstore.NewTxFromID(i)
2331
binary.BigEndian.PutUint64(tx, uint64(i))
2432
if err := mp.CheckTx(tx, nil, TxInfo{}); err != nil {
2533
b.Fatal(err)
@@ -37,14 +45,12 @@ func BenchmarkCheckTx(b *testing.B) {
3745
mp, cleanup := newMempoolWithApp(cc)
3846
defer cleanup()
3947

40-
mp.config.Size = 1000000
48+
mp.config.Size = 100_000_000
4149

4250
b.ResetTimer()
43-
4451
for i := 0; i < b.N; i++ {
4552
b.StopTimer()
46-
tx := make([]byte, 8)
47-
binary.BigEndian.PutUint64(tx, uint64(i))
53+
tx := kvstore.NewTxFromID(i)
4854
b.StartTimer()
4955

5056
if err := mp.CheckTx(tx, nil, TxInfo{}); err != nil {
@@ -59,18 +65,17 @@ func BenchmarkParallelCheckTx(b *testing.B) {
5965
mp, cleanup := newMempoolWithApp(cc)
6066
defer cleanup()
6167

62-
mp.config.Size = 100000000
68+
mp.config.Size = 100_000_000
6369

6470
var txcnt uint64
6571
next := func() uint64 {
66-
return atomic.AddUint64(&txcnt, 1) - 1
72+
return atomic.AddUint64(&txcnt, 1)
6773
}
6874

6975
b.ResetTimer()
7076
b.RunParallel(func(pb *testing.PB) {
7177
for pb.Next() {
72-
tx := make([]byte, 8)
73-
binary.BigEndian.PutUint64(tx, next())
78+
tx := kvstore.NewTxFromID(int(next()))
7479
if err := mp.CheckTx(tx, nil, TxInfo{}); err != nil {
7580
b.Fatal(err)
7681
}
@@ -84,17 +89,60 @@ func BenchmarkCheckDuplicateTx(b *testing.B) {
8489
mp, cleanup := newMempoolWithApp(cc)
8590
defer cleanup()
8691

87-
mp.config.Size = 1000000
92+
mp.config.Size = 2
8893

89-
for i := 0; i < b.N; i++ {
90-
tx := make([]byte, 8)
91-
binary.BigEndian.PutUint64(tx, uint64(i))
92-
if err := mp.CheckTx(tx, nil, TxInfo{}); err != nil {
93-
b.Fatal(err)
94-
}
94+
tx := kvstore.NewTxFromID(1)
95+
if err := mp.CheckTx(tx, nil, TxInfo{}); err != nil {
96+
b.Fatal(err)
97+
}
98+
e := mp.FlushAppConn()
99+
require.True(b, e == nil)
95100

101+
b.ResetTimer()
102+
for i := 0; i < b.N; i++ {
96103
if err := mp.CheckTx(tx, nil, TxInfo{}); err == nil {
97104
b.Fatal("tx should be duplicate")
98105
}
99106
}
107+
108+
}
109+
110+
func BenchmarkUpdateRemoteClient(b *testing.B) {
111+
sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", cmtrand.Str(6))
112+
app := kvstore.NewInMemoryApplication()
113+
114+
// Start server
115+
server := abciserver.NewSocketServer(sockPath, app)
116+
server.SetLogger(log.TestingLogger().With("module", "abci-server"))
117+
if err := server.Start(); err != nil {
118+
b.Fatalf("Error starting socket server: %v", err.Error())
119+
}
120+
121+
b.Cleanup(func() {
122+
if err := server.Stop(); err != nil {
123+
b.Error(err)
124+
}
125+
})
126+
cfg := test.ResetTestRoot("mempool_test")
127+
mp, cleanup := newMempoolWithAppAndConfig(proxy.NewRemoteClientCreator(sockPath, "socket", true), cfg)
128+
defer cleanup()
129+
130+
b.ResetTimer()
131+
for i := 1; i <= b.N; i++ {
132+
133+
tx := kvstore.NewTxFromID(i)
134+
135+
e := mp.CheckTx(tx, nil, TxInfo{})
136+
require.True(b, e == nil)
137+
138+
e = mp.FlushAppConn()
139+
require.True(b, e == nil)
140+
141+
require.True(b, mp.Size() == 1)
142+
143+
var txs = mp.ReapMaxTxs(mp.Size())
144+
doCommit(b, mp, app, txs, int64(i))
145+
assert.True(b, true)
146+
}
147+
100148
}

‎mempool/cache_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package mempool
33
import (
44
"crypto/rand"
55
"crypto/sha256"
6-
"fmt"
76
"testing"
87

8+
"fmt"
9+
910
"github.com/cometbft/cometbft/abci/example/kvstore"
1011
abci "github.com/cometbft/cometbft/abci/types"
1112
"github.com/cometbft/cometbft/proxy"

‎mempool/clist_mempool_test.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package mempool
33
import (
44
"context"
55
"encoding/binary"
6-
"fmt"
76
mrand "math/rand"
87
"os"
98
"testing"
109
"time"
1110

11+
"fmt"
12+
1213
"github.com/cosmos/gogoproto/proto"
1314
gogotypes "github.com/cosmos/gogoproto/types"
1415
"github.com/stretchr/testify/assert"
@@ -741,3 +742,20 @@ func abciResponses(n int, code uint32) []*abci.ExecTxResult {
741742
}
742743
return responses
743744
}
745+
746+
func doCommit(t require.TestingT, mp Mempool, app abci.Application, txs types.Txs, height int64) {
747+
rfb := &abci.RequestFinalizeBlock{Txs: make([][]byte, len(txs))}
748+
for i, tx := range txs {
749+
rfb.Txs[i] = tx
750+
}
751+
_, e := app.FinalizeBlock(context.Background(), rfb)
752+
require.True(t, e == nil)
753+
mp.Lock()
754+
e = mp.FlushAppConn()
755+
require.True(t, e == nil)
756+
_, e = app.Commit(context.Background(), &abci.RequestCommit{})
757+
require.True(t, e == nil)
758+
e = mp.Update(height, txs, abciResponses(txs.Len(), abci.CodeTypeOK), nil, nil)
759+
require.True(t, e == nil)
760+
mp.Unlock()
761+
}

‎mempool/mempool.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package mempool
33
import (
44
"crypto/sha256"
55
"errors"
6-
"fmt"
76
"math"
87

8+
"fmt"
9+
910
abci "github.com/cometbft/cometbft/abci/types"
1011
"github.com/cometbft/cometbft/types"
1112
)

‎mempool/reactor.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package mempool
22

33
import (
44
"errors"
5-
"fmt"
65
"time"
76

7+
"fmt"
8+
89
cfg "github.com/cometbft/cometbft/config"
910
"github.com/cometbft/cometbft/libs/clist"
1011
"github.com/cometbft/cometbft/libs/log"

0 commit comments

Comments
 (0)
Please sign in to comment.