|
| 1 | + package encoding |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// TestDecodeShards_Performance runs full encode→decode and prints throughput (MB/s) |
| 12 | +func TestDecodeShards_Performance(t *testing.T) { |
| 13 | + tmpIn := t.TempDir() |
| 14 | + tmpOut := t.TempDir() |
| 15 | + |
| 16 | + // prepare a moderately large file (e.g., 50 MB) |
| 17 | + fileSize := 50 * 1024 * 1024 |
| 18 | + fileName := "bigfile.txt" |
| 19 | + inFilePath := filepath.Join(tmpOut, fileName) |
| 20 | + data := make([]byte, fileSize) |
| 21 | + for i := range data { |
| 22 | + data[i] = byte(i % 256) |
| 23 | + } |
| 24 | + |
| 25 | + if err := os.WriteFile(inFilePath, data, 0644); err != nil { |
| 26 | + t.Fatalf("failed to write input file: %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + // create encoder |
| 30 | + enc, err := NewEncoder(4, 2, tmpOut, tmpIn) |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("failed to create encoder: %v", err) |
| 33 | + } |
| 34 | + |
| 35 | + // make .bin dir for encoded output |
| 36 | + if err := os.MkdirAll(filepath.Join(tmpOut, ".bin"), 0755); err != nil { |
| 37 | + t.Fatalf("failed to make .bin dir: %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + // encode |
| 41 | + startEncode := time.Now() |
| 42 | + if err := enc.EncodeFile(fileName); err != nil { |
| 43 | + t.Fatalf("EncodeFile failed: %v", err) |
| 44 | + } |
| 45 | + encodeElapsed := time.Since(startEncode).Seconds() |
| 46 | + |
| 47 | + // move shards to decoder input (.bin/<fileName>) |
| 48 | + shardDir := filepath.Join(tmpOut, ".bin", fileName) |
| 49 | + shards, err := os.ReadDir(shardDir) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("failed to read shard dir: %v", err) |
| 52 | + } |
| 53 | + targetShardDir := filepath.Join(tmpIn, fileName) |
| 54 | + if err := os.MkdirAll(targetShardDir, 0755); err != nil { |
| 55 | + t.Fatalf("failed to create shard dir: %v", err) |
| 56 | + } |
| 57 | + for _, shard := range shards { |
| 58 | + src := filepath.Join(shardDir, shard.Name()) |
| 59 | + dst := filepath.Join(targetShardDir, shard.Name()) |
| 60 | + b, err := os.ReadFile(src) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("failed to read shard: %v", err) |
| 63 | + } |
| 64 | + if err := os.WriteFile(dst, b, 0644); err != nil { |
| 65 | + t.Fatalf("failed to write shard: %v", err) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // decode |
| 70 | + startDecode := time.Now() |
| 71 | + if err := enc.DecodeShards(fileName, len(data)); err != nil { |
| 72 | + t.Fatalf("DecodeShards failed: %v", err) |
| 73 | + } |
| 74 | + decodeElapsed := time.Since(startDecode).Seconds() |
| 75 | + |
| 76 | + // verify output |
| 77 | + decodedPath := filepath.Join(tmpOut, fileName) |
| 78 | + decodedData, err := os.ReadFile(decodedPath) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("failed to read decoded file: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + if !bytes.Equal(decodedData, data) { |
| 84 | + t.Fatalf("decoded file does not match original data") |
| 85 | + } |
| 86 | + |
| 87 | + // calculate and log throughput |
| 88 | + mb := float64(fileSize) / (1024 * 1024) |
| 89 | + encodeThroughput := mb / encodeElapsed |
| 90 | + decodeThroughput := mb / decodeElapsed |
| 91 | + |
| 92 | + t.Logf("Encode: %.2f MB in %.2f s (%.2f MB/s)", mb, encodeElapsed, encodeThroughput) |
| 93 | + t.Logf("Decode: %.2f MB in %.2f s (%.2f MB/s)", mb, decodeElapsed, decodeThroughput) |
| 94 | +} |
| 95 | + |
0 commit comments