Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 30, 2025

📄 9,045% (90.45x) speedup for load_aws_kms in litellm/secret_managers/aws_secret_manager.py

⏱️ Runtime : 48.7 milliseconds 533 microseconds (best of 66 runs)

📝 Explanation and details

The optimized code achieves a 90x speedup through several key optimizations:

Primary optimization: Eliminating expensive boto3 import

  • The line profiler shows import boto3 consumed 97.4% of execution time in the original code (98.6ms out of 101ms total)
  • The optimized version strategically moves the boto3 import after validation, reducing import overhead for failed validation cases

Condition simplification

  • Changed if use_aws_kms is None or use_aws_kms is False: to if not use_aws_kms:
  • This simplifies the boolean logic and provides modest performance gains (8-30% faster in early-return test cases)

Environment variable access optimization

  • Replaced os.getenv("AWS_REGION_NAME") with direct os.environ["AWS_REGION_NAME"] access
  • Direct dictionary access avoids function call overhead and is safe since validation ensures the key exists
  • Stored in local variable region to avoid repeated lookups

Test case performance analysis:

  • Early returns (None/False cases): 8-30% faster due to simplified condition checking
  • Validation failures: 33-40% faster by avoiding boto3 import before validation
  • Successful cases: 3-12% faster from environment variable optimization
  • Large-scale scenarios: 7-9% consistent improvement across repeated calls

The optimization particularly excels when use_aws_kms is falsy or when validation fails, as it completely avoids the expensive boto3 import that dominated the original runtime.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 271 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 2 Passed
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import os
from typing import Optional

# function to test
import litellm
# imports
import pytest
from litellm.secret_managers.aws_secret_manager import load_aws_kms


# Dummy KeyManagementSystem for testing purposes
class KeyManagementSystem:
    AWS_KMS = "AWS_KMS"
    OTHER_KMS = "OTHER_KMS"
from litellm.secret_managers.aws_secret_manager import load_aws_kms

# -------- Basic Test Cases --------

def test_load_aws_kms_none_returns_nothing():
    # Should do nothing if use_aws_kms is None
    codeflash_output = load_aws_kms(None); result = codeflash_output # 459ns -> 425ns (8.00% faster)

def test_load_aws_kms_false_returns_nothing():
    # Should do nothing if use_aws_kms is False
    codeflash_output = load_aws_kms(False); result = codeflash_output # 463ns -> 364ns (27.2% faster)

def test_load_aws_kms_true_sets_client_and_kms(monkeypatch):
    # Should set litellm.secret_manager_client and _key_management_system when use_aws_kms is True
    os.environ["AWS_REGION_NAME"] = "us-west-2"

    class DummyBoto3Client:
        def __init__(self, service, region_name):
            self.service = service
            self.region_name = region_name

    class DummyBoto3:
        def client(self, service, region_name):
            return DummyBoto3Client(service, region_name)

    monkeypatch.setitem(__import__('sys').modules, "boto3", DummyBoto3())
    load_aws_kms(True) # 12.2μs -> 9.01μs (35.3% faster)
    del os.environ["AWS_REGION_NAME"]

# -------- Edge Test Cases --------

def test_load_aws_kms_missing_env_var_raises(monkeypatch):
    # Should raise ValueError if AWS_REGION_NAME is missing
    if "AWS_REGION_NAME" in os.environ:
        del os.environ["AWS_REGION_NAME"]
    class DummyBoto3:
        def client(self, service, region_name):
            return None
    monkeypatch.setitem(__import__('sys').modules, "boto3", DummyBoto3())
    with pytest.raises(ValueError) as excinfo:
        load_aws_kms(True) # 3.63μs -> 2.72μs (33.6% faster)

def test_load_aws_kms_env_var_empty_string(monkeypatch):
    # Should use empty string as region name, which may be valid for boto3 but we check assignment
    os.environ["AWS_REGION_NAME"] = ""
    class DummyBoto3Client:
        def __init__(self, service, region_name):
            self.service = service
            self.region_name = region_name
    class DummyBoto3:
        def client(self, service, region_name):
            return DummyBoto3Client(service, region_name)
    monkeypatch.setitem(__import__('sys').modules, "boto3", DummyBoto3())
    load_aws_kms(True) # 6.11μs -> 5.47μs (11.5% faster)
    del os.environ["AWS_REGION_NAME"]


def test_load_aws_kms_true_with_non_bool(monkeypatch):
    # Should treat non-bool truthy values as True
    os.environ["AWS_REGION_NAME"] = "us-west-1"
    class DummyBoto3Client:
        def __init__(self, service, region_name):
            self.service = service
            self.region_name = region_name
    class DummyBoto3:
        def client(self, service, region_name):
            return DummyBoto3Client(service, region_name)
    monkeypatch.setitem(__import__('sys').modules, "boto3", DummyBoto3())
    load_aws_kms(1) # 6.96μs -> 6.47μs (7.51% faster)
    del os.environ["AWS_REGION_NAME"]

def test_load_aws_kms_true_with_string(monkeypatch):
    # Should treat non-bool truthy string values as True
    os.environ["AWS_REGION_NAME"] = "us-west-3"
    class DummyBoto3Client:
        def __init__(self, service, region_name):
            self.service = service
            self.region_name = region_name
    class DummyBoto3:
        def client(self, service, region_name):
            return DummyBoto3Client(service, region_name)
    monkeypatch.setitem(__import__('sys').modules, "boto3", DummyBoto3())
    load_aws_kms("yes") # 5.81μs -> 5.25μs (10.6% faster)
    del os.environ["AWS_REGION_NAME"]

def test_load_aws_kms_true_with_falsey_string(monkeypatch):
    # Should treat falsey string as True (since it's not None/False)
    os.environ["AWS_REGION_NAME"] = "us-west-4"
    class DummyBoto3Client:
        def __init__(self, service, region_name):
            self.service = service
            self.region_name = region_name
    class DummyBoto3:
        def client(self, service, region_name):
            return DummyBoto3Client(service, region_name)
    monkeypatch.setitem(__import__('sys').modules, "boto3", DummyBoto3())
    load_aws_kms("") # 5.57μs -> 343ns (1523% faster)
    del os.environ["AWS_REGION_NAME"]

# -------- Large Scale Test Cases --------

def test_load_aws_kms_many_calls(monkeypatch):
    # Should handle repeated calls and always set correct values
    os.environ["AWS_REGION_NAME"] = "us-east-2"
    class DummyBoto3Client:
        def __init__(self, service, region_name):
            self.service = service
            self.region_name = region_name
    class DummyBoto3:
        def client(self, service, region_name):
            return DummyBoto3Client(service, region_name)
    monkeypatch.setitem(__import__('sys').modules, "boto3", DummyBoto3())
    for i in range(100):  # Large scale: 100 calls
        load_aws_kms(True) # 197μs -> 183μs (7.52% faster)
    del os.environ["AWS_REGION_NAME"]

def test_load_aws_kms_varied_region_names(monkeypatch):
    # Should handle many different region names correctly
    class DummyBoto3Client:
        def __init__(self, service, region_name):
            self.service = service
            self.region_name = region_name
    class DummyBoto3:
        def client(self, service, region_name):
            return DummyBoto3Client(service, region_name)
    monkeypatch.setitem(__import__('sys').modules, "boto3", DummyBoto3())
    for i in range(50):  # 50 different region names
        region = f"test-region-{i}"
        os.environ["AWS_REGION_NAME"] = region
        load_aws_kms(True) # 102μs -> 95.6μs (7.51% faster)
    del os.environ["AWS_REGION_NAME"]

def test_load_aws_kms_with_large_env_var(monkeypatch):
    # Should handle very large region name string
    region = "us-west-" + "x" * 900  # 900 chars
    os.environ["AWS_REGION_NAME"] = region
    class DummyBoto3Client:
        def __init__(self, service, region_name):
            self.service = service
            self.region_name = region_name
    class DummyBoto3:
        def client(self, service, region_name):
            return DummyBoto3Client(service, region_name)
    monkeypatch.setitem(__import__('sys').modules, "boto3", DummyBoto3())
    load_aws_kms(True) # 5.33μs -> 4.81μs (10.9% faster)
    del os.environ["AWS_REGION_NAME"]
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import os
import sys
from typing import Optional

# function to test
# (as provided above)
import litellm
# imports
import pytest
from litellm.secret_managers.aws_secret_manager import load_aws_kms


