Skip to content

Conversation

codeflash-ai[bot]
Copy link

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

📄 72% (0.72x) speedup for get_experiment_url in google/cloud/aiplatform/utils/tensorboard_utils.py

⏱️ Runtime : 2.75 milliseconds 1.60 milliseconds (best of 200 runs)

📝 Explanation and details

The optimized code achieves a 72% speedup by replacing the heavyweight TensorboardServiceClient.parse_tensorboard_experiment_path() call with a pre-compiled regex pattern.

Key optimizations:

  1. Pre-compiled regex pattern: The _TBOARD_EXPERIMENT_RE pattern is compiled once at module load time, eliminating repeated compilation overhead on every function call.

  2. Direct regex matching: Instead of calling into the TensorboardServiceClient class method (which adds import overhead and method dispatch), the code uses direct re.match() calls.

  3. Eliminated function call overhead: The optimized get_experiment_url() performs regex matching inline rather than delegating to _parse_experiment_name(), reducing function call stack depth.

Performance benefits by test case type:

  • Valid inputs (70-90% faster): Pre-compiled regex avoids re-compilation and client method overhead
  • Invalid inputs (45-112% faster): Fast regex failure is much quicker than client parsing logic
  • Large-scale tests (18-77% faster): Benefits compound with volume due to eliminated per-call compilation

The line profiler shows the original code spent 85.7% of time in TensorboardServiceClient.parse_tensorboard_experiment_path(), which is completely eliminated in the optimized version. The new implementation maintains identical behavior and error handling while being significantly more efficient for all input patterns.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 1995 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import re
from typing import Dict

# imports
import pytest  # used for our unit tests
from aiplatform.utils.tensorboard_utils import get_experiment_url

# function to test
# -*- coding: utf-8 -*-
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


_SERVING_DOMAIN = "tensorboard.googleusercontent.com"
from aiplatform.utils.tensorboard_utils import get_experiment_url

# unit tests

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

def test_basic_valid_experiment_url():
    # Test a typical valid experiment name
    exp_name = "projects/123/locations/us-central1/tensorboards/456/experiments/exp1"
    expected_url = "https://us-central1.tensorboard.googleusercontent.com/experiment/projects+123+locations+us-central1+tensorboards+456+experiments+exp1"
    codeflash_output = get_experiment_url(exp_name) # 4.84μs -> 2.53μs (91.8% faster)

def test_basic_valid_experiment_url_with_different_location():
    # Test a valid experiment name with a different location
    exp_name = "projects/abc/locations/europe-west4/tensorboards/def/experiments/exp2"
    expected_url = "https://europe-west4.tensorboard.googleusercontent.com/experiment/projects+abc+locations+europe-west4+tensorboards+def+experiments+exp2"
    codeflash_output = get_experiment_url(exp_name) # 4.26μs -> 2.30μs (85.1% faster)

def test_basic_valid_experiment_url_with_numeric_ids():
    # Test valid experiment name with numeric IDs
    exp_name = "projects/999/locations/asia-east1/tensorboards/888/experiments/777"
    expected_url = "https://asia-east1.tensorboard.googleusercontent.com/experiment/projects+999+locations+asia-east1+tensorboards+888+experiments+777"
    codeflash_output = get_experiment_url(exp_name) # 3.91μs -> 2.20μs (77.5% faster)

def test_basic_valid_experiment_url_with_special_chars():
    # Test valid experiment name with special characters in experiment id
    exp_name = "projects/123/locations/us-central1/tensorboards/456/experiments/exp_1-2.3"
    expected_url = "https://us-central1.tensorboard.googleusercontent.com/experiment/projects+123+locations+us-central1+tensorboards+456+experiments+exp_1-2.3"
    codeflash_output = get_experiment_url(exp_name) # 3.97μs -> 2.36μs (68.1% faster)

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

def test_edge_invalid_experiment_name_missing_segments():
    # Missing 'experiments' segment
    exp_name = "projects/123/locations/us-central1/tensorboards/456"
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 3.72μs -> 2.56μs (45.4% faster)

