| 1 |
package reedsolomon |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
// TestDecodeIdx_InvalidExpectInputLength tests that DecodeIdx returns an error |
| 12 |
// when expectInput length doesn't match totalShards |
| 13 |
func TestDecodeIdx_InvalidExpectInputLength(t *testing.T) { |
| 14 |
enc, err := New(5, 3, testOptions()...) |
| 15 |
if err != nil { |
| 16 |
t.Fatal(err) |
| 17 |
} |
| 18 |
r := enc.(*reedSolomon) |
| 19 |
|
| 20 |
dst := make([][]byte, 8) |
| 21 |
input := make([][]byte, 8) |
| 22 |
wrongExpectInput := make([]bool, 7) // Should be 8 (5+3) |
| 23 |
|
| 24 |
err = r.DecodeIdx(dst, wrongExpectInput, input) |
| 25 |
if err == nil { |
| 26 |
t.Fatal("expected error for wrong expectInput length") |
| 27 |
} |
| 28 |
if !errors.Is(err, ErrInvalidInput) { |
| 29 |
t.Errorf("expected ErrInvalidInput, got: %v", err) |
| 30 |
} |
| 31 |
if !strings.Contains(err.Error(), "expectInput length") { |
| 32 |
t.Errorf("error should mention expectInput length: %v", err) |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
// TestDecodeIdx_WrongSliceLengths tests that DecodeIdx returns an error |
| 37 |
// when dst or input don't have the correct length |
| 38 |
func TestDecodeIdx_WrongSliceLengths(t *testing.T) { |
| 39 |
enc, err := New(5, 3, testOptions()...) |
| 40 |
if err != nil { |
| 41 |
t.Fatal(err) |
| 42 |
} |
| 43 |
r := enc.(*reedSolomon) |
| 44 |
|
| 45 |
expectInput := make([]bool, 8) |
| 46 |
for i := range 5 { |
| 47 |
expectInput[i] = true |
| 48 |
} |
| 49 |
|
| 50 |
// Test wrong dst length |
| 51 |
dst := make([][]byte, 7) |
| 52 |
input := make([][]byte, 8) |
| 53 |
err = r.DecodeIdx(dst, expectInput, input) |
| 54 |
if err == nil { |
| 55 |
t.Fatal("expected error for wrong dst length") |
| 56 |
} |
| 57 |
if !errors.Is(err, ErrInvalidInput) { |
| 58 |
t.Errorf("expected ErrInvalidInput for wrong dst length, got: %v", err) |
| 59 |
} |
| 60 |
|
| 61 |
// Test wrong input length |
| 62 |
dst = make([][]byte, 8) |
| 63 |
input = make([][]byte, 7) |
| 64 |
err = r.DecodeIdx(dst, expectInput, input) |
| 65 |
if err == nil { |
| 66 |
t.Fatal("expected error for wrong input length") |
| 67 |
} |
| 68 |
if !errors.Is(err, ErrInvalidInput) { |
| 69 |
t.Errorf("expected ErrInvalidInput for wrong input length, got: %v", err) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// TestDecodeIdx_DestinationAlreadyFilled tests that DecodeIdx returns an error |
| 74 |
// when trying to decode into an already filled shard |
| 75 |
func TestDecodeIdx_DestinationAlreadyFilled(t *testing.T) { |
| 76 |
enc, err := New(5, 3, testOptions()...) |
| 77 |
if err != nil { |
| 78 |
t.Fatal(err) |
| 79 |
} |
| 80 |
r := enc.(*reedSolomon) |
| 81 |
|
| 82 |
dst := make([][]byte, 8) |
| 83 |
input := make([][]byte, 8) |
| 84 |
expectInput := make([]bool, 8) |
| 85 |
for i := range 5 { |
| 86 |
expectInput[i] = true |
| 87 |
} |
| 88 |
|
| 89 |
// Mark shard 7 as expected but also provide it in dst |
| 90 |
expectInput[7] = true |
| 91 |
dst[7] = make([]byte, 100) |
| 92 |
|
| 93 |
err = r.DecodeIdx(dst, expectInput, input) |
| 94 |
if err == nil { |
| 95 |
t.Fatal("expected error for already filled destination") |
| 96 |
} |
| 97 |
if !errors.Is(err, ErrInvalidInput) { |
| 98 |
t.Errorf("expected ErrInvalidInput, got: %v", err) |
| 99 |
} |
| 100 |
if !strings.Contains(err.Error(), "should be nil") { |
| 101 |
t.Errorf("error should mention dst should be nil: %v", err) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// TestDecodeIdx_UnexpectedInput tests that DecodeIdx returns an error |
| 106 |
// when input is provided at an index not marked in expectInput |
| 107 |
func TestDecodeIdx_UnexpectedInput(t *testing.T) { |
| 108 |
enc, err := New(5, 3, testOptions()...) |
| 109 |
if err != nil { |
| 110 |
t.Fatal(err) |
| 111 |
} |
| 112 |
r := enc.(*reedSolomon) |
| 113 |
|
| 114 |
dst := make([][]byte, 8) |
| 115 |
input := make([][]byte, 8) |
| 116 |
expectInput := make([]bool, 8) |
| 117 |
for i := range 5 { |
| 118 |
expectInput[i] = true |
| 119 |
} |
| 120 |
|
| 121 |
// Provide input at index 7 which is not marked as expected |
| 122 |
input[7] = make([]byte, 100) |
| 123 |
input[0] = make([]byte, 100) |
| 124 |
dst[6] = make([]byte, 100) // Decode into shard 6 |
| 125 |
|
| 126 |
err = r.DecodeIdx(dst, expectInput, input) |
| 127 |
if err == nil { |
| 128 |
t.Fatal("expected error for unexpected input") |
| 129 |
} |
| 130 |
if !errors.Is(err, ErrInvalidInput) { |
| 131 |
t.Errorf("expected ErrInvalidInput, got: %v", err) |
| 132 |
} |
| 133 |
if !strings.Contains(err.Error(), "unexpected input") { |
| 134 |
t.Errorf("error should mention unexpected input: %v", err) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// TestDecodeIdx_TooFewShards tests that DecodeIdx returns an error |
| 139 |
// when there are too few shards marked in expectInput |
| 140 |
func TestDecodeIdx_TooFewShards(t *testing.T) { |
| 141 |
enc, err := New(5, 3, testOptions()...) |
| 142 |
if err != nil { |
| 143 |
t.Fatal(err) |
| 144 |
} |
| 145 |
r := enc.(*reedSolomon) |
| 146 |
|
| 147 |
dst := make([][]byte, 8) |
| 148 |
input := make([][]byte, 8) |
| 149 |
expectInput := make([]bool, 8) |
| 150 |
// Only mark 4 shards as expected (need at least 5) |
| 151 |
for i := range 4 { |
| 152 |
expectInput[i] = true |
| 153 |
} |
| 154 |
|
| 155 |
err = r.DecodeIdx(dst, expectInput, input) |
| 156 |
if !errors.Is(err, ErrTooFewShards) { |
| 157 |
t.Errorf("expected ErrTooFewShards, got %v", err) |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// TestDecodeIdx_MismatchedBufferSizes tests that DecodeIdx returns an error |
| 162 |
// when buffer sizes don't match |
| 163 |
func TestDecodeIdx_MismatchedBufferSizes(t *testing.T) { |
| 164 |
enc, err := New(5, 3, testOptions()...) |
| 165 |
if err != nil { |
| 166 |
t.Fatal(err) |
| 167 |
} |
| 168 |
r := enc.(*reedSolomon) |
| 169 |
|
| 170 |
dst := make([][]byte, 8) |
| 171 |
input := make([][]byte, 8) |
| 172 |
expectInput := make([]bool, 8) |
| 173 |
for i := range 5 { |
| 174 |
expectInput[i] = true |
| 175 |
} |
| 176 |
|
| 177 |
// Set up mismatched sizes - reconstruct shards 5 and 6 with different sizes |
| 178 |
dst[5] = make([]byte, 100) |
| 179 |
dst[6] = make([]byte, 200) // Different size |
| 180 |
input[0] = make([]byte, 100) // Provide input at index 0 |
| 181 |
|
| 182 |
err = r.DecodeIdx(dst, expectInput, input) |
| 183 |
if !errors.Is(err, ErrInvalidShardSize) { |
| 184 |
t.Errorf("expected ErrInvalidShardSize, got %v", err) |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
// TestDecodeIdx_MergeMode tests merging mode (expectInput == nil) |
| 189 |
func TestDecodeIdx_MergeMode(t *testing.T) { |
| 190 |
enc, err := New(5, 3, testOptions()...) |
| 191 |
if err != nil { |
| 192 |
t.Fatal(err) |
| 193 |
} |
| 194 |
r := enc.(*reedSolomon) |
| 195 |
|
| 196 |
dst := make([][]byte, 8) |
| 197 |
input := make([][]byte, 8) |
| 198 |
|
| 199 |
// Set up data to merge |
| 200 |
dst[0] = []byte{1, 2, 3, 4} |
| 201 |
input[0] = []byte{5, 6, 7, 8} |
| 202 |
|
| 203 |
dst[3] = []byte{10, 20, 30, 40} |
| 204 |
input[3] = []byte{50, 60, 70, 80} |
| 205 |
|
| 206 |
err = r.DecodeIdx(dst, nil, input) |
| 207 |
if err != nil { |
| 208 |
t.Fatal(err) |
| 209 |
} |
| 210 |
|
| 211 |
// Check XOR results |
| 212 |
expected0 := []byte{1 ^ 5, 2 ^ 6, 3 ^ 7, 4 ^ 8} |
| 213 |
if !bytes.Equal(dst[0], expected0) { |
| 214 |
t.Errorf("merge failed for shard 0: got %v, expected %v", dst[0], expected0) |
| 215 |
} |
| 216 |
|
| 217 |
expected3 := []byte{10 ^ 50, 20 ^ 60, 30 ^ 70, 40 ^ 80} |
| 218 |
if !bytes.Equal(dst[3], expected3) { |
| 219 |
t.Errorf("merge failed for shard 3: got %v, expected %v", dst[3], expected3) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
// TestDecodeIdx_ProgressiveDecode tests progressive decoding of shards |
| 224 |
func TestDecodeIdx_ProgressiveDecode(t *testing.T) { |
| 225 |
enc, err := New(5, 3, testOptions()...) |
| 226 |
if err != nil { |
| 227 |
t.Fatal(err) |
| 228 |
} |
| 229 |
r := enc.(*reedSolomon) |
| 230 |
|
| 231 |
// Create and encode data |
| 232 |
shards := make([][]byte, 8) |
| 233 |
for i := range 5 { |
| 234 |
shards[i] = make([]byte, 100) |
| 235 |
fillRandom(shards[i]) |
| 236 |
} |
| 237 |
for i := 5; i < 8; i++ { |
| 238 |
shards[i] = make([]byte, 100) |
| 239 |
} |
| 240 |
|
| 241 |
err = enc.Encode(shards) |
| 242 |
if err != nil { |
| 243 |
t.Fatal(err) |
| 244 |
} |
| 245 |
|
| 246 |
// Save originals |
| 247 |
originals := make([][]byte, 8) |
| 248 |
for i := range shards { |
| 249 |
originals[i] = make([]byte, 100) |
| 250 |
copy(originals[i], shards[i]) |
| 251 |
} |
| 252 |
|
| 253 |
// Progressive decode: reconstruct shards 0 and 7 using shards 1-5 |
| 254 |
dst := make([][]byte, 8) |
| 255 |
dst[0] = make([]byte, 100) // Reconstruct data shard 0 |
| 256 |
dst[7] = make([]byte, 100) // Reconstruct parity shard 7 |
| 257 |
|
| 258 |
expectInput := make([]bool, 8) |
| 259 |
for i := 1; i <= 5; i++ { |
| 260 |
expectInput[i] = true |
| 261 |
} |
| 262 |
|
| 263 |
// First call with shards 1-3 |
| 264 |
input := make([][]byte, 8) |
| 265 |
for i := 1; i <= 3; i++ { |
| 266 |
input[i] = shards[i] |
| 267 |
} |
| 268 |
|
| 269 |
err = r.DecodeIdx(dst, expectInput, input) |
| 270 |
if err != nil { |
| 271 |
t.Fatal(err) |
| 272 |
} |
| 273 |
|
| 274 |
// Second call with shards 4-5 |
| 275 |
input2 := make([][]byte, 8) |
| 276 |
for i := 4; i <= 5; i++ { |
| 277 |
input2[i] = shards[i] |
| 278 |
} |
| 279 |
|
| 280 |
err = r.DecodeIdx(dst, expectInput, input2) |
| 281 |
if err != nil { |
| 282 |
t.Fatal(err) |
| 283 |
} |
| 284 |
|
| 285 |
// Verify reconstructed shards |
| 286 |
if !bytes.Equal(dst[0], originals[0]) { |
| 287 |
t.Error("data shard 0 reconstruction failed") |
| 288 |
} |
| 289 |
if !bytes.Equal(dst[7], originals[7]) { |
| 290 |
t.Error("parity shard 7 reconstruction failed") |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
// TestDecodeIdx_IntegrationWithFullCycle tests full encode/decode cycle |
| 295 |
func TestDecodeIdx_IntegrationWithFullCycle(t *testing.T) { |
| 296 |
testCases := []struct { |
| 297 |
dataShards int |
| 298 |
parityShards int |
| 299 |
}{ |
| 300 |
{5, 3}, |
| 301 |
{10, 4}, |
| 302 |
{2, 2}, |
| 303 |
{17, 3}, |
| 304 |
} |
| 305 |
|
| 306 |
for _, tc := range testCases { |
| 307 |
t.Run(fmt.Sprintf("%d+%d", tc.dataShards, tc.parityShards), func(t *testing.T) { |
| 308 |
enc, err := New(tc.dataShards, tc.parityShards, testOptions()...) |
| 309 |
if err != nil { |
| 310 |
t.Fatal(err) |
| 311 |
} |
| 312 |
r := enc.(*reedSolomon) |
| 313 |
|
| 314 |
totalShards := tc.dataShards + tc.parityShards |
| 315 |
shardSize := 100 |
| 316 |
|
| 317 |
// Create and encode data |
| 318 |
shards := make([][]byte, totalShards) |
| 319 |
for i := 0; i < tc.dataShards; i++ { |
| 320 |
shards[i] = make([]byte, shardSize) |
| 321 |
fillRandom(shards[i]) |
| 322 |
} |
| 323 |
for i := tc.dataShards; i < totalShards; i++ { |
| 324 |
shards[i] = make([]byte, shardSize) |
| 325 |
} |
| 326 |
|
| 327 |
err = enc.Encode(shards) |
| 328 |
if err != nil { |
| 329 |
t.Fatal(err) |
| 330 |
} |
| 331 |
|
| 332 |
// Save original for verification |
| 333 |
originalShards := make([][]byte, totalShards) |
| 334 |
for i := range shards { |
| 335 |
originalShards[i] = make([]byte, shardSize) |
| 336 |
copy(originalShards[i], shards[i]) |
| 337 |
} |
| 338 |
|
| 339 |
// Test reconstruction of ALL shards (both data and parity) |
| 340 |
for targetShard := range totalShards { |
| 341 |
// Prepare dst with only the target shard to reconstruct |
| 342 |
dst := make([][]byte, totalShards) |
| 343 |
dst[targetShard] = make([]byte, shardSize) |
| 344 |
|
| 345 |
// Mark first dataShards shards as expected (excluding target if it's one of them) |
| 346 |
expectInput := make([]bool, totalShards) |
| 347 |
count := 0 |
| 348 |
for i := 0; i < totalShards && count < tc.dataShards; i++ { |
| 349 |
if i != targetShard { |
| 350 |
expectInput[i] = true |
| 351 |
count++ |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
// Provide all expected inputs in one call |
| 356 |
input := make([][]byte, totalShards) |
| 357 |
for i := range totalShards { |
| 358 |
if expectInput[i] { |
| 359 |
input[i] = shards[i] |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
err = r.DecodeIdx(dst, expectInput, input) |
| 364 |
if err != nil { |
| 365 |
t.Fatalf("Failed to decode shard %d: %v", targetShard, err) |
| 366 |
} |
| 367 |
|
| 368 |
// Verify |
| 369 |
if !bytes.Equal(dst[targetShard], originalShards[targetShard]) { |
| 370 |
t.Errorf("Shard %d reconstruction mismatch", targetShard) |
| 371 |
} |
| 372 |
} |
| 373 |
}) |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
// TestDecodeIdx_MultipleShards tests reconstructing multiple shards in one call |
| 378 |
func TestDecodeIdx_MultipleShards(t *testing.T) { |
| 379 |
enc, err := New(5, 3, testOptions()...) |
| 380 |
if err != nil { |
| 381 |
t.Fatal(err) |
| 382 |
} |
| 383 |
r := enc.(*reedSolomon) |
| 384 |
|
| 385 |
// Create and encode data |
| 386 |
shards := make([][]byte, 8) |
| 387 |
for i := range 5 { |
| 388 |
shards[i] = make([]byte, 100) |
| 389 |
fillRandom(shards[i]) |
| 390 |
} |
| 391 |
for i := 5; i < 8; i++ { |
| 392 |
shards[i] = make([]byte, 100) |
| 393 |
} |
| 394 |
|
| 395 |
err = enc.Encode(shards) |
| 396 |
if err != nil { |
| 397 |
t.Fatal(err) |
| 398 |
} |
| 399 |
|
| 400 |
// Save originals |
| 401 |
originals := make([][]byte, 8) |
| 402 |
for i := range shards { |
| 403 |
originals[i] = make([]byte, 100) |
| 404 |
copy(originals[i], shards[i]) |
| 405 |
} |
| 406 |
|
| 407 |
// Reconstruct shards 0, 2, and 7 using shards 1, 3, 4, 5, 6 |
| 408 |
dst := make([][]byte, 8) |
| 409 |
dst[0] = make([]byte, 100) // data shard |
| 410 |
dst[2] = make([]byte, 100) // data shard |
| 411 |
dst[7] = make([]byte, 100) // parity shard |
| 412 |
|
| 413 |
expectInput := make([]bool, 8) |
| 414 |
expectInput[1] = true |
| 415 |
expectInput[3] = true |
| 416 |
expectInput[4] = true |
| 417 |
expectInput[5] = true |
| 418 |
expectInput[6] = true |
| 419 |
|
| 420 |
input := make([][]byte, 8) |
| 421 |
input[1] = shards[1] |
| 422 |
input[3] = shards[3] |
| 423 |
input[4] = shards[4] |
| 424 |
input[5] = shards[5] |
| 425 |
input[6] = shards[6] |
| 426 |
|
| 427 |
err = r.DecodeIdx(dst, expectInput, input) |
| 428 |
if err != nil { |
| 429 |
t.Fatal(err) |
| 430 |
} |
| 431 |
|
| 432 |
// Verify all reconstructed shards |
| 433 |
if !bytes.Equal(dst[0], originals[0]) { |
| 434 |
t.Error("data shard 0 reconstruction failed") |
| 435 |
} |
| 436 |
if !bytes.Equal(dst[2], originals[2]) { |
| 437 |
t.Error("data shard 2 reconstruction failed") |
| 438 |
} |
| 439 |
if !bytes.Equal(dst[7], originals[7]) { |
| 440 |
t.Error("parity shard 7 reconstruction failed") |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
// TestDecodeIdx_MergeTwoPartialDecodings tests merging partial decodings |
| 445 |
func TestDecodeIdx_MergeTwoPartialDecodings(t *testing.T) { |
| 446 |
enc, err := New(5, 3, testOptions()...) |
| 447 |
if err != nil { |
| 448 |
t.Fatal(err) |
| 449 |
} |
| 450 |
r := enc.(*reedSolomon) |
| 451 |
|
| 452 |
// Create and encode data |
| 453 |
shards := make([][]byte, 8) |
| 454 |
for i := range 5 { |
| 455 |
shards[i] = make([]byte, 100) |
| 456 |
fillRandom(shards[i]) |
| 457 |
} |
| 458 |
for i := 5; i < 8; i++ { |
| 459 |
shards[i] = make([]byte, 100) |
| 460 |
} |
| 461 |
|
| 462 |
err = enc.Encode(shards) |
| 463 |
if err != nil { |
| 464 |
t.Fatal(err) |
| 465 |
} |
| 466 |
|
| 467 |
// Save original |
| 468 |
original := make([]byte, 100) |
| 469 |
copy(original, shards[0]) |
| 470 |
|
| 471 |
// First partial decode using shards 1-3 |
| 472 |
dst1 := make([][]byte, 8) |
| 473 |
dst1[0] = make([]byte, 100) |
| 474 |
|
| 475 |
expectInput1 := make([]bool, 8) |
| 476 |
for i := 1; i <= 5; i++ { |
| 477 |
expectInput1[i] = true |
| 478 |
} |
| 479 |
|
| 480 |
input1 := make([][]byte, 8) |
| 481 |
for i := 1; i <= 3; i++ { |
| 482 |
input1[i] = shards[i] |
| 483 |
} |
| 484 |
|
| 485 |
err = r.DecodeIdx(dst1, expectInput1, input1) |
| 486 |
if err != nil { |
| 487 |
t.Fatal(err) |
| 488 |
} |
| 489 |
|
| 490 |
// Second partial decode using shards 4-5 |
| 491 |
dst2 := make([][]byte, 8) |
| 492 |
dst2[0] = make([]byte, 100) |
| 493 |
|
| 494 |
input2 := make([][]byte, 8) |
| 495 |
for i := 4; i <= 5; i++ { |
| 496 |
input2[i] = shards[i] |
| 497 |
} |
| 498 |
|
| 499 |
err = r.DecodeIdx(dst2, expectInput1, input2) |
| 500 |
if err != nil { |
| 501 |
t.Fatal(err) |
| 502 |
} |
| 503 |
|
| 504 |
// Merge the two partial decodings |
| 505 |
err = r.DecodeIdx(dst1, nil, dst2) |
| 506 |
if err != nil { |
| 507 |
t.Fatal(err) |
| 508 |
} |
| 509 |
|
| 510 |
// Verify merged result |
| 511 |
if !bytes.Equal(dst1[0], original) { |
| 512 |
t.Error("merged decoding does not match original") |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
// TestDecodeIdx_AllOptions tests DecodeIdx with all encoder option combinations |
| 517 |
func TestDecodeIdx_AllOptions(t *testing.T) { |
| 518 |
opts := [][]Option{ |
| 519 |
testOptions(), |
| 520 |
testOptions(WithLeopardGF(true)), |
| 521 |
testOptions(WithLeopardGF16(true)), |
| 522 |
testOptions(WithJerasureMatrix()), |
| 523 |
testOptions(WithCauchyMatrix()), |
| 524 |
testOptions(WithFastOneParityMatrix()), |
| 525 |
testOptions(WithInversionCache(false)), |
| 526 |
testOptions(WithMaxGoroutines(1)), |
| 527 |
testOptions(WithMaxGoroutines(8)), |
| 528 |
} |
| 529 |
|
| 530 |
testCases := []struct { |
| 531 |
dataShards int |
| 532 |
parityShards int |
| 533 |
shardSize int |
| 534 |
}{ |
| 535 |
{5, 3, 256}, // Use power of 2 for compatibility |
| 536 |
{10, 4, 256}, |
| 537 |
{17, 3, 256}, |
| 538 |
{3, 2, 256}, |
| 539 |
{2, 1, 256}, |
| 540 |
{8, 8, 256}, |
| 541 |
} |
| 542 |
|
| 543 |
for optIdx, opts := range opts { |
| 544 |
for tcIdx, tc := range testCases { |
| 545 |
t.Run(fmt.Sprintf("opts_%d_case_%d_%d+%d", optIdx, tcIdx, tc.dataShards, tc.parityShards), func(t *testing.T) { |
| 546 |
enc, err := New(tc.dataShards, tc.parityShards, opts...) |
| 547 |
if err != nil { |
| 548 |
t.Skip("encoder creation failed:", err) |
| 549 |
} |
| 550 |
|
| 551 |
// Check if DecodeIdx is supported |
| 552 |
ext, ok := enc.(Extensions) |
| 553 |
if !ok { |
| 554 |
t.Skip("Extensions interface not supported") |
| 555 |
} |
| 556 |
|
| 557 |
// Test basic DecodeIdx functionality |
| 558 |
totalShards := tc.dataShards + tc.parityShards |
| 559 |
|
| 560 |
// Create and encode test data |
| 561 |
shards := make([][]byte, totalShards) |
| 562 |
for i := range shards { |
| 563 |
shards[i] = make([]byte, tc.shardSize) |
| 564 |
} |
| 565 |
|
| 566 |
// Fill data shards with random data |
| 567 |
for i := 0; i < tc.dataShards; i++ { |
| 568 |
fillRandom(shards[i]) |
| 569 |
} |
| 570 |
|
| 571 |
err = enc.Encode(shards) |
| 572 |
if err != nil { |
| 573 |
t.Fatal("encode failed:", err) |
| 574 |
} |
| 575 |
|
| 576 |
// Save originals |
| 577 |
originals := make([][]byte, totalShards) |
| 578 |
for i := range shards { |
| 579 |
originals[i] = make([]byte, len(shards[i])) |
| 580 |
copy(originals[i], shards[i]) |
| 581 |
} |
| 582 |
|
| 583 |
// Test reconstruction of data shards |
| 584 |
t.Run("data_reconstruction", func(t *testing.T) { |
| 585 |
// Remove some data shards |
| 586 |
damaged := make([][]byte, totalShards) |
| 587 |
copy(damaged, shards) |
| 588 |
|
| 589 |
damaged[1] = nil |
| 590 |
if tc.dataShards > 3 { |
| 591 |
damaged[tc.dataShards-1] = nil |
| 592 |
} |
| 593 |
|
| 594 |
// Set up DecodeIdx call |
| 595 |
dst := make([][]byte, totalShards) |
| 596 |
dst[1] = make([]byte, tc.shardSize) |
| 597 |
if tc.dataShards > 3 { |
| 598 |
dst[tc.dataShards-1] = make([]byte, tc.shardSize) |
| 599 |
} |
| 600 |
|
| 601 |
expectInput := make([]bool, totalShards) |
| 602 |
input := make([][]byte, totalShards) |
| 603 |
|
| 604 |
// Use first tc.dataShards available shards as input |
| 605 |
inputCount := 0 |
| 606 |
for i := 0; i < totalShards && inputCount < tc.dataShards; i++ { |
| 607 |
if damaged[i] != nil { |
| 608 |
expectInput[i] = true |
| 609 |
input[i] = damaged[i] |
| 610 |
inputCount++ |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
// Call DecodeIdx |
| 615 |
err = ext.DecodeIdx(dst, expectInput, input) |
| 616 |
if err != nil { |
| 617 |
// Check if this is a "not supported" error (leopard cases) |
| 618 |
if errors.Is(err, ErrNotSupported) { |
| 619 |
t.Skip("DecodeIdx not supported for this encoder type") |
| 620 |
} |
| 621 |
t.Fatal("DecodeIdx failed:", err) |
| 622 |
} |
| 623 |
|
| 624 |
// Verify reconstruction |
| 625 |
if dst[1] != nil && !bytes.Equal(dst[1], originals[1]) { |
| 626 |
t.Error("data shard 1 reconstruction failed") |
| 627 |
} |
| 628 |
if tc.dataShards > 3 && dst[tc.dataShards-1] != nil && !bytes.Equal(dst[tc.dataShards-1], originals[tc.dataShards-1]) { |
| 629 |
t.Error("data shard reconstruction failed") |
| 630 |
} |
| 631 |
}) |
| 632 |
|
| 633 |
// Test reconstruction of parity shards (if supported) |
| 634 |
t.Run("parity_reconstruction", func(t *testing.T) { |
| 635 |
if tc.parityShards == 0 { |
| 636 |
t.Skip("no parity shards") |
| 637 |
} |
| 638 |
|
| 639 |
// Remove a parity shard |
| 640 |
damaged := make([][]byte, totalShards) |
| 641 |
copy(damaged, shards) |
| 642 |
|
| 643 |
parityIdx := tc.dataShards |
| 644 |
damaged[parityIdx] = nil |
| 645 |
|
| 646 |
// Set up DecodeIdx call |
| 647 |
dst := make([][]byte, totalShards) |
| 648 |
dst[parityIdx] = make([]byte, tc.shardSize) |
| 649 |
|
| 650 |
expectInput := make([]bool, totalShards) |
| 651 |
input := make([][]byte, totalShards) |
| 652 |
|
| 653 |
// Use first tc.dataShards shards as input |
| 654 |
for i := 0; i < tc.dataShards; i++ { |
| 655 |
expectInput[i] = true |
| 656 |
input[i] = shards[i] |
| 657 |
} |
| 658 |
|
| 659 |
// Call DecodeIdx |
| 660 |
err = ext.DecodeIdx(dst, expectInput, input) |
| 661 |
if err != nil { |
| 662 |
if errors.Is(err, ErrNotSupported) { |
| 663 |
t.Skip("DecodeIdx not supported for this encoder type") |
| 664 |
} |
| 665 |
t.Fatal("DecodeIdx failed:", err) |
| 666 |
} |
| 667 |
|
| 668 |
// Verify reconstruction |
| 669 |
if !bytes.Equal(dst[parityIdx], originals[parityIdx]) { |
| 670 |
t.Error("parity shard reconstruction failed") |
| 671 |
} |
| 672 |
}) |
| 673 |
|
| 674 |
// Test progressive reconstruction |
| 675 |
t.Run("progressive_reconstruction", func(t *testing.T) { |
| 676 |
if tc.dataShards < 4 { |
| 677 |
t.Skip("need at least 4 data shards for progressive test") |
| 678 |
} |
| 679 |
|
| 680 |
// Remove one data shard for reconstruction |
| 681 |
dst := make([][]byte, totalShards) |
| 682 |
dst[1] = make([]byte, tc.shardSize) |
| 683 |
|
| 684 |
expectInput := make([]bool, totalShards) |
| 685 |
// Mark first tc.dataShards available shards as expected |
| 686 |
inputCount := 0 |
| 687 |
for i := 0; i < totalShards && inputCount < tc.dataShards; i++ { |
| 688 |
if i != 1 { // Skip the one we're reconstructing |
| 689 |
expectInput[i] = true |
| 690 |
inputCount++ |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
// First call - provide half the inputs (but at least 1) |
| 695 |
input1 := make([][]byte, totalShards) |
| 696 |
provided1 := 0 |
| 697 |
target1 := max(tc.dataShards/2, 1) |
| 698 |
for i := 0; i < totalShards && provided1 < target1; i++ { |
| 699 |
if expectInput[i] { |
| 700 |
input1[i] = shards[i] |
| 701 |
provided1++ |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
err = ext.DecodeIdx(dst, expectInput, input1) |
| 706 |
if err != nil { |
| 707 |
if errors.Is(err, ErrNotSupported) { |
| 708 |
t.Skip("DecodeIdx not supported for this encoder type") |
| 709 |
} |
| 710 |
t.Fatal("first DecodeIdx call failed:", err) |
| 711 |
} |
| 712 |
|
| 713 |
// Second call - provide remaining inputs |
| 714 |
input2 := make([][]byte, totalShards) |
| 715 |
provided2 := 0 |
| 716 |
for i := 0; i < totalShards && provided1+provided2 < tc.dataShards; i++ { |
| 717 |
if expectInput[i] && input1[i] == nil { |
| 718 |
input2[i] = shards[i] |
| 719 |
provided2++ |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
err = ext.DecodeIdx(dst, expectInput, input2) |
| 724 |
if err != nil { |
| 725 |
if errors.Is(err, ErrNotSupported) { |
| 726 |
t.Skip("DecodeIdx not supported for this encoder type") |
| 727 |
} |
| 728 |
t.Fatal("second DecodeIdx call failed:", err) |
| 729 |
} |
| 730 |
|
| 731 |
// Verify reconstruction |
| 732 |
if !bytes.Equal(dst[1], originals[1]) { |
| 733 |
t.Error("progressive reconstruction of shard 1 failed") |
| 734 |
} |
| 735 |
}) |
| 736 |
}) |
| 737 |
} |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
// TestDecodeIdx_ExcessValidShards tests DecodeIdx when expectInput marks more than dataShards as true |
| 742 |
func TestDecodeIdx_ExcessValidShards(t *testing.T) { |
| 743 |
enc, err := New(5, 3, testOptions()...) |
| 744 |
if err != nil { |
| 745 |
t.Fatal(err) |
| 746 |
} |
| 747 |
r := enc.(*reedSolomon) |
| 748 |
|
| 749 |
const shardSize = 256 |
| 750 |
const dataShards = 5 |
| 751 |
const parityShards = 3 |
| 752 |
const totalShards = dataShards + parityShards |
| 753 |
|
| 754 |
// Create and encode test data |
| 755 |
shards := make([][]byte, totalShards) |
| 756 |
for i := range shards { |
| 757 |
shards[i] = make([]byte, shardSize) |
| 758 |
} |
| 759 |
|
| 760 |
// Fill data shards with random data |
| 761 |
for i := range dataShards { |
| 762 |
fillRandom(shards[i]) |
| 763 |
} |
| 764 |
|
| 765 |
err = enc.Encode(shards) |
| 766 |
if err != nil { |
| 767 |
t.Fatal("encode failed:", err) |
| 768 |
} |
| 769 |
|
| 770 |
// Save originals for verification |
| 771 |
originals := make([][]byte, totalShards) |
| 772 |
for i := range shards { |
| 773 |
originals[i] = make([]byte, len(shards[i])) |
| 774 |
copy(originals[i], shards[i]) |
| 775 |
} |
| 776 |
|
| 777 |
// Test: expectInput marks 7 shards as valid (more than dataShards=5) |
| 778 |
// This tests the scenario where more shards are marked as available than needed |
| 779 |
t.Run("excess_valid_shards_data_reconstruction", func(t *testing.T) { |
| 780 |
// Set up reconstruction of shard 1 |
| 781 |
dst := make([][]byte, totalShards) |
| 782 |
dst[1] = make([]byte, shardSize) |
| 783 |
|
| 784 |
// Mark 7 shards as expected (more than dataShards=5) |
| 785 |
expectInput := make([]bool, totalShards) |
| 786 |
expectInput[0] = true // data shard |
| 787 |
expectInput[2] = true // data shard |
| 788 |
expectInput[3] = true // data shard |
| 789 |
expectInput[4] = true // data shard |
| 790 |
expectInput[5] = true // parity shard |
| 791 |
expectInput[6] = true // parity shard |
| 792 |
expectInput[7] = true // parity shard |
| 793 |
|
| 794 |
input := make([][]byte, totalShards) |
| 795 |
|
| 796 |
// Test 1: Provide only the first dataShards (5) valid shards |
| 797 |
// This should work since matrix is built from first 5 valid shards |
| 798 |
input[0] = shards[0] |
| 799 |
input[2] = shards[2] |
| 800 |
input[3] = shards[3] |
| 801 |
input[4] = shards[4] |
| 802 |
input[5] = shards[5] |
| 803 |
|
| 804 |
err = r.DecodeIdx(dst, expectInput, input) |
| 805 |
if err != nil { |
| 806 |
t.Fatal("DecodeIdx failed with first 5 valid shards:", err) |
| 807 |
} |
| 808 |
|
| 809 |
// Verify reconstruction |
| 810 |
if !bytes.Equal(dst[1], originals[1]) { |
| 811 |
t.Error("data shard 1 reconstruction failed") |
| 812 |
} |
| 813 |
|
| 814 |
// Test 2: Provide shards beyond the first dataShards |
| 815 |
// This should now work seamlessly - excess valid shards are ignored |
| 816 |
dst2 := make([][]byte, totalShards) |
| 817 |
dst2[1] = make([]byte, shardSize) |
| 818 |
|
| 819 |
input2 := make([][]byte, totalShards) |
| 820 |
input2[6] = shards[6] // This is the 6th valid shard, beyond dataShards (should be ignored) |
| 821 |
input2[7] = shards[7] // This is the 7th valid shard, beyond dataShards (should be ignored) |
| 822 |
|
| 823 |
err = r.DecodeIdx(dst2, expectInput, input2) |
| 824 |
if err != nil { |
| 825 |
t.Error("DecodeIdx should handle excess valid shards seamlessly, but got error:", err) |
| 826 |
} |
| 827 |
|
| 828 |
// Since we only provided excess shards (6,7) and not the required first 5, |
| 829 |
// the reconstruction should not be complete. dst[1] should remain zero. |
| 830 |
if !bytes.Equal(dst2[1], make([]byte, shardSize)) { |
| 831 |
t.Error("dst should remain zero when only excess shards are provided") |
| 832 |
} |
| 833 |
}) |
| 834 |
|
| 835 |
// Test with parity reconstruction |
| 836 |
t.Run("excess_valid_shards_parity_reconstruction", func(t *testing.T) { |
| 837 |
// Set up reconstruction of parity shard 5 |
| 838 |
dst := make([][]byte, totalShards) |
| 839 |
dst[5] = make([]byte, shardSize) |
| 840 |
|
| 841 |
// Mark 6 shards as expected (more than dataShards=5) |
| 842 |
expectInput := make([]bool, totalShards) |
| 843 |
expectInput[0] = true // data shard |
| 844 |
expectInput[1] = true // data shard |
| 845 |
expectInput[2] = true // data shard |
| 846 |
expectInput[3] = true // data shard |
| 847 |
expectInput[4] = true // data shard |
| 848 |
expectInput[6] = true // parity shard (6th valid shard, beyond dataShards) |
| 849 |
|
| 850 |
input := make([][]byte, totalShards) |
| 851 |
|
| 852 |
// Provide all data shards (first 5 valid) |
| 853 |
input[0] = shards[0] |
| 854 |
input[1] = shards[1] |
| 855 |
input[2] = shards[2] |
| 856 |
input[3] = shards[3] |
| 857 |
input[4] = shards[4] |
| 858 |
|
| 859 |
err = r.DecodeIdx(dst, expectInput, input) |
| 860 |
if err != nil { |
| 861 |
t.Fatal("DecodeIdx failed for parity reconstruction with valid data shards:", err) |
| 862 |
} |
| 863 |
|
| 864 |
// Verify reconstruction |
| 865 |
if !bytes.Equal(dst[5], originals[5]) { |
| 866 |
t.Error("parity shard 5 reconstruction failed") |
| 867 |
} |
| 868 |
|
| 869 |
// Test providing the excess shard (should work seamlessly) |
| 870 |
dst2 := make([][]byte, totalShards) |
| 871 |
dst2[5] = make([]byte, shardSize) |
| 872 |
|
| 873 |
input2 := make([][]byte, totalShards) |
| 874 |
input2[6] = shards[6] // This is beyond the first dataShards valid positions (should be ignored) |
| 875 |
|
| 876 |
err = r.DecodeIdx(dst2, expectInput, input2) |
| 877 |
if err != nil { |
| 878 |
t.Error("DecodeIdx should handle excess valid shard seamlessly, but got error:", err) |
| 879 |
} |
| 880 |
|
| 881 |
// Since we only provided excess shard (6) and not all required data shards, |
| 882 |
// the parity reconstruction should not be complete. dst[5] should remain zero. |
| 883 |
if !bytes.Equal(dst2[5], make([]byte, shardSize)) { |
| 884 |
t.Error("dst should remain zero when only excess shard is provided") |
| 885 |
} |
| 886 |
}) |
| 887 |
|
| 888 |
// Test edge case: exactly dataShards valid shards |
| 889 |
t.Run("exactly_dataShards_valid", func(t *testing.T) { |
| 890 |
// Set up reconstruction of shard 1 |
| 891 |
dst := make([][]byte, totalShards) |
| 892 |
dst[1] = make([]byte, shardSize) |
| 893 |
|
| 894 |
// Mark exactly dataShards (5) as expected |
| 895 |
expectInput := make([]bool, totalShards) |
| 896 |
expectInput[0] = true |
| 897 |
expectInput[2] = true |
| 898 |
expectInput[3] = true |
| 899 |
expectInput[4] = true |
| 900 |
expectInput[5] = true // exactly the 5th (last allowed) |
| 901 |
|
| 902 |
input := make([][]byte, totalShards) |
| 903 |
input[0] = shards[0] |
| 904 |
input[2] = shards[2] |
| 905 |
input[3] = shards[3] |
| 906 |
input[4] = shards[4] |
| 907 |
input[5] = shards[5] |
| 908 |
|
| 909 |
err = r.DecodeIdx(dst, expectInput, input) |
| 910 |
if err != nil { |
| 911 |
t.Fatal("DecodeIdx failed with exactly dataShards valid shards:", err) |
| 912 |
} |
| 913 |
|
| 914 |
// Verify reconstruction |
| 915 |
if !bytes.Equal(dst[1], originals[1]) { |
| 916 |
t.Error("reconstruction failed with exactly dataShards valid shards") |
| 917 |
} |
| 918 |
}) |
| 919 |
} |
| 920 |
|
| 921 |
func BenchmarkDecodeIdx10x2x1M(b *testing.B) { |
| 922 |
benchmarkDecodeIdx(b, 10, 2, 1024*1024) |
| 923 |
} |
| 924 |
|
| 925 |
func BenchmarkDecodeIdx50x10x1M(b *testing.B) { |
| 926 |
benchmarkDecodeIdx(b, 50, 10, 1024*1024) |
| 927 |
} |
| 928 |
|
| 929 |
func BenchmarkDecodeIdx10x2x16M(b *testing.B) { |
| 930 |
benchmarkDecodeIdx(b, 10, 2, 16*1024*1024) |
| 931 |
} |
| 932 |
|
| 933 |
func BenchmarkDecodeIdx5x2x1M(b *testing.B) { |
| 934 |
benchmarkDecodeIdx(b, 5, 2, 1024*1024) |
| 935 |
} |
| 936 |
|
| 937 |
func BenchmarkDecodeIdx10x4x16M(b *testing.B) { |
| 938 |
benchmarkDecodeIdx(b, 10, 4, 16*1024*1024) |
| 939 |
} |
| 940 |
|
| 941 |
func BenchmarkDecodeIdx50x20x1M(b *testing.B) { |
| 942 |
benchmarkDecodeIdx(b, 50, 20, 1024*1024) |
| 943 |
} |
| 944 |
|
| 945 |
func BenchmarkDecodeIdx10x4x1M(b *testing.B) { |
| 946 |
benchmarkDecodeIdx(b, 10, 4, 1024*1024) |
| 947 |
} |
| 948 |
|
| 949 |
func benchmarkDecodeIdx(b *testing.B, dataShards, parityShards, shardSize int) { |
| 950 |
r, err := New(dataShards, parityShards, testOptions()...) |
| 951 |
if err != nil { |
| 952 |
b.Fatal(err) |
| 953 |
} |
| 954 |
ext := r.(Extensions) |
| 955 |
totalShards := dataShards + parityShards |
| 956 |
|
| 957 |
shards := make([][]byte, totalShards) |
| 958 |
for s := range shards { |
| 959 |
shards[s] = make([]byte, shardSize) |
| 960 |
} |
| 961 |
|
| 962 |
for s := range dataShards { |
| 963 |
fillRandom(shards[s]) |
| 964 |
} |
| 965 |
|
| 966 |
err = r.Encode(shards) |
| 967 |
if err != nil { |
| 968 |
b.Fatal(err) |
| 969 |
} |
| 970 |
|
| 971 |
expectInput := make([]bool, totalShards) |
| 972 |
inputs := make([][]byte, totalShards) |
| 973 |
dst := make([][]byte, totalShards) |
| 974 |
|
| 975 |
dst[0] = make([]byte, shardSize) |
| 976 |
|
| 977 |
// Only provide exactly dataShards inputs (minimum needed) |
| 978 |
inputCount := 0 |
| 979 |
for j := 1; j < totalShards && inputCount < dataShards; j++ { |
| 980 |
expectInput[j] = true |
| 981 |
inputs[j] = shards[j] |
| 982 |
inputCount++ |
| 983 |
} |
| 984 |
|
| 985 |
b.SetBytes(int64(shardSize * totalShards)) |
| 986 |
b.ResetTimer() |
| 987 |
b.ReportAllocs() |
| 988 |
for range b.N { |
| 989 |
err = ext.DecodeIdx(dst, expectInput, inputs) |
| 990 |
if err != nil { |
| 991 |
b.Fatal(err) |
| 992 |
} |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
func BenchmarkDecodeIdxSingleData10x2x1M(b *testing.B) { |
| 997 |
benchmarkDecodeIdxSingle(b, 10, 2, 1024*1024, true) |
| 998 |
} |
| 999 |
|
| 1000 |
func BenchmarkDecodeIdxSingleParity10x2x1M(b *testing.B) { |
| 1001 |
benchmarkDecodeIdxSingle(b, 10, 2, 1024*1024, false) |
| 1002 |
} |
| 1003 |
|
| 1004 |
func BenchmarkDecodeIdxSingleData50x10x1M(b *testing.B) { |
| 1005 |
benchmarkDecodeIdxSingle(b, 50, 10, 1024*1024, true) |
| 1006 |
} |
| 1007 |
|
| 1008 |
func BenchmarkDecodeIdxSingleParity50x10x1M(b *testing.B) { |
| 1009 |
benchmarkDecodeIdxSingle(b, 50, 10, 1024*1024, false) |
| 1010 |
} |
| 1011 |
|
| 1012 |
func benchmarkDecodeIdxSingle(b *testing.B, dataShards, parityShards, shardSize int, reconstructData bool) { |
| 1013 |
r, err := New(dataShards, parityShards, testOptions()...) |
| 1014 |
if err != nil { |
| 1015 |
b.Fatal(err) |
| 1016 |
} |
| 1017 |
ext := r.(Extensions) |
| 1018 |
totalShards := dataShards + parityShards |
| 1019 |
|
| 1020 |
shards := make([][]byte, totalShards) |
| 1021 |
for s := range shards { |
| 1022 |
shards[s] = make([]byte, shardSize) |
| 1023 |
} |
| 1024 |
|
| 1025 |
for s := range dataShards { |
| 1026 |
fillRandom(shards[s]) |
| 1027 |
} |
| 1028 |
|
| 1029 |
err = r.Encode(shards) |
| 1030 |
if err != nil { |
| 1031 |
b.Fatal(err) |
| 1032 |
} |
| 1033 |
|
| 1034 |
targetIdx := 0 |
| 1035 |
if !reconstructData { |
| 1036 |
targetIdx = dataShards |
| 1037 |
} |
| 1038 |
|
| 1039 |
expectInput := make([]bool, totalShards) |
| 1040 |
inputs := make([][]byte, totalShards) |
| 1041 |
dst := make([][]byte, totalShards) |
| 1042 |
dst[targetIdx] = make([]byte, shardSize) |
| 1043 |
|
| 1044 |
// Only provide exactly dataShards inputs (minimum needed) |
| 1045 |
inputCount := 0 |
| 1046 |
for j := 0; j < totalShards && inputCount < dataShards; j++ { |
| 1047 |
if j != targetIdx { |
| 1048 |
expectInput[j] = true |
| 1049 |
inputs[j] = shards[j] |
| 1050 |
inputCount++ |
| 1051 |
} |
| 1052 |
} |
| 1053 |
b.SetBytes(int64(shardSize * totalShards)) |
| 1054 |
b.ResetTimer() |
| 1055 |
b.ReportAllocs() |
| 1056 |
for range b.N { |
| 1057 |
err = ext.DecodeIdx(dst, expectInput, inputs) |
| 1058 |
if err != nil { |
| 1059 |
b.Fatal(err) |
| 1060 |
} |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|
| 1064 |
func BenchmarkDecodeIdxMultiple10x2x1M(b *testing.B) { |
| 1065 |
benchmarkDecodeIdxMultiple(b, 10, 2, 1024*1024, 2) |
| 1066 |
} |
| 1067 |
|
| 1068 |
func BenchmarkDecodeIdxMultiple50x10x1M(b *testing.B) { |
| 1069 |
benchmarkDecodeIdxMultiple(b, 50, 10, 1024*1024, 5) |
| 1070 |
} |
| 1071 |
|
| 1072 |
func BenchmarkDecodeIdxMultiple10x4x16M(b *testing.B) { |
| 1073 |
benchmarkDecodeIdxMultiple(b, 10, 4, 16*1024*1024, 3) |
| 1074 |
} |
| 1075 |
|
| 1076 |
func benchmarkDecodeIdxMultiple(b *testing.B, dataShards, parityShards, shardSize, numLost int) { |
| 1077 |
r, err := New(dataShards, parityShards, testOptions()...) |
| 1078 |
if err != nil { |
| 1079 |
b.Fatal(err) |
| 1080 |
} |
| 1081 |
ext := r.(Extensions) |
| 1082 |
totalShards := dataShards + parityShards |
| 1083 |
|
| 1084 |
shards := make([][]byte, totalShards) |
| 1085 |
for s := range shards { |
| 1086 |
shards[s] = make([]byte, shardSize) |
| 1087 |
} |
| 1088 |
|
| 1089 |
for s := range dataShards { |
| 1090 |
fillRandom(shards[s]) |
| 1091 |
} |
| 1092 |
|
| 1093 |
err = r.Encode(shards) |
| 1094 |
if err != nil { |
| 1095 |
b.Fatal(err) |
| 1096 |
} |
| 1097 |
|
| 1098 |
expectInput := make([]bool, totalShards) |
| 1099 |
inputs := make([][]byte, totalShards) |
| 1100 |
dst := make([][]byte, totalShards) |
| 1101 |
|
| 1102 |
for j := range numLost { |
| 1103 |
dst[j] = make([]byte, shardSize) |
| 1104 |
} |
| 1105 |
|
| 1106 |
// Only provide exactly dataShards inputs (minimum needed) |
| 1107 |
inputCount := 0 |
| 1108 |
for j := numLost; j < totalShards && inputCount < dataShards; j++ { |
| 1109 |
expectInput[j] = true |
| 1110 |
inputs[j] = shards[j] |
| 1111 |
inputCount++ |
| 1112 |
} |
| 1113 |
|
| 1114 |
b.SetBytes(int64(shardSize * totalShards)) |
| 1115 |
b.ResetTimer() |
| 1116 |
b.ReportAllocs() |
| 1117 |
for range b.N { |
| 1118 |
err = ext.DecodeIdx(dst, expectInput, inputs) |
| 1119 |
if err != nil { |
| 1120 |
b.Fatal(err) |
| 1121 |
} |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
func BenchmarkDecodeIdxProgressive10x2x1M(b *testing.B) { |
| 1126 |
benchmarkDecodeIdxProgressive(b, 10, 2, 1024*1024) |
| 1127 |
} |
| 1128 |
|
| 1129 |
func BenchmarkDecodeIdxProgressive50x10x1M(b *testing.B) { |
| 1130 |
benchmarkDecodeIdxProgressive(b, 50, 10, 1024*1024) |
| 1131 |
} |
| 1132 |
|
| 1133 |
func BenchmarkDecodeIdxProgressive10x4x16M(b *testing.B) { |
| 1134 |
benchmarkDecodeIdxProgressive(b, 10, 4, 16*1024*1024) |
| 1135 |
} |
| 1136 |
|
| 1137 |
func benchmarkDecodeIdxProgressive(b *testing.B, dataShards, parityShards, shardSize int) { |
| 1138 |
r, err := New(dataShards, parityShards, testOptions()...) |
| 1139 |
if err != nil { |
| 1140 |
b.Fatal(err) |
| 1141 |
} |
| 1142 |
ext := r.(Extensions) |
| 1143 |
totalShards := dataShards + parityShards |
| 1144 |
|
| 1145 |
shards := make([][]byte, totalShards) |
| 1146 |
for s := range shards { |
| 1147 |
shards[s] = make([]byte, shardSize) |
| 1148 |
} |
| 1149 |
|
| 1150 |
for s := range dataShards { |
| 1151 |
fillRandom(shards[s]) |
| 1152 |
} |
| 1153 |
|
| 1154 |
err = r.Encode(shards) |
| 1155 |
if err != nil { |
| 1156 |
b.Fatal(err) |
| 1157 |
} |
| 1158 |
|
| 1159 |
expectInput := make([]bool, totalShards) |
| 1160 |
dst := make([][]byte, totalShards) |
| 1161 |
dst[0] = make([]byte, shardSize) |
| 1162 |
|
| 1163 |
// Select exactly dataShards inputs to split across two calls |
| 1164 |
availableIndices := make([]int, 0, totalShards-1) |
| 1165 |
for j := 1; j < totalShards; j++ { |
| 1166 |
availableIndices = append(availableIndices, j) |
| 1167 |
} |
| 1168 |
|
| 1169 |
// Mark expectInput for exactly dataShards indices |
| 1170 |
for i := 0; i < dataShards && i < len(availableIndices); i++ { |
| 1171 |
expectInput[availableIndices[i]] = true |
| 1172 |
} |
| 1173 |
|
| 1174 |
// Split inputs across two calls |
| 1175 |
half := dataShards / 2 |
| 1176 |
inputs1 := make([][]byte, totalShards) |
| 1177 |
inputs2 := make([][]byte, totalShards) |
| 1178 |
|
| 1179 |
for i := 0; i < half && i < len(availableIndices); i++ { |
| 1180 |
idx := availableIndices[i] |
| 1181 |
inputs1[idx] = shards[idx] |
| 1182 |
} |
| 1183 |
for i := half; i < dataShards && i < len(availableIndices); i++ { |
| 1184 |
idx := availableIndices[i] |
| 1185 |
inputs2[idx] = shards[idx] |
| 1186 |
} |
| 1187 |
|
| 1188 |
b.SetBytes(int64(shardSize * totalShards)) |
| 1189 |
b.ResetTimer() |
| 1190 |
b.ReportAllocs() |
| 1191 |
for range b.N { |
| 1192 |
err = ext.DecodeIdx(dst, expectInput, inputs1) |
| 1193 |
if err != nil { |
| 1194 |
b.Fatal(err) |
| 1195 |
} |
| 1196 |
|
| 1197 |
err = ext.DecodeIdx(dst, expectInput, inputs2) |
| 1198 |
if err != nil { |
| 1199 |
b.Fatal(err) |
| 1200 |
} |
| 1201 |
} |
| 1202 |
} |
| 1203 |
|
| 1204 |
func BenchmarkDecodeIdxVsReconstruct10x2x1M(b *testing.B) { |
| 1205 |
benchmarkDecodeIdxVsReconstruct(b, 10, 2, 1024*1024) |
| 1206 |
} |
| 1207 |
|
| 1208 |
func BenchmarkDecodeIdxVsReconstruct50x10x1M(b *testing.B) { |
| 1209 |
benchmarkDecodeIdxVsReconstruct(b, 50, 10, 1024*1024) |
| 1210 |
} |
| 1211 |
|
| 1212 |
func benchmarkDecodeIdxVsReconstruct(b *testing.B, dataShards, parityShards, shardSize int) { |
| 1213 |
r, err := New(dataShards, parityShards, testOptions()...) |
| 1214 |
if err != nil { |
| 1215 |
b.Fatal(err) |
| 1216 |
} |
| 1217 |
ext := r.(Extensions) |
| 1218 |
totalShards := dataShards + parityShards |
| 1219 |
|
| 1220 |
shards := make([][]byte, totalShards) |
| 1221 |
for s := range shards { |
| 1222 |
shards[s] = make([]byte, shardSize) |
| 1223 |
} |
| 1224 |
|
| 1225 |
for s := range dataShards { |
| 1226 |
fillRandom(shards[s]) |
| 1227 |
} |
| 1228 |
|
| 1229 |
err = r.Encode(shards) |
| 1230 |
if err != nil { |
| 1231 |
b.Fatal(err) |
| 1232 |
} |
| 1233 |
|
| 1234 |
b.Run("DecodeIdx", func(b *testing.B) { |
| 1235 |
|
| 1236 |
expectInput := make([]bool, totalShards) |
| 1237 |
inputs := make([][]byte, totalShards) |
| 1238 |
dst := make([][]byte, totalShards) |
| 1239 |
|
| 1240 |
dst[0] = make([]byte, shardSize) |
| 1241 |
|
| 1242 |
// Only provide exactly dataShards inputs (minimum needed) |
| 1243 |
inputCount := 0 |
| 1244 |
for j := 1; j < totalShards && inputCount < dataShards; j++ { |
| 1245 |
expectInput[j] = true |
| 1246 |
inputs[j] = shards[j] |
| 1247 |
inputCount++ |
| 1248 |
} |
| 1249 |
b.SetBytes(int64(shardSize * totalShards)) |
| 1250 |
b.ResetTimer() |
| 1251 |
b.ReportAllocs() |
| 1252 |
for range b.N { |
| 1253 |
err = ext.DecodeIdx(dst, expectInput, inputs) |
| 1254 |
if err != nil { |
| 1255 |
b.Fatal(err) |
| 1256 |
} |
| 1257 |
} |
| 1258 |
}) |
| 1259 |
|
| 1260 |
b.Run("Reconstruct", func(b *testing.B) { |
| 1261 |
shardsTemp := make([][]byte, totalShards) |
| 1262 |
for s := range shardsTemp { |
| 1263 |
if s == 0 { |
| 1264 |
shardsTemp[s] = nil |
| 1265 |
} else { |
| 1266 |
shardsTemp[s] = make([]byte, shardSize) |
| 1267 |
copy(shardsTemp[s], shards[s]) |
| 1268 |
} |
| 1269 |
} |
| 1270 |
|
| 1271 |
b.SetBytes(int64(shardSize * totalShards)) |
| 1272 |
b.ResetTimer() |
| 1273 |
b.ReportAllocs() |
| 1274 |
for range b.N { |
| 1275 |
for s := range shardsTemp { |
| 1276 |
if s == 0 { |
| 1277 |
shardsTemp[s] = shardsTemp[s][:0] |
| 1278 |
} |
| 1279 |
} |
| 1280 |
err = r.Reconstruct(shardsTemp) |
| 1281 |
if err != nil { |
| 1282 |
b.Fatal(err) |
| 1283 |
} |
| 1284 |
} |
| 1285 |
}) |
| 1286 |
} |
| 1287 |
|
| 1288 |
func BenchmarkDecodeIdxParityOnly10x2x1M(b *testing.B) { |
| 1289 |
benchmarkDecodeIdxParityOnly(b, 10, 2, 1024*1024) |
| 1290 |
} |
| 1291 |
|
| 1292 |
func BenchmarkDecodeIdxParityOnly50x10x1M(b *testing.B) { |
| 1293 |
benchmarkDecodeIdxParityOnly(b, 50, 10, 1024*1024) |
| 1294 |
} |
| 1295 |
|
| 1296 |
func benchmarkDecodeIdxParityOnly(b *testing.B, dataShards, parityShards, shardSize int) { |
| 1297 |
r, err := New(dataShards, parityShards, testOptions()...) |
| 1298 |
if err != nil { |
| 1299 |
b.Fatal(err) |
| 1300 |
} |
| 1301 |
ext := r.(Extensions) |
| 1302 |
totalShards := dataShards + parityShards |
| 1303 |
|
| 1304 |
shards := make([][]byte, totalShards) |
| 1305 |
for s := range shards { |
| 1306 |
shards[s] = make([]byte, shardSize) |
| 1307 |
} |
| 1308 |
|
| 1309 |
for s := range dataShards { |
| 1310 |
fillRandom(shards[s]) |
| 1311 |
} |
| 1312 |
|
| 1313 |
err = r.Encode(shards) |
| 1314 |
if err != nil { |
| 1315 |
b.Fatal(err) |
| 1316 |
} |
| 1317 |
|
| 1318 |
expectInput := make([]bool, totalShards) |
| 1319 |
inputs := make([][]byte, totalShards) |
| 1320 |
dst := make([][]byte, totalShards) |
| 1321 |
|
| 1322 |
for j := range parityShards { |
| 1323 |
dst[dataShards+j] = make([]byte, shardSize) |
| 1324 |
} |
| 1325 |
|
| 1326 |
for j := range dataShards { |
| 1327 |
expectInput[j] = true |
| 1328 |
inputs[j] = shards[j] |
| 1329 |
} |
| 1330 |
b.SetBytes(int64(shardSize * totalShards)) |
| 1331 |
b.ResetTimer() |
| 1332 |
b.ReportAllocs() |
| 1333 |
for range b.N { |
| 1334 |
err = ext.DecodeIdx(dst, expectInput, inputs) |
| 1335 |
if err != nil { |
| 1336 |
b.Fatal(err) |
| 1337 |
} |
| 1338 |
} |
| 1339 |
} |