# Dummy KeyManagementSystem for testing
class DummyKMS:
    AWS_KMS = "aws_kms"

# Patch litellm to use DummyKMS for testing
litellm._key_management_system = None
litellm.secret_manager_client = None
litellm.KeyManagementSystem = DummyKMS
from litellm.secret_managers.aws_secret_manager import load_aws_kms

# unit tests

# --- Basic Test Cases ---

def test_load_aws_kms_none_returns_none(monkeypatch):
    """
    Test that passing None disables KMS loading and does not modify litellm.
    """
    # Save original state
    orig_client = getattr(litellm, "secret_manager_client", None)
    orig_kms = getattr(litellm, "_key_management_system", None)
    load_aws_kms(None) # 434ns -> 406ns (6.90% faster)

def test_load_aws_kms_false_returns_none(monkeypatch):
    """
    Test that passing False disables KMS loading and does not modify litellm.
    """
    orig_client = getattr(litellm, "secret_manager_client", None)
    orig_kms = getattr(litellm, "_key_management_system", None)
    load_aws_kms(False) # 480ns -> 369ns (30.1% faster)

def test_load_aws_kms_true_with_env(monkeypatch):
    """
    Test that passing True with AWS_REGION_NAME set loads KMS and sets litellm attributes.
    """
    # Set up environment variable
    monkeypatch.setenv("AWS_REGION_NAME", "us-east-1")

    # Patch boto3 to avoid real AWS calls
    class DummyBoto3:
        def client(self, name, region_name=None):
            return "dummy_client"

    monkeypatch.setitem(sys.modules, "boto3", DummyBoto3())

    load_aws_kms(True) # 4.78μs -> 4.64μs (3.00% faster)

def test_load_aws_kms_true_with_env_other_region(monkeypatch):
    """
    Test that region name is respected.
    """
    monkeypatch.setenv("AWS_REGION_NAME", "eu-west-2")

    class DummyBoto3:
        def client(self, name, region_name=None):
            return "dummy_client2"

    monkeypatch.setitem(sys.modules, "boto3", DummyBoto3())

    load_aws_kms(True) # 4.63μs -> 4.12μs (12.5% faster)

# --- Edge Test Cases ---

def test_load_aws_kms_true_missing_env(monkeypatch):
    """
    Test that missing AWS_REGION_NAME raises ValueError.
    """
    # Remove env var if present
    monkeypatch.delenv("AWS_REGION_NAME", raising=False)

    # Patch boto3 so import works
    class DummyBoto3:
        def client(self, name, region_name=None):
            return "should_not_be_called"
    monkeypatch.setitem(sys.modules, "boto3", DummyBoto3())

    with pytest.raises(ValueError) as e:
        load_aws_kms(True) # 3.62μs -> 2.59μs (40.0% faster)


def test_load_aws_kms_true_boto3_client_exception(monkeypatch):
    """
    Test that boto3.client raises exception is propagated.
    """
    monkeypatch.setenv("AWS_REGION_NAME", "us-east-1")

    class DummyBoto3:
        def client(self, name, region_name=None):
            raise RuntimeError("boto3 client error")

    monkeypatch.setitem(sys.modules, "boto3", DummyBoto3())

    with pytest.raises(RuntimeError) as e:
        load_aws_kms(True) # 5.80μs -> 5.51μs (5.20% faster)

def test_load_aws_kms_true_env_empty_string(monkeypatch):
    """
    Test that AWS_REGION_NAME='' is accepted and passed to boto3.
    """
    monkeypatch.setenv("AWS_REGION_NAME", "")

    class DummyBoto3:
        def client(self, name, region_name=None):
            return "empty_region_client"

    monkeypatch.setitem(sys.modules, "boto3", DummyBoto3())

    load_aws_kms(True) # 4.83μs -> 4.67μs (3.40% faster)

def test_load_aws_kms_true_env_special_characters(monkeypatch):
    """
    Test that AWS_REGION_NAME with special characters is passed through.
    """
    monkeypatch.setenv("AWS_REGION_NAME", "us-east-1$%")

    class DummyBoto3:
        def client(self, name, region_name=None):
            return "special_region_client"

    monkeypatch.setitem(sys.modules, "boto3", DummyBoto3())

    load_aws_kms(True) # 4.42μs -> 4.11μs (7.74% faster)

