|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +import pytest |
| 5 | +from dummy_stat_logger.dummy_stat_logger import DummyStatLogger |
| 6 | + |
| 7 | +from vllm.config import VllmConfig |
| 8 | +from vllm.engine.arg_utils import AsyncEngineArgs |
| 9 | +from vllm.v1.engine.async_llm import AsyncLLM |
| 10 | +from vllm.v1.metrics.loggers import load_stat_logger_plugin_factories |
| 11 | + |
| 12 | + |
| 13 | +def test_stat_logger_plugin_is_discovered(monkeypatch: pytest.MonkeyPatch): |
| 14 | + with monkeypatch.context() as m: |
| 15 | + m.setenv("VLLM_PLUGINS", "dummy_stat_logger") |
| 16 | + |
| 17 | + factories = load_stat_logger_plugin_factories() |
| 18 | + assert len(factories) == 1, f"Expected 1 factory, got {len(factories)}" |
| 19 | + assert factories[0] is DummyStatLogger, ( |
| 20 | + f"Expected DummyStatLogger class, got {factories[0]}" |
| 21 | + ) |
| 22 | + |
| 23 | + # instantiate and confirm the right type |
| 24 | + vllm_config = VllmConfig() |
| 25 | + instance = factories[0](vllm_config) |
| 26 | + assert isinstance(instance, DummyStatLogger) |
| 27 | + |
| 28 | + |
| 29 | +def test_no_plugins_loaded_if_env_empty(monkeypatch: pytest.MonkeyPatch): |
| 30 | + with monkeypatch.context() as m: |
| 31 | + m.setenv("VLLM_PLUGINS", "") |
| 32 | + |
| 33 | + factories = load_stat_logger_plugin_factories() |
| 34 | + assert factories == [] |
| 35 | + |
| 36 | + |
| 37 | +def test_invalid_stat_logger_plugin_raises(monkeypatch: pytest.MonkeyPatch): |
| 38 | + def fake_plugin_loader(group: str): |
| 39 | + assert group == "vllm.stat_logger_plugins" |
| 40 | + return {"bad": object()} |
| 41 | + |
| 42 | + with monkeypatch.context() as m: |
| 43 | + m.setattr( |
| 44 | + "vllm.v1.metrics.loggers.load_plugins_by_group", |
| 45 | + fake_plugin_loader, |
| 46 | + ) |
| 47 | + with pytest.raises( |
| 48 | + TypeError, |
| 49 | + match="Stat logger plugin 'bad' must be a subclass of StatLoggerBase", |
| 50 | + ): |
| 51 | + load_stat_logger_plugin_factories() |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.asyncio |
| 55 | +async def test_stat_logger_plugin_integration_with_engine( |
| 56 | + monkeypatch: pytest.MonkeyPatch, |
| 57 | +): |
| 58 | + with monkeypatch.context() as m: |
| 59 | + m.setenv("VLLM_PLUGINS", "dummy_stat_logger") |
| 60 | + |
| 61 | + engine_args = AsyncEngineArgs( |
| 62 | + model="facebook/opt-125m", |
| 63 | + enforce_eager=True, # reduce test time |
| 64 | + disable_log_stats=True, # disable default loggers |
| 65 | + ) |
| 66 | + |
| 67 | + engine = AsyncLLM.from_engine_args(engine_args=engine_args) |
| 68 | + |
| 69 | + assert len(engine.logger_manager.stat_loggers) == 2 |
| 70 | + assert len(engine.logger_manager.stat_loggers[0].per_engine_stat_loggers) == 1 |
| 71 | + assert isinstance( |
| 72 | + engine.logger_manager.stat_loggers[0].per_engine_stat_loggers[0], |
| 73 | + DummyStatLogger, |
| 74 | + ) |
| 75 | + |
| 76 | + engine.shutdown() |
0 commit comments