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 HTTPAdapter.request_url by 13% #25

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

📄 13% (0.13x) speedup for HTTPAdapter.request_url in src/requests/adapters.py

⏱️ Runtime : 867 microseconds 769 microseconds (best of 70 runs)

📝 Explanation and details

Here is a rewritten and optimized version of the given Python program. The changes are highlighted in comments.

Key optimizations include.

  • Reduced multiple calls to urlparse(url) by reusing the parsed URL components.
  • Used shortcuts for conditional assignments and early returns to simplify logic.
  • Removed redundant check for proxy_keys (iteration over an in check automatically stops on first match).
  • Simplified repeated assignment evaluations inside loop conditions and returns.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 1 Passed
🌀 Generated Regression Tests 32 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
⚙️ Existing Unit Tests Details
- test_adapters.py
🌀 Generated Regression Tests Details
from urllib.parse import urlparse

# imports
import pytest  # used for our unit tests
from requests.models import PreparedRequest
from src.requests.adapters import HTTPAdapter
# function to test
from src.requests.compat import urlparse, urlunparse
from src.requests.utils import select_proxy, urldefragauth
from urllib3.util.retry import Retry

# unit tests

class TestRequestURL:
    @pytest.fixture
    def adapter(self):
        return HTTPAdapter()

    @pytest.fixture
    def prepared_request(self):
        req = PreparedRequest()
        return req

    def test_http_url_without_proxy(self, adapter, prepared_request):
        # Basic test case: HTTP URL without proxy
        prepared_request.prepare_url('http://example.com/path', None)
        proxies = {}
        codeflash_output = adapter.request_url(prepared_request, proxies)

    def test_https_url_without_proxy(self, adapter, prepared_request):
        # Basic test case: HTTPS URL without proxy
        prepared_request.prepare_url('https://example.com/path', None)
        proxies = {}
        codeflash_output = adapter.request_url(prepared_request, proxies)

    def test_http_url_with_proxy(self, adapter, prepared_request):
        # Basic test case: HTTP URL with proxy
        prepared_request.prepare_url('http://example.com/path', None)
        proxies = {'http': 'http://proxy.com'}
        codeflash_output = adapter.request_url(prepared_request, proxies)

    def test_url_with_fragment_and_auth(self, adapter, prepared_request):
        # Edge case: URL with fragment and authentication
        prepared_request.prepare_url('http://user:[email protected]/path#fragment', None)
        proxies = {'http': 'http://proxy.com'}
        codeflash_output = adapter.request_url(prepared_request, proxies)

    

from urllib.parse import urlparse, urlunparse

# imports
import pytest  # used for our unit tests
from src.requests.adapters import HTTPAdapter
# function to test
from src.requests.compat import urlparse, urlunparse
from src.requests.utils import select_proxy, urldefragauth
from urllib3.util.retry import Retry


class BaseAdapter:
    pass
from src.requests.adapters import HTTPAdapter

# unit tests

class MockRequest:
    def __init__(self, url, path_url):
        self.url = url
        self.path_url = path_url

@pytest.fixture
def adapter():
    return HTTPAdapter()

def test_http_url_without_proxy(adapter):
    request = MockRequest('http://example.com/path', '/path')
    proxies = {}
    codeflash_output = adapter.request_url(request, proxies)

def test_https_url_without_proxy(adapter):
    request = MockRequest('https://example.com/path', '/path')
    proxies = {}
    codeflash_output = adapter.request_url(request, proxies)

def test_http_url_with_http_proxy(adapter):
    request = MockRequest('http://example.com/path', '/path')
    proxies = {'http': 'http://proxy.com'}
    codeflash_output = adapter.request_url(request, proxies)

def test_http_url_with_socks_proxy(adapter):
    request = MockRequest('http://example.com/path', '/path')
    proxies = {'http': 'socks5://proxy.com'}
    codeflash_output = adapter.request_url(request, proxies)

def test_https_url_with_http_proxy(adapter):
    request = MockRequest('https://example.com/path', '/path')
    proxies = {'http': 'http://proxy.com'}
    codeflash_output = adapter.request_url(request, proxies)

def test_https_url_with_socks_proxy(adapter):
    request = MockRequest('https://example.com/path', '/path')
    proxies = {'https': 'socks5://proxy.com'}
    codeflash_output = adapter.request_url(request, proxies)

def test_http_url_with_authentication(adapter):
    request = MockRequest('http://user:[email protected]/path', '/path')
    proxies = {'http': 'http://proxy.com'}
    codeflash_output = adapter.request_url(request, proxies)

def test_https_url_with_authentication(adapter):
    request = MockRequest('https://user:[email protected]/path', '/path')
    proxies = {'https': 'http://proxy.com'}
    codeflash_output = adapter.request_url(request, proxies)

def test_http_url_with_fragment(adapter):
    request = MockRequest('http://example.com/path#section', '/path')
    proxies = {'http': 'http://proxy.com'}
    codeflash_output = adapter.request_url(request, proxies)

def test_https_url_with_fragment(adapter):
    request = MockRequest('https://example.com/path#section', '/path')
    proxies = {'https': 'http://proxy.com'}
    codeflash_output = adapter.request_url(request, proxies)

def test_http_url_with_authentication_and_fragment(adapter):
    request = MockRequest('http://user:[email protected]/path#section', '/path')
    proxies = {'http': 'http://proxy.com'}
    codeflash_output = adapter.request_url(request, proxies)

def test_https_url_with_authentication_and_fragment(adapter):
    request = MockRequest('https://user:[email protected]/path#section', '/path')
    proxies = {'https': 'http://proxy.com'}
    codeflash_output = adapter.request_url(request, proxies)

def test_http_url_starting_with_double_slashes(adapter):
    request = MockRequest('http://example.com//path', '//path')
    proxies = {'http': 'http://proxy.com'}
    codeflash_output = adapter.request_url(request, proxies)

def test_https_url_starting_with_double_slashes(adapter):
    request = MockRequest('https://example.com//path', '//path')
    proxies = {'https': 'http://proxy.com'}
    codeflash_output = adapter.request_url(request, proxies)




def test_large_url(adapter):
    large_url = 'http://example.com/' + 'a' * 10000
    request = MockRequest(large_url, '/' + 'a' * 10000)
    proxies = {'http': 'http://proxy.com'}
    codeflash_output = adapter.request_url(request, proxies)

def test_large_number_of_proxies(adapter):
    proxies = {f'http://proxy{i}.com': 'http://proxy.com' for i in range(1000)}
    request = MockRequest('http://example.com/path', '/path')
    codeflash_output = adapter.request_url(request, proxies)
# 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

Here is a rewritten and optimized version of the given Python program. The changes are highlighted in comments.



Key optimizations include.
- Reduced multiple calls to `urlparse(url)` by reusing the parsed URL components.
- Used shortcuts for conditional assignments and early returns to simplify logic.
- Removed redundant check for `proxy_keys` (iteration over an `in` check automatically stops on first match).
- Simplified repeated assignment evaluations inside loop conditions and returns.
@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 18:35
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