# --- Large Scale Test Cases ---

def test_load_aws_kms_true_many_calls(monkeypatch):
    """
    Test repeated calls with different regions and ensure correct state each time.
    """
    regions = [f"region-{i}" for i in range(100)]  # 100 regions for scale

    class DummyBoto3:
        def __init__(self):
            self.calls = []
        def client(self, name, region_name=None):
            self.calls.append(region_name)
            return f"client-{region_name}"

    dummy_boto3 = DummyBoto3()
    monkeypatch.setitem(sys.modules, "boto3", dummy_boto3)

    for region in regions:
        monkeypatch.setenv("AWS_REGION_NAME", region)
        load_aws_kms(True) # 191μs -> 176μs (8.76% faster)

def test_load_aws_kms_true_large_env_var(monkeypatch):
    """
    Test with a very large AWS_REGION_NAME value.
    """
    large_region = "us-east-1" + "x" * 500  # 508 chars
    monkeypatch.setenv("AWS_REGION_NAME", large_region)

    class DummyBoto3:
        def client(self, name, region_name=None):
            return "large_region_client"

    monkeypatch.setitem(sys.modules, "boto3", DummyBoto3())

    load_aws_kms(True) # 4.50μs -> 4.26μs (5.68% faster)

def test_load_aws_kms_true_toggle_on_off(monkeypatch):
    """
    Test toggling use_aws_kms between True and False in sequence.
    """
    monkeypatch.setenv("AWS_REGION_NAME", "us-east-1")

    class DummyBoto3:
        def client(self, name, region_name=None):
            return "toggle_client"

    monkeypatch.setitem(sys.modules, "boto3", DummyBoto3())

    # True: should set
    load_aws_kms(True) # 3.93μs -> 3.71μs (5.93% faster)

    # False: should not change
    prev_client = litellm.secret_manager_client
    prev_kms = litellm._key_management_system
    load_aws_kms(False) # 276ns -> 236ns (16.9% faster)

    # True again: should set again
    load_aws_kms(True) # 2.27μs -> 2.06μs (10.4% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from litellm.secret_managers.aws_secret_manager import load_aws_kms
import pytest

def test_load_aws_kms():
    with pytest.raises(ValueError, match='Missing\\ required\\ environment\\ variable\\ \\-\\ AWS_REGION_NAME'):
        load_aws_kms(True)

def test_load_aws_kms_2():
    load_aws_kms(None)
🔎 Concolic Coverage Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
codeflash_concolic_kt42dg31/tmpgdyw_yuu/test_concolic_coverage.py::test_load_aws_kms 48.2ms 5.30μs 908486%✅
codeflash_concolic_kt42dg31/tmpgdyw_yuu/test_concolic_coverage.py::test_load_aws_kms_2 448ns 455ns -1.54%⚠️

To edit these changes git checkout codeflash/optimize-load_aws_kms-mhdddil7 and push.

Codeflash Static Badge

The optimized code achieves a **90x speedup** through several key optimizations:

**Primary optimization: Eliminating expensive boto3 import**
- The line profiler shows `import boto3` consumed 97.4% of execution time in the original code (98.6ms out of 101ms total)
- The optimized version strategically moves the boto3 import after validation, reducing import overhead for failed validation cases

**Condition simplification**
- Changed `if use_aws_kms is None or use_aws_kms is False:` to `if not use_aws_kms:`
- This simplifies the boolean logic and provides modest performance gains (8-30% faster in early-return test cases)

**Environment variable access optimization**
- Replaced `os.getenv("AWS_REGION_NAME")` with direct `os.environ["AWS_REGION_NAME"]` access
- Direct dictionary access avoids function call overhead and is safe since validation ensures the key exists
- Stored in local variable `region` to avoid repeated lookups

**Test case performance analysis:**
- **Early returns** (None/False cases): 8-30% faster due to simplified condition checking
- **Validation failures**: 33-40% faster by avoiding boto3 import before validation
- **Successful cases**: 3-12% faster from environment variable optimization
- **Large-scale scenarios**: 7-9% consistent improvement across repeated calls

The optimization particularly excels when `use_aws_kms` is falsy or when validation fails, as it completely avoids the expensive boto3 import that dominated the original runtime.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 30, 2025 11:57
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash labels Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant