integration_validation_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package indexer
  2. import (
  3. "context"
  4. "os"
  5. "testing"
  6. "time"
  7. )
  8. // TestOptimizedIntegrationValidation validates the complete integration of optimizations
  9. func TestOptimizedIntegrationValidation(t *testing.T) {
  10. t.Log("=== Validating Optimized Indexer Integration ===")
  11. // Create test log content
  12. testLogContent := `127.0.0.1 - - [25/Dec/2023:10:00:00 +0000] "GET /api/test HTTP/1.1" 200 1234 "-" "test-agent"
  13. 127.0.0.2 - - [25/Dec/2023:10:01:00 +0000] "POST /api/data HTTP/1.1" 201 5678 "http://example.com" "another-agent"
  14. 127.0.0.3 - - [25/Dec/2023:10:02:00 +0000] "PUT /api/update HTTP/1.1" 204 0 "-" "update-agent"`
  15. // Create temporary test file
  16. tmpFile, err := os.CreateTemp("", "test_nginx_*.log")
  17. if err != nil {
  18. t.Fatalf("Failed to create temp file: %v", err)
  19. }
  20. defer os.Remove(tmpFile.Name())
  21. defer tmpFile.Close()
  22. if _, err := tmpFile.WriteString(testLogContent); err != nil {
  23. t.Fatalf("Failed to write test content: %v", err)
  24. }
  25. tmpFile.Close()
  26. // Test 1: Validate optimized parsing
  27. t.Log("Testing optimized parsing...")
  28. ctx := context.Background()
  29. file, err := os.Open(tmpFile.Name())
  30. if err != nil {
  31. t.Fatalf("Failed to open test file: %v", err)
  32. }
  33. defer file.Close()
  34. logDocs, err := ParseLogStream(ctx, file, tmpFile.Name())
  35. if err != nil {
  36. t.Fatalf("Optimized parsing failed: %v", err)
  37. }
  38. if len(logDocs) != 3 {
  39. t.Errorf("Expected 3 parsed documents, got %d", len(logDocs))
  40. }
  41. // Test 2: Validate single line parsing with SIMD optimization
  42. t.Log("Testing SIMD-optimized single line parsing...")
  43. testLine := `127.0.0.1 - - [25/Dec/2023:10:00:00 +0000] "GET /api/test HTTP/1.1" 200 1234 "-" "test-agent"`
  44. logDoc, err := ParseLogLine(testLine)
  45. if err != nil {
  46. t.Fatalf("SIMD-optimized parsing failed: %v", err)
  47. }
  48. if logDoc.IP != "127.0.0.1" {
  49. t.Errorf("Expected IP 127.0.0.1, got %s", logDoc.IP)
  50. }
  51. if logDoc.Method != "GET" {
  52. t.Errorf("Expected method GET, got %s", logDoc.Method)
  53. }
  54. if logDoc.Path != "/api/test" {
  55. t.Errorf("Expected path /api/test, got %s", logDoc.Path)
  56. }
  57. // Test 3: Validate optimized indexer with ProgressTracker
  58. t.Log("Testing optimized indexer with ProgressTracker...")
  59. config := DefaultIndexerConfig()
  60. shardManager := NewGroupedShardManager(config)
  61. indexer := NewParallelIndexer(config, shardManager)
  62. // Start the indexer
  63. err = indexer.Start(context.Background())
  64. if err != nil {
  65. t.Fatalf("Failed to start indexer: %v", err)
  66. }
  67. defer indexer.Stop()
  68. // Create progress tracker
  69. progressConfig := &ProgressConfig{
  70. NotifyInterval: time.Millisecond * 100,
  71. OnProgress: func(notification ProgressNotification) {
  72. t.Logf("Progress: %+v", notification)
  73. },
  74. OnCompletion: func(notification CompletionNotification) {
  75. t.Logf("Completion: %+v", notification)
  76. },
  77. }
  78. progressTracker := NewProgressTracker("test-group", progressConfig)
  79. // Test optimized indexing with progress tracking
  80. docCount, minTime, maxTime, err := indexer.OptimizedIndexSingleFileWithProgress(tmpFile.Name(), progressTracker)
  81. if err != nil {
  82. t.Fatalf("OptimizedIndexSingleFileWithProgress failed: %v", err)
  83. }
  84. if docCount != 3 {
  85. t.Errorf("Expected 3 indexed documents, got %d", docCount)
  86. }
  87. if minTime == nil || maxTime == nil {
  88. t.Errorf("Expected time ranges to be calculated, got minTime=%v, maxTime=%v", minTime, maxTime)
  89. }
  90. if minTime != nil && maxTime != nil {
  91. if minTime.After(*maxTime) {
  92. t.Errorf("minTime (%v) should be before maxTime (%v)", minTime, maxTime)
  93. }
  94. t.Logf("Calculated time range: %v to %v", minTime, maxTime)
  95. }
  96. // Test 4: Validate incremental indexing method
  97. t.Log("Testing IndexSingleFileIncrementally...")
  98. docsCountMap, minTime2, maxTime2, err := indexer.IndexSingleFileIncrementally(tmpFile.Name(), progressConfig)
  99. if err != nil {
  100. t.Fatalf("IndexSingleFileIncrementally failed: %v", err)
  101. }
  102. if len(docsCountMap) != 1 {
  103. t.Errorf("Expected 1 entry in docsCountMap, got %d", len(docsCountMap))
  104. }
  105. if docsCount, exists := docsCountMap[tmpFile.Name()]; !exists || docsCount != 3 {
  106. t.Errorf("Expected 3 documents for file %s, got %d (exists: %v)", tmpFile.Name(), docsCount, exists)
  107. }
  108. if minTime2 == nil || maxTime2 == nil {
  109. t.Errorf("Expected time ranges from incremental indexing, got minTime=%v, maxTime=%v", minTime2, maxTime2)
  110. }
  111. // Test 5: Validate optimization status
  112. t.Log("Testing optimization status...")
  113. status := GetOptimizationStatus()
  114. expectedKeys := []string{"parser_optimized", "simd_enabled", "memory_pools_enabled", "batch_processing"}
  115. for _, key := range expectedKeys {
  116. if _, exists := status[key]; !exists {
  117. t.Errorf("Expected optimization status key %s to exist", key)
  118. }
  119. }
  120. t.Logf("Optimization status: %+v", status)
  121. // Test 6: Validate production configuration
  122. t.Log("Testing production configuration...")
  123. // Test the current indexer's configuration (which should have production optimizations)
  124. currentConfig := indexer.GetConfig()
  125. if currentConfig.WorkerCount <= 0 {
  126. t.Errorf("Expected positive WorkerCount in config, got %d", currentConfig.WorkerCount)
  127. }
  128. if currentConfig.BatchSize <= 0 {
  129. t.Errorf("Expected positive BatchSize in config, got %d", currentConfig.BatchSize)
  130. }
  131. t.Log("=== All Optimized Indexer Integration Tests Passed ===")
  132. }
  133. // TestOptimizationCompatibility ensures backward compatibility
  134. func TestOptimizationCompatibility(t *testing.T) {
  135. t.Log("=== Testing Optimization Backward Compatibility ===")
  136. // Create test log content
  137. testLogContent := `127.0.0.1 - - [25/Dec/2023:10:00:00 +0000] "GET /test HTTP/1.1" 200 1234`
  138. tmpFile, err := os.CreateTemp("", "compat_test_*.log")
  139. if err != nil {
  140. t.Fatalf("Failed to create temp file: %v", err)
  141. }
  142. defer os.Remove(tmpFile.Name())
  143. defer tmpFile.Close()
  144. if _, err := tmpFile.WriteString(testLogContent); err != nil {
  145. t.Fatalf("Failed to write test content: %v", err)
  146. }
  147. tmpFile.Close()
  148. config := DefaultIndexerConfig()
  149. shardManager := NewGroupedShardManager(config)
  150. indexer := NewParallelIndexer(config, shardManager)
  151. err = indexer.Start(context.Background())
  152. if err != nil {
  153. t.Fatalf("Failed to start indexer: %v", err)
  154. }
  155. defer indexer.Stop()
  156. // Test that original methods still work (they should delegate to optimized versions)
  157. t.Log("Testing IndexLogFile (should use optimized implementation)...")
  158. err = indexer.IndexLogFile(tmpFile.Name())
  159. if err != nil {
  160. t.Fatalf("IndexLogFile failed: %v", err)
  161. }
  162. t.Log("Testing indexSingleFile (should use optimized implementation)...")
  163. docCount, minTime, maxTime, err := indexer.indexSingleFile(tmpFile.Name())
  164. if err != nil {
  165. t.Fatalf("indexSingleFile failed: %v", err)
  166. }
  167. if docCount != 1 {
  168. t.Errorf("Expected 1 document, got %d", docCount)
  169. }
  170. if minTime == nil || maxTime == nil {
  171. t.Errorf("Expected time ranges, got minTime=%v, maxTime=%v", minTime, maxTime)
  172. }
  173. t.Log("=== Backward Compatibility Tests Passed ===")
  174. }