File size: 5,930 Bytes
09281fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
"""
Performance analysis script to identify bottlenecks
"""

import os
import sys
import time
import cProfile
import pstats
from pathlib import Path

def analyze_performance():
    """Analyze performance of each component"""
    print("⚑ PERFORMANCE ANALYSIS")
    print("=" * 50)
    
    try:
        from rag_system import AdvancedRAGSystem
        from document_processer import AdvancedDocumentProcessor
        from vector_database import VectorDatabase
        from query_parser import AdvancedQueryParser
        from llm_reasoning import AdvancedLLMReasoning
        
        file_path = "doc2.pdf"
        if not os.path.exists(file_path):
            print(f"❌ File not found: {file_path}")
            return
        
        print(f"πŸ“„ Testing with file: {file_path}")
        
        # Test 1: Document Processing Performance
        print("\n1️⃣ DOCUMENT PROCESSING PERFORMANCE")
        print("-" * 40)
        
        doc_processor = AdvancedDocumentProcessor()
        start_time = time.time()
        chunks = doc_processor.process_document(file_path, use_ocr=False)
        doc_time = time.time() - start_time
        
        print(f"βœ… Document processing: {doc_time:.2f}s")
        print(f"πŸ“Š Chunks created: {len(chunks)}")
        print(f"πŸ“Š Average time per chunk: {doc_time/len(chunks):.4f}s")
        
        # Test 2: Vector Database Performance
        print("\n2️⃣ VECTOR DATABASE PERFORMANCE")
        print("-" * 40)
        
        vector_db = VectorDatabase()
        start_time = time.time()
        success = vector_db.add_documents(chunks)
        vector_time = time.time() - start_time
        
        print(f"βœ… Vector database addition: {vector_time:.2f}s")
        print(f"πŸ“Š Success: {success}")
        print(f"πŸ“Š Average time per chunk: {vector_time/len(chunks):.4f}s")
        
        # Test 3: Query Parser Performance
        print("\n3️⃣ QUERY PARSER PERFORMANCE")
        print("-" * 40)
        
        query_parser = AdvancedQueryParser()
        test_query = "Does the policy cover newborn care after hospital discharge?"
        
        start_time = time.time()
        parsed = query_parser.parse_query(test_query)
        parser_time = time.time() - start_time
        
        print(f"βœ… Query parsing: {parser_time:.2f}s")
        print(f"πŸ“Š Query type: {parsed.query_type}")
        print(f"πŸ“Š Confidence: {parsed.confidence}")
        
        # Test 4: LLM Reasoning Performance
        print("\n4️⃣ LLM REASONING PERFORMANCE")
        print("-" * 40)
        
        reasoning_engine = AdvancedLLMReasoning(use_gpu=False)
        test_context = [{"content": "Sample policy content", "source_file": "test.pdf"}]
        
        start_time = time.time()
        result = reasoning_engine.analyze_query(test_query, test_context, "coverage_inquiry")
        reasoning_time = time.time() - start_time
        
        print(f"βœ… LLM reasoning: {reasoning_time:.2f}s")
        print(f"πŸ“Š Decision: {result.decision}")
        print(f"πŸ“Š Confidence: {result.confidence_score}")
        
        # Test 5: Full RAG System Performance
        print("\n5️⃣ FULL RAG SYSTEM PERFORMANCE")
        print("-" * 40)
        
        rag_system = AdvancedRAGSystem(use_gpu=False)
        
        # Document ingestion
        start_time = time.time()
        rag_chunks = rag_system.ingest_document(file_path, use_ocr=False)
        ingestion_time = time.time() - start_time
        
        print(f"βœ… Document ingestion: {ingestion_time:.2f}s")
        print(f"πŸ“Š Chunks ingested: {len(rag_chunks)}")
        
        # Query processing
        start_time = time.time()
        query_result = rag_system.process_query(test_query)
        query_time = time.time() - start_time
        
        print(f"βœ… Query processing: {query_time:.2f}s")
        print(f"πŸ“Š Total time: {ingestion_time + query_time:.2f}s")
        
        # Performance Summary
        print("\nπŸ“Š PERFORMANCE SUMMARY")
        print("=" * 50)
        print(f"Document Processing: {doc_time:.2f}s ({doc_time/(ingestion_time + query_time)*100:.1f}%)")
        print(f"Vector Database: {vector_time:.2f}s ({vector_time/(ingestion_time + query_time)*100:.1f}%)")
        print(f"Query Parsing: {parser_time:.2f}s ({parser_time/(ingestion_time + query_time)*100:.1f}%)")
        print(f"LLM Reasoning: {reasoning_time:.2f}s ({reasoning_time/(ingestion_time + query_time)*100:.1f}%)")
        print(f"TOTAL TIME: {ingestion_time + query_time:.2f}s")
        
        # Optimization Recommendations
        print("\nπŸ’‘ OPTIMIZATION RECOMMENDATIONS")
        print("=" * 50)
        
        if doc_time > 10:
            print("πŸ”§ Document processing is slow - consider:")
            print("   - Reduce chunk size")
            print("   - Use parallel processing")
            print("   - Optimize OCR settings")
        
        if vector_time > 20:
            print("πŸ”§ Vector database is slow - consider:")
            print("   - Use GPU for embeddings")
            print("   - Batch processing")
            print("   - Reduce embedding dimensions")
        
        if reasoning_time > 30:
            print("πŸ”§ LLM reasoning is slow - consider:")
            print("   - Use smaller model")
            print("   - Enable GPU acceleration")
            print("   - Reduce max tokens")
            print("   - Use caching")
        
        if ingestion_time + query_time > 30:
            print("πŸ”§ Overall system is slow - consider:")
            print("   - Enable GPU for all components")
            print("   - Use model quantization")
            print("   - Implement caching")
            print("   - Parallel processing")
        
    except Exception as e:
        print(f"❌ Error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    analyze_performance()