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 method SessionRedirectMixin.should_strip_auth by 12% #9

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

📄 12% (0.12x) speedup for SessionRedirectMixin.should_strip_auth in src/requests/sessions.py

⏱️ Runtime : 48.8 milliseconds 43.5 milliseconds (best of 5 runs)

📝 Explanation and details

These changes should not affect the correctness but will help in improving the performance slightly due to reduced redundancy and efficient condition checking.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 234 Passed
🌀 Generated Regression Tests 2048 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
⚙️ Existing Unit Tests Details
- test_requests.py
🌀 Generated Regression Tests Details
import pytest  # used for our unit tests
# function to test
from src.requests.compat import urlparse
from src.requests.sessions import SessionRedirectMixin
from src.requests.utils import DEFAULT_PORTS

DEFAULT_PORTS = {"http": 80, "https": 443}
from src.requests.sessions import SessionRedirectMixin

# unit tests

@pytest.fixture
def mixin():
    return SessionRedirectMixin()

def test_different_hostnames(mixin):
    codeflash_output = mixin.should_strip_auth("http://example.com/resource", "http://another.com/resource")
    codeflash_output = mixin.should_strip_auth("https://example.com/resource", "https://another.com/resource")

def test_same_hostname_different_schemes(mixin):
    codeflash_output = mixin.should_strip_auth("http://example.com/resource", "https://example.com/resource")
    codeflash_output = mixin.should_strip_auth("http://example.com:80/resource", "https://example.com:443/resource")
    codeflash_output = mixin.should_strip_auth("https://example.com/resource", "http://example.com/resource")
    codeflash_output = mixin.should_strip_auth("https://example.com:443/resource", "http://example.com:80/resource")

def test_same_hostname_different_ports(mixin):
    codeflash_output = mixin.should_strip_auth("http://example.com:8080/resource", "http://example.com:9090/resource")
    codeflash_output = mixin.should_strip_auth("https://example.com:8443/resource", "https://example.com:9443/resource")

def test_same_hostname_default_ports(mixin):
    codeflash_output = mixin.should_strip_auth("http://example.com/resource", "http://example.com:80/resource")
    codeflash_output = mixin.should_strip_auth("https://example.com/resource", "https://example.com:443/resource")

def test_same_hostname_no_ports(mixin):
    codeflash_output = mixin.should_strip_auth("http://example.com/resource", "http://example.com/resource")
    codeflash_output = mixin.should_strip_auth("https://example.com/resource", "https://example.com/resource")

def test_special_case_http_to_https(mixin):
    codeflash_output = mixin.should_strip_auth("http://example.com/resource", "https://example.com/resource")
    codeflash_output = mixin.should_strip_auth("http://example.com:80/resource", "https://example.com:443/resource")


def test_empty_urls(mixin):
    codeflash_output = mixin.should_strip_auth("", "http://example.com/resource")
    codeflash_output = mixin.should_strip_auth("http://example.com/resource", "")

def test_urls_with_credentials(mixin):
    codeflash_output = mixin.should_strip_auth("http://user:[email protected]/resource", "http://user:[email protected]/resource")
    codeflash_output = mixin.should_strip_auth("https://user:[email protected]/resource", "https://user:[email protected]/resource")

def test_long_urls(mixin):
    codeflash_output = mixin.should_strip_auth("http://example.com/" + "a" * 1000, "http://example.com/" + "a" * 1000)
    codeflash_output = mixin.should_strip_auth("http://example.com:80/" + "a" * 1000, "https://example.com:443/" + "a" * 1000)

def test_mixed_schemes_and_ports(mixin):
    codeflash_output = mixin.should_strip_auth("http://example.com:8080/resource", "https://example.com:8443/resource")
    codeflash_output = mixin.should_strip_auth("https://example.com:443/resource", "http://example.com:80/resource")

def test_internationalized_domain_names(mixin):
    codeflash_output = mixin.should_strip_auth("http://xn--fsq.com/resource", "http://xn--fsq.com/resource")
    codeflash_output = mixin.should_strip_auth("http://xn--fsq.com/resource", "http://xn--fsq-1na.com/resource")
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

import pytest  # used for our unit tests
# function to test
from src.requests.compat import urlparse
from src.requests.sessions import SessionRedirectMixin
from src.requests.utils import DEFAULT_PORTS

