1
1
package mempool
2
2
3
3
import (
4
+ "fmt"
5
+ "testing"
6
+
4
7
"encoding/binary"
5
8
"sync/atomic"
6
- "testing"
7
9
8
10
"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"
9
15
"github.com/cometbft/cometbft/proxy"
16
+ "github.com/stretchr/testify/assert"
17
+ "github.com/stretchr/testify/require"
10
18
)
11
19
12
20
func BenchmarkReap (b * testing.B ) {
@@ -15,11 +23,11 @@ func BenchmarkReap(b *testing.B) {
15
23
mp , cleanup := newMempoolWithApp (cc )
16
24
defer cleanup ()
17
25
18
- mp .config .Size = 100000
26
+ mp .config .Size = 100_000_000 // so that the nmempool never saturates
19
27
20
28
size := 10000
21
29
for i := 0 ; i < size ; i ++ {
22
- tx := make ([] byte , 8 )
30
+ tx := kvstore . NewTxFromID ( i )
23
31
binary .BigEndian .PutUint64 (tx , uint64 (i ))
24
32
if err := mp .CheckTx (tx , nil , TxInfo {}); err != nil {
25
33
b .Fatal (err )
@@ -37,14 +45,12 @@ func BenchmarkCheckTx(b *testing.B) {
37
45
mp , cleanup := newMempoolWithApp (cc )
38
46
defer cleanup ()
39
47
40
- mp .config .Size = 1000000
48
+ mp .config .Size = 100_000_000
41
49
42
50
b .ResetTimer ()
43
-
44
51
for i := 0 ; i < b .N ; i ++ {
45
52
b .StopTimer ()
46
- tx := make ([]byte , 8 )
47
- binary .BigEndian .PutUint64 (tx , uint64 (i ))
53
+ tx := kvstore .NewTxFromID (i )
48
54
b .StartTimer ()
49
55
50
56
if err := mp .CheckTx (tx , nil , TxInfo {}); err != nil {
@@ -59,18 +65,17 @@ func BenchmarkParallelCheckTx(b *testing.B) {
59
65
mp , cleanup := newMempoolWithApp (cc )
60
66
defer cleanup ()
61
67
62
- mp .config .Size = 100000000
68
+ mp .config .Size = 100_000_000
63
69
64
70
var txcnt uint64
65
71
next := func () uint64 {
66
- return atomic .AddUint64 (& txcnt , 1 ) - 1
72
+ return atomic .AddUint64 (& txcnt , 1 )
67
73
}
68
74
69
75
b .ResetTimer ()
70
76
b .RunParallel (func (pb * testing.PB ) {
71
77
for pb .Next () {
72
- tx := make ([]byte , 8 )
73
- binary .BigEndian .PutUint64 (tx , next ())
78
+ tx := kvstore .NewTxFromID (int (next ()))
74
79
if err := mp .CheckTx (tx , nil , TxInfo {}); err != nil {
75
80
b .Fatal (err )
76
81
}
@@ -84,17 +89,60 @@ func BenchmarkCheckDuplicateTx(b *testing.B) {
84
89
mp , cleanup := newMempoolWithApp (cc )
85
90
defer cleanup ()
86
91
87
- mp .config .Size = 1000000
92
+ mp .config .Size = 2
88
93
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 )
95
100
101
+ b .ResetTimer ()
102
+ for i := 0 ; i < b .N ; i ++ {
96
103
if err := mp .CheckTx (tx , nil , TxInfo {}); err == nil {
97
104
b .Fatal ("tx should be duplicate" )
98
105
}
99
106
}
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
+
100
148
}
0 commit comments