forked from celestiaorg/rsmt2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_test.go
84 lines (76 loc) · 1.56 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
package rsmt2d
import (
"fmt"
"math/rand"
"testing"
)
var (
encodedDataDump [][]byte
decodedDataDump [][]byte
)
func BenchmarkEncoding(b *testing.B) {
// generate some fake data
data := generateRandData(128)
for codecName, codec := range codecs {
b.Run(
fmt.Sprintf("Encoding 128 shares using %s", codecName),
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) [][]byte {
out := make([][]byte, count)
for i := 0; i < count; i++ {
randData := make([]byte, count)
_, err := rand.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 {
data := generateMissingData(128, codec)
b.Run(
fmt.Sprintf("Decoding 128 shares using %s", codecName),
func(b *testing.B) {
for n := 0; n < b.N; n++ {
encodedData, err := codec.Decode(data)
if err != nil {
b.Error(err)
}
encodedDataDump = encodedData
}
},
)
}
}
func generateMissingData(count int, codec Codec) [][]byte {
randData := generateRandData(count)
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
}