forked from wormhole-foundation/wormhole
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
242 lines (211 loc) · 8.45 KB
/
Copy pathbenchmark_test.go
File metadata and controls
242 lines (211 loc) · 8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package processor
import (
"context"
"fmt"
"os"
"runtime/pprof"
"testing"
"time"
"github.com/certusone/wormhole/node/pkg/common"
"github.com/certusone/wormhole/node/pkg/db"
"github.com/certusone/wormhole/node/pkg/guardiansigner"
"github.com/certusone/wormhole/node/pkg/gwrelayer"
gossipv1 "github.com/certusone/wormhole/node/pkg/proto/gossip/v1"
// gossipv1 "github.com/certusone/wormhole/node/pkg/proto/gossip/v1"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
"github.com/wormhole-foundation/wormhole/sdk/vaa"
"go.uber.org/zap"
)
/*
No gwrelayer:
average time to do 1800000 observations: 51.246µs
there were 1100000 under quorum, taking an average time of 62.888µs
there were 100000 quorum reached, taking an average time of 68.049µs
there were 600000 over quorum, taking an average time of 27.101µs
there were 100000 handle message calls, taking an average time of 28.723µs
With gwrelayer (where observations are not relayed):
average time to do 1800000 observations: 51.18µs
there were 1100000 under quorum, taking an average time of 62.713µs
there were 100000 quorum reached, taking an average time of 68.316µs
there were 600000 over quorum, taking an average time of 27.182µs
there were 100000 handle message calls, taking an average time of 28.704µs
*/
// go test -bench ^BenchmarkHandleObservation -benchtime=1x
func BenchmarkHandleObservation(b *testing.B) {
const NumObservations = 100000
ctx := context.Background()
db := db.OpenDb(nil, nil)
defer db.Close()
p, pd := createProcessorForTest(b, NumObservations, ctx, db, false)
require.NotNil(b, p)
require.NotNil(b, pd)
var totalTime, underQuorumTime, quorumReachedTime, overQuorumTime, handleMsgTime time.Duration
var totalCount, underQuorumCount, quorumReachedCount, overQuorumCount int
for count := 0; count < NumObservations; count++ {
k := pd.createMessagePublication(b, uint64(count))
start := time.Now()
p.handleMessage(k)
handleMsgTime += time.Since(start)
for guardianIdx := 1; guardianIdx < 19; guardianIdx++ {
start := time.Now()
p.handleSingleObservation(pd.guardianAddrs[guardianIdx], pd.createObservation(b, guardianIdx, k))
duration := time.Since(start)
totalCount++
totalTime += duration
if guardianIdx < 12 {
underQuorumCount++
underQuorumTime += duration
} else if guardianIdx == 12 {
quorumReachedCount++
quorumReachedTime += duration
} else {
overQuorumCount++
overQuorumTime += duration
}
}
}
require.Equal(b, NumObservations, len(pd.gossipVaaSendC))
// This won't work once batching is enabled.
// require.Equal(b, NumObservations, len(pd.gossipAttestationSendC))
fmt.Println("average time to do ", totalCount, " observations: ", totalTime/time.Duration(totalCount))
fmt.Println("there were ", underQuorumCount, " under quorum, taking an average time of ", underQuorumTime/time.Duration(underQuorumCount))
fmt.Println("there were ", quorumReachedCount, " quorum reached, taking an average time of ", quorumReachedTime/time.Duration(quorumReachedCount))
fmt.Println("there were ", overQuorumCount, " over quorum, taking an average time of ", overQuorumTime/time.Duration(overQuorumCount))
fmt.Println("there were ", NumObservations, " handle message calls, taking an average time of ", handleMsgTime/time.Duration(NumObservations))
}
// go test -bench ^BenchmarkProfileHandleObservation -benchtime=1x
// To view profiling results:
// go install github.com/google/pprof@latest
// sudo apt install graphviz
// pprof -http=:8080 handleObs.prof
func BenchmarkProfileHandleObservation(b *testing.B) {
// return
const NumObservations = 100000
f, err := os.Create("handleObs.prof")
require.NoError(b, err)
err = pprof.StartCPUProfile(f)
require.NoError(b, err)
defer pprof.StopCPUProfile()
ctx := context.Background()
db := db.OpenDb(nil, nil)
defer db.Close()
p, pd := createProcessorForTest(b, NumObservations, ctx, db, false)
require.NotNil(b, p)
require.NotNil(b, pd)
for count := 0; count < NumObservations; count++ {
k := pd.createMessagePublication(b, uint64(count))
p.handleMessage(k)
for guardianIdx := 1; guardianIdx < 19; guardianIdx++ {
p.handleSingleObservation(pd.guardianAddrs[guardianIdx], pd.createObservation(b, guardianIdx, k))
}
}
require.Equal(b, NumObservations, len(pd.gossipVaaSendC))
}
type ProcessorData struct {
gossipAttestationSendC chan []byte
gossipVaaSendC chan []byte
emitterChain vaa.ChainID
emitterAddress vaa.Address
guardianSigners []guardiansigner.GuardianSigner
guardianAddrs [][]byte
}
func (pd *ProcessorData) messageID(seqNum uint64) string {
return fmt.Sprintf("%d/%s/%d", pd.emitterChain, pd.emitterAddress, seqNum)
}
// createProcessorForTest creates a processor for benchmarking. It assumes we are index zero in the guardian set.
func createProcessorForTest(b *testing.B, numVAAs int, ctx context.Context, db *db.Database, useBatching bool) (*Processor, *ProcessorData) {
b.Helper()
logger := zap.NewNop()
var ourSigner guardiansigner.GuardianSigner
keys := []ethCommon.Address{}
guardianSigners := []guardiansigner.GuardianSigner{}
guardianAddrs := [][]byte{}
for count := 0; count < 19; count++ {
guardianSigner, err := guardiansigner.GenerateSignerWithPrivatekeyUnsafe(nil)
require.NoError(b, err)
keys = append(keys, crypto.PubkeyToAddress(guardianSigner.PublicKey()))
guardianSigners = append(guardianSigners, guardianSigner)
guardianAddrs = append(guardianAddrs, crypto.PubkeyToAddress(guardianSigner.PublicKey()).Bytes())
if count == 0 {
ourSigner = guardianSigner
}
}
gs := common.NewGuardianSet(keys, 0)
gst := common.NewGuardianSetState(nil)
gst.Set(gs)
emitterAddress, err := vaa.StringToAddress("0x3ee18B2214AFF97000D974cf647E7C347E8fa585")
require.NoError(b, err)
gwRelayer := gwrelayer.NewGatewayRelayer(ctx, logger, "wormhole14ejqjyq8um4p3xfqj74yld5waqljf88fz25yxnma0cngspxe3les00fpj", nil, common.MainNet)
require.NoError(b, gwRelayer.Start(ctx))
pd := &ProcessorData{
gossipAttestationSendC: make(chan []byte, numVAAs+100),
gossipVaaSendC: make(chan []byte, numVAAs+100),
emitterChain: vaa.ChainIDEthereum,
emitterAddress: emitterAddress,
guardianSigners: guardianSigners,
guardianAddrs: guardianAddrs,
}
p := &Processor{
gossipAttestationSendC: pd.gossipAttestationSendC,
gossipVaaSendC: pd.gossipVaaSendC,
guardianSigner: ourSigner,
gst: gst,
db: db,
logger: logger,
state: &aggregationState{signatures: observationMap{}},
ourAddr: crypto.PubkeyToAddress(ourSigner.PublicKey()),
pythnetVaas: make(map[string]PythNetVaaEntry),
updatedVAAs: make(map[string]*updateVaaEntry),
gatewayRelayer: gwRelayer,
}
batchCutoverCompleteFlag.Store(useBatching)
go func() { _ = p.vaaWriter(ctx) }()
go func() { _ = p.batchProcessor(ctx) }()
return p, pd
}
func (pd *ProcessorData) createMessagePublication(b *testing.B, sequence uint64) *common.MessagePublication {
b.Helper()
return &common.MessagePublication{
TxHash: ethCommon.HexToHash(fmt.Sprintf("%064x", sequence)),
Timestamp: time.Now(),
Nonce: 42,
Sequence: sequence,
EmitterChain: pd.emitterChain,
EmitterAddress: pd.emitterAddress,
Payload: []byte{0x01, 0x02, 0x03, 0x04},
ConsistencyLevel: 32,
}
}
func (pd *ProcessorData) createObservation(b *testing.B, guardianIdx int, k *common.MessagePublication) *gossipv1.Observation {
b.Helper()
v := &VAA{
VAA: vaa.VAA{
Version: vaa.SupportedVAAVersion,
GuardianSetIndex: uint32(guardianIdx),
Signatures: nil,
Timestamp: k.Timestamp,
Nonce: k.Nonce,
EmitterChain: k.EmitterChain,
EmitterAddress: k.EmitterAddress,
Payload: k.Payload,
Sequence: k.Sequence,
ConsistencyLevel: k.ConsistencyLevel,
},
Unreliable: k.Unreliable,
Reobservation: k.IsReobservation,
}
// Generate digest of the unsigned VAA.
digest := v.SigningDigest()
// Sign the digest using our node's guardian signer
guardianSigner := pd.guardianSigners[guardianIdx]
signature, err := guardianSigner.Sign(digest.Bytes())
require.NoError(b, err)
return &gossipv1.Observation{
Hash: digest.Bytes(),
Signature: signature,
TxHash: k.TxHash.Bytes(),
MessageId: pd.messageID(k.Sequence),
}
}