progress_hybrid_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package indexer
  2. import (
  3. "testing"
  4. )
  5. func TestProgressTracker_HybridCalculation(t *testing.T) {
  6. // Test the hybrid progress calculation logic
  7. tracker := NewProgressTracker("/var/log/nginx/access.log", nil)
  8. // Add 4 files with different estimates
  9. files := []string{
  10. "/var/log/nginx/access.log.1",
  11. "/var/log/nginx/access.log.2",
  12. "/var/log/nginx/access.log.3",
  13. "/var/log/nginx/access.log.4",
  14. }
  15. estimates := []int64{1000, 2000, 1500, 2500}
  16. for i, file := range files {
  17. tracker.AddFile(file, false)
  18. tracker.SetFileEstimate(file, estimates[i])
  19. }
  20. // Test scenario 1: 2 files completed, 1 processing
  21. tracker.CompleteFile(files[0], 1000) // File 1: completed with 1000 lines
  22. tracker.CompleteFile(files[1], 2000) // File 2: completed with 2000 lines
  23. tracker.StartFile(files[2])
  24. tracker.UpdateFileProgress(files[2], 750) // File 3: processing, 750/1500 lines
  25. progress := tracker.GetProgress()
  26. // Calculate expected hybrid progress with dynamic line estimation
  27. // After 2 completed files, dynamic estimation kicks in
  28. // Completed files: 1000 + 2000 = 3000 lines, average = 1500 per file
  29. // Dynamic total estimate: 1500 * 4 = 6000 lines
  30. // Line progress: (1000 + 2000 + 750) / 6000 = 62.5%
  31. // File progress: 2 / 4 = 50% (only completed files count fully)
  32. // Hybrid: (62.5 * 0.4) + (50 * 0.6) = 25 + 30 = 55%
  33. avgLinesPerCompletedFile := float64(3000) / 2.0 // 1500
  34. dynamicTotalEstimate := avgLinesPerCompletedFile * 4 // 6000
  35. expectedLineProgress := float64(3750) / dynamicTotalEstimate * 100 // 62.5%
  36. expectedFileProgress := 2.0 / 4.0 * 100 // 50%
  37. expectedHybrid := (expectedLineProgress * 0.4) + (expectedFileProgress * 0.6)
  38. if progress.Percentage < expectedHybrid-1 || progress.Percentage > expectedHybrid+1 {
  39. t.Errorf("Expected hybrid progress around %.2f%%, got %.2f%%", expectedHybrid, progress.Percentage)
  40. }
  41. // Verify the safety cap: percentage shouldn't exceed file progress by more than 15%
  42. maxAllowed := expectedFileProgress + 15.0
  43. if progress.Percentage > maxAllowed {
  44. t.Errorf("Progress %.2f%% exceeded safety cap of %.2f%%", progress.Percentage, maxAllowed)
  45. }
  46. t.Logf("Line progress: %.2f%%, File progress: %.2f%%, Hybrid: %.2f%%, Actual: %.2f%%",
  47. expectedLineProgress, expectedFileProgress, expectedHybrid, progress.Percentage)
  48. }
  49. func TestProgressTracker_DynamicEstimation(t *testing.T) {
  50. // Test dynamic estimation adjustment
  51. tracker := NewProgressTracker("/var/log/nginx/access.log", nil)
  52. // Add 5 files with initial low estimates
  53. files := []string{
  54. "/var/log/nginx/access.log.1",
  55. "/var/log/nginx/access.log.2",
  56. "/var/log/nginx/access.log.3",
  57. "/var/log/nginx/access.log.4",
  58. "/var/log/nginx/access.log.5",
  59. }
  60. initialEstimate := int64(1000) // Low estimate
  61. for _, file := range files {
  62. tracker.AddFile(file, false)
  63. tracker.SetFileEstimate(file, initialEstimate)
  64. }
  65. // Complete 3 files with much higher actual counts
  66. actualCounts := []int64{3000, 3500, 2800} // Much higher than estimated
  67. for i := 0; i < 3; i++ {
  68. tracker.CompleteFile(files[i], actualCounts[i])
  69. }
  70. progress := tracker.GetProgress()
  71. // After completing 3 files (60% of files), the estimate should be fully adjusted
  72. // Average per completed file: (3000 + 3500 + 2800) / 3 = 3100
  73. // Projected total: 3100 * 5 = 15500
  74. // Since 3/5 = 60% > 20%, full dynamic adjustment is applied
  75. avgLinesPerCompletedFile := float64(3000+3500+2800) / 3 // 3100
  76. expectedAdjustedEstimate := int64(avgLinesPerCompletedFile * 5) // 15500
  77. if progress.EstimatedLines != expectedAdjustedEstimate {
  78. t.Errorf("Expected adjusted estimate to be %d (dynamic), got %d", expectedAdjustedEstimate, progress.EstimatedLines)
  79. }
  80. t.Logf("Original total estimate: %d, Adjusted estimate: %d, Actual average per file: %.0f",
  81. initialEstimate*5, progress.EstimatedLines, float64(3000+3500+2800)/3)
  82. }