Skip to content

Conversation

@codeflash-ai
Copy link
Contributor

@codeflash-ai codeflash-ai bot commented Jan 10, 2025

📄 260% (2.60x) speedup for get_runners_by_hostname in bench_runner/runners.py

⏱️ Runtime : 23.2 microseconds 6.46 microseconds (best of 207 runs)

📝 Explanation and details

Key Changes and Optimizations.

  1. Early Return for Empty Config: Include the runtime error check before processing the config to avoid unnecessary iterations.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 24 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 87.5%
🌀 Generated Regression Tests Details
from __future__ import annotations

import functools
from unittest.mock import MagicMock, patch

# imports
import pytest  # used for our unit tests
from bench_runner import config
from bench_runner.runners import get_runners_by_hostname

# unit tests

# Helper function to mock configuration
def mock_config(runners_config):
    return {"get_bench_runner_config.return_value": {"runners": [runners_config]}}

# Basic Test Cases
def test_single_runner():
    mock_runners_config = {
        "runner1": {
            "os": "linux",
            "arch": "x86_64",
            "hostname": "host1",
            "available": True,
            "env": {"KEY": "VALUE"},
            "github_runner_name": "custom-runner1"
        }
    }
    with patch.dict(config.__dict__, mock_config(mock_runners_config)):
        codeflash_output = get_runners_by_hostname()

def test_multiple_runners():
    mock_runners_config = {
        "runner1": {
            "os": "linux",
            "arch": "x86_64",
            "hostname": "host1",
            "available": True,
            "env": {"KEY": "VALUE"},
            "github_runner_name": "custom-runner1"
        },
        "runner2": {
            "os": "windows",
            "arch": "amd64",
            "hostname": "host2",
            "available": False,
            "env": {"KEY": "VALUE2"},
            "github_runner_name": "custom-runner2"
        }
    }
    with patch.dict(config.__dict__, mock_config(mock_runners_config)):
        codeflash_output = get_runners_by_hostname()

# Edge Test Cases

def test_missing_optional_fields():
    mock_runners_config = {
        "runner1": {
            "os": "linux",
            "arch": "x86_64",
            "hostname": "host1"
        }
    }
    with patch.dict(config.__dict__, mock_config(mock_runners_config)):
        codeflash_output = get_runners_by_hostname()

def test_duplicate_hostnames():
    mock_runners_config = {
        "runner1": {
            "os": "linux",
            "arch": "x86_64",
            "hostname": "host1"
        },
        "runner2": {
            "os": "windows",
            "arch": "amd64",
            "hostname": "host1"
        }
    }
    with patch.dict(config.__dict__, mock_config(mock_runners_config)):
        codeflash_output = get_runners_by_hostname()




def test_large_number_of_runners():
    mock_runners_config = {
        f"runner{i}": {
            "os": "linux",
            "arch": "x86_64",
            "hostname": f"host{i}",
            "available": True,
            "env": {"KEY": "VALUE"},
            "github_runner_name": f"custom-runner{i}"
        } for i in range(1000)
    }
    with patch.dict(config.__dict__, mock_config(mock_runners_config)):
        codeflash_output = get_runners_by_hostname()

# Special Characters and Unicode Test Cases
def test_special_characters_in_fields():
    mock_runners_config = {
        "runner1": {
            "os": "linux",
            "arch": "x86_64",
            "hostname": "host!@#",
            "available": True,
            "env": {"KEY": "VALUE"},
            "github_runner_name": "custom-runner1"
        }
    }
    with patch.dict(config.__dict__, mock_config(mock_runners_config)):
        codeflash_output = get_runners_by_hostname()

def test_unicode_characters():
    mock_runners_config = {
        "runner1": {
            "os": "linux",
            "arch": "x86_64",
            "hostname": "host1",
            "nickname": "测试",
            "available": True,
            "env": {"KEY": "VALUE"},
            "github_runner_name": "custom-runner1"
        }
    }
    with patch.dict(config.__dict__, mock_config(mock_runners_config)):
        codeflash_output = get_runners_by_hostname()