def test_edge_invalid_experiment_name_wrong_order():
    # Wrong order of segments
    exp_name = "projects/123/tensorboards/456/locations/us-central1/experiments/exp1"
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 3.93μs -> 2.59μs (52.0% faster)

def test_edge_invalid_experiment_name_empty_string():
    # Empty string should raise ValueError
    exp_name = ""
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 2.35μs -> 1.21μs (93.9% faster)


def test_edge_invalid_experiment_name_missing_project():
    # Missing project segment
    exp_name = "locations/us-central1/tensorboards/456/experiments/exp1"
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 3.86μs -> 1.91μs (102% faster)

def test_edge_invalid_experiment_name_missing_location():
    # Missing location segment
    exp_name = "projects/123/tensorboards/456/experiments/exp1"
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 2.53μs -> 1.32μs (90.8% faster)

def test_edge_invalid_experiment_name_missing_tensorboard():
    # Missing tensorboard segment
    exp_name = "projects/123/locations/us-central1/experiments/exp1"
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 3.99μs -> 2.72μs (46.9% faster)

def test_edge_invalid_experiment_name_missing_experiment():
    # Missing experiment segment
    exp_name = "projects/123/locations/us-central1/tensorboards/456/"
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 3.97μs -> 2.65μs (49.8% faster)

def test_edge_invalid_experiment_name_only_experiment():
    # Only experiment segment
    exp_name = "experiments/exp1"
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 2.41μs -> 1.17μs (107% faster)

def test_edge_valid_experiment_name_with_long_ids():
    # Very long experiment id, but valid format
    long_id = "a" * 200
    exp_name = f"projects/123/locations/us-central1/tensorboards/456/experiments/{long_id}"
    expected_url = f"https://us-central1.tensorboard.googleusercontent.com/experiment/projects+123+locations+us-central1+tensorboards+456+experiments+{long_id}"
    codeflash_output = get_experiment_url(exp_name) # 6.76μs -> 4.76μs (41.8% faster)

def test_edge_valid_experiment_name_with_unicode():
    # Unicode characters in experiment id
    exp_name = "projects/123/locations/us-central1/tensorboards/456/experiments/实验一"
    expected_url = "https://us-central1.tensorboard.googleusercontent.com/experiment/projects+123+locations+us-central1+tensorboards+456+experiments+实验一"
    codeflash_output = get_experiment_url(exp_name) # 5.92μs -> 4.06μs (45.9% faster)

def test_edge_valid_experiment_name_with_dash_and_underscore():
    # Dash and underscore in experiment id
    exp_name = "projects/123/locations/us-central1/tensorboards/456/experiments/exp-1_2"
    expected_url = "https://us-central1.tensorboard.googleusercontent.com/experiment/projects+123+locations+us-central1+tensorboards+456+experiments+exp-1_2"
    codeflash_output = get_experiment_url(exp_name) # 4.45μs -> 2.59μs (71.9% faster)

def test_edge_valid_experiment_name_with_dot():
    # Dot in experiment id
    exp_name = "projects/123/locations/us-central1/tensorboards/456/experiments/exp.1"
    expected_url = "https://us-central1.tensorboard.googleusercontent.com/experiment/projects+123+locations+us-central1+tensorboards+456+experiments+exp.1"
    codeflash_output = get_experiment_url(exp_name) # 4.22μs -> 2.47μs (70.7% faster)

def test_edge_valid_experiment_name_with_numeric_location():
    # Numeric location
    exp_name = "projects/123/locations/12345/tensorboards/456/experiments/exp1"
    expected_url = "https://12345.tensorboard.googleusercontent.com/experiment/projects+123+locations+12345+tensorboards+456+experiments+exp1"
    codeflash_output = get_experiment_url(exp_name) # 4.09μs -> 2.30μs (78.0% faster)

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

def test_large_scale_many_experiment_names():
    # Test with many valid experiment names (scalability)
    for i in range(1000):
        exp_name = f"projects/{i}/locations/loc{i}/tensorboards/{i}/experiments/exp{i}"
        expected_url = f"https://loc{i}.tensorboard.googleusercontent.com/experiment/projects+{i}+locations+loc{i}+tensorboards+{i}+experiments+exp{i}"
        codeflash_output = get_experiment_url(exp_name) # 1.30ms -> 726μs (79.0% faster)

def test_large_scale_long_experiment_name():
    # Test with very long experiment name (but still valid)
    project = "p" * 100
    location = "l" * 100
    tensorboard = "t" * 100
    experiment = "e" * 100
    exp_name = f"projects/{project}/locations/{location}/tensorboards/{tensorboard}/experiments/{experiment}"
    expected_url = f"https://{location}.tensorboard.googleusercontent.com/experiment/projects+{project}+locations+{location}+tensorboards+{tensorboard}+experiments+{experiment}"
    codeflash_output = get_experiment_url(exp_name) # 8.90μs -> 6.36μs (40.0% faster)

def test_large_scale_experiment_name_with_max_segment_lengths():
    # Test with segments of maximum reasonable length (less than 1000 chars per segment)
    project = "p" * 250
    location = "l" * 250
    tensorboard = "t" * 250
    experiment = "e" * 250
    exp_name = f"projects/{project}/locations/{location}/tensorboards/{tensorboard}/experiments/{experiment}"
    expected_url = f"https://{location}.tensorboard.googleusercontent.com/experiment/projects+{project}+locations+{location}+tensorboards+{tensorboard}+experiments+{experiment}"
    codeflash_output = get_experiment_url(exp_name) # 12.8μs -> 10.8μs (17.8% faster)

def test_large_scale_experiment_name_with_numeric_and_alpha_segments():
    # Test with mixed numeric and alpha segments, large scale
    for i in range(0, 1000, 100):
        project = f"proj{i}"
        location = f"loc{i}"
        tensorboard = f"tb{i}"
        experiment = f"exp{i}"
        exp_name = f"projects/{project}/locations/{location}/tensorboards/{tensorboard}/experiments/{experiment}"
        expected_url = f"https://{location}.tensorboard.googleusercontent.com/experiment/projects+{project}+locations+{location}+tensorboards+{tensorboard}+experiments+{experiment}"
        codeflash_output = get_experiment_url(exp_name) # 17.5μs -> 10.2μs (71.1% faster)

def test_large_scale_invalid_experiment_names():
    # Test many invalid experiment names to check error handling at scale
    invalid_names = [
        f"projects/{i}/locations/loc{i}/tensorboards/{i}/experiments" for i in range(100)
    ] + [
        f"projects/{i}/locations/loc{i}/tensorboards/{i}" for i in range(100)
    ] + [
        f"projects/{i}/locations/loc{i}/experiments/exp{i}" for i in range(100)
    ]
    for name in invalid_names:
        with pytest.raises(ValueError):
            get_experiment_url(name)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import re
from typing import Dict

# imports
import pytest
from aiplatform.utils.tensorboard_utils import get_experiment_url

# function to test
# -*- coding: utf-8 -*-
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


_SERVING_DOMAIN = "tensorboard.googleusercontent.com"
from aiplatform.utils.tensorboard_utils import get_experiment_url

# unit tests

# ----------------- BASIC TEST CASES -----------------

def test_basic_valid_experiment_url():
    # Basic correct input
    exp_name = "projects/123/locations/us-central1/tensorboards/456/experiments/exp1"
    expected_url = "https://us-central1.tensorboard.googleusercontent.com/experiment/projects+123+locations+us-central1+tensorboards+456+experiments+exp1"
    codeflash_output = get_experiment_url(exp_name) # 5.48μs -> 3.18μs (72.0% faster)

def test_basic_valid_experiment_url_with_different_location():
    # Location other than us-central1
    exp_name = "projects/abc/locations/europe-west4/tensorboards/def/experiments/myexp"
    expected_url = "https://europe-west4.tensorboard.googleusercontent.com/experiment/projects+abc+locations+europe-west4+tensorboards+def+experiments+myexp"
    codeflash_output = get_experiment_url(exp_name) # 4.44μs -> 2.56μs (73.3% faster)

