-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathprofile_suite.py
More file actions
394 lines (315 loc) · 12.2 KB
/
profile_suite.py
File metadata and controls
394 lines (315 loc) · 12.2 KB
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
"""Profiling test suite for identifying bottlenecks in Empathy Framework.
Runs performance profiling on key operations to identify optimization opportunities.
Usage:
python benchmarks/profile_suite.py
Copyright 2025 Smart-AI-Memory
Licensed under Fair Source License 0.9
"""
import sys
from pathlib import Path
# Add project root to path (for scripts/ and src/)
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
sys.path.insert(0, str(project_root / "src"))
from scripts.profile_utils import profile_function, time_function
@profile_function(output_file="benchmarks/profiles/scanner_scan.prof")
@time_function
def profile_scanner():
"""Profile project scanner on real codebase."""
from empathy_os.project_index.scanner import ProjectScanner
print("\n" + "=" * 60)
print("Profiling: Project Scanner")
print("=" * 60)
scanner = ProjectScanner(project_root=".")
records, summary = scanner.scan()
print(f"✓ Scanned {summary.total_files} files")
print(f"✓ Source files: {summary.source_files}")
print(f"✓ Test files: {summary.test_files}")
print(f"✓ Lines of code: {summary.total_lines_of_code:,}")
@profile_function(output_file="benchmarks/profiles/pattern_library.prof")
@time_function
def profile_pattern_library():
"""Profile pattern library operations."""
from empathy_os.pattern_library import Pattern, PatternLibrary
print("\n" + "=" * 60)
print("Profiling: Pattern Library")
print("=" * 60)
library = PatternLibrary()
# Create some test patterns
for i in range(100):
pattern = Pattern(
id=f"pat_{i:03d}",
agent_id=f"agent_{i % 10}",
pattern_type=["sequential", "temporal", "conditional", "behavioral"][i % 4],
name=f"test_pattern_{i}",
description=f"Test pattern {i}",
tags=[f"tag_{i % 10}"],
confidence=0.5 + (i % 50) / 100,
)
library.contribute_pattern(f"agent_{i % 10}", pattern)
# Simulate pattern matching
match_count = 0
for i in range(1000):
context = {
"task_type": f"task_{i % 5}",
"user_role": "developer",
"time_of_day": ["morning", "afternoon", "evening"][i % 3],
}
matches = library.query_patterns(
agent_id=f"agent_{i % 10}", context=context, pattern_type=None
)
match_count += len(matches)
print("✓ Created 100 patterns")
print("✓ Performed 1000 pattern matches")
print(f"✓ Total matches: {match_count}")
@profile_function(output_file="benchmarks/profiles/cost_tracker.prof")
@time_function
def profile_cost_tracker():
"""Profile cost tracking operations."""
from empathy_os.cost_tracker import CostTracker
print("\n" + "=" * 60)
print("Profiling: Cost Tracker")
print("=" * 60)
tracker = CostTracker()
# Simulate logging 1000 requests
for i in range(1000):
tracker.log_request(
model=f"claude-3-{'haiku' if i % 3 == 0 else 'sonnet'}",
input_tokens=100 + i % 100,
output_tokens=50 + i % 50,
task_type=f"task_{i % 5}",
)
summary = tracker.get_summary(days=7)
print("✓ Logged 1000 requests")
print(f"✓ Actual cost: ${summary['actual_cost']:.4f}")
print(f"✓ Input tokens: {summary['input_tokens']:,}")
print(f"✓ Output tokens: {summary['output_tokens']:,}")
@profile_function(output_file="benchmarks/profiles/feedback_loops.prof")
@time_function
def profile_feedback_loops():
"""Profile feedback loop detection."""
from empathy_os.feedback_loops import FeedbackLoopDetector
print("\n" + "=" * 60)
print("Profiling: Feedback Loop Detector")
print("=" * 60)
detector = FeedbackLoopDetector()
# Generate test session history
session_history = []
for i in range(500):
session_history.append(
{
"trust": 0.5 + (i % 50) / 100,
"success_rate": 0.6 + (i % 40) / 100,
"patterns_used": i % 10,
"user_satisfaction": 0.7 + (i % 30) / 100,
}
)
# Detect loops multiple times (simulate repeated checks)
virtuous_count = 0
vicious_count = 0
active_count = 0
for _ in range(100):
if detector.detect_virtuous_cycle(session_history):
virtuous_count += 1
if detector.detect_vicious_cycle(session_history):
vicious_count += 1
active = detector.detect_active_loop(session_history)
if active:
active_count += 1
print("✓ Generated 500-item session history")
print("✓ Ran 100 detection cycles")
print(f"✓ Virtuous cycles detected: {virtuous_count}")
print(f"✓ Vicious cycles detected: {vicious_count}")
print(f"✓ Active loops detected: {active_count}")
@profile_function(output_file="benchmarks/profiles/workflow_execution.prof")
@time_function
def profile_workflow_execution():
"""Profile workflow execution data processing."""
from empathy_os.workflows.base import ModelTier, _load_workflow_history
print("\n" + "=" * 60)
print("Profiling: Workflow Execution")
print("=" * 60)
# Load workflow history
history = _load_workflow_history()
print(f"✓ Loaded {len(history)} workflow history entries")
# Simulate workflow data processing
results = []
for i in range(200):
result_data = {
"workflow_id": f"workflow_{i % 5}",
"success": i % 10 != 0,
"output": {"result": f"Output {i}", "confidence": 0.5 + (i % 50) / 100},
"error": None if i % 10 != 0 else "Simulated error",
"metadata": {
"execution_time": 0.1 + (i % 10) / 100,
"tokens_used": 100 + i % 100,
"tier": ModelTier.CHEAP.value if i % 3 == 0 else ModelTier.CAPABLE.value,
},
}
results.append(result_data)
print(f"✓ Created {len(results)} workflow result objects")
# Process results
successful = sum(1 for r in results if r["success"])
failed = [r for r in results if not r["success"]]
avg_tokens = sum(r["metadata"]["tokens_used"] for r in results) / len(results)
print(f"✓ Success rate: {successful}/{len(results)} ({successful/len(results)*100:.1f}%)")
print(f"✓ Failed: {len(failed)}")
print(f"✓ Avg tokens: {avg_tokens:.0f}")
@profile_function(output_file="benchmarks/profiles/memory_operations.prof")
@time_function
def profile_memory_operations():
"""Profile unified memory operations."""
from empathy_os.memory.unified import UnifiedMemory
print("\n" + "=" * 60)
print("Profiling: Memory Operations")
print("=" * 60)
memory = UnifiedMemory(user_id="profiler_test")
# Test stash/retrieve operations (short-term memory)
stash_count = 0
for i in range(200):
key = f"temp_data_{i}"
value = {
"content": f"Memory content {i} " * 10,
"metadata": {
"timestamp": f"2026-01-10T00:{i:02d}:00Z",
"type": ["fact", "procedure", "experience"][i % 3],
"importance": 0.5 + (i % 50) / 100,
},
}
if memory.stash(key, value, ttl_seconds=3600):
stash_count += 1
print(f"✓ Stashed {stash_count} items in short-term memory")
# Test retrieval
retrieve_count = 0
for i in range(100):
key = f"temp_data_{i}"
result = memory.retrieve(key)
if result is not None:
retrieve_count += 1
print(f"✓ Retrieved {retrieve_count} items from short-term memory")
# Test pattern staging and persistence
pattern_count = 0
for i in range(50):
pattern_data = {
"id": f"memory_pattern_{i}",
"name": f"Test pattern {i}",
"description": f"Memory test pattern {i}",
"tags": [f"tag_{i % 10}"],
"confidence": 0.5 + (i % 50) / 100,
"examples": [f"example_{j}" for j in range(3)],
}
pattern_id = memory.stage_pattern(
pattern_data=pattern_data, pattern_type="sequential", ttl_hours=24
)
if pattern_id:
pattern_count += 1
print(f"✓ Staged {pattern_count} patterns")
# Get staged patterns
staged = memory.get_staged_patterns()
print(f"✓ Retrieved {len(staged)} staged patterns")
# Test pattern recall
recall_count = 0
for i in range(50):
pattern = memory.recall_pattern(pattern_id=f"memory_pattern_{i % pattern_count}")
if pattern:
recall_count += 1
print(f"✓ Recalled {recall_count} patterns")
@profile_function(output_file="benchmarks/profiles/test_generation.prof")
@time_function
def profile_test_generation():
"""Profile test generation workflow."""
from empathy_os.workflows.test_gen import TestGenerationWorkflow
print("\n" + "=" * 60)
print("Profiling: Test Generation")
print("=" * 60)
workflow = TestGenerationWorkflow()
# Simulate analyzing functions for test generation
test_functions = []
for i in range(50):
func_code = f"""
def calculate_sum_{i}(a: int, b: int) -> int:
'''Calculate sum of two integers.
Args:
a: First integer
b: Second integer
Returns:
Sum of a and b
Raises:
TypeError: If inputs are not integers
'''
if not isinstance(a, int) or not isinstance(b, int):
raise TypeError("Inputs must be integers")
return a + b
"""
test_functions.append(
{
"name": f"calculate_sum_{i}",
"code": func_code,
"docstring": "Calculate sum of two integers.",
}
)
print(f"✓ Created {len(test_functions)} function definitions")
# Simulate test case generation analysis
# (Note: We're not actually calling LLM, just testing the data structures)
test_cases_generated = 0
for func in test_functions:
# Simulate generating 3 test cases per function
test_cases_generated += 3
print(f"✓ Simulated generation of {test_cases_generated} test cases")
print(f"✓ Functions analyzed: {len(test_functions)}")
@time_function
def profile_file_operations():
"""Profile file I/O operations."""
from pathlib import Path
print("\n" + "=" * 60)
print("Profiling: File Operations")
print("=" * 60)
# Test glob operations
py_files = list(Path("src").rglob("*.py"))
print(f"✓ Found {len(py_files)} Python files")
# Test file reading (sample)
sample_files = py_files[:10]
total_lines = 0
for file in sample_files:
try:
lines = len(file.read_text().splitlines())
total_lines += lines
except (OSError, UnicodeDecodeError):
# Skip files that can't be read (permissions, encoding issues)
pass
print(f"✓ Read {len(sample_files)} sample files")
print(f"✓ Total lines in sample: {total_lines:,}")
if __name__ == "__main__":
import os
os.makedirs("benchmarks/profiles", exist_ok=True)
print("\n" + "=" * 60)
print("PROFILING SUITE - Empathy Framework")
print("Phase 2 Performance Optimization")
print("=" * 60)
try:
# Run profiling on key areas
profile_scanner()
profile_pattern_library()
profile_workflow_execution()
profile_memory_operations()
profile_test_generation()
profile_cost_tracker()
profile_feedback_loops()
profile_file_operations()
print("\n" + "=" * 60)
print("PROFILING COMPLETE")
print("=" * 60)
print("\nProfile files saved to benchmarks/profiles/")
print("\nVisualize with snakeviz:")
print(" snakeviz benchmarks/profiles/scanner_scan.prof")
print(" snakeviz benchmarks/profiles/pattern_library.prof")
print(" snakeviz benchmarks/profiles/workflow_execution.prof")
print(" snakeviz benchmarks/profiles/memory_operations.prof")
print(" snakeviz benchmarks/profiles/test_generation.prof")
print(" snakeviz benchmarks/profiles/cost_tracker.prof")
print(" snakeviz benchmarks/profiles/feedback_loops.prof")
except Exception as e:
print(f"\n❌ Error during profiling: {e}")
import traceback
traceback.print_exc()
sys.exit(1)