Skip to content

⚡️ Speed up _get_google_cloud_logs_url() by 6% in sentry_sdk/integrations/gcp.py#1

Open
codeflash-ai[bot] wants to merge 1 commit intomasterfrom
codeflash/optimize-_get_google_cloud_logs_url-2024-06-14T01.07.15
Open

⚡️ Speed up _get_google_cloud_logs_url() by 6% in sentry_sdk/integrations/gcp.py#1
codeflash-ai[bot] wants to merge 1 commit intomasterfrom
codeflash/optimize-_get_google_cloud_logs_url-2024-06-14T01.07.15

Conversation

@codeflash-ai
Copy link
Copy Markdown

@codeflash-ai codeflash-ai Bot commented Jun 14, 2024

📄 _get_google_cloud_logs_url() in sentry_sdk/integrations/gcp.py

📈 Performance improved by 6% (0.06x faster)

⏱️ Runtime went down from 268 microseconds to 252 microseconds

Explanation and details

To optimize the given function for runtime and memory utilization, you can make a few changes such as utilizing f-strings for faster string formatting and minimize calls to environ.get by storing the results in local variables. Here is the optimized version of the function.

Key Optimizations.

  1. Use of f-strings: These are faster than str.format() for string formatting in Python.
  2. Reduced environment variable accesses: Access environ.get only once for each variable to reduce dictionary lookups.

These changes can lead to a noticeable improvement in both performance and memory efficiency, especially in cases where the function is called repeatedly.

Correctness verification

The new optimized code was tested for correctness. The results are listed below.

🔘 (none found) − ⚙️ Existing Unit Tests

✅ 9 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
# imports
from datetime import datetime, timedelta
from os import environ

import pytest  # used for our unit tests
from sentry_sdk.integrations.gcp import _get_google_cloud_logs_url


# unit tests
def test_basic_functionality(monkeypatch):
    # Set environment variables
    monkeypatch.setenv("GCP_PROJECT", "test-project")
    monkeypatch.setenv("FUNCTION_NAME", "test-function")
    monkeypatch.setenv("FUNCTION_REGION", "us-central1")
    
    # Define a final_time
    final_time = datetime(2023, 1, 1, 12, 0, 0)
    
    # Expected URL
    expected_url = (
        "https://console.cloud.google.com/logs/viewer?project=test-project&resource=cloud_function"
        "%2Ffunction_name%2Ftest-function%2Fregion%2Fus-central1&minLogLevel=0&expandAll=false"
        "&timestamp=2023-01-01T12:00:00Z&customFacets=&limitCustomFacetWidth=true"
        "&dateRangeStart=2023-01-01T11:00:00Z&dateRangeEnd=2023-01-01T12:00:00Z"
        "&interval=PT1H&scrollTimestamp=2023-01-01T12:00:00Z"
    )
    
    # Assert the URL is as expected
    assert _get_google_cloud_logs_url(final_time) == expected_url

def test_missing_gcp_project(monkeypatch):
    monkeypatch.delenv("GCP_PROJECT", raising=False)
    monkeypatch.setenv("FUNCTION_NAME", "test-function")
    monkeypatch.setenv("FUNCTION_REGION", "us-central1")
    
    final_time = datetime(2023, 1, 1, 12, 0, 0)
    
    expected_url = (
        "https://console.cloud.google.com/logs/viewer?project=None&resource=cloud_function"
        "%2Ffunction_name%2Ftest-function%2Fregion%2Fus-central1&minLogLevel=0&expandAll=false"
        "&timestamp=2023-01-01T12:00:00Z&customFacets=&limitCustomFacetWidth=true"
        "&dateRangeStart=2023-01-01T11:00:00Z&dateRangeEnd=2023-01-01T12:00:00Z"
        "&interval=PT1H&scrollTimestamp=2023-01-01T12:00:00Z"
    )
    
    assert _get_google_cloud_logs_url(final_time) == expected_url

def test_missing_function_name(monkeypatch):
    monkeypatch.setenv("GCP_PROJECT", "test-project")
    monkeypatch.delenv("FUNCTION_NAME", raising=False)
    monkeypatch.setenv("FUNCTION_REGION", "us-central1")
    
    final_time = datetime(2023, 1, 1, 12, 0, 0)
    
    expected_url = (
        "https://console.cloud.google.com/logs/viewer?project=test-project&resource=cloud_function"
        "%2Ffunction_name%2FNone%2Fregion%2Fus-central1&minLogLevel=0&expandAll=false"
        "&timestamp=2023-01-01T12:00:00Z&customFacets=&limitCustomFacetWidth=true"
        "&dateRangeStart=2023-01-01T11:00:00Z&dateRangeEnd=2023-01-01T12:00:00Z"
        "&interval=PT1H&scrollTimestamp=2023-01-01T12:00:00Z"
    )
    
    assert _get_google_cloud_logs_url(final_time) == expected_url

def test_missing_function_region(monkeypatch):
    monkeypatch.setenv("GCP_PROJECT", "test-project")
    monkeypatch.setenv("FUNCTION_NAME", "test-function")
    monkeypatch.delenv("FUNCTION_REGION", raising=False)
    
    final_time = datetime(2023, 1, 1, 12, 0, 0)
    
    expected_url = (
        "https://console.cloud.google.com/logs/viewer?project=test-project&resource=cloud_function"
        "%2Ffunction_name%2Ftest-function%2Fregion%2FNone&minLogLevel=0&expandAll=false"
        "&timestamp=2023-01-01T12:00:00Z&customFacets=&limitCustomFacetWidth=true"
        "&dateRangeStart=2023-01-01T11:00:00Z&dateRangeEnd=2023-01-01T12:00:00Z"
        "&interval=PT1H&scrollTimestamp=2023-01-01T12:00:00Z"
    )
    
    assert _get_google_cloud_logs_url(final_time) == expected_url