DEFAULT_PORTS = {"http": 80, "https": 443}
from src.requests.sessions import SessionRedirectMixin


# unit tests
@pytest.fixture
def mixin():
    return SessionRedirectMixin()

# Basic Test Cases
def test_same_hostname_same_scheme_default_ports(mixin):
    codeflash_output = not mixin.should_strip_auth('http://example.com:80', 'http://example.com:80')
    codeflash_output = not mixin.should_strip_auth('https://example.com:443', 'https://example.com:443')

def test_same_hostname_different_scheme_default_ports(mixin):
    codeflash_output = not mixin.should_strip_auth('http://example.com:80', 'https://example.com:443')

def test_same_hostname_same_scheme_different_ports(mixin):
    codeflash_output = mixin.should_strip_auth('http://example.com:80', 'http://example.com:8080')
    codeflash_output = mixin.should_strip_auth('https://example.com:443', 'https://example.com:8443')

# Edge Test Cases
def test_different_hostnames(mixin):
    codeflash_output = mixin.should_strip_auth('http://example.com', 'http://anotherexample.com')
    codeflash_output = mixin.should_strip_auth('https://example.com', 'https://anotherexample.com')

def test_edge_cases_with_missing_components(mixin):
    codeflash_output = not mixin.should_strip_auth('http://example.com', 'http://example.com:80')
    codeflash_output = not mixin.should_strip_auth('https://example.com', 'https://example.com:443')

def test_subdomains(mixin):
    codeflash_output = mixin.should_strip_auth('http://sub.example.com', 'http://example.com')
    codeflash_output = mixin.should_strip_auth('https://sub.example.com', 'https://example.com')

def test_unusual_but_valid_urls(mixin):
    codeflash_output = mixin.should_strip_auth('http://example.com/path', 'http://example.com/otherpath')
    codeflash_output = mixin.should_strip_auth('https://example.com/path', 'https://example.com/otherpath')

# Rare or Unexpected Edge Cases
def test_urls_with_user_info(mixin):
    codeflash_output = mixin.should_strip_auth('http://user:[email protected]', 'http://user:[email protected]')
    codeflash_output = not mixin.should_strip_auth('http://user:[email protected]', 'https://user:[email protected]')

def test_urls_with_query_parameters(mixin):
    codeflash_output = mixin.should_strip_auth('http://example.com?query=1', 'http://example.com?query=2')

def test_urls_with_fragments(mixin):
    codeflash_output = mixin.should_strip_auth('http://example.com#section1', 'http://example.com#section2')

def test_urls_with_mixed_case_hostnames(mixin):
    codeflash_output = not mixin.should_strip_auth('http://Example.com', 'http://example.com')

def test_urls_with_trailing_dots_in_hostnames(mixin):
    codeflash_output = not mixin.should_strip_auth('http://example.com.', 'http://example.com.')

def test_urls_with_unusual_but_valid_characters_in_path(mixin):
    codeflash_output = mixin.should_strip_auth('http://example.com/!{report_table}\'()*+,;=', 'http://example.com/!{report_table}\'()*+,;=')

def test_urls_with_port_0(mixin):
    codeflash_output = not mixin.should_strip_auth('http://example.com:0', 'http://example.com:0')

def test_urls_with_loopback_addresses(mixin):
    codeflash_output = not mixin.should_strip_auth('http://127.0.0.1', 'http://127.0.0.1')

def test_urls_with_localhost(mixin):
    codeflash_output = not mixin.should_strip_auth('http://localhost', 'http://localhost')

# Large Scale Test Cases
def test_large_scale_http_to_https(mixin):
    for i in range(1000):
        codeflash_output = not mixin.should_strip_auth(f'http://example.com:{80+i}', f'https://example.com:{443+i}')

def test_large_scale_same_scheme_different_ports(mixin):
    for i in range(1000):
        codeflash_output = mixin.should_strip_auth(f'http://example.com:{80+i}', f'http://example.com:{8080+i}')
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

📢 Feedback on this optimization? Discord

These changes should not affect the correctness but will help in improving the performance slightly due to reduced redundancy and efficient condition checking.
@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 06:06
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