File size: 14,086 Bytes
a963d65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python3
"""
Comprehensive Batch Processing Demo Analysis
Deep analysis of Modal scaling implementation and batch processing capabilities
"""

import asyncio
import sys
import os
import time
import json
from datetime import datetime

# Add src to path for imports
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'fhirflame', 'src'))
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'fhirflame'))

def test_heavy_workload_demo_import():
    """Test 1: Heavy Workload Demo Import and Initialization"""
    print("πŸ” TEST 1: Heavy Workload Demo Import")
    print("-" * 50)
    
    try:
        from fhirflame.src.heavy_workload_demo import ModalContainerScalingDemo, RealTimeBatchProcessor
        print("βœ… Successfully imported ModalContainerScalingDemo")
        print("βœ… Successfully imported RealTimeBatchProcessor")
        
        # Test initialization
        demo = ModalContainerScalingDemo()
        processor = RealTimeBatchProcessor()
        
        print(f"βœ… Modal demo initialized with {len(demo.regions)} regions")
        print(f"βœ… Batch processor initialized with {len(processor.medical_datasets)} datasets")
        
        # Test configuration
        print(f"   Scaling tiers: {len(demo.scaling_tiers)}")
        print(f"   Workload configs: {len(demo.workload_configs)}")
        print(f"   Default region: {demo.default_region}")
        
        return True, demo, processor
        
    except Exception as e:
        print(f"❌ Heavy workload demo import failed: {e}")
        import traceback
        traceback.print_exc()
        return False, None, None

async def test_modal_scaling_simulation(demo):
    """Test 2: Modal Container Scaling Simulation"""
    print("\nπŸ” TEST 2: Modal Container Scaling Simulation")
    print("-" * 50)
    
    try:
        # Start the Modal scaling demo
        result = await demo.start_modal_scaling_demo()
        print(f"βœ… Modal scaling demo started: {result}")
        
        # Let it run for a few seconds to simulate scaling
        print("πŸ”„ Running Modal scaling simulation for 10 seconds...")
        await asyncio.sleep(10)
        
        # Get statistics during operation
        stats = demo.get_demo_statistics()
        print(f"πŸ“Š Demo Status: {stats['demo_status']}")
        print(f"πŸ“ˆ Active Containers: {stats['active_containers']}")
        print(f"⚑ Requests/sec: {stats['requests_per_second']}")
        print(f"πŸ“¦ Total Processed: {stats['total_requests_processed']}")
        print(f"πŸ”„ Concurrent Requests: {stats['concurrent_requests']}")
        print(f"πŸ’° Cost per Request: {stats['cost_per_request']}")
        print(f"🎯 Scaling Strategy: {stats['scaling_strategy']}")
        
        # Get container details
        containers = demo.get_container_details()
        print(f"🏭 Container Details: {len(containers)} containers active")
        
        if containers:
            print("   Top 3 Container Details:")
            for i, container in enumerate(containers[:3]):
                print(f"   [{i+1}] {container['Container ID']}: {container['Status']} - {container['Requests/sec']} RPS")
        
        # Stop the demo
        demo.stop_demo()
        print("βœ… Modal scaling demo stopped successfully")
        
        return True
        
    except Exception as e:
        print(f"❌ Modal scaling simulation failed: {e}")
        import traceback
        traceback.print_exc()
        return False

def test_batch_processor_datasets(processor):
    """Test 3: Batch Processor Medical Datasets"""
    print("\nπŸ” TEST 3: Batch Processor Medical Datasets")
    print("-" * 50)
    
    try:
        datasets = processor.medical_datasets
        
        for dataset_name, documents in datasets.items():
            print(f"πŸ“‹ Dataset: {dataset_name}")
            print(f"   Documents: {len(documents)}")
            print(f"   Avg length: {sum(len(doc) for doc in documents) // len(documents)} chars")
            
            # Show sample content
            if documents:
                sample = documents[0][:100].replace('\n', ' ').strip()
                print(f"   Sample: {sample}...")
        
        print("βœ… All medical datasets validated")
        return True
        
    except Exception as e:
        print(f"❌ Batch processor dataset test failed: {e}")
        return False

