forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_test.go
103 lines (88 loc) · 2.51 KB
/
bench_test.go
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
package codec_test
import (
"testing"
"github.com/stretchr/testify/require"
protov2 "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/dynamicpb"
counterv1 "cosmossdk.io/api/cosmos/counter/v1"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
countertypes "github.com/cosmos/cosmos-sdk/testutil/x/counter/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type msgCounterWrapper struct {
*countertypes.MsgIncreaseCounter
}
func (msg msgCounterWrapper) GetSigners() []sdk.AccAddress {
fromAddress, _ := sdk.AccAddressFromBech32(msg.Signer)
return []sdk.AccAddress{fromAddress}
}
func BenchmarkLegacyGetSigners(b *testing.B) {
_, _, addr := testdata.KeyTestPubAddr()
msg := msgCounterWrapper{&countertypes.MsgIncreaseCounter{
Signer: addr.String(),
Count: 2,
}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = msg.GetSigners()
}
}
func BenchmarkProtoreflectGetSigners(b *testing.B) {
cdc := codectestutil.CodecOptions{}.NewCodec()
signingCtx := cdc.InterfaceRegistry().SigningContext()
_, _, addr := testdata.KeyTestPubAddr()
// use a pulsar message
msg := &counterv1.MsgIncreaseCounter{
Signer: addr.String(),
Count: 1,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := signingCtx.GetSigners(msg)
if err != nil {
panic(err)
}
}
}
func BenchmarkProtoreflectGetSignersWithUnmarshal(b *testing.B) {
cdc := codectestutil.CodecOptions{}.NewCodec()
_, _, addr := testdata.KeyTestPubAddr()
// start with a protoreflect message
msg := &countertypes.MsgIncreaseCounter{
Signer: addr.String(),
Count: 1,
}
// marshal to an any first because this is what we get from the wire
a, err := codectypes.NewAnyWithValue(msg)
require.NoError(b, err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, err := cdc.GetMsgAnySigners(a)
if err != nil {
panic(err)
}
}
}
func BenchmarkProtoreflectGetSignersDynamicpb(b *testing.B) {
cdc := codectestutil.CodecOptions{}.NewCodec()
signingCtx := cdc.InterfaceRegistry().SigningContext()
_, _, addr := testdata.KeyTestPubAddr()
msg := &counterv1.MsgIncreaseCounter{
Signer: addr.String(),
Count: 1,
}
bz, err := protov2.Marshal(msg)
require.NoError(b, err)
dynamicmsg := dynamicpb.NewMessage(msg.ProtoReflect().Descriptor())
err = protov2.Unmarshal(bz, dynamicmsg)
require.NoError(b, err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := signingCtx.GetSigners(dynamicmsg)
if err != nil {
panic(err)
}
}
}