File size: 14,847 Bytes
168b0da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
"""
Metrics Collector - Tracks performance metrics for dual storage comparison.
Provides background analytics and comparison reporting without user complexity.
"""

import json
import time
import logging
from typing import Dict, List, Any, Optional
from pathlib import Path
from collections import defaultdict, deque
import statistics


class MetricsCollector:
    """
    Collects and analyzes performance metrics for dual storage comparison.
    Tracks storage/search performance, accuracy, and provides comparison analytics.
    """

    def __init__(self, max_samples: int = 1000):
        """
        Initialize metrics collector.

        Args:
            max_samples (int): Maximum number of samples to keep in memory
        """
        self.logger = logging.getLogger(__name__)
        self.max_samples = max_samples

        # Storage metrics
        self.storage_metrics = {
            "memvid": deque(maxlen=max_samples),
            "vector": deque(maxlen=max_samples),
        }

        # Search metrics
        self.search_metrics = {
            "memvid": deque(maxlen=max_samples),
            "vector": deque(maxlen=max_samples),
        }

        # Comparison metrics
        self.comparison_data = {
            "storage_comparisons": deque(maxlen=max_samples),
            "search_comparisons": deque(maxlen=max_samples),
        }

        # Client-specific metrics
        self.client_metrics = defaultdict(
            lambda: {
                "storage_count": 0,
                "search_count": 0,
                "total_data_stored": 0,
                "preferred_mode": "unknown",
            }
        )

        self.logger.info("MetricsCollector initialized")

    def track_storage_operation(
        self, backend: str, duration: float, data_size: int, client_id: str = ""
    ) -> None:
        """
        Track a storage operation.

        Args:
            backend (str): Storage backend (memvid/vector)
            duration (float): Operation duration in seconds
            data_size (int): Size of data stored in bytes
            client_id (str): Client identifier
        """
        metric = {
            "timestamp": time.time(),
            "backend": backend,
            "duration": duration,
            "data_size": data_size,
            "client_id": client_id,
        }

        self.storage_metrics[backend].append(metric)

        if client_id:
            self.client_metrics[client_id]["storage_count"] += 1
            self.client_metrics[client_id]["total_data_stored"] += data_size

    def track_search_operation(
        self, backend: str, duration: float, top_k: int, client_id: str = ""
    ) -> None:
        """
        Track a search operation.

        Args:
            backend (str): Storage backend (memvid/vector)
            duration (float): Operation duration in seconds
            top_k (int): Number of results requested
            client_id (str): Client identifier
        """
        metric = {
            "timestamp": time.time(),
            "backend": backend,
            "duration": duration,
            "top_k": top_k,
            "client_id": client_id,
        }

        self.search_metrics[backend].append(metric)

        if client_id:
            self.client_metrics[client_id]["search_count"] += 1

    def track_dual_storage_comparison(
        self, memvid_time: float, vector_time: float, data_size: int, client_id: str
    ) -> None:
        """
        Track dual storage comparison metrics.

        Args:
            memvid_time (float): Memvid storage time
            vector_time (float): Vector storage time
            data_size (int): Size of data stored
            client_id (str): Client identifier
        """
        comparison = {
            "timestamp": time.time(),
            "memvid_time": memvid_time,
            "vector_time": vector_time,
            "data_size": data_size,
            "client_id": client_id,
            "winner": "memvid" if memvid_time < vector_time else "vector",
            "speedup": max(memvid_time, vector_time) / min(memvid_time, vector_time),
        }

        self.comparison_data["storage_comparisons"].append(comparison)

    def track_dual_search_comparison(
        self, memvid_time: float, vector_time: float, query: str, client_id: str
    ) -> None:
        """
        Track dual search comparison metrics.

        Args:
            memvid_time (float): Memvid search time
            vector_time (float): Vector search time
            query (str): Search query
            client_id (str): Client identifier
        """
        comparison = {
            "timestamp": time.time(),
            "memvid_time": memvid_time,
            "vector_time": vector_time,
            "query_length": len(query),
            "client_id": client_id,
            "winner": "memvid" if memvid_time < vector_time else "vector",
            "speedup": (
                max(memvid_time, vector_time) / min(memvid_time, vector_time)
                if min(memvid_time, vector_time) > 0
                else 1.0
            ),
        }

        self.comparison_data["search_comparisons"].append(comparison)

    def get_comparison_report(self, client_id: str = "") -> str:
        """
        Generate comprehensive comparison report.

        Args:
            client_id (str): Client identifier (empty for global report)

        Returns:
            str: JSON string with comparison analytics
        """
        try:
            report = {
                "report_timestamp": time.time(),
                "client_id": client_id or "global",
                "storage_mode": "dual",
                "summary": self._generate_summary(client_id),
                "performance_analysis": self._analyze_performance(client_id),
                "recommendations": self._generate_recommendations(client_id),
            }

            return json.dumps(report, indent=2)

        except Exception as e:
            self.logger.error(f"Error generating comparison report: {e}")
            return json.dumps({"error": f"Failed to generate report: {str(e)}"})

    def _generate_summary(self, client_id: str = "") -> Dict[str, Any]:
        """Generate performance summary."""
        storage_comps = list(self.comparison_data["storage_comparisons"])
        search_comps = list(self.comparison_data["search_comparisons"])

        # Filter by client if specified
        if client_id:
            storage_comps = [c for c in storage_comps if c["client_id"] == client_id]
            search_comps = [c for c in search_comps if c["client_id"] == client_id]

        if not storage_comps and not search_comps:
            return {"message": "No comparison data available"}

        summary = {
            "total_comparisons": len(storage_comps) + len(search_comps),
            "storage_comparisons": len(storage_comps),
            "search_comparisons": len(search_comps),
        }

        # Storage performance summary
        if storage_comps:
            memvid_wins = sum(1 for c in storage_comps if c["winner"] == "memvid")
            avg_speedup = statistics.mean([c["speedup"] for c in storage_comps])

            summary["storage_performance"] = {
                "memvid_wins": memvid_wins,
                "vector_wins": len(storage_comps) - memvid_wins,
                "avg_speedup_factor": round(avg_speedup, 2),
                "faster_backend": (
                    "memvid" if memvid_wins > len(storage_comps) / 2 else "vector"
                ),
            }

        # Search performance summary
        if search_comps:
            memvid_wins = sum(1 for c in search_comps if c["winner"] == "memvid")
            avg_speedup = statistics.mean([c["speedup"] for c in search_comps])

            summary["search_performance"] = {
                "memvid_wins": memvid_wins,
                "vector_wins": len(search_comps) - memvid_wins,
                "avg_speedup_factor": round(avg_speedup, 2),
                "faster_backend": (
                    "memvid" if memvid_wins > len(search_comps) / 2 else "vector"
                ),
            }

        return summary

    def _analyze_performance(self, client_id: str = "") -> Dict[str, Any]:
        """Analyze detailed performance metrics."""
        analysis = {}

        # Analyze storage performance
        memvid_storage = [
            m
            for m in self.storage_metrics["memvid"]
            if not client_id or m["client_id"] == client_id
        ]
        vector_storage = [
            m
            for m in self.storage_metrics["vector"]
            if not client_id or m["client_id"] == client_id
        ]

        if memvid_storage:
            analysis["memvid_storage"] = {
                "avg_duration_ms": round(
                    statistics.mean([m["duration"] for m in memvid_storage]) * 1000, 2
                ),
                "total_operations": len(memvid_storage),
                "total_data_mb": round(
                    sum([m["data_size"] for m in memvid_storage]) / (1024 * 1024), 2
                ),
            }

        if vector_storage:
            analysis["vector_storage"] = {
                "avg_duration_ms": round(
                    statistics.mean([m["duration"] for m in vector_storage]) * 1000, 2
                ),
                "total_operations": len(vector_storage),
                "total_data_mb": round(
                    sum([m["data_size"] for m in vector_storage]) / (1024 * 1024), 2
                ),
            }

        # Analyze search performance
        memvid_search = [
            m
            for m in self.search_metrics["memvid"]
            if not client_id or m["client_id"] == client_id
        ]
        vector_search = [
            m
            for m in self.search_metrics["vector"]
            if not client_id or m["client_id"] == client_id
        ]

        if memvid_search:
            analysis["memvid_search"] = {
                "avg_duration_ms": round(
                    statistics.mean([m["duration"] for m in memvid_search]) * 1000, 2
                ),
                "total_searches": len(memvid_search),
            }

        if vector_search:
            analysis["vector_search"] = {
                "avg_duration_ms": round(
                    statistics.mean([m["duration"] for m in vector_search]) * 1000, 2
                ),
                "total_searches": len(vector_search),
            }

        return analysis

    def _generate_recommendations(self, client_id: str = "") -> List[str]:
        """Generate performance-based recommendations."""
        recommendations = []

        storage_comps = list(self.comparison_data["storage_comparisons"])
        search_comps = list(self.comparison_data["search_comparisons"])

        # Filter by client if specified
        if client_id:
            storage_comps = [c for c in storage_comps if c["client_id"] == client_id]
            search_comps = [c for c in search_comps if c["client_id"] == client_id]

        if not storage_comps and not search_comps:
            recommendations.append("No comparison data available for recommendations")
            return recommendations

        # Storage recommendations
        if storage_comps:
            memvid_wins = sum(1 for c in storage_comps if c["winner"] == "memvid")
            if memvid_wins > len(storage_comps) * 0.7:
                recommendations.append(
                    "πŸ“Ή Memvid shows consistently faster storage - consider memvid_only mode for write-heavy workloads"
                )
            elif memvid_wins < len(storage_comps) * 0.3:
                recommendations.append(
                    "⚑ Vector storage shows faster performance - consider vector_only mode for high-frequency storage"
                )
            else:
                recommendations.append(
                    "βš–οΈ Storage performance is balanced - dual mode provides good comparison data"
                )

        # Search recommendations
        if search_comps:
            memvid_wins = sum(1 for c in search_comps if c["winner"] == "memvid")
            if memvid_wins > len(search_comps) * 0.7:
                recommendations.append(
                    "πŸ” Memvid shows superior search performance - excellent for semantic search workloads"
                )
            elif memvid_wins < len(search_comps) * 0.3:
                recommendations.append(
                    "πŸš€ Vector search outperforms memvid - consider vector_only for search-heavy applications"
                )
            else:
                recommendations.append(
                    "🎯 Search performance varies - dual mode provides valuable insights"
                )

        # Data size recommendations
        if storage_comps:
            avg_data_size = statistics.mean([c["data_size"] for c in storage_comps])
            if avg_data_size > 10000:  # Large chunks
                recommendations.append(
                    "πŸ“Š Large data chunks detected - memvid compression may provide storage efficiency benefits"
                )
            elif avg_data_size < 1000:  # Small chunks
                recommendations.append(
                    "⚑ Small data chunks detected - vector storage may have lower overhead"
                )

        return recommendations

    def export_metrics(self, format: str = "json") -> str:
        """
        Export metrics data.

        Args:
            format (str): Export format (json, csv)

        Returns:
            str: Exported metrics data
        """
        try:
            if format.lower() == "json":
                export_data = {
                    "export_timestamp": time.time(),
                    "storage_metrics": {
                        "memvid": list(self.storage_metrics["memvid"]),
                        "vector": list(self.storage_metrics["vector"]),
                    },
                    "search_metrics": {
                        "memvid": list(self.search_metrics["memvid"]),
                        "vector": list(self.search_metrics["vector"]),
                    },
                    "comparison_data": {
                        "storage_comparisons": list(
                            self.comparison_data["storage_comparisons"]
                        ),
                        "search_comparisons": list(
                            self.comparison_data["search_comparisons"]
                        ),
                    },
                    "client_metrics": dict(self.client_metrics),
                }
                return json.dumps(export_data, indent=2)
            else:
                return f"Error: Unsupported format '{format}'. Supported: json"

        except Exception as e:
            return f"Error exporting metrics: {str(e)}"