async def test_real_time_batch_processing(processor):
    """Test 4: Real-Time Batch Processing"""
    print("\nπŸ” TEST 4: Real-Time Batch Processing")
    print("-" * 50)
    
    try:
        # Test different workflow types
        workflows_to_test = [
            ("clinical_fhir", 3),
            ("lab_entities", 2),
            ("mixed_workflow", 2)
        ]
        
        results = {}
        
        for workflow_type, batch_size in workflows_to_test:
            print(f"\nπŸ”¬ Testing workflow: {workflow_type} (batch size: {batch_size})")
            
            # Start processing
            success = processor.start_processing(workflow_type, batch_size)
            
            if not success:
                print(f"❌ Failed to start processing for {workflow_type}")
                continue
            
            # Monitor progress
            start_time = time.time()
            while processor.processing:
                status = processor.get_status()
                if status['status'] == 'processing':
                    print(f"   Progress: {status['progress']:.1f}% - {status['processed']}/{status['total']}")
                    await asyncio.sleep(2)
                elif status['status'] == 'completed':
                    break
                else:
                    break
                
                # Timeout after 30 seconds
                if time.time() - start_time > 30:
                    processor.stop_processing()
                    break
            
            # Get final status
            final_status = processor.get_status()
            results[workflow_type] = final_status
            
            if final_status['status'] == 'completed':
                print(f"βœ… {workflow_type} completed: {final_status['processed']} documents")
                print(f"   Total time: {final_status['total_time']:.2f}s")
            else:
                print(f"⚠️ {workflow_type} did not complete fully")
        
        print(f"\nπŸ“Š Batch Processing Summary:")
        for workflow, result in results.items():
            status = result.get('status', 'unknown')
            processed = result.get('processed', 0)
            total_time = result.get('total_time', 0)
            print(f"   {workflow}: {status} - {processed} docs in {total_time:.2f}s")
        
        return True
        
    except Exception as e:
        print(f"❌ Real-time batch processing test failed: {e}")
        import traceback
        traceback.print_exc()
        return False

def test_modal_integration_components():
    """Test 5: Modal Integration Components"""
    print("\nπŸ” TEST 5: Modal Integration Components")
    print("-" * 50)
    
    try:
        # Test Modal functions import
        try:
            from fhirflame.cloud_modal.functions import calculate_real_modal_cost
            print("βœ… Modal functions imported successfully")
            
            # Test cost calculation
            cost_1s = calculate_real_modal_cost(1.0, "L4")
            cost_10s = calculate_real_modal_cost(10.0, "L4")
            
            print(f"   L4 GPU cost (1s): ${cost_1s:.6f}")
            print(f"   L4 GPU cost (10s): ${cost_10s:.6f}")
            
            if cost_10s > cost_1s:
                print("βœ… Cost calculation scaling works correctly")
            else:
                print("⚠️ Cost calculation may have issues")
                
        except ImportError as e:
            print(f"⚠️ Modal functions not available: {e}")
        
        # Test Modal deployment
        try:
            from fhirflame.modal_deployments.fhirflame_modal_app import app, GPU_CONFIGS
            print("βœ… Modal deployment app imported successfully")
            print(f"   GPU configs available: {list(GPU_CONFIGS.keys())}")
            
        except ImportError as e:
            print(f"⚠️ Modal deployment not available: {e}")
        
        # Test Enhanced CodeLlama Processor
        try:
            from fhirflame.src.enhanced_codellama_processor import EnhancedCodeLlamaProcessor
            processor = EnhancedCodeLlamaProcessor()
            print("βœ… Enhanced CodeLlama processor initialized")
            print(f"   Modal available: {processor.router.modal_available}")
            print(f"   Ollama available: {processor.router.ollama_available}")
            print(f"   HuggingFace available: {processor.router.hf_available}")
            
        except Exception as e:
            print(f"⚠️ Enhanced CodeLlama processor issues: {e}")
        
        return True
        
    except Exception as e:
        print(f"❌ Modal integration test failed: {e}")
        return False

def test_frontend_integration():
    """Test 6: Frontend Integration"""
    print("\nπŸ” TEST 6: Frontend Integration")
    print("-" * 50)
    
    try:
        from fhirflame.frontend_ui import heavy_workload_demo, batch_processor
        print("βœ… Frontend UI integration working")
        
        # Test if components are properly initialized
        if heavy_workload_demo is not None:
            print("βœ… Heavy workload demo available in frontend")
        else:
            print("⚠️ Heavy workload demo not properly initialized in frontend")
            
        if batch_processor is not None:
            print("βœ… Batch processor available in frontend")
        else:
            print("⚠️ Batch processor not properly initialized in frontend")
        
        return True
        
    except Exception as e:
        print(f"❌ Frontend integration test failed: {e}")
        return False