# Boolean Edge Cases
def test_all_runners_unavailable():
    mock_runners_config = {
        "runner1": {
            "os": "linux",
            "arch": "x86_64",
            "hostname": "host1",
            "available": False,
            "env": {"KEY": "VALUE"},
            "github_runner_name": "custom-runner1"
        }
    }
    with patch.dict(config.__dict__, mock_config(mock_runners_config)):
        codeflash_output = get_runners_by_hostname()

def test_mixed_availability():
    mock_runners_config = {
        "runner1": {
            "os": "linux",
            "arch": "x86_64",
            "hostname": "host1",
            "available": True,
            "env": {"KEY": "VALUE"},
            "github_runner_name": "custom-runner1"
        },
        "runner2": {
            "os": "windows",
            "arch": "amd64",
            "hostname": "host2",
            "available": False,
            "env": {"KEY": "VALUE2"},
            "github_runner_name": "custom-runner2"
        }
    }
    with patch.dict(config.__dict__, mock_config(mock_runners_config)):
        codeflash_output = get_runners_by_hostname()

# Environment Variables Test Cases
def test_empty_environment_variables():
    mock_runners_config = {
        "runner1": {
            "os": "linux",
            "arch": "x86_64",
            "hostname": "host1",
            "available": True,
            "env": {},
            "github_runner_name": "custom-runner1"
        }
    }
    with patch.dict(config.__dict__, mock_config(mock_runners_config)):
        codeflash_output = get_runners_by_hostname()

def test_multiple_environment_variables():
    mock_runners_config = {
        "runner1": {
            "os": "linux",
            "arch": "x86_64",
            "hostname": "host1",
            "available": True,
            "env": {"KEY1": "VALUE1", "KEY2": "VALUE2"},
            "github_runner_name": "custom-runner1"
        }
    }
    with patch.dict(config.__dict__, mock_config(mock_runners_config)):
        codeflash_output = get_runners_by_hostname()

# GitHub Runner Name Overrides Test Cases
def test_custom_github_runner_name():
    mock_runners_config = {
        "runner1": {
            "os": "linux",
            "arch": "x86_64",
            "hostname": "host1",
            "available": True,
            "env": {"KEY": "VALUE"},
            "github_runner_name": "custom-runner1"
        }
    }
    with patch.dict(config.__dict__, mock_config(mock_runners_config)):
        codeflash_output = get_runners_by_hostname()

def test_default_github_runner_name():
    mock_runners_config = {
        "runner1": {
            "os": "linux",
            "arch": "x86_64",
            "hostname": "host1",
            "available": True,
            "env": {"KEY": "VALUE"}
        }
    }
    with patch.dict(config.__dict__, mock_config(mock_runners_config)):
        codeflash_output = get_runners_by_hostname()
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

from __future__ import annotations

import functools
from unittest.mock import patch

# imports
import pytest  # used for our unit tests
from bench_runner import config
from bench_runner.runners import get_runners_by_hostname


# unit tests
def test_single_runner_configuration():
    config_data = {
        "runners": [{
            "runner1": {
                "os": "linux",
                "arch": "x86_64",
                "hostname": "host1",
                "available": True,
                "env": {"VAR1": "value1"},
                "github_runner_name": "custom-runner1"
            }
        }]
    }
    with patch.object(config, 'get_bench_runner_config', return_value=config_data):
        codeflash_output = get_runners_by_hostname()

def test_multiple_runners_configuration():
    config_data = {
        "runners": [{
            "runner1": {
                "os": "linux",
                "arch": "x86_64",
                "hostname": "host1",
                "available": True,
                "env": {"VAR1": "value1"},
                "github_runner_name": "custom-runner1"
            },
            "runner2": {
                "os": "windows",
                "arch": "amd64",
                "hostname": "host2",
                "available": False,
                "env": {"VAR2": "value2"},
                "github_runner_name": "custom-runner2"
            }
        }]
    }
    with patch.object(config, 'get_bench_runner_config', return_value=config_data):
        codeflash_output = get_runners_by_hostname()


def test_missing_optional_fields():
    config_data = {
        "runners": [{
            "runner1": {
                "os": "linux",
                "arch": "x86_64",
                "hostname": "host1"
            }
        }]
    }
    with patch.object(config, 'get_bench_runner_config', return_value=config_data):
        codeflash_output = get_runners_by_hostname()

def test_duplicate_hostnames():
    config_data = {
        "runners": [{
            "runner1": {
                "os": "linux",
                "arch": "x86_64",
                "hostname": "host1"
            },
            "runner2": {
                "os": "windows",
                "arch": "amd64",
                "hostname": "host1"
            }
        }]
    }
    with patch.object(config, 'get_bench_runner_config', return_value=config_data):
        codeflash_output = get_runners_by_hostname()



def test_cache_verification():
    config_data_initial = {
        "runners": [{
            "runner1": {
                "os": "linux",
                "arch": "x86_64",
                "hostname": "host1"
            }
        }]
    }
    config_data_modified = {
        "runners": [{
            "runner1": {
                "os": "linux",
                "arch": "x86_64",
                "hostname": "host1"
            },
            "runner2": {
                "os": "windows",
                "arch": "amd64",
                "hostname": "host2"
            }
        }]
    }
    with patch.object(config, 'get_bench_runner_config', return_value=config_data_initial):
        codeflash_output = get_runners_by_hostname()

    with patch.object(config, 'get_bench_runner_config', return_value=config_data_modified):
        codeflash_output = get_runners_by_hostname()

def test_large_number_of_runners():
    config_data = {
        "runners": [{
            f"runner{i}": {
                "os": "linux",
                "arch": "x86_64",
                "hostname": f"host{i}"
            } for i in range(1000)
        }]
    }
    with patch.object(config, 'get_bench_runner_config', return_value=config_data):
        codeflash_output = get_runners_by_hostname()
        for i in range(1000):
            pass

def test_special_characters_in_fields():
    config_data = {
        "runners": [{
            "runner1": {
                "os": "lin@ux",
                "arch": "x86_64",
                "hostname": "host#1",
                "env": {"VAR1": "val$ue1"}
            }
        }]
    }
    with patch.object(config, 'get_bench_runner_config', return_value=config_data):
        codeflash_output = get_runners_by_hostname()

def test_different_formats():
    config_data = {
        "runners": [{
            "runner1": {
                "os": "linux",
                "arch": "x86_64",
                "hostname": "host1",
                "env": {"VAR1": "value with spaces", "VAR2": "value@special"}
            }
        }]
    }
    with patch.object(config, 'get_bench_runner_config', return_value=config_data):
        codeflash_output = get_runners_by_hostname()

def test_minimum_configuration():
    config_data = {
        "runners": [{
            "runner1": {
                "os": "linux",
                "arch": "x86_64",
                "hostname": "host1"
            }
        }]
    }
    with patch.object(config, 'get_bench_runner_config', return_value=config_data):
        codeflash_output = get_runners_by_hostname()

def test_maximum_configuration():
    config_data = {
        "runners": [{
            "runner1": {
                "os": "linux",
                "arch": "x86_64",
                "hostname": "host1",
                "available": True,
                "env": {"VAR1": "value1", "VAR2": "value2"},
                "github_runner_name": "custom-runner1"
            },
            "runner2": {
                "os": "windows",
                "arch": "amd64",
                "hostname": "host2",
                "available": False,
                "env": {"VAR3": "value3", "VAR4": "value4"},
                "github_runner_name": "custom-runner2"
            }
        }]
    }
    with patch.object(config, 'get_bench_runner_config', return_value=config_data):
        codeflash_output = get_runners_by_hostname()
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

from bench_runner.runners import get_runners_by_hostname
import pytest

def test_get_runners_by_hostname():
    with pytest.raises(FileNotFoundError, match="\\[Errno\\ 2\\]\\ No\\ such\\ file\\ or\\ directory:\\ 'bench_runner\\.toml'"):
        get_runners_by_hostname()

📢 Feedback on this optimization? Discord

Key Changes and Optimizations.

3. **Early Return for Empty Config:** Include the runtime error check before processing the config to avoid unnecessary iterations.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Jan 10, 2025
@codeflash-ai codeflash-ai bot requested a review from mdboom January 10, 2025 22:02
@mdboom
Copy link
Contributor

mdboom commented Mar 12, 2025

Obsolete.

@mdboom mdboom closed this Mar 12, 2025
@codeflash-ai codeflash-ai bot deleted the codeflash/optimize-get_runners_by_hostname-m5raz9fs branch March 12, 2025 21:38
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.

1 participant