⚡️ Speed up function check_api_key by 349%
#51
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 349% (3.49x) speedup for
check_api_keyincognee/modules/cloud/operations/check_api_key.py⏱️ Runtime :
150 milliseconds→33.4 milliseconds(best of14runs)📝 Explanation and details
The optimization introduces a singleton SSL context pattern that eliminates expensive SSL context creation on every function call.
Key Optimization:
ssl.create_default_context()on every request, the optimized code creates a single_SECURE_SSL_CONTEXTat module import time and reuses it across all function calls.Why This Works:
The line profiler reveals that
ssl.create_default_context()was consuming 23.1% of total execution time (152ms out of 662ms) in the original code. This function performs expensive operations like loading system certificates, validating trust stores, and configuring SSL protocols. Since SSL context configuration is static and doesn't change between requests, recreating it repeatedly is pure waste.Performance Impact:
create_secure_ssl_context()function now takes only 0.2ms vs 150ms in the originalTest Case Benefits:
The optimization particularly shines in concurrent scenarios - all throughput tests (small, medium, high volume) benefit significantly because each concurrent call no longer recreates the SSL context. The 200-request high volume test shows substantial gains as the SSL context creation overhead is eliminated across all parallel operations.
Safety: The SSL context is immutable after creation and thread-safe, making this singleton pattern safe for concurrent use in async environments.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
import asyncio # used to run async functions
function to test
import ssl
from unittest.mock import AsyncMock, MagicMock, patch
import aiohttp
import pytest # used for our unit tests
from cognee.modules.cloud.operations.check_api_key import check_api_key
class CloudConnectionError(Exception):
pass
from cognee.modules.cloud.operations.check_api_key import check_api_key
----------------- UNIT TESTS -----------------
Helper to mock aiohttp.ClientSession and its context managers
class MockResponse:
def init(self, status, text=""):
self.status = status
self._text = text
class MockSession:
def init(self, response: MockResponse):
self._response = response
---------------- BASIC TEST CASES ----------------
@pytest.mark.asyncio
async def test_check_api_key_success():
"""Test that check_api_key returns None for a successful API key check (HTTP 200)."""
mock_response = MockResponse(status=200)
with patch("aiohttp.ClientSession", return_value=MockSession(mock_response)):
result = await check_api_key("valid_token")
@pytest.mark.asyncio
async def test_check_api_key_many_concurrent_success():
"""Test many concurrent successful API key checks."""
mock_response = MockResponse(status=200)
with patch("aiohttp.ClientSession", return_value=MockSession(mock_response)):
tasks = [check_api_key(f"valid_token_{i}") for i in range(50)]
results = await asyncio.gather(*tasks)
@pytest.mark.asyncio
async def test_check_api_key_throughput_small_load():
"""Throughput test: small load of concurrent requests."""
mock_response = MockResponse(status=200)
with patch("aiohttp.ClientSession", return_value=MockSession(mock_response)):
tasks = [check_api_key(f"token_{i}") for i in range(10)]
results = await asyncio.gather(*tasks)
@pytest.mark.asyncio
async def test_check_api_key_throughput_medium_load():
"""Throughput test: medium load of concurrent requests."""
mock_response = MockResponse(status=200)
with patch("aiohttp.ClientSession", return_value=MockSession(mock_response)):
tasks = [check_api_key(f"token_{i}") for i in range(100)]
results = await asyncio.gather(*tasks)
@pytest.mark.asyncio
async def test_check_api_key_throughput_mixed_load():
"""Throughput test: mix of successful and failed requests."""
# Alternate between success and failure
def session_factory(*args, **kwargs):
class Session:
async def aenter(self): return self
async def aexit(self, exc_type, exc, tb): pass
def post(self, url, headers):
idx = int(headers["X-Api-Key"].split("")[-1])
if idx % 2 == 0:
return MockResponse(status=200)
else:
return MockResponse(status=401, text="Unauthorized")
return Session()
with patch("aiohttp.ClientSession", side_effect=session_factory):
tasks = [check_api_key(f"token{i}") for i in range(50)]
results = await asyncio.gather(*tasks, return_exceptions=True)
# Even indices should succeed, odd should fail
for i, r in enumerate(results):
if i % 2 == 0:
pass
else:
pass
@pytest.mark.asyncio
async def test_check_api_key_throughput_high_volume():
"""Throughput test: high volume of concurrent requests (up to 200)."""
mock_response = MockResponse(status=200)
with patch("aiohttp.ClientSession", return_value=MockSession(mock_response)):
tasks = [check_api_key(f"token_{i}") for i in range(200)]
results = await asyncio.gather(*tasks)
codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import asyncio # used to run async functions
function to test
import ssl
from unittest.mock import AsyncMock, MagicMock, patch
import aiohttp
import pytest # used for our unit tests
from cognee.modules.cloud.operations.check_api_key import check_api_key
class CloudConnectionError(Exception):
pass
from cognee.modules.cloud.operations.check_api_key import check_api_key
-------------------- UNIT TESTS BELOW --------------------
Helper to patch aiohttp.ClientSession for all tests
def mock_response(status=200, text="OK"):
"""
Helper to create a mock aiohttp response object.
"""
response = AsyncMock()
response.status = status
response.text = AsyncMock(return_value=text)
return response
def mock_session_post(status=200, text="OK"):
"""
Helper to patch session.post to return a mock response.
"""
response = mock_response(status, text)
post_cm = AsyncMock()
post_cm.aenter.return_value = response
post_cm.aexit.return_value = None
return post_cm
Basic Test Cases
@pytest.mark.asyncio
async def test_check_api_key_success():
"""
Test that check_api_key returns None for status 200 (success).
"""
with patch("aiohttp.ClientSession") as mock_client_session:
# Mock the session context manager
mock_session = MagicMock()
mock_client_session.return_value.aenter.return_value = mock_session
# Mock the post context manager to return status 200
mock_session.post.return_value = mock_session_post(status=200, text="OK")
@pytest.mark.asyncio
async def test_check_api_key_many_concurrent_success():
"""
Test many concurrent successful calls.
"""
with patch("aiohttp.ClientSession") as mock_client_session:
mock_session = MagicMock()
mock_client_session.return_value.aenter.return_value = mock_session
mock_session.post.return_value = mock_session_post(status=200, text="OK")
@pytest.mark.asyncio
async def test_check_api_key_throughput_small_load():
"""
Throughput test: small load of concurrent calls.
"""
with patch("aiohttp.ClientSession") as mock_client_session:
mock_session = MagicMock()
mock_client_session.return_value.aenter.return_value = mock_session
mock_session.post.return_value = mock_session_post(status=200, text="OK")
@pytest.mark.asyncio
async def test_check_api_key_throughput_medium_load():
"""
Throughput test: medium load of concurrent calls.
"""
with patch("aiohttp.ClientSession") as mock_client_session:
mock_session = MagicMock()
mock_client_session.return_value.aenter.return_value = mock_session
mock_session.post.return_value = mock_session_post(status=200, text="OK")
@pytest.mark.asyncio
async def test_check_api_key_throughput_high_volume():
"""
Throughput test: high volume concurrent calls (but under 1000).
"""
with patch("aiohttp.ClientSession") as mock_client_session:
mock_session = MagicMock()
mock_client_session.return_value.aenter.return_value = mock_session
mock_session.post.return_value = mock_session_post(status=200, text="OK")
codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
To edit these changes
git checkout codeflash/optimize-check_api_key-mhv3a8g8and push.