-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_performance_optimization.py
More file actions
427 lines (322 loc) · 13.4 KB
/
test_performance_optimization.py
File metadata and controls
427 lines (322 loc) · 13.4 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env python3
"""
Test Performance Optimization and Caching
========================================
Tests the performance optimization system including caching, memoization,
and performance monitoring.
"""
import os
import tempfile
import time
from pathlib import Path
from unittest.mock import patch
import pytest
import yaml
from core.performance import (
CacheManager,
FileCache,
LLMCache,
PerformanceMonitor,
get_file_cache,
get_llm_cache,
get_performance_monitor,
memoize,
)
from core.exceptions import PerformanceError
class TestPerformanceMonitor:
"""Test performance monitoring functionality."""
def test_performance_monitor_initialization(self):
"""Test performance monitor initialization."""
monitor = PerformanceMonitor()
assert monitor.metrics == {}
assert monitor.start_times == {}
def test_timer_functionality(self):
"""Test timer start and end functionality."""
monitor = PerformanceMonitor()
# Start timer
monitor.start_timer("test_operation")
time.sleep(0.1) # Simulate work
duration = monitor.end_timer("test_operation")
assert duration > 0.1
assert "test_operation" in monitor.metrics
assert len(monitor.metrics["test_operation"]) == 1
def test_multiple_timings(self):
"""Test multiple timing operations."""
monitor = PerformanceMonitor()
for i in range(3):
monitor.start_timer("repeated_operation")
time.sleep(0.01)
monitor.end_timer("repeated_operation")
assert len(monitor.metrics["repeated_operation"]) == 3
assert monitor.get_average_time("repeated_operation") > 0.01
def test_metrics_summary(self):
"""Test metrics summary generation."""
monitor = PerformanceMonitor()
monitor.start_timer("operation1")
time.sleep(0.01)
monitor.end_timer("operation1")
monitor.start_timer("operation2")
time.sleep(0.02)
monitor.end_timer("operation2")
summary = monitor.get_metrics_summary()
assert "operation1" in summary
assert "operation2" in summary
assert summary["operation1"]["count"] == 1
assert summary["operation2"]["count"] == 1
assert summary["operation1"]["total_time"] > 0.01
assert summary["operation2"]["total_time"] > 0.02
def test_end_timer_without_start(self):
"""Test ending timer without starting it."""
monitor = PerformanceMonitor()
duration = monitor.end_timer("nonexistent_operation")
assert duration == 0.0
class TestCacheManager:
"""Test cache management functionality."""
def setup_method(self):
"""Set up test environment."""
self.temp_dir = tempfile.mkdtemp()
self.cache_manager = CacheManager(Path(self.temp_dir))
def teardown_method(self):
"""Clean up test environment."""
import shutil
shutil.rmtree(self.temp_dir, ignore_errors=True)
def test_cache_initialization(self):
"""Test cache manager initialization."""
assert self.cache_manager.memory_cache == {}
assert self.cache_manager.cache_metadata == {}
assert self.cache_manager.max_size == 1000
def test_cache_set_and_get(self):
"""Test basic cache set and get operations."""
test_data = {"key": "value", "number": 42}
# Set cache
self.cache_manager.set("test_key", test_data)
# Get cache
result = self.cache_manager.get("test_key")
assert result == test_data
def test_cache_with_metadata(self):
"""Test cache with metadata."""
test_data = {"key": "value"}
metadata = {"source": "test", "version": "1.0"}
self.cache_manager.set("test_key", test_data, metadata)
result = self.cache_manager.get("test_key")
assert result == test_data
assert "test_key" in self.cache_manager.cache_metadata
def test_cache_expiration(self):
"""Test cache expiration functionality."""
test_data = {"key": "value"}
# Set cache with short expiration
self.cache_manager.set("test_key", test_data)
# Should be valid immediately
result = self.cache_manager.get("test_key", max_age_hours=1)
assert result == test_data
# Should be invalid with very short expiration
result = self.cache_manager.get("test_key", max_age_hours=0)
assert result is None
def test_cache_eviction(self):
"""Test cache eviction when size limit is exceeded."""
# Create a small cache
small_cache = CacheManager(Path(self.temp_dir), max_size=5)
# Add more items than the limit
for i in range(10):
small_cache.set(f"key_{i}", f"value_{i}")
# Should have evicted some items
assert len(small_cache.memory_cache) < 10
def test_cache_clear(self):
"""Test cache clearing functionality."""
# Add some cache entries
for i in range(5):
self.cache_manager.set(f"key_{i}", f"value_{i}")
# Clear all cache
self.cache_manager.clear()
assert len(self.cache_manager.memory_cache) == 0
assert len(self.cache_manager.cache_metadata) == 0
def test_cache_clear_with_pattern(self):
"""Test cache clearing with pattern matching."""
# Add cache entries with different patterns
self.cache_manager.set("test_key_1", "value1")
self.cache_manager.set("other_key", "value2")
self.cache_manager.set("test_key_2", "value3")
# Clear only test keys
self.cache_manager.clear(pattern="test_key")
# Should only have other_key remaining
assert "other_key" in self.cache_manager.memory_cache
assert "test_key_1" not in self.cache_manager.memory_cache
assert "test_key_2" not in self.cache_manager.memory_cache
def test_cache_stats(self):
"""Test cache statistics generation."""
# Add some cache entries
for i in range(3):
self.cache_manager.set(f"key_{i}", f"value_{i}")
stats = self.cache_manager.get_stats()
assert stats["memory_cache_size"] == 3
assert stats["metadata_size"] == 3
assert stats["max_size"] == 1000
assert "performance_metrics" in stats
class TestFileCache:
"""Test file caching functionality."""
def setup_method(self):
"""Set up test environment."""
self.temp_dir = tempfile.mkdtemp()
self.file_cache = FileCache(Path(self.temp_dir))
def teardown_method(self):
"""Clean up test environment."""
import shutil
shutil.rmtree(self.temp_dir, ignore_errors=True)
def test_yaml_file_loading(self):
"""Test YAML file loading with caching."""
# Create a test YAML file
test_data = {"key": "value", "list": [1, 2, 3]}
yaml_file = Path(self.temp_dir) / "test.yaml"
with open(yaml_file, "w") as f:
yaml.dump(test_data, f)
# Load the file
result = self.file_cache.load_yaml_file(yaml_file)
assert result == test_data
# Load again (should be cached)
result2 = self.file_cache.load_yaml_file(yaml_file)
assert result2 == test_data
def test_yaml_file_loading_error(self):
"""Test YAML file loading error handling."""
# Try to load non-existent file
non_existent_file = Path(self.temp_dir) / "nonexistent.yaml"
with pytest.raises(PerformanceError):
self.file_cache.load_yaml_file(non_existent_file)
def test_job_parsing_caching(self):
"""Test job description parsing caching."""
job_text = "Senior Product Manager at TechCorp"
# Parse job description
result1 = self.file_cache.parse_job_description(job_text)
assert isinstance(result1, dict)
assert "company_name" in result1
assert "job_title" in result1
# Parse again (should be cached)
result2 = self.file_cache.parse_job_description(job_text)
assert result2 == result1
def test_blurb_scoring_caching(self):
"""Test blurb scoring caching."""
blurb = {"id": "test", "text": "test text", "tags": ["test"]}
job_requirements = ["test", "product", "management"]
# Score blurb
score1 = self.file_cache.score_blurb(blurb, job_requirements)
assert isinstance(score1, float)
# Score again (should be cached)
score2 = self.file_cache.score_blurb(blurb, job_requirements)
assert score2 == score1
class TestLLMCache:
"""Test LLM caching functionality."""
def setup_method(self):
"""Set up test environment."""
self.temp_dir = tempfile.mkdtemp()
self.llm_cache = LLMCache(Path(self.temp_dir))
def teardown_method(self):
"""Clean up test environment."""
import shutil
shutil.rmtree(self.temp_dir, ignore_errors=True)
def test_llm_enhancement_caching(self):
"""Test LLM enhancement caching."""
draft = "I am excited to apply for this position."
job_description = "Senior Product Manager role"
# Enhance cover letter
result1 = self.llm_cache.enhance_cover_letter(draft, job_description)
assert isinstance(result1, str)
# Enhance again (should be cached)
result2 = self.llm_cache.enhance_cover_letter(draft, job_description)
assert result2 == result1
def test_llm_requirements_extraction_caching(self):
"""Test LLM requirements extraction caching."""
job_description = "Senior Product Manager role with AI/ML focus"
# Extract requirements
result1 = self.llm_cache.extract_requirements(job_description)
assert isinstance(result1, dict)
assert "tools" in result1
assert "team_dynamics" in result1
# Extract again (should be cached)
result2 = self.llm_cache.extract_requirements(job_description)
assert result2 == result1
class TestMemoizeDecorator:
"""Test memoization decorator functionality."""
def setup_method(self):
"""Set up test environment."""
self.temp_dir = tempfile.mkdtemp()
def teardown_method(self):
"""Clean up test environment."""
import shutil
shutil.rmtree(self.temp_dir, ignore_errors=True)
def test_memoize_decorator(self):
"""Test memoization decorator."""
call_count = 0
@memoize(max_age_hours=1, cache_key_prefix="test")
def expensive_function(x, y):
nonlocal call_count
call_count += 1
return x + y
# First call
result1 = expensive_function(1, 2)
assert result1 == 3
assert call_count == 1
# Second call with same arguments (should be cached)
result2 = expensive_function(1, 2)
assert result2 == 3
assert call_count == 1 # Should not have been called again
# Third call with different arguments
result3 = expensive_function(3, 4)
assert result3 == 7
assert call_count == 2 # Should have been called again
class TestGlobalInstances:
"""Test global performance instances."""
def test_global_instances(self):
"""Test global performance instances."""
# Test performance monitor
monitor = get_performance_monitor()
assert isinstance(monitor, PerformanceMonitor)
# Test file cache
file_cache = get_file_cache()
assert isinstance(file_cache, FileCache)
# Test LLM cache
llm_cache = get_llm_cache()
assert isinstance(llm_cache, LLMCache)
def test_global_instances_singleton(self):
"""Test that global instances are singletons."""
monitor1 = get_performance_monitor()
monitor2 = get_performance_monitor()
assert monitor1 is monitor2
file_cache1 = get_file_cache()
file_cache2 = get_file_cache()
assert file_cache1 is file_cache2
llm_cache1 = get_llm_cache()
llm_cache2 = get_llm_cache()
assert llm_cache1 is llm_cache2
class TestPerformanceIntegration:
"""Test performance optimization integration with existing code."""
def test_file_cache_integration(self):
"""Test file cache integration with YAML loading."""
# Create test YAML file
test_data = {"test": "data", "nested": {"key": "value"}}
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
yaml.dump(test_data, f)
yaml_file = Path(f.name)
try:
# Load using file cache
file_cache = get_file_cache()
result = file_cache.load_yaml_file(yaml_file)
assert result == test_data
# Load again (should be cached)
result2 = file_cache.load_yaml_file(yaml_file)
assert result2 == test_data
finally:
# Clean up
yaml_file.unlink()
def test_performance_monitoring_integration(self):
"""Test performance monitoring integration."""
monitor = get_performance_monitor()
# Simulate some operations
monitor.start_timer("test_operation")
time.sleep(0.01)
monitor.end_timer("test_operation")
# Check metrics
summary = monitor.get_metrics_summary()
assert "test_operation" in summary
assert summary["test_operation"]["count"] == 1
assert summary["test_operation"]["total_time"] > 0.01
if __name__ == "__main__":
pytest.main([__file__])