Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ Speed up function _basic_auth_str by 6% #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Dec 22, 2024

📄 6% (0.06x) speedup for _basic_auth_str in src/requests/auth.py

⏱️ Runtime : 3.14 milliseconds 2.96 milliseconds (best of 5 runs)

📝 Explanation and details

Certainly! Below is an optimized version of the provided Python program.

Explanation of Optimizations.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 231 Passed
🌀 Generated Regression Tests 1132 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 93.3%
⚙️ Existing Unit Tests Details
- test_requests.py
🌀 Generated Regression Tests Details
import warnings
from base64 import b64encode

# imports
import pytest  # used for our unit tests
from src.requests._internal_utils import to_native_string
from src.requests.auth import _basic_auth_str
from src.requests.compat import basestring, builtin_str, str

# function to test
basestring = (str, bytes)

str = str
from src.requests.auth import _basic_auth_str

# unit tests

# Basic Valid Inputs
def test_basic_valid_inputs():
    codeflash_output = _basic_auth_str("user", "pass")
    codeflash_output = _basic_auth_str("admin", "1234")
    codeflash_output = _basic_auth_str("", "")

# Non-String Inputs (with Deprecation Warnings)
def test_non_string_inputs():
    with pytest.warns(DeprecationWarning):
        codeflash_output = _basic_auth_str(123, 456)
    with pytest.warns(DeprecationWarning):
        codeflash_output = _basic_auth_str(12.34, 56.78)
    with pytest.warns(DeprecationWarning):
        codeflash_output = _basic_auth_str(True, False)

# Unicode Characters

def test_special_characters():
    codeflash_output = _basic_auth_str("user!@#", "pass$%^")
    codeflash_output = _basic_auth_str("user:colon", "pass:colon")
    codeflash_output = _basic_auth_str("user name", "pass word")
    codeflash_output = _basic_auth_str(" user ", " pass ")

# Edge Cases
def test_edge_cases():
    codeflash_output = _basic_auth_str("user\0name", "pass\0word")

# Mixed Types
def test_mixed_types():
    with pytest.warns(DeprecationWarning):
        codeflash_output = _basic_auth_str("user", 1234)
    with pytest.warns(DeprecationWarning):
        codeflash_output = _basic_auth_str(1234, "pass")
    codeflash_output = _basic_auth_str(b"user", "pass")
    codeflash_output = _basic_auth_str("user", b"pass")

# Performance and Scalability
def test_performance_scalability():
    for i in range(100):
        codeflash_output = _basic_auth_str(f"user{i}", f"pass{i}")
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

import warnings
from base64 import b64encode

# imports
import pytest  # used for our unit tests
from src.requests._internal_utils import to_native_string
from src.requests.auth import _basic_auth_str
from src.requests.compat import basestring, builtin_str, str

# function to test
basestring = (str, bytes)

str = str
from src.requests.auth import _basic_auth_str


# unit tests
def test_basic_valid_inputs():
    # Test with normal strings
    codeflash_output = _basic_auth_str("user", "pass")
    # Test with empty strings
    codeflash_output = _basic_auth_str("", "")

def test_non_string_inputs():
    # Test with integers
    with pytest.warns(DeprecationWarning):
        codeflash_output = _basic_auth_str(123, 456)
    # Test with floats
    with pytest.warns(DeprecationWarning):
        codeflash_output = _basic_auth_str(12.34, 56.78)
    # Test with booleans
    with pytest.warns(DeprecationWarning):
        codeflash_output = _basic_auth_str(True, False)

def test_special_characters():
    # Test with special characters
    codeflash_output = _basic_auth_str("user!@#", "pass$%^")
    # Test with spaces
    codeflash_output = _basic_auth_str("user name", "pass word")


def test_byte_strings():
    # Test with byte strings
    codeflash_output = _basic_auth_str(b"user", b"pass")
    # Test with mixed types
    codeflash_output = _basic_auth_str("user", b"pass")

def test_long_strings():
    # Test with very long strings
    long_username = "u" * 1000
    long_password = "p" * 1000
    expected_output = "Basic " + b64encode((long_username + ":" + long_password).encode("latin1")).decode("ascii")
    codeflash_output = _basic_auth_str(long_username, long_password)

def test_edge_cases():
    # Test with None as username
    with pytest.warns(DeprecationWarning):
        codeflash_output = _basic_auth_str(None, "pass")
    # Test with None as password
    with pytest.warns(DeprecationWarning):
        codeflash_output = _basic_auth_str("user", None)
    # Test with None as both username and password
    with pytest.warns(DeprecationWarning):
        codeflash_output = _basic_auth_str(None, None)
    # Test with newline characters
    codeflash_output = _basic_auth_str("user\nname", "pass\nword")

def test_performance_and_scalability():
    # Test with a large dataset
    usernames = ["user{}".format(i) for i in range(1000)]
    passwords = ["pass{}".format(i) for i in range(1000)]
    for i in range(1000):
        expected_output = "Basic " + b64encode(("user{}:pass{}".format(i, i)).encode("latin1")).decode("ascii")
        codeflash_output = _basic_auth_str(usernames[i], passwords[i])

def test_deprecation_warning_verification():
    # Verify deprecation warning for non-string username
    with pytest.warns(DeprecationWarning):
        _basic_auth_str(123, "pass")
    # Verify deprecation warning for non-string password
    with pytest.warns(DeprecationWarning):
        _basic_auth_str("user", 456)

📢 Feedback on this optimization? Discord

Certainly! Below is an optimized version of the provided Python program.

### Explanation of Optimizations.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Dec 22, 2024
@codeflash-ai codeflash-ai bot requested a review from alvin-r December 22, 2024 12:36
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants