-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathtest_privacy_plugin_performance.py
More file actions
217 lines (175 loc) · 8.6 KB
/
test_privacy_plugin_performance.py
File metadata and controls
217 lines (175 loc) · 8.6 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
#!/usr/bin/env python3
"""
Test to ensure privacy plugin resources are properly cached and not reloaded on each request.
This test will fail if resources are being recreated on every call, preventing performance regressions.
"""
import time
import sys
import os
from unittest.mock import Mock, patch, MagicMock
import importlib
# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def test_privacy_plugin_resource_caching():
"""
Test that expensive resources (AnalyzerEngine, AnonymizerEngine) are created only once
and reused across multiple plugin invocations.
"""
print("Testing privacy plugin resource caching...")
# Need to reset the module state before testing
if 'optillm.plugins.privacy_plugin' in sys.modules:
del sys.modules['optillm.plugins.privacy_plugin']
# Mock the expensive AnalyzerEngine and AnonymizerEngine at the module level before import
with patch('presidio_analyzer.AnalyzerEngine') as MockAnalyzerEngine, \
patch('presidio_anonymizer.AnonymizerEngine') as MockAnonymizerEngine, \
patch('spacy.util.is_package', return_value=True):
# Set up mock instances
mock_analyzer_instance = MagicMock()
mock_analyzer_instance.analyze.return_value = []
MockAnalyzerEngine.return_value = mock_analyzer_instance
mock_anonymizer_instance = MagicMock()
mock_anonymizer_instance.anonymize.return_value = MagicMock(text="anonymized text")
mock_anonymizer_instance.add_anonymizer = MagicMock()
MockAnonymizerEngine.return_value = mock_anonymizer_instance
# Import the module with mocks in place
import optillm.plugins.privacy_plugin as privacy_plugin
# Mock client for the run function
mock_client = Mock()
mock_response = Mock()
mock_response.choices = [Mock(message=Mock(content="response"))]
mock_response.usage.completion_tokens = 10
mock_client.chat.completions.create.return_value = mock_response
# First invocation
print("First invocation...")
result1, tokens1 = privacy_plugin.run("system", "query 1", mock_client, "model")
# Check that resources were created once
assert MockAnalyzerEngine.call_count == 1, f"AnalyzerEngine created {MockAnalyzerEngine.call_count} times, expected 1"
assert MockAnonymizerEngine.call_count == 1, f"AnonymizerEngine created {MockAnonymizerEngine.call_count} times, expected 1"
# Second invocation
print("Second invocation...")
result2, tokens2 = privacy_plugin.run("system", "query 2", mock_client, "model")
# Check that resources were NOT created again
assert MockAnalyzerEngine.call_count == 1, f"AnalyzerEngine created {MockAnalyzerEngine.call_count} times after 2nd call, expected 1"
assert MockAnonymizerEngine.call_count == 1, f"AnonymizerEngine created {MockAnonymizerEngine.call_count} times after 2nd call, expected 1"
# Third invocation to be extra sure
print("Third invocation...")
result3, tokens3 = privacy_plugin.run("system", "query 3", mock_client, "model")
# Still should be 1
assert MockAnalyzerEngine.call_count == 1, f"AnalyzerEngine created {MockAnalyzerEngine.call_count} times after 3rd call, expected 1"
assert MockAnonymizerEngine.call_count == 1, f"AnonymizerEngine created {MockAnonymizerEngine.call_count} times after 3rd call, expected 1"
print("✅ Privacy plugin resource caching test PASSED - Resources are properly cached!")
return True
def test_privacy_plugin_performance():
"""
Test that multiple invocations of the privacy plugin don't have degraded performance.
This catches the actual performance issue even without mocking.
"""
print("\nTesting privacy plugin performance (real execution)...")
try:
# Try to import the actual plugin
import optillm.plugins.privacy_plugin as privacy_plugin
# Check if required dependencies are available
try:
import spacy
from presidio_analyzer import AnalyzerEngine
from presidio_anonymizer import AnonymizerEngine
except ImportError as e:
print(f"⚠️ Skipping performance test - dependencies not installed: {e}")
return True
# Mock client
mock_client = Mock()
mock_response = Mock()
mock_response.choices = [Mock(message=Mock(content="response"))]
mock_response.usage.completion_tokens = 10
mock_client.chat.completions.create.return_value = mock_response
# Warm-up call (might include model download)
print("Warm-up call...")
start = time.time()
privacy_plugin.run("system", "warm up query", mock_client, "model")
warmup_time = time.time() - start
print(f"Warm-up time: {warmup_time:.2f}s")
# First real measurement
print("First measurement call...")
start = time.time()
privacy_plugin.run("system", "test query 1", mock_client, "model")
first_time = time.time() - start
print(f"First call time: {first_time:.2f}s")
# Second measurement - should be fast if caching works
print("Second measurement call...")
start = time.time()
privacy_plugin.run("system", "test query 2", mock_client, "model")
second_time = time.time() - start
print(f"Second call time: {second_time:.2f}s")
# Third measurement
print("Third measurement call...")
start = time.time()
privacy_plugin.run("system", "test query 3", mock_client, "model")
third_time = time.time() - start
print(f"Third call time: {third_time:.2f}s")
# Performance assertions
# Second and third calls should be much faster than first (at least 10x faster)
# Allow some tolerance for the first call as it might still be initializing
max_acceptable_time = 2.0 # 2 seconds max for subsequent calls
if second_time > max_acceptable_time:
raise AssertionError(f"Second call took {second_time:.2f}s, expected < {max_acceptable_time}s. Resources might not be cached!")
if third_time > max_acceptable_time:
raise AssertionError(f"Third call took {third_time:.2f}s, expected < {max_acceptable_time}s. Resources might not be cached!")
print(f"✅ Privacy plugin performance test PASSED - Subsequent calls are fast ({second_time:.2f}s, {third_time:.2f}s)!")
return True
except Exception as e:
print(f"❌ Performance test failed: {e}")
raise
def test_singleton_instances_are_reused():
"""
Direct test that singleton instances are the same object across calls.
"""
print("\nTesting singleton instance reuse...")
try:
import optillm.plugins.privacy_plugin as privacy_plugin
importlib.reload(privacy_plugin)
# Get first instances
analyzer1 = privacy_plugin.get_analyzer_engine()
anonymizer1 = privacy_plugin.get_anonymizer_engine()
# Get second instances
analyzer2 = privacy_plugin.get_analyzer_engine()
anonymizer2 = privacy_plugin.get_anonymizer_engine()
# They should be the exact same object
assert analyzer1 is analyzer2, "AnalyzerEngine instances are not the same object!"
assert anonymizer1 is anonymizer2, "AnonymizerEngine instances are not the same object!"
print("✅ Singleton instance test PASSED - Same objects are reused!")
return True
except ImportError as e:
print(f"⚠️ Skipping singleton test - dependencies not installed: {e}")
return True
except Exception as e:
print(f"❌ Singleton test failed: {e}")
raise
if __name__ == "__main__":
print("=" * 60)
print("Privacy Plugin Performance & Caching Tests")
print("=" * 60)
all_passed = True
try:
test_privacy_plugin_resource_caching()
except Exception as e:
all_passed = False
print(f"❌ Resource caching test failed: {e}")
try:
test_singleton_instances_are_reused()
except Exception as e:
all_passed = False
print(f"❌ Singleton instance test failed: {e}")
try:
test_privacy_plugin_performance()
except Exception as e:
all_passed = False
print(f"❌ Performance test failed: {e}")
print("\n" + "=" * 60)
if all_passed:
print("✅ ALL TESTS PASSED!")
print("Privacy plugin resources are properly cached.")
sys.exit(0)
else:
print("❌ SOME TESTS FAILED!")
print("Privacy plugin may have performance issues.")
sys.exit(1)