File size: 9,242 Bytes
b6ae7b8 | 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 | #!/usr/bin/env python3
"""
Benchmarks for Stack 2.9 - Throughput Tests
Concurrency and throughput benchmarks.
"""
import pytest
import sys
import time
import threading
from pathlib import Path
from unittest.mock import MagicMock, patch
# Add stack_cli to path
sys.path.insert(0, str(Path(__file__).parent.parent / "stack_cli"))
from stack_cli.agent import StackAgent, create_agent
class TestConcurrentQueries:
"""Test concurrent query handling."""
def test_sequential_throughput(self):
"""Test sequential query throughput."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": True})
mock_get_tool.return_value = mock_tool
agent = StackAgent()
start = time.time()
for i in range(20):
agent.process(f"query {i}")
elapsed = time.time() - start
throughput = 20 / elapsed
# Should handle at least 5 queries per second
assert throughput > 5
def test_rapid_fire_queries(self):
"""Test rapid fire query submission."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": True})
mock_get_tool.return_value = mock_tool
agent = StackAgent()
# Submit quickly
results = []
start = time.time()
for i in range(10):
results.append(agent.process(f"rapid {i}"))
elapsed = time.time() - start
assert len(results) == 10
assert elapsed < 3.0
class TestThreadSafety:
"""Test thread safety."""
def test_concurrent_agent_creation(self):
"""Test concurrent agent creation."""
agents = []
def create_agent_thread():
with patch('stack_cli.context.create_context_manager'):
agents.append(create_agent())
threads = []
for _ in range(5):
t = threading.Thread(target=create_agent_thread)
threads.append(t)
t.start()
for t in threads:
t.join()
assert len(agents) == 5
def test_concurrent_tool_access(self):
"""Test concurrent tool access."""
from stack_cli.tools import get_tool, list_tools
results = []
def access_tools():
for _ in range(10):
tool = get_tool("read")
tools = list_tools()
results.append((tool is not None, len(tools) > 0))
threads = []
for _ in range(3):
t = threading.Thread(target=access_tools)
threads.append(t)
t.start()
for t in threads:
t.join()
# All accesses should succeed
assert all(success for success, _ in results)
class TestBatchProcessing:
"""Test batch processing capabilities."""
def test_batch_file_operations(self):
"""Test batch file operations."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": True})
mock_get_tool.return_value = mock_tool
files = [f"file{i}.py" for i in range(10)]
start = time.time()
for f in files:
get_tool("read")(path=f)
elapsed = time.time() - start
assert elapsed < 2.0
def test_batch_tool_chains(self):
"""Test batch tool chains."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": True})
mock_get_tool.return_value = mock_tool
chains = [
("read", {"path": "a.py"}),
("write", {"path": "b.py", "content": "x"}),
("grep", {"path": ".", "pattern": "test"}),
]
start = time.time()
for tool_name, params in chains * 5:
get_tool(tool_name)(**params)
elapsed = time.time() - start
assert elapsed < 2.0
class TestThroughputMetrics:
"""Test throughput metrics."""
def test_queries_per_second(self):
"""Test queries per second throughput."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": True})
mock_get_tool.return_value = mock_tool
agent = StackAgent()
start = time.time()
query_count = 30
for i in range(query_count):
agent.process(f"query {i}")
elapsed = time.time() - start
qps = query_count / elapsed
# Should achieve reasonable QPS
assert qps > 3, f"QPS too low: {qps}"
def test_tools_per_second(self):
"""Test tools per second throughput."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": True})
mock_get_tool.return_value = mock_tool
start = time.time()
tool_count = 100
for i in range(tool_count):
get_tool("read")(path=f"file{i}.py")
elapsed = time.time() - start
tps = tool_count / elapsed
# Should be very fast
assert tps > 50
class TestConcurrentContext:
"""Test concurrent context operations."""
def test_concurrent_context_updates(self):
"""Test concurrent context updates."""
from stack_cli.context import SessionMemory
session = SessionMemory()
errors = []
def update_session(i):
try:
session.add_message("user", f"message {i}")
session.add_tool_usage("read", {"success": True})
except Exception as e:
errors.append(e)
threads = []
for i in range(10):
t = threading.Thread(target=update_session, args=(i,))
threads.append(t)
t.start()
for t in threads:
t.join()
# Should complete without errors
assert len(errors) == 0
assert len(session.messages) == 10
class TestResourceUtilization:
"""Test resource utilization."""
def test_memory_usage_stable(self):
"""Test memory usage remains stable."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": True})
mock_get_tool.return_value = mock_tool
agent = StackAgent()
# Process many queries
for i in range(100):
agent.process(f"query {i}")
# History should not grow unbounded
# (Old entries may be truncated in real implementation)
assert len(agent.conversation_history) <= 200
def test_context_growth_bounded(self):
"""Test context growth is bounded."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": True})
mock_get_tool.return_value = mock_tool
agent = StackAgent()
session = agent.context_manager.session
# Add many operations
for i in range(50):
session.add_message("user", f"msg {i}")
session.add_tool_usage("read", {"success": True})
summary = session.get_summary()
# Counts should be accurate
assert summary["messages_count"] == 50
if __name__ == "__main__":
pytest.main([__file__, "-v"])
|