-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_test.go
119 lines (103 loc) · 2.57 KB
/
codec_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
package rsmt2d
import (
cryptorand "crypto/rand"
"fmt"
"math/rand"
"testing"
)
var (
encodedDataDump [][]byte
decodedDataDump [][]byte
)
func BenchmarkEncoding(b *testing.B) {
// generate some fake data
data := generateRandData(128, shareSize)
for codecName, codec := range codecs {
// For some implementations we want to ensure the encoder for this data length
// is already cached and initialized. For this run with same sized arbitrary data.
_, _ = codec.Encode(generateRandData(128, shareSize))
b.Run(
fmt.Sprintf("%s 128 shares %d", codecName, shareSize),
func(b *testing.B) {
for n := 0; n < b.N; n++ {
encodedData, err := codec.Encode(data)
if err != nil {
b.Error(err)
}
encodedDataDump = encodedData
}
},
)
}
}
func generateRandData(count int, shareSize int) [][]byte {
out := make([][]byte, count)
for i := 0; i < count; i++ {
randData := make([]byte, shareSize)
_, err := cryptorand.Read(randData)
if err != nil {
panic(err)
}
out[i] = randData
}
return out
}
func BenchmarkDecoding(b *testing.B) {
// generate some fake data
for codecName, codec := range codecs {
// For some implementations we want to ensure the encoder for this data length
// is already cached and initialized. For this run with same sized arbitrary data.
_, _ = codec.Decode(generateMissingData(128, shareSize, codec))
data := generateMissingData(128, shareSize, codec)
b.Run(
fmt.Sprintf("%s 128 shares %d", codecName, shareSize),
func(b *testing.B) {
for n := 0; n < b.N; n++ {
decodedData, err := codec.Decode(data)
if err != nil {
b.Error(err)
}
decodedDataDump = decodedData
}
},
)
}
}
func generateMissingData(count int, shareSize int, codec Codec) [][]byte {
randData := generateRandData(count, shareSize)
encoded, err := codec.Encode(randData)
if err != nil {
panic(err)
}
output := append(randData, encoded...)
// remove half of the shares randomly
for i := 0; i < (count / 2); {
ind := rand.Intn(count)
if len(output[ind]) == 0 {
continue
}
output[ind] = []byte{}
i++
}
return output
}
// testCodec is a codec that is used for testing purposes.
type testCodec struct{}
func newTestCodec() Codec {
return &testCodec{}
}
func (c *testCodec) Encode(share [][]byte) ([][]byte, error) {
return share, nil
}
func (c *testCodec) Decode(share [][]byte) ([][]byte, error) {
return share, nil
}
func (c *testCodec) MaxChunks() int {
return 0
}
func (c *testCodec) Name() string {
return "testCodec"
}
func (c *testCodec) ValidateChunkSize(_ int) error {
return nil
}