def test_epoch_time(monkeypatch):
    monkeypatch.setenv("GCP_PROJECT", "test-project")
    monkeypatch.setenv("FUNCTION_NAME", "test-function")
    monkeypatch.setenv("FUNCTION_REGION", "us-central1")
    
    final_time = datetime(1970, 1, 1, 0, 0, 0)
    
    expected_url = (
        "https://console.cloud.google.com/logs/viewer?project=test-project&resource=cloud_function"
        "%2Ffunction_name%2Ftest-function%2Fregion%2Fus-central1&minLogLevel=0&expandAll=false"
        "&timestamp=1970-01-01T00:00:00Z&customFacets=&limitCustomFacetWidth=true"
        "&dateRangeStart=1969-12-31T23:00:00Z&dateRangeEnd=1970-01-01T00:00:00Z"
        "&interval=PT1H&scrollTimestamp=1970-01-01T00:00:00Z"
    )
    
    assert _get_google_cloud_logs_url(final_time) == expected_url

def test_far_future_time(monkeypatch):
    monkeypatch.setenv("GCP_PROJECT", "test-project")
    monkeypatch.setenv("FUNCTION_NAME", "test-function")
    monkeypatch.setenv("FUNCTION_REGION", "us-central1")
    
    final_time = datetime(3000, 1, 1, 0, 0, 0)
    
    expected_url = (
        "https://console.cloud.google.com/logs/viewer?project=test-project&resource=cloud_function"
        "%2Ffunction_name%2Ftest-function%2Fregion%2Fus-central1&minLogLevel=0&expandAll=false"
        "&timestamp=3000-01-01T00:00:00Z&customFacets=&limitCustomFacetWidth=true"
        "&dateRangeStart=2999-12-31T23:00:00Z&dateRangeEnd=3000-01-01T00:00:00Z"
        "&interval=PT1H&scrollTimestamp=3000-01-01T00:00:00Z"
    )
    
    assert _get_google_cloud_logs_url(final_time) == expected_url

def test_different_timezones(monkeypatch):
    monkeypatch.setenv("GCP_PROJECT", "test-project")
    monkeypatch.setenv("FUNCTION_NAME", "test-function")
    monkeypatch.setenv("FUNCTION_REGION", "us-central1")
    
    final_time = datetime(2023, 1, 1, 12, 0, 0)
    
    expected_url = (
        "https://console.cloud.google.com/logs/viewer?project=test-project&resource=cloud_function"
        "%2Ffunction_name%2Ftest-function%2Fregion%2Fus-central1&minLogLevel=0&expandAll=false"
        "&timestamp=2023-01-01T12:00:00Z&customFacets=&limitCustomFacetWidth=true"
        "&dateRangeStart=2023-01-01T11:00:00Z&dateRangeEnd=2023-01-01T12:00:00Z"
        "&interval=PT1H&scrollTimestamp=2023-01-01T12:00:00Z"
    )
    
    assert _get_google_cloud_logs_url(final_time) == expected_url

def test_special_characters_in_env_vars(monkeypatch):
    monkeypatch.setenv("GCP_PROJECT", "test-project with spaces")
    monkeypatch.setenv("FUNCTION_NAME", "test-function@123")
    monkeypatch.setenv("FUNCTION_REGION", "us-central1!")
    
    final_time = datetime(2023, 1, 1, 12, 0, 0)
    
    expected_url = (
        "https://console.cloud.google.com/logs/viewer?project=test-project with spaces&resource=cloud_function"
        "%2Ffunction_name%2Ftest-function@123%2Fregion%2Fus-central1!&minLogLevel=0&expandAll=false"
        "&timestamp=2023-01-01T12:00:00Z&customFacets=&limitCustomFacetWidth=true"
        "&dateRangeStart=2023-01-01T11:00:00Z&dateRangeEnd=2023-01-01T12:00:00Z"
        "&interval=PT1H&scrollTimestamp=2023-01-01T12:00:00Z"
    )
    
    assert _get_google_cloud_logs_url(final_time) == expected_url

def test_large_scale_performance(monkeypatch):
    monkeypatch.setenv("GCP_PROJECT", "test-project")
    monkeypatch.setenv("FUNCTION_NAME", "test-function")
    monkeypatch.setenv("FUNCTION_REGION", "us-central1")
    
    final_time = datetime(2023, 1, 1, 12, 0, 0)
    
    for _ in range(10000):
        _get_google_cloud_logs_url(final_time)

def test_invalid_final_time():
    with pytest.raises(AttributeError):
        _get_google_cloud_logs_url(None)
    
    with pytest.raises(AttributeError):
        _get_google_cloud_logs_url("2023-01-01T12:00:00Z")

🔘 (none found) − ⏪ Replay Tests

To optimize the given function for runtime and memory utilization, you can make a few changes such as utilizing f-strings for faster string formatting and minimize calls to `environ.get` by storing the results in local variables. Here is the optimized version of the function.



### Key Optimizations.
1. **Use of f-strings:** These are faster than `str.format()` for string formatting in Python.
2. **Reduced environment variable accesses:** Access `environ.get` only once for each variable to reduce dictionary lookups.

These changes can lead to a noticeable improvement in both performance and memory efficiency, especially in cases where the function is called repeatedly.
@codeflash-ai codeflash-ai Bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Jun 14, 2024
@codeflash-ai codeflash-ai Bot requested a review from misrasaurabh1 June 14, 2024 01:07
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