File size: 7,857 Bytes
2fb680d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
import hashlib
import time
import psutil
import gc
from typing import Dict, List, Optional, Any
from collections import OrderedDict
from dataclasses import dataclass
from datetime import datetime, timedelta
import redis
import json


@dataclass
class CacheEntry:
    response: str
    timestamp: datetime
    hit_count: int = 0


class PerformanceOptimizer:
    def __init__(self,
                 cache_size: int = 100,
                 cache_ttl_hours: int = 24,
                 use_redis: bool = False):
        """Initialize performance optimizer with caching"""

        self.cache_size = cache_size
        self.cache_ttl = timedelta(hours=cache_ttl_hours)

        # Use Redis if available, fallback to in-memory
        self.use_redis = use_redis
        if use_redis:
            try:
                self.redis_client = redis.Redis(
                    host='localhost',
                    port=6379,
                    decode_responses=True
                )
                self.redis_client.ping()
            except:
                print("Redis not available, using in-memory cache")
                self.use_redis = False
                self.cache = OrderedDict()
        else:
            self.cache = OrderedDict()

        # Metrics
        self.metrics = {
            "cache_hits": 0,
            "cache_misses": 0,
            "total_requests": 0,
            "average_response_time": 0,
            "memory_usage_mb": 0
        }

    def _hash_prompt(self, prompt: str) -> str:
        """Create hash for caching"""
        normalized = prompt.lower().strip()
        return hashlib.md5(normalized.encode()).hexdigest()

    def get_cached_response(self, prompt: str) -> Optional[str]:
        """Get response from cache if available"""

        self.metrics["total_requests"] += 1
        prompt_hash = self._hash_prompt(prompt)

        if self.use_redis:
            cached = self.redis_client.get(f"chat:{prompt_hash}")
            if cached:
                self.metrics["cache_hits"] += 1
                # Update hit count
                self.redis_client.hincrby(f"chat:stats:{prompt_hash}", "hits", 1)
                return json.loads(cached)["response"]
        else:
            if prompt_hash in self.cache:
                entry = self.cache[prompt_hash]
                # Check TTL
                if datetime.now() - entry.timestamp < self.cache_ttl:
                    self.metrics["cache_hits"] += 1
                    entry.hit_count += 1
                    # Move to end (LRU)
                    self.cache.move_to_end(prompt_hash)
                    return entry.response
                else:
                    # Expired
                    del self.cache[prompt_hash]

        self.metrics["cache_misses"] += 1
        return None

    def cache_response(self, prompt: str, response: str):
        """Cache a response"""

        prompt_hash = self._hash_prompt(prompt)

        if self.use_redis:
            cache_data = {
                "response": response,
                "timestamp": datetime.now().isoformat()
            }
            self.redis_client.setex(
                f"chat:{prompt_hash}",
                int(self.cache_ttl.total_seconds()),
                json.dumps(cache_data)
            )
            self.redis_client.hset(
                f"chat:stats:{prompt_hash}",
                mapping={"hits": 0, "created": datetime.now().isoformat()}
            )
        else:
            # LRU cache management
            if len(self.cache) >= self.cache_size:
                # Remove least recently used
                self.cache.popitem(last=False)

            self.cache[prompt_hash] = CacheEntry(
                response=response,
                timestamp=datetime.now()
            )

    def get_metrics(self) -> Dict[str, Any]:
        """Get performance metrics"""

        # Update memory usage
        process = psutil.Process()
        self.metrics["memory_usage_mb"] = process.memory_info().rss / 1024 / 1024

        # Calculate cache hit rate
        if self.metrics["total_requests"] > 0:
            self.metrics["cache_hit_rate"] = (
                    self.metrics["cache_hits"] / self.metrics["total_requests"]
            )

        return self.metrics

    def clear_cache(self):
        """Clear all cached responses"""

        if self.use_redis:
            for key in self.redis_client.scan_iter("chat:*"):
                self.redis_client.delete(key)
        else:
            self.cache.clear()

        gc.collect()


class MemoryManager:
    def __init__(self, max_memory_mb: int = 8192):
        """Initialize memory manager"""

        self.max_memory_mb = max_memory_mb
        self.warning_threshold = 0.8  # Warn at 80% usage
        self.critical_threshold = 0.9  # Critical at 90% usage

    def check_memory(self) -> Dict[str, Any]:
        """Check current memory usage"""

        process = psutil.Process()
        memory_info = process.memory_info()

        current_mb = memory_info.rss / 1024 / 1024
        percentage = current_mb / self.max_memory_mb

        status = "normal"
        if percentage > self.critical_threshold:
            status = "critical"
        elif percentage > self.warning_threshold:
            status = "warning"

        return {
            "current_mb": round(current_mb, 2),
            "max_mb": self.max_memory_mb,
            "percentage": round(percentage * 100, 2),
            "status": status,
            "available_mb": round(self.max_memory_mb - current_mb, 2)
        }

    def optimize_if_needed(self) -> bool:
        """Run optimization if memory usage is high"""

        memory_status = self.check_memory()

        if memory_status["status"] in ["warning", "critical"]:
            # Force garbage collection
            gc.collect()

            # Clear unused objects
            if memory_status["status"] == "critical":
                # More aggressive cleanup
                gc.collect(2)

                return True

        return False


class RequestBatcher:
    def __init__(self, batch_size: int = 5, timeout_ms: int = 100):
        """Initialize request batcher for efficiency"""

        self.batch_size = batch_size
        self.timeout_ms = timeout_ms
        self.pending_requests = []
        self.results = {}

    async def add_request(self, request_id: str, prompt: str) -> str:
        """Add request to batch"""

        self.pending_requests.append({
            "id": request_id,
            "prompt": prompt,
            "timestamp": time.time()
        })

        # Process if batch is full
        if len(self.pending_requests) >= self.batch_size:
            await self._process_batch()
        else:
            # Wait for timeout
            await asyncio.sleep(self.timeout_ms / 1000)
            if request_id not in self.results:
                await self._process_batch()

        return self.results.get(request_id, "Error processing request")

    async def _process_batch(self):
        """Process pending requests as batch"""

        if not self.pending_requests:
            return

        batch = self.pending_requests[:self.batch_size]
        self.pending_requests = self.pending_requests[self.batch_size:]

        # Process batch (simulate concurrent processing)
        tasks = []
        for request in batch:
            # In production, this would call the LLM
            tasks.append(self._process_single(request))

        results = await asyncio.gather(*tasks)

        for request, result in zip(batch, results):
            self.results[request["id"]] = result

    async def _process_single(self, request: Dict[str, Any]) -> str:
        """Process single request (placeholder)"""

        # Simulate processing
        await asyncio.sleep(0.1)
        return f"Response to: {request['prompt']}"