validation_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package indexer
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. )
  7. func TestThroughputOptimizations(t *testing.T) {
  8. // Test that batch sizes have been properly increased
  9. config := DefaultIndexerConfig()
  10. t.Run("IncreasedBatchSizes", func(t *testing.T) {
  11. // Verify that batch sizes are significantly increased
  12. if config.BatchSize < 15000 {
  13. t.Errorf("Expected batch size >= 15000, got %d", config.BatchSize)
  14. }
  15. t.Logf("✅ Batch size optimized: %d", config.BatchSize)
  16. // Test adaptive optimization with increased limits
  17. ao := NewAdaptiveOptimizer(config)
  18. batchController := ao.batchSizeController
  19. if batchController.minBatchSize < 500 {
  20. t.Errorf("Expected min batch size >= 500, got %d", batchController.minBatchSize)
  21. }
  22. if batchController.maxBatchSize < config.BatchSize*4 {
  23. t.Errorf("Expected max batch size >= %d, got %d", config.BatchSize*4, batchController.maxBatchSize)
  24. }
  25. t.Logf("✅ Adaptive optimization limits: min=%d, max=%d",
  26. batchController.minBatchSize, batchController.maxBatchSize)
  27. })
  28. t.Run("RotationScannerInitialization", func(t *testing.T) {
  29. // Test rotation scanner initialization
  30. scanner := NewRotationScanner(nil)
  31. if scanner == nil {
  32. t.Fatal("Failed to create rotation scanner")
  33. }
  34. if scanner.config == nil {
  35. t.Fatal("Rotation scanner config is nil")
  36. }
  37. // Verify default configuration is optimized for throughput
  38. if !scanner.config.EnableParallelScan {
  39. t.Error("Expected parallel scanning to be enabled by default")
  40. }
  41. if !scanner.config.PrioritizeBySize {
  42. t.Error("Expected size-based prioritization to be enabled")
  43. }
  44. t.Log("✅ Rotation scanner initialized with optimized defaults")
  45. })
  46. t.Run("ThroughputOptimizerIntegration", func(t *testing.T) {
  47. // Create a parallel indexer with rotation scanner
  48. indexer := NewParallelIndexer(config, nil)
  49. if indexer == nil {
  50. t.Fatal("Failed to create parallel indexer")
  51. }
  52. if indexer.rotationScanner == nil {
  53. t.Fatal("Rotation scanner not initialized in parallel indexer")
  54. }
  55. // Test throughput optimizer
  56. optimizer := NewThroughputOptimizer(indexer, nil)
  57. if optimizer == nil {
  58. t.Fatal("Failed to create throughput optimizer")
  59. }
  60. // Verify optimizer configuration
  61. optimizedConfig := optimizer.OptimizeIndexerConfig()
  62. if optimizedConfig.BatchSize < 15000 {
  63. t.Errorf("Expected optimized batch size >= 15000, got %d", optimizedConfig.BatchSize)
  64. }
  65. t.Logf("✅ Throughput optimizer created optimized config with batch size: %d", optimizedConfig.BatchSize)
  66. })
  67. t.Run("RotationLogFileInfo", func(t *testing.T) {
  68. // Test that RotationLogFileInfo works correctly
  69. scanner := NewRotationScanner(nil)
  70. // Test scan configuration
  71. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  72. defer cancel()
  73. // Test with empty paths to ensure no crashes
  74. err := scanner.ScanLogGroups(ctx, []string{})
  75. if err != nil {
  76. t.Errorf("Expected no error for empty paths, got: %v", err)
  77. }
  78. // Verify queue operations work
  79. queueSize := scanner.GetQueueSize()
  80. if queueSize < 0 {
  81. t.Error("Queue size should not be negative")
  82. }
  83. // Test batch retrieval
  84. batch := scanner.GetNextBatch(10)
  85. if batch == nil {
  86. t.Error("GetNextBatch should return non-nil slice even when empty")
  87. }
  88. if len(batch) != 0 && batch == nil {
  89. t.Error("Batch should be empty slice, not nil")
  90. }
  91. t.Log("✅ Rotation log scanning operations work correctly")
  92. })
  93. }
  94. func TestParserBatchSizeOptimization(t *testing.T) {
  95. t.Run("ParserConfigOptimized", func(t *testing.T) {
  96. // Since parser.go has a global init, we need to check if it's properly configured
  97. // We'll test this by verifying the default config is optimized
  98. config := DefaultIndexerConfig()
  99. // Verify batch sizes are appropriately large for throughput
  100. expectedMinBatch := 15000
  101. if config.BatchSize < expectedMinBatch {
  102. t.Errorf("Expected batch size >= %d, got %d", expectedMinBatch, config.BatchSize)
  103. }
  104. // Verify queue size scales with batch size
  105. expectedMinQueue := config.BatchSize * 10
  106. if config.MaxQueueSize < expectedMinQueue {
  107. t.Errorf("Expected queue size >= %d, got %d", expectedMinQueue, config.MaxQueueSize)
  108. }
  109. t.Logf("✅ Parser configuration optimized: BatchSize=%d, QueueSize=%d",
  110. config.BatchSize, config.MaxQueueSize)
  111. })
  112. }
  113. // Benchmark to verify performance characteristics
  114. func BenchmarkBatchSizeCalculation(b *testing.B) {
  115. config := DefaultIndexerConfig()
  116. b.ResetTimer()
  117. for i := 0; i < b.N; i++ {
  118. _ = config.BatchSize * 2
  119. }
  120. }
  121. func BenchmarkRotationScannerCreation(b *testing.B) {
  122. b.ResetTimer()
  123. for i := 0; i < b.N; i++ {
  124. scanner := NewRotationScanner(nil)
  125. _ = scanner
  126. }
  127. }