async def main():
    """Main comprehensive test execution"""
    print("πŸ”₯ FHIRFLAME BATCH PROCESSING COMPREHENSIVE ANALYSIS")
    print("=" * 60)
    print(f"πŸ• Starting at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print()
    
    # Test results tracking
    test_results = {}
    
    # Test 1: Import and initialization
    success, demo, processor = test_heavy_workload_demo_import()
    test_results["Heavy Workload Demo Import"] = success
    
    if not success:
        print("❌ Critical import failure - cannot continue with tests")
        return 1
    
    # Test 2: Modal scaling simulation
    if demo:
        success = await test_modal_scaling_simulation(demo)
        test_results["Modal Scaling Simulation"] = success
    
    # Test 3: Batch processor datasets
    if processor:
        success = test_batch_processor_datasets(processor)
        test_results["Batch Processor Datasets"] = success
    
    # Test 4: Real-time batch processing
    if processor:
        success = await test_real_time_batch_processing(processor)
        test_results["Real-Time Batch Processing"] = success
    
    # Test 5: Modal integration components
    success = test_modal_integration_components()
    test_results["Modal Integration Components"] = success
    
    # Test 6: Frontend integration
    success = test_frontend_integration()
    test_results["Frontend Integration"] = success
    
    # Final Summary
    print("\n" + "=" * 60)
    print("πŸ“Š COMPREHENSIVE ANALYSIS RESULTS")
    print("=" * 60)
    
    passed = sum(1 for result in test_results.values() if result)
    total = len(test_results)
    
    for test_name, result in test_results.items():
        status = "βœ… PASS" if result else "❌ FAIL"
        print(f"{test_name}: {status}")
    
    print(f"\nOverall Score: {passed}/{total} tests passed ({passed/total*100:.1f}%)")
    
    # Analysis Summary
    print(f"\n🎯 BATCH PROCESSING IMPLEMENTATION ANALYSIS:")
    print(f"=" * 60)
    
    if passed >= total * 0.8:  # 80% or higher
        print("πŸŽ‰ EXCELLENT: Batch processing implementation is comprehensive and working")
        print("βœ… Modal scaling demo is properly implemented")
        print("βœ… Real-time batch processing is functional")
        print("βœ… Integration between components is solid")
        print("βœ… Frontend integration is working")
        print("\nπŸš€ READY FOR PRODUCTION DEMONSTRATION")
    elif passed >= total * 0.6:  # 60-79%
        print("πŸ‘ GOOD: Batch processing implementation is mostly working")
        print("βœ… Core functionality is implemented")
        print("⚠️ Some integration issues may exist")
        print("\nπŸ”§ MINOR FIXES RECOMMENDED")
    else:  # Below 60%
        print("⚠️ ISSUES DETECTED: Batch processing implementation needs attention")
        print("❌ Critical components may not be working properly")
        print("❌ Integration issues present")
        print("\nπŸ› οΈ SIGNIFICANT FIXES REQUIRED")
    
    print(f"\nπŸ“‹ RECOMMENDATIONS:")
    
    if not test_results.get("Modal Scaling Simulation", True):
        print("- Fix Modal container scaling simulation")
    
    if not test_results.get("Real-Time Batch Processing", True):
        print("- Debug real-time batch processing workflow")
    
    if not test_results.get("Modal Integration Components", True):
        print("- Ensure Modal integration components are properly configured")
    
    if not test_results.get("Frontend Integration", True):
        print("- Fix frontend UI integration issues")
    
    print(f"\n🏁 Analysis completed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    
    return 0 if passed >= total * 0.8 else 1

if __name__ == "__main__":
    try:
        exit_code = asyncio.run(main())
        sys.exit(exit_code)
    except KeyboardInterrupt:
        print("\nπŸ›‘ Analysis interrupted by user")
        sys.exit(1)
    except Exception as e:
        print(f"\nπŸ’₯ Analysis failed with error: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)