def test_basic_valid_experiment_url_with_numbers_and_underscores():
    # IDs with numbers and underscores
    exp_name = "projects/1_2_3/locations/asia-east1/tensorboards/456_789/experiments/exp_42"
    expected_url = "https://asia-east1.tensorboard.googleusercontent.com/experiment/projects+1_2_3+locations+asia-east1+tensorboards+456_789+experiments+exp_42"
    codeflash_output = get_experiment_url(exp_name) # 4.34μs -> 2.54μs (70.4% faster)

def test_basic_valid_experiment_url_with_long_names():
    # Longer experiment name
    exp_name = "projects/longprojectname/locations/australia-southeast1/tensorboards/longtbname/experiments/longexpname"
    expected_url = "https://australia-southeast1.tensorboard.googleusercontent.com/experiment/projects+longprojectname+locations+australia-southeast1+tensorboards+longtbname+experiments+longexpname"
    codeflash_output = get_experiment_url(exp_name) # 4.59μs -> 2.84μs (61.4% faster)

# ----------------- EDGE TEST CASES -----------------

def test_edge_missing_segments():
    # Missing experiment segment
    exp_name = "projects/123/locations/us-central1/tensorboards/456/experiments/"
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 4.38μs -> 3.15μs (39.0% faster)

def test_edge_missing_tensorboard_segment():
    # Missing tensorboard segment
    exp_name = "projects/123/locations/us-central1/tensorboards//experiments/exp1"
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 4.35μs -> 2.98μs (46.1% faster)

def test_edge_missing_location_segment():
    # Missing location segment
    exp_name = "projects/123/locations//tensorboards/456/experiments/exp1"
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 3.78μs -> 2.66μs (42.3% faster)

def test_edge_missing_project_segment():
    # Missing project segment
    exp_name = "projects//locations/us-central1/tensorboards/456/experiments/exp1"
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 3.38μs -> 2.05μs (64.9% faster)

def test_edge_wrong_order_segments():
    # Wrong order of segments
    exp_name = "projects/123/tensorboards/456/locations/us-central1/experiments/exp1"
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 3.79μs -> 2.48μs (53.1% faster)


def test_edge_empty_string():
    # Empty string input
    exp_name = ""
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 3.71μs -> 1.75μs (112% faster)


def test_edge_special_characters_in_ids():
    # IDs with special characters (should still work if regex matches)
    exp_name = "projects/proj!@#/locations/loc$%^/tensorboards/tb&*()/experiments/exp?<>"
    expected_url = "https://loc$%^.tensorboard.googleusercontent.com/experiment/projects+proj!@#+locations+loc$%^+tensorboards+tb&*()+experiments+exp?<>"
    codeflash_output = get_experiment_url(exp_name) # 6.26μs -> 3.53μs (77.2% faster)

def test_edge_unicode_characters_in_ids():
    # IDs with unicode characters
    exp_name = "projects/项目/locations/位置/tensorboards/看板/experiments/实验"
    expected_url = "https://位置.tensorboard.googleusercontent.com/experiment/projects+项目+locations+位置+tensorboards+看板+experiments+实验"
    codeflash_output = get_experiment_url(exp_name) # 5.67μs -> 3.80μs (49.1% faster)

def test_edge_leading_and_trailing_spaces():
    # Leading/trailing spaces should fail regex
    exp_name = " projects/123/locations/us-central1/tensorboards/456/experiments/exp1 "
    with pytest.raises(ValueError):
        get_experiment_url(exp_name) # 2.70μs -> 1.28μs (111% faster)


def test_edge_segments_with_plus_signs():
    # Plus signs in IDs
    exp_name = "projects/proj+ect/locations/loc+ation/tensorboards/tb+name/experiments/exp+name"
    expected_url = "https://loc+ation.tensorboard.googleusercontent.com/experiment/projects+proj+ect+locations+loc+ation+tensorboards+tb+name+experiments+exp+name"
    codeflash_output = get_experiment_url(exp_name) # 6.54μs -> 3.86μs (69.5% faster)

# ----------------- LARGE SCALE TEST CASES -----------------

