Debian Codegraph @

reedsolomon_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /**
2 * Unit tests for ReedSolomon
3 *
4 * Copyright 2015, Klaus Post
5 * Copyright 2015, Backblaze, Inc. All rights reserved.
6 */
7
8 package reedsolomon
9
10 import (
11 "bytes"
12 "errors"
13 "flag"
14 "fmt"
15 "math/rand"
16 "os"
17 "runtime"
18 "strconv"
19 "testing"
20 "time"
21 )
22
23 var noSSE2 = flag.Bool("no-sse2", !defaultOptions.useSSE2, "Disable SSE2")
24 var noSSSE3 = flag.Bool("no-ssse3", !defaultOptions.useSSSE3, "Disable SSSE3")
25 var noAVX2 = flag.Bool("no-avx2", !defaultOptions.useAVX2, "Disable AVX2")
26 var noAVX512 = flag.Bool("no-avx512", !defaultOptions.useAVX512, "Disable AVX512")
27 var noGNFI = flag.Bool("no-gfni", !defaultOptions.useAvx512GFNI, "Disable AVX512+GFNI")
28 var noAVX2GNFI = flag.Bool("no-avx-gfni", !defaultOptions.useAvxGNFI, "Disable AVX+GFNI")
29
30 func TestMain(m *testing.M) {
31 flag.Parse()
32 rs, _ := New(10, 3, testOptions()...)
33 if rs != nil {
34 if rst, ok := rs.(*reedSolomon); ok {
35 fmt.Println("Using", rst.o.cpuOptions())
36 }
37 }
38 os.Exit(m.Run())
39 }
40
41 func testOptions(o ...Option) []Option {
42 o = append(o, WithFastOneParityMatrix())
43 if *noSSSE3 {
44 o = append(o, WithSSSE3(false))
45 }
46 if *noSSE2 {
47 o = append(o, WithSSE2(false))
48 }
49 if *noAVX2 {
50 o = append(o, WithAVX2(false))
51 }
52 if *noAVX512 {
53 o = append(o, WithAVX512(false))
54 }
55 if *noGNFI {
56 o = append(o, WithGFNI(false))
57 }
58 if *noAVX2GNFI {
59 o = append(o, WithAVXGFNI(false))
60 }
61 return o
62 }
63
64 func isIncreasingAndContainsDataRow(indices []int) bool {
65 cols := len(indices)
66 for i := 0; i < cols-1; i++ {
67 if indices[i] >= indices[i+1] {
68 return false
69 }
70 }
71 // Data rows are in the upper square portion of the matrix.
72 return indices[0] < cols
73 }
74
75 func incrementIndices(indices []int, indexBound int) (valid bool) {
76 for i := len(indices) - 1; i >= 0; i-- {
77 indices[i]++
78 if indices[i] < indexBound {
79 break
80 }
81
82 if i == 0 {
83 return false
84 }
85
86 indices[i] = 0
87 }
88
89 return true
90 }
91
92 func incrementIndicesUntilIncreasingAndContainsDataRow(
93 indices []int, maxIndex int) bool {
94 for {
95 valid := incrementIndices(indices, maxIndex)
96 if !valid {
97 return false
98 }
99
100 if isIncreasingAndContainsDataRow(indices) {
101 return true
102 }
103 }
104 }
105
106 func findSingularSubMatrix(m matrix) (matrix, error) {
107 rows := len(m)
108 cols := len(m[0])
109 rowIndices := make([]int, cols)
110 for incrementIndicesUntilIncreasingAndContainsDataRow(rowIndices, rows) {
111 subMatrix, _ := newMatrix(cols, cols)
112 for i, r := range rowIndices {
113 for c := range cols {
114 subMatrix[i][c] = m[r][c]
115 }
116 }
117
118 _, err := subMatrix.Invert()
119 if err == errSingular {
120 return subMatrix, nil
121 } else if err != nil {
122 return nil, err
123 }
124 }
125
126 return nil, nil
127 }
128
129 func TestBuildMatrixJerasure(t *testing.T) {
130 totalShards := 12
131 dataShards := 8
132 m, err := buildMatrixJerasure(dataShards, totalShards)
133 if err != nil {
134 t.Fatal(err)
135 }
136 refMatrix := matrix{
137 {1, 1, 1, 1, 1, 1, 1, 1},
138 {1, 55, 39, 73, 84, 181, 225, 217},
139 {1, 39, 217, 161, 92, 60, 172, 90},
140 {1, 172, 70, 235, 143, 34, 200, 101},
141 }
142 for i := range 8 {
143 for j := range 8 {
144 if i != j && m[i][j] != 0 || i == j && m[i][j] != 1 {
145 t.Fatal("Top part of the matrix is not identity")
146 }
147 }
148 }
149 for i := range 4 {
150 for j := range 8 {
151 if m[8+i][j] != refMatrix[i][j] {
152 t.Fatal("Coding matrix for EC 8+4 differs from Jerasure")
153 }
154 }
155 }
156 }
157
158 func TestBuildMatrixPAR1Singular(t *testing.T) {
159 totalShards := 8
160 dataShards := 4
161 m, err := buildMatrixPAR1(dataShards, totalShards)
162 if err != nil {
163 t.Fatal(err)
164 }
165
166 singularSubMatrix, err := findSingularSubMatrix(m)
167 if err != nil {
168 t.Fatal(err)
169 }
170
171 if singularSubMatrix == nil {
172 t.Fatal("No singular sub-matrix found")
173 }
174
175 t.Logf("matrix %s has singular sub-matrix %s", m, singularSubMatrix)
176 }
177
178 func testOpts() [][]Option {
179 if testing.Short() {
180 return [][]Option{
181 {WithCauchyMatrix()}, {WithLeopardGF16(true)}, {WithLeopardGF(true)},
182 }
183 }
184 opts := [][]Option{
185 {WithPAR1Matrix()}, {WithCauchyMatrix()},
186 {WithFastOneParityMatrix()}, {WithPAR1Matrix(), WithFastOneParityMatrix()}, {WithCauchyMatrix(), WithFastOneParityMatrix()},
187 {WithMaxGoroutines(1), WithMinSplitSize(500), WithSSSE3(false), WithAVX2(false), WithAVX512(false)},
188 {WithMaxGoroutines(5000), WithMinSplitSize(50), WithSSSE3(false), WithAVX2(false), WithAVX512(false)},
189 {WithMaxGoroutines(5000), WithMinSplitSize(500000), WithSSSE3(false), WithAVX2(false), WithAVX512(false)},
190 {WithMaxGoroutines(1), WithMinSplitSize(500000), WithSSSE3(false), WithAVX2(false), WithAVX512(false)},
191 {WithAutoGoroutines(50000), WithMinSplitSize(500)},
192 {WithInversionCache(false)},
193 {WithJerasureMatrix()},
194 {WithLeopardGF16(true)},
195 {WithLeopardGF(true)},
196 }
197
198 for _, o := range opts[:] {
199 if defaultOptions.useSSSE3 {
200 n := make([]Option, len(o), len(o)+1)
201 copy(n, o)
202 n = append(n, WithSSSE3(true))
203 opts = append(opts, n)
204 }
205 if defaultOptions.useAVX2 {
206 n := make([]Option, len(o), len(o)+1)
207 copy(n, o)
208 n = append(n, WithAVX2(true))
209 opts = append(opts, n)
210 }
211 if defaultOptions.useAVX512 {
212 n := make([]Option, len(o), len(o)+1)
213 copy(n, o)
214 n = append(n, WithAVX512(true))
215 opts = append(opts, n)
216 }
217 if defaultOptions.useAvx512GFNI {
218 n := make([]Option, len(o), len(o)+1)
219 copy(n, o)
220 n = append(n, WithGFNI(false))
221 opts = append(opts, n)
222 }
223 }
224 return opts
225 }
226
227 func parallelIfNotShort(t *testing.T) {
228 if !testing.Short() {
229 t.Parallel()
230 }
231 }
232
233 func TestEncoding(t *testing.T) {
234 t.Run("default", func(t *testing.T) {
235 parallelIfNotShort(t)
236 testEncoding(t, testOptions()...)
237 })
238 t.Run("default-idx", func(t *testing.T) {
239 parallelIfNotShort(t)
240 testEncodingIdx(t, testOptions()...)
241 })
242 if testing.Short() {
243 return
244 }
245 // Spread somewhat, but don't overload...
246 to := testOpts()
247 to2 := to[len(to)/2:]
248 to = to[:len(to)/2]
249 t.Run("reg", func(t *testing.T) {
250 parallelIfNotShort(t)
251 for i, o := range to {
252 t.Run(fmt.Sprintf("opt-%d", i), func(t *testing.T) {
253 testEncoding(t, o...)
254 })
255 }
256 })
257 t.Run("reg2", func(t *testing.T) {
258 parallelIfNotShort(t)
259 for i, o := range to2 {
260 t.Run(fmt.Sprintf("opt-%d", i), func(t *testing.T) {
261 testEncoding(t, o...)
262 })
263 }
264 })
265 if !testing.Short() {
266 t.Run("idx", func(t *testing.T) {
267 parallelIfNotShort(t)
268 for i, o := range to {
269 t.Run(fmt.Sprintf("idx-opt-%d", i), func(t *testing.T) {
270 testEncodingIdx(t, o...)
271 })
272 }
273 })
274 t.Run("idx2", func(t *testing.T) {
275 parallelIfNotShort(t)
276 for i, o := range to2 {
277 t.Run(fmt.Sprintf("idx-opt-%d", i), func(t *testing.T) {
278 testEncodingIdx(t, o...)
279 })
280 }
281 })
282
283 }
284 }
285
286 // matrix sizes to test.
287 // note that par1 matrix will fail on some combinations.
288 func testSizes() [][2]int {
289 if testing.Short() {
290 return [][2]int{
291 {3, 0},
292 {1, 1}, {1, 2}, {8, 4}, {10, 30}, {41, 17},
293 {256, 20}, {500, 300},
294 }
295 }
296 return [][2]int{
297 {1, 0}, {10, 0}, {12, 0}, {49, 0},
298 {1, 1}, {1, 2}, {3, 3}, {3, 1}, {5, 3}, {8, 4}, {10, 30}, {12, 10}, {14, 7}, {41, 17}, {49, 1}, {5, 20},
299 {256, 20}, {500, 300}, {2945, 129},
300 }
301 }
302
303 var testDataSizes = []int{10, 100, 1000, 10001, 100003, 1000055}
304 var testDataSizesShort = []int{10, 10001, 100003}
305
306 func testEncoding(t *testing.T, o ...Option) {
307 for _, size := range testSizes() {
308 data, parity := size[0], size[1]
309 rng := rand.New(rand.NewSource(0xabadc0cac01a))
310 t.Run(fmt.Sprintf("%dx%d", data, parity), func(t *testing.T) {
311 sz := testDataSizes
312 if testing.Short() || data+parity > 256 {
313 sz = testDataSizesShort
314 if raceEnabled {
315 sz = testDataSizesShort[:1]
316 }
317 }
318 for _, perShard := range sz {
319 r, err := New(data, parity, testOptions(o...)...)
320 if err != nil {
321 t.Fatal(err)
322 }
323 x := r.(Extensions)
324 if want, got := data, x.DataShards(); want != got {
325 t.Errorf("DataShards returned %d, want %d", got, want)
326 }
327 if want, got := parity, x.ParityShards(); want != got {
328 t.Errorf("ParityShards returned %d, want %d", got, want)
329 }
330 if want, got := parity+data, x.TotalShards(); want != got {
331 t.Errorf("TotalShards returned %d, want %d", got, want)
332 }
333 mul := x.ShardSizeMultiple()
334 if mul <= 0 {
335 t.Fatalf("Got unexpected ShardSizeMultiple: %d", mul)
336 }
337 perShard = ((perShard + mul - 1) / mul) * mul
338
339 t.Run(fmt.Sprint(perShard), func(t *testing.T) {
340
341 shards := make([][]byte, data+parity)
342 for s := range shards {
343 shards[s] = make([]byte, perShard)
344 }
345
346 for s := range shards {
347 rng.Read(shards[s])
348 }
349
350 err = r.Encode(shards)
351 if err != nil {
352 t.Fatal(err)
353 }
354 ok, err := r.Verify(shards)
355 if err != nil {
356 t.Fatal(err)
357 }
358 if !ok {
359 t.Fatal("Verification failed")
360 }
361
362 if parity == 0 {
363 // Check that Reconstruct and ReconstructData do nothing
364 err = r.ReconstructData(shards)
365 if err != nil {
366 t.Fatal(err)
367 }
368 err = r.Reconstruct(shards)
369 if err != nil {
370 t.Fatal(err)
371 }
372
373 // Skip integrity checks
374 return
375 }
376
377 // Delete one in data
378 idx := rng.Intn(data)
379 want := shards[idx]
380 shards[idx] = nil
381
382 err = r.ReconstructData(shards)
383 if err != nil {
384 t.Fatal(err)
385 }
386 if !bytes.Equal(shards[idx], want) {
387 t.Fatal("did not ReconstructData correctly")
388 }
389
390 // Delete one randomly
391 idx = rng.Intn(data + parity)
392 want = shards[idx]
393 shards[idx] = nil
394 err = r.Reconstruct(shards)
395 if err != nil {
396 t.Fatal(err)
397 }
398 if !bytes.Equal(shards[idx], want) {
399 t.Fatal("did not Reconstruct correctly")
400 }
401
402 err = r.Encode(make([][]byte, 1))
403 if err != ErrTooFewShards {
404 t.Errorf("expected %v, got %v", ErrTooFewShards, err)
405 }
406
407 // Make one too short.
408 shards[idx] = shards[idx][:perShard-1]
409 err = r.Encode(shards)
410 if err != ErrShardSize {
411 t.Errorf("expected %v, got %v", ErrShardSize, err)
412 }
413 })
414 }
415 })
416
417 }
418 }
419
420 func testEncodingIdx(t *testing.T, o ...Option) {
421 for _, size := range testSizes() {
422 data, parity := size[0], size[1]
423 rng := rand.New(rand.NewSource(0xabadc0cac01a))
424 t.Run(fmt.Sprintf("%dx%d", data, parity), func(t *testing.T) {
425
426 sz := testDataSizes
427 if testing.Short() {
428 sz = testDataSizesShort
429 }
430 for _, perShard := range sz {
431 r, err := New(data, parity, testOptions(o...)...)
432 if err != nil {
433 t.Fatal(err)
434 }
435 if err := r.EncodeIdx(nil, 0, nil); err == ErrNotSupported {
436 t.Skip(err)
437 return
438 }
439 mul := r.(Extensions).ShardSizeMultiple()
440 perShard = ((perShard + mul - 1) / mul) * mul
441
442 t.Run(fmt.Sprint(perShard), func(t *testing.T) {
443
444 shards := AllocAligned(data+parity, perShard)
445 shuffle := make([]int, data)
446 for i := range shuffle {
447 shuffle[i] = i
448 }
449 rng.Shuffle(len(shuffle), func(i, j int) { shuffle[i], shuffle[j] = shuffle[j], shuffle[i] })
450
451 // Send shards in random order.
452 for s := range data {
453 s := shuffle[s]
454 rng.Read(shards[s])
455 err = r.EncodeIdx(shards[s], s, shards[data:])
456 if err != nil {
457 t.Fatal(err)
458 }
459 }
460
461 ok, err := r.Verify(shards)
462 if err != nil {
463 t.Fatal(err)
464 }
465 if !ok {
466 t.Fatal("Verification failed")
467 }
468
469 if parity == 0 {
470 // Check that Reconstruct and ReconstructData do nothing
471 err = r.ReconstructData(shards)
472 if err != nil {
473 t.Fatal(err)
474 }
475 err = r.Reconstruct(shards)
476 if err != nil {
477 t.Fatal(err)
478 }
479
480 // Skip integrity checks
481 return
482 }
483
484 // Delete one in data
485 idx := rng.Intn(data)
486 want := shards[idx]
487 shards[idx] = nil
488
489 err = r.ReconstructData(shards)
490 if err != nil {
491 t.Fatal(err)
492 }
493 if !bytes.Equal(shards[idx], want) {
494 t.Fatal("did not ReconstructData correctly")
495 }
496
497 // Delete one randomly
498 idx = rng.Intn(data + parity)
499 want = shards[idx]
500 shards[idx] = nil
501 err = r.Reconstruct(shards)
502 if err != nil {
503 t.Fatal(err)
504 }
505 if !bytes.Equal(shards[idx], want) {
506 t.Fatal("did not Reconstruct correctly")
507 }
508
509 err = r.Encode(make([][]byte, 1))
510 if err != ErrTooFewShards {
511 t.Errorf("expected %v, got %v", ErrTooFewShards, err)
512 }
513
514 // Make one too short.
515 shards[idx] = shards[idx][:perShard-1]
516 err = r.Encode(shards)
517 if err != ErrShardSize {
518 t.Errorf("expected %v, got %v", ErrShardSize, err)
519 }
520 })
521 }
522 })
523
524 }
525 }
526
527 func TestUpdate(t *testing.T) {
528 parallelIfNotShort(t)
529 for i, o := range testOpts() {
530 t.Run(fmt.Sprintf("options %d", i), func(t *testing.T) {
531 testUpdate(t, o...)
532 })
533 }
534 }
535
536 func testUpdate(t *testing.T, o ...Option) {
537 for _, size := range [][2]int{{10, 3}, {17, 2}} {
538 data, parity := size[0], size[1]
539 t.Run(fmt.Sprintf("%dx%d", data, parity), func(t *testing.T) {
540 sz := testDataSizesShort
541 if testing.Short() {
542 sz = []int{50000}
543 }
544 for _, perShard := range sz {
545 r, err := New(data, parity, testOptions(o...)...)
546 if err != nil {
547 t.Fatal(err)
548 }
549 mul := r.(Extensions).ShardSizeMultiple()
550 perShard = ((perShard + mul - 1) / mul) * mul
551
552 t.Run(fmt.Sprint(perShard), func(t *testing.T) {
553
554 shards := make([][]byte, data+parity)
555 for s := range shards {
556 shards[s] = make([]byte, perShard)
557 }
558
559 for s := range shards {
560 fillRandom(shards[s])
561 }
562
563 err = r.Encode(shards)
564 if err != nil {
565 t.Fatal(err)
566 }
567 ok, err := r.Verify(shards)
568 if err != nil {
569 t.Fatal(err)
570 }
571 if !ok {
572 t.Fatal("Verification failed")
573 }
574
575 newdatashards := make([][]byte, data)
576 for s := range newdatashards {
577 newdatashards[s] = make([]byte, perShard)
578 fillRandom(newdatashards[s])
579 err = r.Update(shards, newdatashards)
580 if err != nil {
581 if errors.Is(err, ErrNotSupported) {
582 t.Skip(err)
583 return
584 }
585 t.Fatal(err)
586 }
587 shards[s] = newdatashards[s]
588 ok, err := r.Verify(shards)
589 if err != nil {
590 t.Fatal(err)
591 }
592 if !ok {
593 t.Fatal("Verification failed")
594 }
595 newdatashards[s] = nil
596 }
597 for s := 0; s < len(newdatashards)-1; s++ {
598 newdatashards[s] = make([]byte, perShard)
599 newdatashards[s+1] = make([]byte, perShard)
600 fillRandom(newdatashards[s])
601 fillRandom(newdatashards[s+1])
602 err = r.Update(shards, newdatashards)
603 if err != nil {
604 t.Fatal(err)
605 }
606 shards[s] = newdatashards[s]
607 shards[s+1] = newdatashards[s+1]
608 ok, err := r.Verify(shards)
609 if err != nil {
610 t.Fatal(err)
611 }
612 if !ok {
613 t.Fatal("Verification failed")
614 }
615 newdatashards[s] = nil
616 newdatashards[s+1] = nil
617 }
618 for newNum := 1; newNum <= data; newNum++ {
619 for s := 0; s <= data-newNum; s++ {
620 for i := 0; i < newNum; i++ {
621 newdatashards[s+i] = make([]byte, perShard)
622 fillRandom(newdatashards[s+i])
623 }
624 err = r.Update(shards, newdatashards)
625 if err != nil {
626 t.Fatal(err)
627 }
628 for i := 0; i < newNum; i++ {
629 shards[s+i] = newdatashards[s+i]
630 }
631 ok, err := r.Verify(shards)
632 if err != nil {
633 t.Fatal(err)
634 }
635 if !ok {
636 t.Fatal("Verification failed")
637 }
638 for i := 0; i < newNum; i++ {
639 newdatashards[s+i] = nil
640 }
641 }
642 }
643 })
644 }
645 })
646 }
647 }
648
649 func TestReconstruct(t *testing.T) {
650 parallelIfNotShort(t)
651 testReconstruct(t)
652 for i, o := range testOpts() {
653 t.Run(fmt.Sprintf("options %d", i), func(t *testing.T) {
654 testReconstruct(t, o...)
655 })
656 }
657 }
658
659 func testReconstruct(t *testing.T, o ...Option) {
660 perShard := 50000
661 r, err := New(10, 3, testOptions(o...)...)
662 if err != nil {
663 t.Fatal(err)
664 }
665 xt := r.(Extensions)
666 mul := xt.ShardSizeMultiple()
667 perShard = ((perShard + mul - 1) / mul) * mul
668
669 t.Log(perShard)
670 shards := make([][]byte, 13)
671 for s := range shards {
672 shards[s] = make([]byte, perShard)
673 }
674
675 for s := range 13 {
676 fillRandom(shards[s])
677 }
678
679 err = r.Encode(shards)
680 if err != nil {
681 t.Fatal(err)
682 }
683
684 // Reconstruct with all shards present
685 err = r.Reconstruct(shards)
686 if err != nil {
687 t.Fatal(err)
688 }
689
690 // Reconstruct with 10 shards present. Use pre-allocated memory for one of them.
691 shards[0] = nil
692 shards[7] = nil
693 shard11 := shards[11]
694 shards[11] = shard11[:0]
695 fillRandom(shard11)
696
697 err = r.Reconstruct(shards)
698 if err != nil {
699 t.Fatal(err)
700 }
701
702 ok, err := r.Verify(shards)
703 if err != nil {
704 t.Fatal(err)
705 }
706 if !ok {
707 t.Fatal("Verification failed")
708 }
709
710 if &shard11[0] != &shards[11][0] {
711 t.Errorf("Shard was not reconstructed into pre-allocated memory")
712 }
713
714 // Reconstruct with 9 shards present (should fail)
715 shards[0] = nil
716 shards[4] = nil
717 shards[7] = nil
718 shards[11] = nil
719
720 err = r.Reconstruct(shards)
721 if err != ErrTooFewShards {
722 t.Errorf("expected %v, got %v", ErrTooFewShards, err)
723 }
724
725 err = r.Reconstruct(make([][]byte, 1))
726 if err != ErrTooFewShards {
727 t.Errorf("expected %v, got %v", ErrTooFewShards, err)
728 }
729 err = r.Reconstruct(make([][]byte, 13))
730 if err != ErrShardNoData {
731 t.Errorf("expected %v, got %v", ErrShardNoData, err)
732 }
733 }
734
735 func TestReconstructCustom(t *testing.T) {
736 perShard := 50000
737 r, err := New(4, 3, WithCustomMatrix([][]byte{
738 {1, 1, 0, 0},
739 {0, 0, 1, 1},
740 {1, 2, 3, 4},
741 }))
742 if err != nil {
743 t.Fatal(err)
744 }
745 shards := make([][]byte, 7)
746 for s := range shards {
747 shards[s] = make([]byte, perShard)
748 }
749
750 for s := range shards {
751 fillRandom(shards[s])
752 }
753
754 err = r.Encode(shards)
755 if err != nil {
756 t.Fatal(err)
757 }
758
759 // Reconstruct with 1 shard absent.
760 shards1 := make([][]byte, len(shards))
761 copy(shards1, shards)
762 shards1[0] = nil
763
764 err = r.Reconstruct(shards1)
765 if err != nil {
766 t.Fatal(err)
767 }
768
769 ok, err := r.Verify(shards)
770 if err != nil {
771 t.Fatal(err)
772 }
773 if !ok {
774 t.Fatal("Verification failed")
775 }
776
777 // Reconstruct with 3 shards absent.
778 copy(shards1, shards)
779 shards1[0] = nil
780 shards1[1] = nil
781 shards1[2] = nil
782
783 err = r.Reconstruct(shards1)
784 if err != nil {
785 t.Fatal(err)
786 }
787
788 ok, err = r.Verify(shards)
789 if err != nil {
790 t.Fatal(err)
791 }
792 if !ok {
793 t.Fatal("Verification failed")
794 }
795 }
796
797 func TestReconstructData(t *testing.T) {
798 parallelIfNotShort(t)
799 testReconstructData(t)
800 for i, o := range testOpts() {
801 t.Run(fmt.Sprintf("options %d", i), func(t *testing.T) {
802 testReconstructData(t, o...)
803 })
804 }
805 }
806
807 func testReconstructData(t *testing.T, o ...Option) {
808 perShard := 100000
809 r, err := New(8, 5, testOptions(o...)...)
810 if err != nil {
811 t.Fatal(err)
812 }
813 mul := r.(Extensions).ShardSizeMultiple()
814 perShard = ((perShard + mul - 1) / mul) * mul
815
816 shards := make([][]byte, 13)
817 for s := range shards {
818 shards[s] = make([]byte, perShard)
819 }
820
821 for s := range 13 {
822 fillRandom(shards[s], int64(s))
823 }
824
825 err = r.Encode(shards)
826 if err != nil {
827 t.Fatal(err)
828 }
829
830 // Reconstruct with all shards present
831 err = r.ReconstructData(shards)
832 if err != nil {
833 t.Fatal(err)
834 }
835
836 // Reconstruct parity shards from data
837 shardsCopy := make([][]byte, 13)
838 for i := range 8 {
839 shardsCopy[i] = shards[i]
840 }
841
842 shardsRequired := make([]bool, 13)
843 shardsRequired[10] = true
844
845 err = r.ReconstructSome(shardsCopy, shardsRequired)
846 if err != nil {
847 t.Fatal(err)
848 }
849
850 if !bytes.Equal(shardsCopy[10], shards[10]) {
851 t.Fatal("ReconstructSome did not reconstruct required shards correctly")
852 }
853
854 // Reconstruct 3 shards with 3 data and 5 parity shards
855 shardsCopy = make([][]byte, 13)
856 copy(shardsCopy, shards)
857 shardsCopy[2] = nil
858 shardsCopy[3] = nil
859 shardsCopy[4] = nil
860 shardsCopy[5] = nil
861 shardsCopy[6] = nil
862
863 shardsRequired = make([]bool, 8)
864 shardsRequired[3] = true
865 shardsRequired[4] = true
866 err = r.ReconstructSome(shardsCopy, shardsRequired)
867 if err != nil {
868 t.Fatal(err)
869 }
870
871 if 0 != bytes.Compare(shardsCopy[3], shards[3]) ||
872 0 != bytes.Compare(shardsCopy[4], shards[4]) {
873 t.Fatal("ReconstructSome did not reconstruct required shards correctly")
874 }
875
876 if shardsCopy[2] != nil || shardsCopy[5] != nil || shardsCopy[6] != nil {
877 // This is expected in some cases.
878 t.Log("ReconstructSome reconstructed extra shards")
879 }
880
881 // Reconstruct with 10 shards present. Use pre-allocated memory for one of them.
882 shards[0] = nil
883 shards[2] = nil
884 shard4 := shards[4]
885 shards[4] = shard4[:0]
886 fillRandom(shard4, 4)
887
888 err = r.ReconstructData(shards)
889 if err != nil {
890 t.Fatal(err)
891 }
892
893 // Since all parity shards are available, verification will succeed
894 ok, err := r.Verify(shards)
895 if err != nil {
896 t.Fatal(err)
897 }
898 if !ok {
899 t.Fatal("Verification failed")
900 }
901
902 if &shard4[0] != &shards[4][0] {
903 t.Errorf("Shard was not reconstructed into pre-allocated memory")
904 }
905
906 // Reconstruct with 6 data and 4 parity shards
907 shards[0] = nil
908 shards[2] = nil
909 shards[12] = nil
910
911 err = r.ReconstructData(shards)
912 if err != nil {
913 t.Fatal(err)
914 }
915
916 // Verification will fail now due to absence of a parity block
917 _, err = r.Verify(shards)
918 if err != ErrShardSize {
919 t.Errorf("expected %v, got %v", ErrTooFewShards, err)
920 }
921
922 // Reconstruct with 7 data and 1 parity shards
923 shards[0] = nil
924 shards[9] = nil
925 shards[10] = nil
926 shards[11] = nil
927 shards[12] = nil
928
929 err = r.ReconstructData(shards)
930 if err != nil {
931 t.Fatal(err)
932 }
933
934 _, err = r.Verify(shards)
935 if err != ErrShardSize {
936 t.Errorf("expected %v, got %v", ErrTooFewShards, err)
937 }
938
939 // Reconstruct with 6 data and 1 parity shards (should fail)
940 shards[0] = nil
941 shards[1] = nil
942 shards[9] = nil
943 shards[10] = nil
944 shards[11] = nil
945 shards[12] = nil
946
947 err = r.ReconstructData(shards)
948 if err != ErrTooFewShards {
949 t.Errorf("expected %v, got %v", ErrTooFewShards, err)
950 }
951
952 err = r.ReconstructData(make([][]byte, 1))
953 if err != ErrTooFewShards {
954 t.Errorf("expected %v, got %v", ErrTooFewShards, err)
955 }
956 err = r.ReconstructData(make([][]byte, 13))
957 if err != ErrShardNoData {
958 t.Errorf("expected %v, got %v", ErrShardNoData, err)
959 }
960 }
961
962 func TestReconstructPAR1Singular(t *testing.T) {
963 parallelIfNotShort(t)
964 perShard := 50
965 r, err := New(4, 4, testOptions(WithPAR1Matrix())...)
966 if err != nil {
967 t.Fatal(err)
968 }
969 shards := make([][]byte, 8)
970 for s := range shards {
971 shards[s] = make([]byte, perShard)
972 }
973
974 for s := range 8 {
975 fillRandom(shards[s])
976 }
977
978 err = r.Encode(shards)
979 if err != nil {
980 t.Fatal(err)
981 }
982
983 // Reconstruct with only the last data shard present, and the
984 // first, second, and fourth parity shard present (based on
985 // the result of TestBuildMatrixPAR1Singular). This should
986 // fail.
987 shards[0] = nil
988 shards[1] = nil
989 shards[2] = nil
990 shards[6] = nil
991
992 err = r.Reconstruct(shards)
993 if err != errSingular {
994 t.Fatal(err)
995 t.Errorf("expected %v, got %v", errSingular, err)
996 }
997 }
998
999 func TestVerify(t *testing.T) {
1000 parallelIfNotShort(t)
1001 testVerify(t)
1002 for i, o := range testOpts() {
1003 t.Run(fmt.Sprintf("options %d", i), func(t *testing.T) {
1004 testVerify(t, o...)
1005 })
1006 }
1007 }
1008
1009 func testVerify(t *testing.T, o ...Option) {
1010 perShard := 33333
1011 r, err := New(10, 4, testOptions(o...)...)
1012 if err != nil {
1013 t.Fatal(err)
1014 }
1015 mul := r.(Extensions).ShardSizeMultiple()
1016 perShard = ((perShard + mul - 1) / mul) * mul
1017
1018 shards := make([][]byte, 14)
1019 for s := range shards {
1020 shards[s] = make([]byte, perShard)
1021 }
1022
1023 for s := range 10 {
1024 fillRandom(shards[s], 0)
1025 }
1026
1027 err = r.Encode(shards)
1028 if err != nil {
1029 t.Fatal(err)
1030 }
1031 ok, err := r.Verify(shards)
1032 if err != nil {
1033 t.Fatal(err)
1034 }
1035 if !ok {
1036 t.Error("Verification failed")
1037 return
1038 }
1039
1040 // Put in random data. Verification should fail
1041 fillRandom(shards[10], 1)
1042 ok, err = r.Verify(shards)
1043 if err != nil {
1044 t.Fatal(err)
1045 }
1046 if ok {
1047 t.Fatal("Verification did not fail")
1048 }
1049 // Re-encode
1050 err = r.Encode(shards)
1051 if err != nil {
1052 t.Fatal(err)
1053 }
1054 // Fill a data segment with random data
1055 fillRandom(shards[0], 2)
1056 ok, err = r.Verify(shards)
1057 if err != nil {
1058 t.Fatal(err)
1059 }
1060 if ok {
1061 t.Fatal("Verification did not fail")
1062 }
1063
1064 _, err = r.Verify(make([][]byte, 1))
1065 if err != ErrTooFewShards {
1066 t.Errorf("expected %v, got %v", ErrTooFewShards, err)
1067 }
1068
1069 _, err = r.Verify(make([][]byte, 14))
1070 if err != ErrShardNoData {
1071 t.Errorf("expected %v, got %v", ErrShardNoData, err)
1072 }
1073 }
1074
1075 func TestOneEncode(t *testing.T) {
1076 codec, err := New(5, 5, testOptions()...)
1077 if err != nil {
1078 t.Fatal(err)
1079 }
1080 shards := [][]byte{
1081 {0, 1},
1082 {4, 5},
1083 {2, 3},
1084 {6, 7},
1085 {8, 9},
1086 {0, 0},
1087 {0, 0},
1088 {0, 0},
1089 {0, 0},
1090 {0, 0},
1091 }
1092 codec.Encode(shards)
1093 if shards[5][0] != 12 || shards[5][1] != 13 {
1094 t.Fatal("shard 5 mismatch")
1095 }
1096 if shards[6][0] != 10 || shards[6][1] != 11 {
1097 t.Fatal("shard 6 mismatch")
1098 }
1099 if shards[7][0] != 14 || shards[7][1] != 15 {
1100 t.Fatal("shard 7 mismatch")
1101 }
1102 if shards[8][0] != 90 || shards[8][1] != 91 {
1103 t.Fatal("shard 8 mismatch")
1104 }
1105 if shards[9][0] != 94 || shards[9][1] != 95 {
1106 t.Fatal("shard 9 mismatch")
1107 }
1108
1109 ok, err := codec.Verify(shards)
1110 if err != nil {
1111 t.Fatal(err)
1112 }
1113 if !ok {
1114 t.Fatal("did not verify")
1115 }
1116 shards[8][0]++
1117 ok, err = codec.Verify(shards)
1118 if err != nil {
1119 t.Fatal(err)
1120 }
1121 if ok {
1122 t.Fatal("verify did not fail as expected")
1123 }
1124
1125 }
1126
1127 func fillRandom(p []byte, seed ...int64) {
1128 src := rand.NewSource(time.Now().UnixNano())
1129 if len(seed) > 0 {
1130 src = rand.NewSource(seed[0])
1131 }
1132 rng := rand.New(src)
1133 for i := 0; i < len(p); i += 7 {
1134 val := rng.Int63()
1135 for j := 0; i+j < len(p) && j < 7; j++ {
1136 p[i+j] = byte(val)
1137 val >>= 8
1138 }
1139 }
1140 }
1141
1142 func benchmarkEncode(b *testing.B, dataShards, parityShards, shardSize int, opts ...Option) {
1143 opts = append(testOptions(WithAutoGoroutines(shardSize)), opts...)
1144 r, err := New(dataShards, parityShards, opts...)
1145 if err != nil {
1146 b.Fatal(err)
1147 }
1148
1149 shards := r.(Extensions).AllocAligned(shardSize)
1150 for s := range dataShards {
1151 fillRandom(shards[s])
1152 }
1153 // Warm up so initialization is eliminated.
1154 err = r.Encode(shards)
1155 if err != nil {
1156 b.Fatal(err)
1157 }
1158
1159 b.SetBytes(int64(shardSize * (dataShards + parityShards)))
1160 b.ResetTimer()
1161 b.ReportAllocs()
1162 for i := 0; i < b.N; i++ {
1163 err = r.Encode(shards)
1164 if err != nil {
1165 b.Fatal(err)
1166 }
1167 }
1168 }
1169
1170 func benchmarkDecode(b *testing.B, dataShards, parityShards, shardSize, deleteShards int, opts ...Option) {
1171 opts = append(testOptions(WithAutoGoroutines(shardSize)), opts...)
1172 r, err := New(dataShards, parityShards, opts...)
1173 if err != nil {
1174 b.Fatal(err)
1175 }
1176
1177 shards := r.(Extensions).AllocAligned(shardSize)
1178 for s := range dataShards {
1179 fillRandom(shards[s])
1180 }
1181 if err := r.Encode(shards); err != nil {
1182 b.Fatal(err)
1183 }
1184
1185 b.SetBytes(int64(shardSize * (dataShards + parityShards)))
1186 b.ResetTimer()
1187 b.ReportAllocs()
1188 for i := 0; i < b.N; i++ {
1189 // Clear maximum number of data shards.
1190 for s := range deleteShards {
1191 shards[s] = nil
1192 }
1193
1194 err = r.Reconstruct(shards)
1195 if err != nil {
1196 b.Fatal(err)
1197 }
1198 }
1199 }
1200
1201 func BenchmarkEncode2x1x1M(b *testing.B) {
1202 benchmarkEncode(b, 2, 1, 1024*1024)
1203 }
1204
1205 // Benchmark 800 data slices with 200 parity slices
1206 func BenchmarkEncode800x200(b *testing.B) {
1207 for size := 64; size <= 1<<20; size *= 4 {
1208 b.Run(fmt.Sprintf("%v", size), func(b *testing.B) {
1209 benchmarkEncode(b, 800, 200, size)
1210 })
1211 }
1212 }
1213
1214 // Benchmark 1K encode with symmetric shard sizes.
1215 func BenchmarkEncode1K(b *testing.B) {
1216 for shards := 4; shards < 65536; shards *= 2 {
1217 b.Run(fmt.Sprintf("%v+%v", shards, shards), func(b *testing.B) {
1218 if shards*2 <= 256 {
1219 b.Run(fmt.Sprint("cauchy"), func(b *testing.B) {
1220 benchmarkEncode(b, shards, shards, 1024, WithCauchyMatrix())
1221 })
1222 b.Run(fmt.Sprint("leopard-gf8"), func(b *testing.B) {
1223 benchmarkEncode(b, shards, shards, 1024, WithLeopardGF(true))
1224 })
1225 }
1226 b.Run(fmt.Sprint("leopard-gf16"), func(b *testing.B) {
1227 benchmarkEncode(b, shards, shards, 1024, WithLeopardGF16(true))
1228 })
1229 })
1230 }
1231 }
1232
1233 // Benchmark 1K decode with symmetric shard sizes.
1234 func BenchmarkDecode1K(b *testing.B) {
1235 for shards := 4; shards < 65536; shards *= 2 {
1236 b.Run(fmt.Sprintf("%v+%v", shards, shards), func(b *testing.B) {
1237 if shards*2 <= 256 {
1238 b.Run(fmt.Sprint("cauchy"), func(b *testing.B) {
1239 benchmarkDecode(b, shards, shards, 1024, shards, WithCauchyMatrix(), WithInversionCache(false))
1240 })
1241 b.Run(fmt.Sprint("cauchy-inv"), func(b *testing.B) {
1242 benchmarkDecode(b, shards, shards, 1024, shards, WithCauchyMatrix(), WithInversionCache(true))
1243 })
1244 b.Run(fmt.Sprint("cauchy-single"), func(b *testing.B) {
1245 benchmarkDecode(b, shards, shards, 1024, 1, WithCauchyMatrix(), WithInversionCache(false))
1246 })
1247 b.Run(fmt.Sprint("cauchy-single-inv"), func(b *testing.B) {
1248 benchmarkDecode(b, shards, shards, 1024, 1, WithCauchyMatrix(), WithInversionCache(true))
1249 })
1250 b.Run(fmt.Sprint("leopard-gf8"), func(b *testing.B) {
1251 benchmarkDecode(b, shards, shards, 1024, shards, WithLeopardGF(true), WithInversionCache(false))
1252 })
1253 b.Run(fmt.Sprint("leopard-gf8-inv"), func(b *testing.B) {
1254 benchmarkDecode(b, shards, shards, 1024, shards, WithLeopardGF(true), WithInversionCache(true))
1255 })
1256 b.Run(fmt.Sprint("leopard-gf8-single"), func(b *testing.B) {
1257 benchmarkDecode(b, shards, shards, 1024, 1, WithLeopardGF(true), WithInversionCache(false))
1258 })
1259 b.Run(fmt.Sprint("leopard-gf8-single-inv"), func(b *testing.B) {
1260 benchmarkDecode(b, shards, shards, 1024, 1, WithLeopardGF(true), WithInversionCache(true))
1261 })
1262 }
1263 b.Run(fmt.Sprint("leopard-gf16"), func(b *testing.B) {
1264 benchmarkDecode(b, shards, shards, 1024, shards, WithLeopardGF16(true))
1265 })
1266 b.Run(fmt.Sprint("leopard-gf16-single"), func(b *testing.B) {
1267 benchmarkDecode(b, shards, shards, 1024, 1, WithLeopardGF16(true))
1268 })
1269 })
1270 }
1271 }
1272
1273 func BenchmarkEncodeLeopard(b *testing.B) {
1274 size := (64 << 20) / 800 / 64 * 64
1275 b.Run(strconv.Itoa(size), func(b *testing.B) {
1276 benchmarkEncode(b, 800, 200, size)
1277 })
1278 }
1279
1280 func BenchmarkEncode10x2x10000(b *testing.B) {
1281 benchmarkEncode(b, 10, 2, 10000)
1282 }
1283
1284 func BenchmarkEncode100x20x10000(b *testing.B) {
1285 benchmarkEncode(b, 100, 20, 10000)
1286 }
1287
1288 func BenchmarkEncode17x3x1M(b *testing.B) {
1289 benchmarkEncode(b, 17, 3, 1024*1024)
1290 }
1291
1292 // Benchmark 10 data shards and 4 parity shards with 16MB each.
1293 func BenchmarkEncode10x4x16M(b *testing.B) {
1294 benchmarkEncode(b, 10, 4, 16*1024*1024)
1295 }
1296
1297 // Benchmark 5 data shards and 2 parity shards with 1MB each.
1298 func BenchmarkEncode5x2x1M(b *testing.B) {
1299 benchmarkEncode(b, 5, 2, 1024*1024)
1300 }
1301
1302 // Benchmark 1 data shards and 2 parity shards with 1MB each.
1303 func BenchmarkEncode10x2x1M(b *testing.B) {
1304 benchmarkEncode(b, 10, 2, 1024*1024)
1305 }
1306
1307 // Benchmark 10 data shards and 4 parity shards with 1MB each.
1308 func BenchmarkEncode10x4x1M(b *testing.B) {
1309 benchmarkEncode(b, 10, 4, 1024*1024)
1310 }
1311
1312 // Benchmark 50 data shards and 20 parity shards with 1M each.
1313 func BenchmarkEncode50x20x1M(b *testing.B) {
1314 benchmarkEncode(b, 50, 20, 1024*1024)
1315 }
1316
1317 // Benchmark 50 data shards and 20 parity shards with 1M each.
1318 func BenchmarkEncodeLeopard50x20x1M(b *testing.B) {
1319 benchmarkEncode(b, 50, 20, 1024*1024, WithLeopardGF(true))
1320 }
1321
1322 // Benchmark 17 data shards and 3 parity shards with 16MB each.
1323 func BenchmarkEncode17x3x16M(b *testing.B) {
1324 benchmarkEncode(b, 17, 3, 16*1024*1024)
1325 }
1326
1327 func BenchmarkEncode_8x4x8M(b *testing.B) { benchmarkEncode(b, 8, 4, 8*1024*1024) }
1328 func BenchmarkEncode_12x4x12M(b *testing.B) { benchmarkEncode(b, 12, 4, 12*1024*1024) }
1329 func BenchmarkEncode_16x4x16M(b *testing.B) { benchmarkEncode(b, 16, 4, 16*1024*1024) }
1330 func BenchmarkEncode_16x4x32M(b *testing.B) { benchmarkEncode(b, 16, 4, 32*1024*1024) }
1331 func BenchmarkEncode_16x4x64M(b *testing.B) { benchmarkEncode(b, 16, 4, 64*1024*1024) }
1332
1333 func BenchmarkEncode_8x5x8M(b *testing.B) { benchmarkEncode(b, 8, 5, 8*1024*1024) }
1334 func BenchmarkEncode_8x6x8M(b *testing.B) { benchmarkEncode(b, 8, 6, 8*1024*1024) }
1335 func BenchmarkEncode_8x7x8M(b *testing.B) { benchmarkEncode(b, 8, 7, 8*1024*1024) }
1336 func BenchmarkEncode_8x9x8M(b *testing.B) { benchmarkEncode(b, 8, 9, 8*1024*1024) }
1337 func BenchmarkEncode_8x10x8M(b *testing.B) { benchmarkEncode(b, 8, 10, 8*1024*1024) }
1338 func BenchmarkEncode_8x11x8M(b *testing.B) { benchmarkEncode(b, 8, 11, 8*1024*1024) }
1339
1340 func BenchmarkEncode_8x8x05M(b *testing.B) { benchmarkEncode(b, 8, 8, 1*1024*1024/2) }
1341 func BenchmarkEncode_8x8x1M(b *testing.B) { benchmarkEncode(b, 8, 8, 1*1024*1024) }
1342 func BenchmarkEncode_8x8x8M(b *testing.B) { benchmarkEncode(b, 8, 8, 8*1024*1024) }
1343 func BenchmarkEncode_8x8x32M(b *testing.B) { benchmarkEncode(b, 8, 8, 32*1024*1024) }
1344
1345 func BenchmarkEncode_24x8x24M(b *testing.B) { benchmarkEncode(b, 24, 8, 24*1024*1024) }
1346 func BenchmarkEncode_24x8x48M(b *testing.B) { benchmarkEncode(b, 24, 8, 48*1024*1024) }
1347
1348 func BenchmarkEncodeLeopardGF16Chunking(b *testing.B) {
1349 // Tests GF16 encoder across shard sizes where L3 cache chunking matters.
1350 for _, tc := range []struct {
1351 data, parity int
1352 }{
1353 {800, 200},
1354 {500, 500},
1355 {50, 20},
1356 } {
1357 const maxBytes = 1 << 30
1358 for _, sz := range []int{
1359 64 << 10, 256 << 10, 1 << 20, 4 << 20, 16 << 20,
1360 } {
1361 sz = min(sz, maxBytes/(tc.data+tc.parity))
1362 sz = sz / 64 * 64
1363 if sz == 0 {
1364 continue
1365 }
1366 name := fmt.Sprintf("%dx%d/%dK", tc.data, tc.parity, sz>>10)
1367 b.Run(name, func(b *testing.B) {
1368 benchmarkEncode(b, tc.data, tc.parity, sz, WithLeopardGF16(true))
1369 })
1370 }
1371 }
1372 }
1373
1374 func benchmarkVerify(b *testing.B, dataShards, parityShards, shardSize int) {
1375 r, err := New(dataShards, parityShards, testOptions(WithAutoGoroutines(shardSize))...)
1376 if err != nil {
1377 b.Fatal(err)
1378 }
1379 shards := r.(Extensions).AllocAligned(shardSize)
1380
1381 for s := range dataShards {
1382 fillRandom(shards[s])
1383 }
1384 err = r.Encode(shards)
1385 if err != nil {
1386 b.Fatal(err)
1387 }
1388
1389 b.SetBytes(int64(shardSize * (dataShards + parityShards)))
1390 b.ResetTimer()
1391 b.ReportAllocs()
1392 for i := 0; i < b.N; i++ {
1393 _, err = r.Verify(shards)
1394 if err != nil {
1395 b.Fatal(err)
1396 }
1397 }
1398 }
1399
1400 // Benchmark 800 data slices with 200 parity slices
1401 func BenchmarkVerify800x200(b *testing.B) {
1402 for size := 64; size <= 1<<20; size *= 4 {
1403 b.Run(fmt.Sprintf("%v", size), func(b *testing.B) {
1404 benchmarkVerify(b, 800, 200, size)
1405 })
1406 }
1407 }
1408
1409 // Benchmark 10 data slices with 2 parity slices holding 10000 bytes each
1410 func BenchmarkVerify10x2x10000(b *testing.B) {
1411 benchmarkVerify(b, 10, 2, 10000)
1412 }
1413
1414 // Benchmark 50 data slices with 5 parity slices holding 100000 bytes each
1415 func BenchmarkVerify50x5x100000(b *testing.B) {
1416 benchmarkVerify(b, 50, 5, 100000)
1417 }
1418
1419 // Benchmark 10 data slices with 2 parity slices holding 1MB bytes each
1420 func BenchmarkVerify10x2x1M(b *testing.B) {
1421 benchmarkVerify(b, 10, 2, 1024*1024)
1422 }
1423
1424 // Benchmark 5 data slices with 2 parity slices holding 1MB bytes each
1425 func BenchmarkVerify5x2x1M(b *testing.B) {
1426 benchmarkVerify(b, 5, 2, 1024*1024)
1427 }
1428
1429 // Benchmark 10 data slices with 4 parity slices holding 1MB bytes each
1430 func BenchmarkVerify10x4x1M(b *testing.B) {
1431 benchmarkVerify(b, 10, 4, 1024*1024)
1432 }
1433
1434 // Benchmark 5 data slices with 2 parity slices holding 1MB bytes each
1435 func BenchmarkVerify50x20x1M(b *testing.B) {
1436 benchmarkVerify(b, 50, 20, 1024*1024)
1437 }
1438
1439 // Benchmark 10 data slices with 4 parity slices holding 16MB bytes each
1440 func BenchmarkVerify10x4x16M(b *testing.B) {
1441 benchmarkVerify(b, 10, 4, 16*1024*1024)
1442 }
1443
1444 func corruptRandom(shards [][]byte, dataShards, parityShards int) {
1445 shardsToCorrupt := rand.Intn(parityShards) + 1
1446 for range shardsToCorrupt {
1447 n := rand.Intn(dataShards + parityShards)
1448 shards[n] = shards[n][:0]
1449 }
1450 }
1451
1452 func benchmarkReconstruct(b *testing.B, dataShards, parityShards, shardSize int, opts ...Option) {
1453 o := []Option{WithAutoGoroutines(shardSize)}
1454 o = append(o, opts...)
1455 r, err := New(dataShards, parityShards, testOptions(o...)...)
1456 if err != nil {
1457 b.Fatal(err)
1458 }
1459 shards := r.(Extensions).AllocAligned(shardSize)
1460
1461 for s := range dataShards {
1462 fillRandom(shards[s])
1463 }
1464 err = r.Encode(shards)
1465 if err != nil {
1466 b.Fatal(err)
1467 }
1468
1469 b.SetBytes(int64(shardSize * (dataShards + parityShards)))
1470 b.ResetTimer()
1471 b.ReportAllocs()
1472 for i := 0; i < b.N; i++ {
1473 corruptRandom(shards, dataShards, parityShards)
1474
1475 err = r.Reconstruct(shards)
1476 if err != nil {
1477 b.Fatal(err)
1478 }
1479 }
1480 }
1481
1482 // Benchmark 10 data slices with 2 parity slices holding 10000 bytes each
1483 func BenchmarkReconstruct10x2x10000(b *testing.B) {
1484 benchmarkReconstruct(b, 10, 2, 10000)
1485 }
1486
1487 // Benchmark 800 data slices with 200 parity slices
1488 func BenchmarkReconstruct800x200(b *testing.B) {
1489 for size := 64; size <= 1<<20; size *= 4 {
1490 b.Run(fmt.Sprintf("%v", size), func(b *testing.B) {
1491 benchmarkReconstruct(b, 800, 200, size)
1492 })
1493 }
1494 }
1495
1496 // Benchmark 50 data slices with 5 parity slices holding 100000 bytes each
1497 func BenchmarkReconstruct50x5x50000(b *testing.B) {
1498 benchmarkReconstruct(b, 50, 5, 100000)
1499 }
1500
1501 // Benchmark 10 data slices with 2 parity slices holding 1MB bytes each
1502 func BenchmarkReconstruct10x2x1M(b *testing.B) {
1503 benchmarkReconstruct(b, 10, 2, 1024*1024)
1504 }
1505
1506 // Benchmark 5 data slices with 2 parity slices holding 1MB bytes each
1507 func BenchmarkReconstruct5x2x1M(b *testing.B) {
1508 benchmarkReconstruct(b, 5, 2, 1024*1024)
1509 }
1510
1511 // Benchmark 10 data slices with 4 parity slices holding 1MB bytes each
1512 func BenchmarkReconstruct10x4x1M(b *testing.B) {
1513 benchmarkReconstruct(b, 10, 4, 1024*1024)
1514 }
1515
1516 // Benchmark 5 data slices with 2 parity slices holding 1MB bytes each
1517 func BenchmarkReconstruct50x20x1M(b *testing.B) {
1518 benchmarkReconstruct(b, 50, 20, 1024*1024)
1519 }
1520
1521 // Benchmark 5 data slices with 2 parity slices holding 1MB bytes each
1522 func BenchmarkReconstructLeopard50x20x1M(b *testing.B) {
1523 benchmarkReconstruct(b, 50, 20, 1024*1024, WithLeopardGF(true), WithInversionCache(true))
1524 }
1525
1526 func BenchmarkReconstructDataLeopardGF16(b *testing.B) {
1527 b.Run("sparse-data-only", func(b *testing.B) {
1528 benchmarkReconstructDataSparse(b, 1024, 3072, 4096, WithLeopardGF16(true))
1529 })
1530 }
1531
1532 // Benchmark 10 data slices with 4 parity slices holding 16MB bytes each
1533 func BenchmarkReconstruct10x4x16M(b *testing.B) {
1534 benchmarkReconstruct(b, 10, 4, 16*1024*1024)
1535 }
1536
1537 func corruptRandomData(shards [][]byte, dataShards, parityShards int) {
1538 shardsToCorrupt := rand.Intn(parityShards) + 1
1539 for i := 1; i <= shardsToCorrupt; i++ {
1540 n := rand.Intn(dataShards)
1541 shards[n] = shards[n][:0]
1542 }
1543 }
1544
1545 func benchmarkReconstructData(b *testing.B, dataShards, parityShards, shardSize int) {
1546 r, err := New(dataShards, parityShards, testOptions(WithAutoGoroutines(shardSize))...)
1547 if err != nil {
1548 b.Fatal(err)
1549 }
1550 shards := r.(Extensions).AllocAligned(shardSize)
1551
1552 for s := range dataShards {
1553 fillRandom(shards[s])
1554 }
1555 err = r.Encode(shards)
1556 if err != nil {
1557 b.Fatal(err)
1558 }
1559
1560 b.SetBytes(int64(shardSize * (dataShards + parityShards)))
1561 b.ResetTimer()
1562 b.ReportAllocs()
1563 for i := 0; i < b.N; i++ {
1564 corruptRandomData(shards, dataShards, parityShards)
1565
1566 err = r.ReconstructData(shards)
1567 if err != nil {
1568 b.Fatal(err)
1569 }
1570 }
1571 }
1572
1573 func benchmarkReconstructDataSparse(b *testing.B, dataShards, parityShards, shardSize int, opts ...Option) {
1574 o := []Option{WithAutoGoroutines(shardSize)}
1575 o = append(o, opts...)
1576 r, err := New(dataShards, parityShards, testOptions(o...)...)
1577 if err != nil {
1578 b.Fatal(err)
1579 }
1580 shards := r.(Extensions).AllocAligned(shardSize)
1581
1582 for s := range dataShards {
1583 fillRandom(shards[s])
1584 }
1585 if err := r.Encode(shards); err != nil {
1586 b.Fatal(err)
1587 }
1588
1589 presentData := dataShards / 4
1590 presentParity := dataShards - presentData
1591 if presentParity > parityShards {
1592 b.Fatalf("need %d parity shards, have %d", presentParity, parityShards)
1593 }
1594 for s := dataShards + presentParity; s < dataShards+parityShards; s++ {
1595 shards[s] = nil
1596 }
1597
1598 b.SetBytes(int64(shardSize * (dataShards + parityShards)))
1599 b.ResetTimer()
1600 b.ReportAllocs()
1601 for i := 0; i < b.N; i++ {
1602 for s := presentData; s < dataShards; s++ {
1603 shards[s] = shards[s][:0]
1604 }
1605 if err := r.ReconstructData(shards); err != nil {
1606 b.Fatal(err)
1607 }
1608 }
1609 }
1610
1611 // Benchmark 10 data slices with 2 parity slices holding 10000 bytes each
1612 func BenchmarkReconstructData10x2x10000(b *testing.B) {
1613 benchmarkReconstructData(b, 10, 2, 10000)
1614 }
1615
1616 // Benchmark 800 data slices with 200 parity slices
1617 func BenchmarkReconstructData800x200(b *testing.B) {
1618 for size := 64; size <= 1<<20; size *= 4 {
1619 b.Run(fmt.Sprintf("%v", size), func(b *testing.B) {
1620 benchmarkReconstructData(b, 800, 200, size)
1621 })
1622 }
1623 }
1624
1625 // Benchmark 50 data slices with 5 parity slices holding 100000 bytes each
1626 func BenchmarkReconstructData50x5x50000(b *testing.B) {
1627 benchmarkReconstructData(b, 50, 5, 100000)
1628 }
1629
1630 // Benchmark 10 data slices with 2 parity slices holding 1MB bytes each
1631 func BenchmarkReconstructData10x2x1M(b *testing.B) {
1632 benchmarkReconstructData(b, 10, 2, 1024*1024)
1633 }
1634
1635 // Benchmark 5 data slices with 2 parity slices holding 1MB bytes each
1636 func BenchmarkReconstructData5x2x1M(b *testing.B) {
1637 benchmarkReconstructData(b, 5, 2, 1024*1024)
1638 }
1639
1640 // Benchmark 10 data slices with 4 parity slices holding 1MB bytes each
1641 func BenchmarkReconstructData10x4x1M(b *testing.B) {
1642 benchmarkReconstructData(b, 10, 4, 1024*1024)
1643 }
1644
1645 // Benchmark 5 data slices with 2 parity slices holding 1MB bytes each
1646 func BenchmarkReconstructData50x20x1M(b *testing.B) {
1647 benchmarkReconstructData(b, 50, 20, 1024*1024)
1648 }
1649
1650 // Benchmark 10 data slices with 4 parity slices holding 16MB bytes each
1651 func BenchmarkReconstructData10x4x16M(b *testing.B) {
1652 benchmarkReconstructData(b, 10, 4, 16*1024*1024)
1653 }
1654
1655 func benchmarkReconstructP(b *testing.B, dataShards, parityShards, shardSize int) {
1656 r, err := New(dataShards, parityShards, testOptions(WithMaxGoroutines(1))...)
1657 if err != nil {
1658 b.Fatal(err)
1659 }
1660
1661 b.SetBytes(int64(shardSize * (dataShards + parityShards)))
1662 b.ResetTimer()
1663 b.ReportAllocs()
1664
1665 b.RunParallel(func(pb *testing.PB) {
1666 shards := r.(Extensions).AllocAligned(shardSize)
1667
1668 for s := range dataShards {
1669 fillRandom(shards[s])
1670 }
1671 err = r.Encode(shards)
1672 if err != nil {
1673 b.Fatal(err)
1674 }
1675 b.ResetTimer()
1676 for pb.Next() {
1677 corruptRandom(shards, dataShards, parityShards)
1678
1679 err = r.Reconstruct(shards)
1680 if err != nil {
1681 b.Fatal(err)
1682 }
1683 }
1684 })
1685 }
1686
1687 // Benchmark 10 data slices with 2 parity slices holding 10000 bytes each
1688 func BenchmarkReconstructP10x2x10000(b *testing.B) {
1689 benchmarkReconstructP(b, 10, 2, 10000)
1690 }
1691
1692 // Benchmark 10 data slices with 5 parity slices holding 20000 bytes each
1693 func BenchmarkReconstructP10x5x20000(b *testing.B) {
1694 benchmarkReconstructP(b, 10, 5, 20000)
1695 }
1696
1697 func TestEncoderReconstruct(t *testing.T) {
1698 parallelIfNotShort(t)
1699 testEncoderReconstruct(t)
1700 for _, o := range testOpts() {
1701 testEncoderReconstruct(t, o...)
1702 }
1703 }
1704
1705 func testEncoderReconstruct(t *testing.T, o ...Option) {
1706 // Create some sample data
1707 var data = make([]byte, 250<<10)
1708 fillRandom(data)
1709
1710 // Create 5 data slices of 50000 elements each
1711 enc, err := New(7, 6, testOptions(o...)...)
1712 if err != nil {
1713 t.Fatal(err)
1714 }
1715 shards, err := enc.Split(data)
1716 if err != nil {
1717 t.Fatal(err)
1718 }
1719 err = enc.Encode(shards)
1720 if err != nil {
1721 t.Fatal(err)
1722 }
1723
1724 // Check that it verifies
1725 ok, err := enc.Verify(shards)
1726 if !ok || err != nil {
1727 t.Fatal("not ok:", ok, "err:", err)
1728 }
1729
1730 // Delete a shard
1731 shards[0] = nil
1732
1733 // Should reconstruct
1734 err = enc.Reconstruct(shards)
1735 if err != nil {
1736 t.Fatal(err)
1737 }
1738
1739 // Check that it verifies
1740 ok, err = enc.Verify(shards)
1741 if !ok || err != nil {
1742 t.Fatal("not ok:", ok, "err:", err)
1743 }
1744
1745 // Recover original bytes
1746 buf := new(bytes.Buffer)
1747 err = enc.Join(buf, shards, len(data))
1748 if err != nil {
1749 t.Fatal(err)
1750 }
1751 if !bytes.Equal(buf.Bytes(), data) {
1752 t.Fatal("recovered bytes do not match")
1753 }
1754
1755 // Corrupt a shard
1756 shards[0] = nil
1757 shards[1][0], shards[1][500] = 75, 75
1758
1759 // Should reconstruct (but with corrupted data)
1760 err = enc.Reconstruct(shards)
1761 if err != nil {
1762 t.Fatal(err)
1763 }
1764
1765 // Check that it verifies
1766 ok, err = enc.Verify(shards)
1767 if ok || err != nil {
1768 t.Fatal("error or ok:", ok, "err:", err)
1769 }
1770
1771 // Recovered data should not match original
1772 buf.Reset()
1773 err = enc.Join(buf, shards, len(data))
1774 if err != nil {
1775 t.Fatal(err)
1776 }
1777 if bytes.Equal(buf.Bytes(), data) {
1778 t.Fatal("corrupted data matches original")
1779 }
1780 }
1781
1782 func TestSplitJoin(t *testing.T) {
1783 opts := [][]Option{
1784 testOptions(),
1785 append(testOptions(), WithLeopardGF(true)),
1786 append(testOptions(), WithLeopardGF16(true)),
1787 }
1788 for i, opts := range opts {
1789 t.Run("opt-"+strconv.Itoa(i), func(t *testing.T) {
1790 for _, dp := range [][2]int{{1, 0}, {5, 0}, {5, 1}, {12, 4}, {2, 15}, {17, 1}} {
1791 enc, _ := New(dp[0], dp[1], opts...)
1792 ext := enc.(Extensions)
1793
1794 _, err := enc.Split([]byte{})
1795 if err != ErrShortData {
1796 t.Errorf("expected %v, got %v", ErrShortData, err)
1797 }
1798
1799 buf := new(bytes.Buffer)
1800 err = enc.Join(buf, [][]byte{}, 0)
1801 if err != ErrTooFewShards {
1802 t.Errorf("expected %v, got %v", ErrTooFewShards, err)
1803 }
1804 for _, size := range []int{ext.DataShards(), 1337, 2699} {
1805 for _, extra := range []int{0, 1, ext.ShardSizeMultiple(), ext.ShardSizeMultiple() * ext.DataShards(), ext.ShardSizeMultiple()*ext.ParityShards() + 1, 255} {
1806 buf.Reset()
1807 t.Run(fmt.Sprintf("d-%d-p-%d-sz-%d-cap%d", ext.DataShards(), ext.ParityShards(), size, extra), func(t *testing.T) {
1808 var data = make([]byte, size, size+extra)
1809 var ref = make([]byte, size, size)
1810 fillRandom(data)
1811 copy(ref, data)
1812
1813 shards, err := enc.Split(data)
1814 if err != nil {
1815 t.Fatal(err)
1816 }
1817 err = enc.Encode(shards)
1818 if err != nil {
1819 t.Fatal(err)
1820 }
1821 _, err = enc.Verify(shards)
1822 if err != nil {
1823 t.Fatal(err)
1824 }
1825 for i := range shards[:ext.ParityShards()] {
1826 // delete data shards up to parity
1827 shards[i] = nil
1828 }
1829 err = enc.Reconstruct(shards)
1830 if err != nil {
1831 t.Fatal(err)
1832 }
1833
1834 // Rejoin....
1835 err = enc.Join(buf, shards, size)
1836 if err != nil {
1837 t.Fatal(err)
1838 }
1839 if !bytes.Equal(buf.Bytes(), ref) {
1840 t.Log("")
1841 t.Fatal("recovered data does match original")
1842 }
1843
1844 err = enc.Join(buf, shards, len(data)+ext.DataShards()*ext.ShardSizeMultiple())
1845 if err != ErrShortData {
1846 t.Errorf("expected %v, got %v", ErrShortData, err)
1847 }
1848
1849 shards[0] = nil
1850 err = enc.Join(buf, shards, len(data))
1851 if err != ErrReconstructRequired {
1852 t.Errorf("expected %v, got %v", ErrReconstructRequired, err)
1853 }
1854 })
1855 }
1856 }
1857 }
1858 })
1859 }
1860 }
1861
1862 func TestCodeSomeShards(t *testing.T) {
1863 var data = make([]byte, 250000)
1864 fillRandom(data)
1865 enc, _ := New(5, 3, testOptions()...)
1866 r := enc.(*reedSolomon) // need to access private methods
1867 shards, _ := enc.Split(data)
1868
1869 old := runtime.GOMAXPROCS(1)
1870 r.codeSomeShards(r.parity, shards[:r.dataShards], shards[r.dataShards:r.dataShards+r.parityShards], len(shards[0]), true)
1871
1872 // hopefully more than 1 CPU
1873 runtime.GOMAXPROCS(runtime.NumCPU())
1874 r.codeSomeShards(r.parity, shards[:r.dataShards], shards[r.dataShards:r.dataShards+r.parityShards], len(shards[0]), true)
1875
1876 // reset MAXPROCS, otherwise testing complains
1877 runtime.GOMAXPROCS(old)
1878 }
1879
1880 func TestStandardMatrices(t *testing.T) {
1881 if testing.Short() || runtime.GOMAXPROCS(0) < 4 {
1882 // Runtime ~15s.
1883 t.Skip("Skipping slow matrix check")
1884 }
1885 for i := 1; i < 256; i++ {
1886 i := i
1887 t.Run(fmt.Sprintf("x%d", i), func(t *testing.T) {
1888 parallelIfNotShort(t)
1889 // i == n.o. datashards
1890 var shards = make([][]byte, 255)
1891 for p := range shards {
1892 v := byte(i)
1893 shards[p] = []byte{v}
1894 }
1895 rng := rand.New(rand.NewSource(0))
1896 for j := 1; j < 256; j++ {
1897 // j == n.o. parity shards
1898 if i+j > 255 {
1899 continue
1900 }
1901 sh := shards[:i+j]
1902 r, err := New(i, j, testOptions(WithFastOneParityMatrix())...)
1903 if err != nil {
1904 // We are not supposed to write to t from goroutines.
1905 t.Fatal("creating matrix size", i, j, ":", err)
1906 }
1907 err = r.Encode(sh)
1908 if err != nil {
1909 t.Fatal("encoding", i, j, ":", err)
1910 }
1911 for k := 0; k < j; k++ {
1912 // Remove random shard.
1913 r := int(rng.Int63n(int64(i + j)))
1914 sh[r] = sh[r][:0]
1915 }
1916 err = r.Reconstruct(sh)
1917 if err != nil {
1918 t.Fatal("reconstructing", i, j, ":", err)
1919 }
1920 ok, err := r.Verify(sh)
1921 if err != nil {
1922 t.Fatal("verifying", i, j, ":", err)
1923 }
1924 if !ok {
1925 t.Fatal(i, j, ok)
1926 }
1927 for k := range sh {
1928 if k == i {
1929 // Only check data shards
1930 break
1931 }
1932 if sh[k][0] != byte(i) {
1933 t.Fatal("does not match", i, j, k, sh[0], sh[k])
1934 }
1935 }
1936 }
1937 })
1938 }
1939 }
1940
1941 func TestCauchyMatrices(t *testing.T) {
1942 if testing.Short() || runtime.GOMAXPROCS(0) < 4 {
1943 // Runtime ~15s.
1944 t.Skip("Skipping slow matrix check")
1945 }
1946 for i := 1; i < 256; i++ {
1947 i := i
1948 t.Run(fmt.Sprintf("x%d", i), func(t *testing.T) {
1949 parallelIfNotShort(t)
1950 var shards = make([][]byte, 255)
1951 for p := range shards {
1952 v := byte(i)
1953 shards[p] = []byte{v}
1954 }
1955 rng := rand.New(rand.NewSource(0))
1956 for j := 1; j < 256; j++ {
1957 // j == n.o. parity shards
1958 if i+j > 255 {
1959 continue
1960 }
1961 sh := shards[:i+j]
1962 r, err := New(i, j, testOptions(WithCauchyMatrix(), WithFastOneParityMatrix())...)
1963 if err != nil {
1964 // We are not supposed to write to t from goroutines.
1965 t.Fatal("creating matrix size", i, j, ":", err)
1966 }
1967 err = r.Encode(sh)
1968 if err != nil {
1969 t.Fatal("encoding", i, j, ":", err)
1970 }
1971 for k := 0; k < j; k++ {
1972 // Remove random shard.
1973 r := int(rng.Int63n(int64(i + j)))
1974 sh[r] = sh[r][:0]
1975 }
1976 err = r.Reconstruct(sh)
1977 if err != nil {
1978 t.Fatal("reconstructing", i, j, ":", err)
1979 }
1980 ok, err := r.Verify(sh)
1981 if err != nil {
1982 t.Fatal("verifying", i, j, ":", err)
1983 }
1984 if !ok {
1985 t.Fatal(i, j, ok)
1986 }
1987 for k := range sh {
1988 if k == i {
1989 // Only check data shards
1990 break
1991 }
1992 if sh[k][0] != byte(i) {
1993 t.Fatal("does not match", i, j, k, sh[0], sh[k])
1994 }
1995 }
1996 }
1997 })
1998 }
1999 }
2000
2001 func TestPar1Matrices(t *testing.T) {
2002 if testing.Short() || runtime.GOMAXPROCS(0) < 4 {
2003 // Runtime ~15s.
2004 t.Skip("Skipping slow matrix check")
2005 }
2006 for i := 1; i < 256; i++ {
2007 i := i
2008 t.Run(fmt.Sprintf("x%d", i), func(t *testing.T) {
2009 parallelIfNotShort(t)
2010 var shards = make([][]byte, 255)
2011 for p := range shards {
2012 v := byte(i)
2013 shards[p] = []byte{v}
2014 }
2015 rng := rand.New(rand.NewSource(0))
2016 for j := 1; j < 256; j++ {
2017 // j == n.o. parity shards
2018 if i+j > 255 {
2019 continue
2020 }
2021 sh := shards[:i+j]
2022 r, err := New(i, j, testOptions(WithPAR1Matrix())...)
2023 if err != nil {
2024 // We are not supposed to write to t from goroutines.
2025 t.Fatal("creating matrix size", i, j, ":", err)
2026 }
2027 err = r.Encode(sh)
2028 if err != nil {
2029 t.Fatal("encoding", i, j, ":", err)
2030 }
2031 for k := 0; k < j; k++ {
2032 // Remove random shard.
2033 r := int(rng.Int63n(int64(i + j)))
2034 sh[r] = sh[r][:0]
2035 }
2036 err = r.Reconstruct(sh)
2037 if err != nil {
2038 if err == errSingular {
2039 t.Logf("Singular: %d (data), %d (parity)", i, j)
2040 for p := range sh {
2041 if len(sh[p]) == 0 {
2042 shards[p] = []byte{byte(i)}
2043 }
2044 }
2045 continue
2046 }
2047 t.Fatal("reconstructing", i, j, ":", err)
2048 }
2049 ok, err := r.Verify(sh)
2050 if err != nil {
2051 t.Fatal("verifying", i, j, ":", err)
2052 }
2053 if !ok {
2054 t.Fatal(i, j, ok)
2055 }
2056 for k := range sh {
2057 if k == i {
2058 // Only check data shards
2059 break
2060 }
2061 if sh[k][0] != byte(i) {
2062 t.Fatal("does not match", i, j, k, sh[0], sh[k])
2063 }
2064 }
2065 }
2066 })
2067 }
2068 }
2069
2070 func TestNew(t *testing.T) {
2071 tests := []struct {
2072 data, parity int
2073 err error
2074 }{
2075 {127, 127, nil},
2076 {128, 128, nil},
2077 {255, 1, nil},
2078 {255, 0, nil},
2079 {1, 0, nil},
2080 {65536, 65536, ErrMaxShardNum},
2081
2082 {0, 1, ErrInvShardNum},
2083 {1, -1, ErrInvShardNum},
2084 {65636, 1, ErrMaxShardNum},
2085
2086 // overflow causes r.Shards to be negative
2087 {256, int(^uint(0) >> 1), errInvalidRowSize},
2088 }
2089 for _, test := range tests {
2090 _, err := New(test.data, test.parity, testOptions()...)
2091 if err != test.err {
2092 t.Errorf("New(%v, %v): expected %v, got %v", test.data, test.parity, test.err, err)
2093 }
2094 }
2095 }
2096
2097 func TestSplitZero(t *testing.T) {
2098 data := make([]byte, 512)
2099 for _, opts := range testOpts() {
2100 ecctest, err := New(1, 0, opts...)
2101 if err != nil {
2102 t.Fatal(err)
2103 }
2104 _, err = ecctest.Split(data)
2105 if err != nil {
2106 t.Fatal(err)
2107 }
2108 }
2109 }
2110
2111 // Benchmark 10 data shards and 4 parity shards and 160MB data.
2112 func BenchmarkSplit10x4x160M(b *testing.B) {
2113 benchmarkSplit(b, 10, 4, 160*1024*1024)
2114 }
2115
2116 // Benchmark 5 data shards and 2 parity shards with 5MB data.
2117 func BenchmarkSplit5x2x5M(b *testing.B) {
2118 benchmarkSplit(b, 5, 2, 5*1024*1024)
2119 }
2120
2121 // Benchmark 1 data shards and 2 parity shards with 1MB data.
2122 func BenchmarkSplit10x2x1M(b *testing.B) {
2123 benchmarkSplit(b, 10, 2, 1024*1024)
2124 }
2125
2126 // Benchmark 10 data shards and 4 parity shards with 10MB data.
2127 func BenchmarkSplit10x4x10M(b *testing.B) {
2128 benchmarkSplit(b, 10, 4, 10*1024*1024)
2129 }
2130
2131 // Benchmark 50 data shards and 20 parity shards with 50MB data.
2132 func BenchmarkSplit50x20x50M(b *testing.B) {
2133 benchmarkSplit(b, 50, 20, 50*1024*1024)
2134 }
2135
2136 // Benchmark 17 data shards and 3 parity shards with 272MB data.
2137 func BenchmarkSplit17x3x272M(b *testing.B) {
2138 benchmarkSplit(b, 17, 3, 272*1024*1024)
2139 }
2140
2141 func benchmarkSplit(b *testing.B, shards, parity, dataSize int) {
2142 r, err := New(shards, parity, testOptions(WithAutoGoroutines(dataSize))...)
2143 if err != nil {
2144 b.Fatal(err)
2145 }
2146
2147 data := make([]byte, dataSize)
2148 b.ReportAllocs()
2149 b.ResetTimer()
2150 for i := 0; i < b.N; i++ {
2151 _, err = r.Split(data)
2152 if err != nil {
2153 b.Fatal(err)
2154 }
2155 }
2156 }
2157
2158 func benchmarkParallel(b *testing.B, dataShards, parityShards, shardSize int) {
2159 // Run max 1 goroutine per operation.
2160 r, err := New(dataShards, parityShards, testOptions(WithMaxGoroutines(1))...)
2161 if err != nil {
2162 b.Fatal(err)
2163 }
2164 c := runtime.GOMAXPROCS(0)
2165
2166 // Note that concurrency also affects total data size and will make caches less effective.
2167 if testing.Verbose() {
2168 b.Log("Total data:", (c*dataShards*shardSize)>>20, "MiB", "parity:", (c*parityShards*shardSize)>>20, "MiB")
2169 }
2170 // Create independent shards
2171 shardsCh := make(chan [][]byte, c)
2172 for range c {
2173 shards := r.(Extensions).AllocAligned(shardSize)
2174
2175 for s := range dataShards {
2176 fillRandom(shards[s])
2177 }
2178 shardsCh <- shards
2179 }
2180
2181 b.SetBytes(int64(shardSize * (dataShards + parityShards)))
2182 b.SetParallelism(c)
2183 b.ReportAllocs()
2184 b.ResetTimer()
2185
2186 b.RunParallel(func(pb *testing.PB) {
2187 for pb.Next() {
2188 shards := <-shardsCh
2189 err = r.Encode(shards)
2190 if err != nil {
2191 b.Fatal(err)
2192 }
2193 shardsCh <- shards
2194 }
2195 })
2196 }
2197
2198 func BenchmarkParallel_8x8x64K(b *testing.B) { benchmarkParallel(b, 8, 8, 64<<10) }
2199 func BenchmarkParallel_8x8x05M(b *testing.B) { benchmarkParallel(b, 8, 8, 512<<10) }
2200 func BenchmarkParallel_20x10x05M(b *testing.B) { benchmarkParallel(b, 20, 10, 512<<10) }
2201 func BenchmarkParallel_8x8x1M(b *testing.B) { benchmarkParallel(b, 8, 8, 1<<20) }
2202 func BenchmarkParallel_8x8x8M(b *testing.B) { benchmarkParallel(b, 8, 8, 8<<20) }
2203 func BenchmarkParallel_8x8x32M(b *testing.B) { benchmarkParallel(b, 8, 8, 32<<20) }
2204
2205 func BenchmarkParallel_8x3x1M(b *testing.B) { benchmarkParallel(b, 8, 3, 1<<20) }
2206 func BenchmarkParallel_8x4x1M(b *testing.B) { benchmarkParallel(b, 8, 4, 1<<20) }
2207 func BenchmarkParallel_8x5x1M(b *testing.B) { benchmarkParallel(b, 8, 5, 1<<20) }
2208
2209 func TestReentrant(t *testing.T) {
2210 for optN, o := range testOpts() {
2211 t.Run(fmt.Sprintf("opt-%d", optN), func(t *testing.T) {
2212 parallelIfNotShort(t)
2213 for _, size := range testSizes() {
2214 data, parity := size[0], size[1]
2215 rng := rand.New(rand.NewSource(0xabadc0cac01a))
2216 t.Run(fmt.Sprintf("size-%dx%d", data, parity), func(t *testing.T) {
2217 perShard := 1024 + 1
2218 if testing.Short() {
2219 perShard = 256 + 1
2220 }
2221 r, err := New(data, parity, testOptions(o...)...)
2222 if err != nil {
2223 t.Fatal(err)
2224 }
2225 x := r.(Extensions)
2226 if want, got := data, x.DataShards(); want != got {
2227 t.Errorf("DataShards returned %d, want %d", got, want)
2228 }
2229 if want, got := parity, x.ParityShards(); want != got {
2230 t.Errorf("ParityShards returned %d, want %d", got, want)
2231 }
2232 if want, got := parity+data, x.TotalShards(); want != got {
2233 t.Errorf("TotalShards returned %d, want %d", got, want)
2234 }
2235 mul := x.ShardSizeMultiple()
2236 if mul <= 0 {
2237 t.Fatalf("Got unexpected ShardSizeMultiple: %d", mul)
2238 }
2239 perShard = ((perShard + mul - 1) / mul) * mul
2240 runs := 10
2241 if testing.Short() {
2242 runs = 2
2243 }
2244 for i := 0; i < runs; i++ {
2245 shards := AllocAligned(data+parity, perShard)
2246 for s := range data {
2247 rng.Read(shards[s])
2248 }
2249 err = r.Encode(shards)
2250 if err != nil {
2251 t.Fatal(err)
2252 }
2253 ok, err := r.Verify(shards)
2254 if err != nil {
2255 t.Fatal(err)
2256 }
2257 if !ok {
2258 t.Fatal("Verification failed")
2259 }
2260
2261 if parity == 0 {
2262 // Check that Reconstruct and ReconstructData do nothing
2263 err = r.ReconstructData(shards)
2264 if err != nil {
2265 t.Fatal(err)
2266 }
2267 err = r.Reconstruct(shards)
2268 if err != nil {
2269 t.Fatal(err)
2270 }
2271
2272 // Skip integrity checks
2273 continue
2274 }
2275
2276 // Delete one in data
2277 idx := rng.Intn(data)
2278 want := shards[idx]
2279 shards[idx] = nil
2280
2281 err = r.ReconstructData(shards)
2282 if err != nil {
2283 t.Fatal(err)
2284 }
2285 if !bytes.Equal(shards[idx], want) {
2286 t.Fatal("did not ReconstructData correctly")
2287 }
2288
2289 // Delete one randomly
2290 idx = rng.Intn(data + parity)
2291 want = shards[idx]
2292 shards[idx] = nil
2293 err = r.Reconstruct(shards)
2294 if err != nil {
2295 t.Fatal(err)
2296 }
2297 if !bytes.Equal(shards[idx], want) {
2298 t.Fatal("did not Reconstruct correctly")
2299 }
2300
2301 err = r.Encode(make([][]byte, 1))
2302 if err != ErrTooFewShards {
2303 t.Errorf("expected %v, got %v", ErrTooFewShards, err)
2304 }
2305
2306 // Make one too short.
2307 shards[idx] = shards[idx][:perShard-1]
2308 err = r.Encode(shards)
2309 if err != ErrShardSize {
2310 t.Errorf("expected %v, got %v", ErrShardSize, err)
2311 }
2312 }
2313 })
2314 }
2315 })
2316 }
2317 }
2318
2319 func TestRSReconstructSome(t *testing.T) {
2320 const (
2321 dataShards = 3
2322 parityShards = 2
2323 sectorSize = 1 << 20 // 1 MiB
2324 )
2325 stripedSplit := func(data []byte, dataShards [][]byte) {
2326 const leafSize = 64
2327 buf := bytes.NewBuffer(data)
2328 for off := 0; buf.Len() > 0; off += leafSize {
2329 for _, shard := range dataShards {
2330 copy(shard[off:], buf.Next(leafSize))
2331 }
2332 }
2333 }
2334
2335 // prepare random data
2336 buf := make([]byte, sectorSize*dataShards)
2337 rand.Read(buf)
2338
2339 // prepare shards
2340 shards := make([][]byte, dataShards+parityShards)
2341 for i := range shards {
2342 shards[i] = make([]byte, sectorSize)
2343 }
2344
2345 for i, o := range testOpts() {
2346 toFill := []int{0, 1, 2, 3, 4}
2347 t.Run(fmt.Sprintf("options %d", i), func(t *testing.T) {
2348 // Shuffle the indices to fill
2349 rand.Shuffle(len(toFill), func(i, j int) {
2350 toFill[i], toFill[j] = toFill[j], toFill[i]
2351 })
2352 stripedSplit(buf, shards[:dataShards])
2353 // encode shards
2354 enc, err := New(dataShards, parityShards, o...)
2355 if err != nil {
2356 t.Fatal(err)
2357 }
2358 err = enc.Encode(shards)
2359 if err != nil {
2360 t.Fatal(err)
2361 }
2362
2363 for _, required := range [][]bool{
2364 {false, false, false, false, true},
2365 {false, false, false, true, true},
2366 {false, false, true, false, true},
2367 {false, false, true, true, true},
2368 {false, true, false, false, true},
2369 {false, true, false, true, true},
2370 {false, true, true, false, true},
2371 {false, true, true, true, true},
2372 {true, false, false, false, true},
2373 {true, false, false, true, true},
2374 {true, false, true, false, true},
2375 {true, false, true, true, true},
2376 {true, true, false, false, true},
2377 {true, true, false, true, true},
2378 {true, true, true, false, true},
2379 {true, true, true, true, true},
2380
2381 {false, false, false, false, false},
2382 {false, false, false, true, false},
2383 {false, false, true, false, false},
2384 {false, false, true, true, false},
2385 {false, true, false, false, false},
2386 {false, true, false, true, false},
2387 {false, true, true, false, false},
2388 {false, true, true, true, false},
2389 {true, false, false, false, false},
2390 {true, false, false, true, false},
2391 {true, false, true, false, false},
2392 {true, false, true, true, false},
2393 {true, true, false, false, false},
2394 {true, true, false, true, false},
2395 {true, true, true, false, false},
2396 {true, true, true, true, false},
2397 } {
2398 downloaded := make([][]byte, len(shards))
2399 downloaded[toFill[0]] = append([]byte{}, shards[toFill[0]]...)
2400 downloaded[toFill[1]] = append([]byte{}, shards[toFill[1]]...)
2401 downloaded[toFill[2]] = append([]byte{}, shards[toFill[2]]...)
2402 err = enc.ReconstructSome(downloaded, required)
2403 if err != nil {
2404 t.Fatal(err)
2405 }
2406 for i, shard := range downloaded {
2407 if shard == nil && required[i] {
2408 t.Errorf("shard idx %d should not be nil", i)
2409 }
2410 if !required[i] && shard == nil {
2411 // Ignore, if it wasn't reconstructed
2412 continue
2413 }
2414 if len(shard) != sectorSize {
2415 t.Errorf("shard idx %d has wrong size: %d, want %d", i, len(shard), sectorSize)
2416 }
2417 if !bytes.Equal(shard, shards[i]) {
2418 t.Errorf("shard idx %d does not match original", i)
2419 }
2420 }
2421 }
2422 })
2423 }
2424 }