main.go
  1  package main
  2  
  3  import (
  4  	"fmt"
  5  	"time"
  6  
  7  	"keepSync/internal/providers/chunking"
  8  )
  9  
 10  func main() {
 11  	fmt.Println("๐Ÿงช Testing Comprehensive Adaptive Chunking Strategy")
 12  	fmt.Println("๐Ÿ“Š This test validates chunking behavior for all file size ranges")
 13  
 14  	// Test different file sizes to verify chunking strategy
 15  	testSizes := []struct {
 16  		name     string
 17  		size     int64
 18  		expected string
 19  	}{
 20  		{"50KB", 50 * 1024, "100KB chunks"},
 21  		{"500KB", 500 * 1024, "100KB chunks"},
 22  		{"800KB", 800 * 1024, "100KB chunks"},
 23  		{"2MB", 2 * 1024 * 1024, "1MB chunks"},
 24  		{"4MB", 4 * 1024 * 1024, "1MB chunks"},
 25  		{"10MB", 10 * 1024 * 1024, "2MB chunks"},
 26  		{"30MB", 30 * 1024 * 1024, "2MB chunks"},
 27  		{"100MB", 100 * 1024 * 1024, "8MB chunks"},
 28  		{"300MB", 300 * 1024 * 1024, "8MB chunks"},
 29  		{"1GB", 1024 * 1024 * 1024, "16MB chunks"},
 30  		{"3GB", 3 * 1024 * 1024 * 1024, "32MB chunks"},
 31  	}
 32  
 33  	fmt.Println("\n๐Ÿ”ง Testing Adaptive Chunking Strategy...")
 34  
 35  	for _, test := range testSizes {
 36  		fmt.Printf("\n๐Ÿ“ Testing %s file (%d bytes) - Expected: %s\n", test.name, test.size, test.expected)
 37  
 38  		// Create adaptive chunking strategy
 39  		strategy := chunking.NewAdaptiveChunkingStrategy()
 40  		strategy = strategy.OptimizeForTransport(chunking.TransportS3).(*chunking.AdaptiveChunkingStrategy)
 41  
 42  		// Create context for chunking decisions
 43  		context := chunking.ChunkingContext{
 44  			NetworkBandwidth:  100 * 1024 * 1024,      // 100 Mbps
 45  			AvailableMemory:   8 * 1024 * 1024 * 1024, // 8GB
 46  			AvailableWorkers:  16,
 47  			TPMOperationTime:  10 * time.Millisecond,
 48  			MLKEMOverhead:     0.35, // 35% overhead
 49  			PriorityLevel:     1,
 50  			CompressionFactor: 1.0, // No compression expected
 51  		}
 52  
 53  		// Calculate optimal chunk size
 54  		chunkSize := strategy.CalculateOptimalChunkSize(test.size, context)
 55  
 56  		// Get performance data for analysis
 57  		perfData := strategy.GetPerformanceData()
 58  
 59  		// Display results
 60  		fmt.Printf("   ๐Ÿ“ฆ Calculated chunk size: %s\n", formatBytes(chunkSize))
 61  		fmt.Printf("   ๐Ÿ“Š Chunk count: %d\n", (test.size+chunkSize-1)/chunkSize)
 62  
 63  		if reason, ok := perfData["optimization_reason"].(string); ok {
 64  			fmt.Printf("   ๐ŸŽฏ Optimization reason: %s\n", reason)
 65  		}
 66  
 67  		if priority, ok := perfData["optimization_priority"].(string); ok {
 68  			fmt.Printf("   โšก Priority: %s\n", priority)
 69  		}
 70  
 71  		// Verify the chunking is working as expected
 72  		expectedChunkSize := getExpectedChunkSize(test.size)
 73  		if chunkSize == expectedChunkSize {
 74  			fmt.Printf("   โœ… Chunking strategy correct!\n")
 75  		} else {
 76  			fmt.Printf("   โš ๏ธ  Expected %s, got %s\n", formatBytes(expectedChunkSize), formatBytes(chunkSize))
 77  		}
 78  	}
 79  
 80  	fmt.Println("\n๐ŸŽ‰ Comprehensive chunking strategy test completed!")
 81  	fmt.Println("\n๐Ÿ“‹ Summary of Expected Chunking Strategy:")
 82  	fmt.Println("   โ€ข Files < 1MB: 100KB chunks (granular for small files)")
 83  	fmt.Println("   โ€ข Files 1-5MB: 1MB chunks (balanced for small-medium files)")
 84  	fmt.Println("   โ€ข Files 5-50MB: 2MB chunks (minimize ML-KEM overhead)")
 85  	fmt.Println("   โ€ข Files 50-500MB: 8MB chunks (balanced approach)")
 86  	fmt.Println("   โ€ข Files 500MB-2GB: 16MB chunks (speed priority)")
 87  	fmt.Println("   โ€ข Files > 2GB: 32MB chunks (maximum efficiency)")
 88  }
 89  
 90  func getExpectedChunkSize(fileSize int64) int64 {
 91  	const (
 92  		KB = 1024
 93  		MB = 1024 * KB
 94  		GB = 1024 * MB
 95  	)
 96  
 97  	switch {
 98  	case fileSize < 1*MB:
 99  		return 100 * KB
100  	case fileSize < 5*MB:
101  		return 1 * MB
102  	case fileSize < 50*MB:
103  		return 2 * MB
104  	case fileSize < 500*MB:
105  		return 8 * MB
106  	case fileSize < 2*GB:
107  		return 16 * MB
108  	default:
109  		return 32 * MB
110  	}
111  }
112  
113  func formatBytes(bytes int64) string {
114  	const (
115  		KB = 1024
116  		MB = 1024 * KB
117  		GB = 1024 * MB
118  	)
119  
120  	switch {
121  	case bytes >= GB:
122  		return fmt.Sprintf("%.1fGB", float64(bytes)/float64(GB))
123  	case bytes >= MB:
124  		return fmt.Sprintf("%.1fMB", float64(bytes)/float64(MB))
125  	case bytes >= KB:
126  		return fmt.Sprintf("%.1fKB", float64(bytes)/float64(KB))
127  	default:
128  		return fmt.Sprintf("%dB", bytes)
129  	}
130  }