[Perf] Cache vllm.env.__getattr__ result to avoid recomputation (#26146)
Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com>
This commit is contained in:
@@ -6,7 +6,54 @@ from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from vllm.envs import env_list_with_choices, env_with_choices
|
||||
import vllm.envs as envs
|
||||
from vllm.envs import (
|
||||
enable_envs_cache,
|
||||
env_list_with_choices,
|
||||
env_with_choices,
|
||||
environment_variables,
|
||||
)
|
||||
|
||||
|
||||
def test_getattr_without_cache(monkeypatch: pytest.MonkeyPatch):
|
||||
assert envs.VLLM_HOST_IP == ""
|
||||
assert envs.VLLM_PORT is None
|
||||
monkeypatch.setenv("VLLM_HOST_IP", "1.1.1.1")
|
||||
monkeypatch.setenv("VLLM_PORT", "1234")
|
||||
assert envs.VLLM_HOST_IP == "1.1.1.1"
|
||||
assert envs.VLLM_PORT == 1234
|
||||
# __getattr__ is not decorated with functools.cache
|
||||
assert not hasattr(envs.__getattr__, "cache_info")
|
||||
|
||||
|
||||
def test_getattr_with_cache(monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setenv("VLLM_HOST_IP", "1.1.1.1")
|
||||
monkeypatch.setenv("VLLM_PORT", "1234")
|
||||
# __getattr__ is not decorated with functools.cache
|
||||
assert not hasattr(envs.__getattr__, "cache_info")
|
||||
|
||||
# Enable envs cache and ignore ongoing environment changes
|
||||
enable_envs_cache()
|
||||
|
||||
# __getattr__ is not decorated with functools.cache
|
||||
assert hasattr(envs.__getattr__, "cache_info")
|
||||
start_hits = envs.__getattr__.cache_info().hits
|
||||
|
||||
# 2 more hits due to VLLM_HOST_IP and VLLM_PORT accesses
|
||||
assert envs.VLLM_HOST_IP == "1.1.1.1"
|
||||
assert envs.VLLM_PORT == 1234
|
||||
assert envs.__getattr__.cache_info().hits == start_hits + 2
|
||||
|
||||
# All environment variables are cached
|
||||
for environment_variable in environment_variables:
|
||||
envs.__getattr__(environment_variable)
|
||||
assert envs.__getattr__.cache_info().hits == start_hits + 2 + len(
|
||||
environment_variables
|
||||
)
|
||||
|
||||
# Reset envs.__getattr__ back to none-cached version to
|
||||
# avoid affecting other tests
|
||||
envs.__getattr__ = envs.__getattr__.__wrapped__
|
||||
|
||||
|
||||
class TestEnvWithChoices:
|
||||
|
||||
Reference in New Issue
Block a user