-
Notifications
You must be signed in to change notification settings - Fork 7
/
msg_encoding_test.go
166 lines (145 loc) · 4.07 KB
/
msg_encoding_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
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
package f3
import (
"math/rand"
"testing"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-f3/gpbft"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"
)
const seed = 1413
func BenchmarkCborEncoding(b *testing.B) {
rng := rand.New(rand.NewSource(seed))
encoder := &cborGMessageEncoding{}
msg := generateRandomGMessage(b, rng)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := encoder.Encode(msg); err != nil {
require.NoError(b, err)
}
}
})
}
func BenchmarkCborDecoding(b *testing.B) {
rng := rand.New(rand.NewSource(seed))
encoder := &cborGMessageEncoding{}
msg := generateRandomGMessage(b, rng)
data, err := encoder.Encode(msg)
require.NoError(b, err)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if got, err := encoder.Decode(data); err != nil {
require.NoError(b, err)
require.Equal(b, msg, got)
}
}
})
}
func BenchmarkZstdEncoding(b *testing.B) {
rng := rand.New(rand.NewSource(seed))
encoder, err := newZstdGMessageEncoding()
require.NoError(b, err)
msg := generateRandomGMessage(b, rng)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := encoder.Encode(msg); err != nil {
require.NoError(b, err)
}
}
})
}
func BenchmarkZstdDecoding(b *testing.B) {
rng := rand.New(rand.NewSource(seed))
encoder, err := newZstdGMessageEncoding()
require.NoError(b, err)
msg := generateRandomGMessage(b, rng)
data, err := encoder.Encode(msg)
require.NoError(b, err)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if got, err := encoder.Decode(data); err != nil {
require.NoError(b, err)
require.Equal(b, msg, got)
}
}
})
}
func generateRandomGMessage(b *testing.B, rng *rand.Rand) *gpbft.GMessage {
var maybeTicket []byte
if rng.Float64() < 0.5 {
generateRandomBytes(b, rng, 96)
}
return &gpbft.GMessage{
Sender: gpbft.ActorID(rng.Uint64()),
Vote: generateRandomPayload(b, rng),
Signature: generateRandomBytes(b, rng, 96),
Ticket: maybeTicket,
Justification: generateRandomJustification(b, rng),
}
}
func generateRandomJustification(b *testing.B, rng *rand.Rand) *gpbft.Justification {
return &gpbft.Justification{
Vote: generateRandomPayload(b, rng),
Signers: generateRandomBitfield(rng),
Signature: generateRandomBytes(b, rng, 96),
}
}
func generateRandomBytes(b *testing.B, rng *rand.Rand, n int) []byte {
buf := make([]byte, n)
_, err := rng.Read(buf)
require.NoError(b, err)
return buf
}
func generateRandomPayload(b *testing.B, rng *rand.Rand) gpbft.Payload {
return gpbft.Payload{
Instance: rng.Uint64(),
Round: rng.Uint64(),
Phase: gpbft.Phase(rng.Intn(int(gpbft.COMMIT_PHASE)) + 1),
Value: generateRandomECChain(b, rng, rng.Intn(gpbft.ChainMaxLen)+1),
SupplementalData: gpbft.SupplementalData{
PowerTable: generateRandomCID(b, rng),
},
}
}
func generateRandomBitfield(rng *rand.Rand) bitfield.BitField {
ids := make([]uint64, rng.Intn(2_000)+1)
for i := range ids {
ids[i] = rng.Uint64()
}
return bitfield.NewFromSet(ids)
}
func generateRandomECChain(b *testing.B, rng *rand.Rand, length int) gpbft.ECChain {
chain := make(gpbft.ECChain, length)
epoch := int64(rng.Uint64())
for i := range length {
chain[i] = generateRandomTipSet(b, rng, epoch+int64(i))
}
return chain
}
func generateRandomTipSet(b *testing.B, rng *rand.Rand, epoch int64) gpbft.TipSet {
return gpbft.TipSet{
Epoch: epoch,
Key: generateRandomTipSetKey(b, rng),
PowerTable: generateRandomCID(b, rng),
}
}
func generateRandomTipSetKey(b *testing.B, rng *rand.Rand) gpbft.TipSetKey {
key := make([]byte, rng.Intn(gpbft.TipsetKeyMaxLen)+1)
_, err := rng.Read(key)
require.NoError(b, err)
return key
}
func generateRandomCID(b *testing.B, rng *rand.Rand) cid.Cid {
sum, err := multihash.Sum(generateRandomBytes(b, rng, 32), multihash.SHA2_256, -1)
require.NoError(b, err)
return cid.NewCidV1(cid.Raw, sum)
}