def test_large_scale_long_experiment_name():
    # Very long experiment name, but still valid
    project = "p" * 200
    location = "us-central1"
    tensorboard = "t" * 200
    experiment = "e" * 200
    exp_name = f"projects/{project}/locations/{location}/tensorboards/{tensorboard}/experiments/{experiment}"
    expected_url = f"https://{location}.tensorboard.googleusercontent.com/experiment/projects+{project}+locations+{location}+tensorboards+{tensorboard}+experiments+{experiment}"
    codeflash_output = get_experiment_url(exp_name) # 9.69μs -> 8.20μs (18.2% faster)

def test_large_scale_many_unique_experiment_names():
    # Test 100 different valid experiment names
    for i in range(100):
        exp_name = f"projects/p{i}/locations/loc{i}/tensorboards/tb{i}/experiments/exp{i}"
        expected_url = f"https://loc{i}.tensorboard.googleusercontent.com/experiment/projects+p{i}+locations+loc{i}+tensorboards+tb{i}+experiments+exp{i}"
        codeflash_output = get_experiment_url(exp_name) # 135μs -> 76.3μs (77.6% faster)

def test_large_scale_max_segment_length():
    # Each segment is very long (but < 1000 chars)
    project = "p" * 900
    location = "l" * 50
    tensorboard = "t" * 20
    experiment = "e" * 20
    exp_name = f"projects/{project}/locations/{location}/tensorboards/{tensorboard}/experiments/{experiment}"
    expected_url = f"https://{location}.tensorboard.googleusercontent.com/experiment/projects+{project}+locations+{location}+tensorboards+{tensorboard}+experiments+{experiment}"
    codeflash_output = get_experiment_url(exp_name) # 12.5μs -> 10.5μs (19.8% faster)

def test_large_scale_performance_under_many_calls():
    # Call function 500 times and check results
    for i in range(500):
        exp_name = f"projects/proj{i}/locations/loc{i}/tensorboards/tb{i}/experiments/exp{i}"
        expected_url = f"https://loc{i}.tensorboard.googleusercontent.com/experiment/projects+proj{i}+locations+loc{i}+tensorboards+tb{i}+experiments+exp{i}"
        codeflash_output = get_experiment_url(exp_name) # 677μs -> 394μs (71.8% faster)

def test_large_scale_ids_with_mixed_characters():
    # IDs with mixed letters, digits, and symbols, 50 cases
    for i in range(50):
        project = f"proj_{i}_!@#"
        location = f"loc_{i}_$%^"
        tensorboard = f"tb_{i}_&*()"
        experiment = f"exp_{i}_?><"
        exp_name = f"projects/{project}/locations/{location}/tensorboards/{tensorboard}/experiments/{experiment}"
        expected_url = f"https://{location}.tensorboard.googleusercontent.com/experiment/projects+{project}+locations+{location}+tensorboards+{tensorboard}+experiments+{experiment}"
        codeflash_output = get_experiment_url(exp_name) # 84.7μs -> 53.6μs (58.0% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-get_experiment_url-mgij20s5 and push.

Codeflash

The optimized code achieves a **72% speedup** by replacing the heavyweight `TensorboardServiceClient.parse_tensorboard_experiment_path()` call with a pre-compiled regex pattern.

**Key optimizations:**

1. **Pre-compiled regex pattern**: The `_TBOARD_EXPERIMENT_RE` pattern is compiled once at module load time, eliminating repeated compilation overhead on every function call.

2. **Direct regex matching**: Instead of calling into the `TensorboardServiceClient` class method (which adds import overhead and method dispatch), the code uses direct `re.match()` calls.

3. **Eliminated function call overhead**: The optimized `get_experiment_url()` performs regex matching inline rather than delegating to `_parse_experiment_name()`, reducing function call stack depth.

**Performance benefits by test case type:**
- **Valid inputs** (70-90% faster): Pre-compiled regex avoids re-compilation and client method overhead
- **Invalid inputs** (45-112% faster): Fast regex failure is much quicker than client parsing logic
- **Large-scale tests** (18-77% faster): Benefits compound with volume due to eliminated per-call compilation

The line profiler shows the original code spent 85.7% of time in `TensorboardServiceClient.parse_tensorboard_experiment_path()`, which is completely eliminated in the optimized version. The new implementation maintains identical behavior and error handling while being significantly more efficient for all input patterns.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 8, 2025 21:55
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 8, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants