-
Notifications
You must be signed in to change notification settings - Fork 173
/
txqr_test.go
250 lines (232 loc) · 5.37 KB
/
txqr_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
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
243
244
245
246
247
248
249
250
package txqr
import (
"fmt"
"math/rand"
"strings"
"testing"
"time"
)
func TestTXQR(t *testing.T) {
var tests = []struct {
length int
chunkSz int
}{
{10 * 1024, 100},
{10 * 1024, 200},
{10 * 1024, 300},
{10 * 1024, 400},
{10 * 1024, 500},
{10 * 1024, 650},
{10 * 1024, 800},
{10 * 1024, 900},
{10 * 1024, 1000},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%d, %d", test.length, test.chunkSz), func(t *testing.T) {
str := newTestData(test.length)
enc := NewEncoder(test.chunkSz)
chunks, err := enc.Encode(str)
if err != nil {
t.Fatalf("Encode failed: %v", err)
}
dec := NewDecoder()
for !dec.IsCompleted() {
for _, chunk := range chunks {
err = dec.Decode(chunk)
if err != nil {
t.Fatalf("Decode failed: %v", err)
}
}
}
got := dec.Data()
if got != str {
t.Fatalf("Expected '%s', but got '%s'", str, got)
}
})
}
}
// TestTXQRErasures tests information decoding over erasure channels
// with different erasure probabilities.
func TestTXQRErasures(t *testing.T) {
var tests = []struct {
length int
chunkSz int
fps int
}{
{10 * 1024, 100, 3},
{10 * 1024, 300, 3},
{10 * 1024, 500, 3},
{10 * 1024, 650, 3},
{10 * 1024, 800, 3},
{10 * 1024, 800, 3},
{10 * 1024, 100, 6},
{10 * 1024, 300, 6},
{10 * 1024, 500, 6},
{10 * 1024, 800, 6},
{10 * 1024, 800, 6},
{10 * 1024, 100, 9},
{10 * 1024, 300, 9},
{10 * 1024, 500, 9},
{10 * 1024, 800, 9},
{10 * 1024, 800, 9},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%d, %d", test.length, test.chunkSz), func(t *testing.T) {
str := newTestData(test.length)
enc := NewEncoder(test.chunkSz)
chunks, err := enc.Encode(str)
if err != nil {
t.Fatalf("Encode failed: %v", err)
}
dec := NewDecoder()
now := time.Now()
for !dec.IsCompleted() {
// erase new set of chunks every time
toDel := chunksCountToDelete(len(chunks))
transmitted := eraseChunks(chunks, toDel)
for _, chunk := range transmitted {
err = dec.Decode(chunk)
if err != nil {
t.Fatalf("Decode failed: %v", err)
}
if dec.IsCompleted() {
break
}
}
time.Sleep(1 * time.Second / time.Duration(test.fps))
}
duration := time.Since(now)
t.Logf("[%db/%d, %dfps] took: %v", test.length, test.chunkSz, test.fps, duration)
got := dec.Data()
if got != str {
t.Fatalf("Expected '%s', but got '%s'", str, got)
}
})
}
}
// eraseChunks randomly erases n chunks from the input slice.
func eraseChunks(chunks []string, n int) []string {
toErase := make(map[int]bool)
for i := 0; i < n; i++ {
idx := rand.Intn(len(chunks))
toErase[idx] = true
}
ret := make([]string, 0, len(chunks)-n)
for idx, chunk := range chunks {
if toErase[idx] {
continue
}
ret = append(ret, chunk)
}
return ret
}
// chunksCountToDelete returns random number of chunks for deletion,
// using normal distribution with 2 std deviation and N/3 as a mean.
func chunksCountToDelete(n int) int {
mean := float64(n / 2)
dev := 2.0
del := int(rand.NormFloat64()*dev + mean)
if del < 0 {
del = 0
}
if del > n {
del = n
}
return del
}
func BenchmarkTXQREncode(b *testing.B) {
var tests = []struct {
length int
chunkSz int
}{
{100, 10},
{1000, 10},
{1000, 100},
{10 * 1024, 100},
{10 * 1024, 1000},
}
for _, test := range tests {
b.Run(fmt.Sprintf("%d, %d", test.length, test.chunkSz), func(b *testing.B) {
str := strings.Repeat("hello, world!", test.length)
enc := NewEncoder(test.chunkSz)
for i := 0; i < b.N; i++ {
_, _ = enc.Encode(str)
}
})
}
}
func BenchmarkTXQRDecode(b *testing.B) {
var tests = []struct {
length int
chunkSz int
}{
{100, 10},
{1000, 10},
{1000, 100},
{10 * 1024, 100},
{10 * 1024, 1000},
}
for _, test := range tests {
b.Run(fmt.Sprintf("%d, %d", test.length, test.chunkSz), func(b *testing.B) {
str := strings.Repeat("hello, world!", test.length)
enc := NewEncoder(test.chunkSz)
chunks, err := enc.Encode(str)
if err != nil {
b.Fatalf("Encode failed: %v", err)
}
dec := NewDecoder()
for i := 0; i < b.N; i++ {
for _, chunk := range chunks {
err = dec.Decode(chunk)
if err != nil {
b.Fatalf("Decode failed: %v", err)
}
}
}
})
}
}
// BenchmarkTXQRErasures benchmarks information decoding over erasure channels
// with different erasure probabilities.
func BenchmarkTXQRErasures(b *testing.B) {
var tests = []struct {
length int
chunkSz int
}{
{10 * 1024, 10},
{10 * 1024, 100},
{10 * 1024, 1000},
}
for _, test := range tests {
b.Run(fmt.Sprintf("%d, %d", test.length, test.chunkSz), func(b *testing.B) {
str := strings.Repeat("hello, world!", test.length)
enc := NewEncoder(test.chunkSz)
chunks, err := enc.Encode(str)
if err != nil {
b.Fatalf("Encode failed: %v", err)
}
for i := 0; i < b.N; i++ {
dec := NewDecoder()
for !dec.IsCompleted() {
// erase new set of chunks every time
toDel := chunksCountToDelete(len(chunks))
transmitted := eraseChunks(chunks, toDel)
for _, chunk := range transmitted {
err = dec.Decode(chunk)
if err != nil {
b.Fatalf("Decode failed: %v", err)
}
}
}
}
})
}
}
func newTestData(size int) string {
data := make([]byte, size)
_, err := rand.Read(data)
if err != nil {
panic(fmt.Sprintf("Can't generate rand data: %v", err))
}
return string(data)
}