test_metrics.py 2.17 KB
Newer Older
Yudi Xue's avatar
Yudi Xue committed
1
2
3
4
5
6
import unittest

import requests

from sglang.srt.utils import kill_child_process
from sglang.test.test_utils import (
Lianmin Zheng's avatar
Lianmin Zheng committed
7
    DEFAULT_SMALL_MODEL_NAME_FOR_TEST,
Yudi Xue's avatar
Yudi Xue committed
8
9
10
11
12
13
14
15
16
17
    DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
    DEFAULT_URL_FOR_TEST,
    popen_launch_server,
)


class TestEnableMetrics(unittest.TestCase):
    def test_metrics_enabled(self):
        """Test that metrics endpoint returns data when enabled"""
        process = popen_launch_server(
Lianmin Zheng's avatar
Lianmin Zheng committed
18
19
            DEFAULT_SMALL_MODEL_NAME_FOR_TEST,
            DEFAULT_URL_FOR_TEST,
Yudi Xue's avatar
Yudi Xue committed
20
            timeout=DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
Lianmin Zheng's avatar
Lianmin Zheng committed
21
            other_args=["--enable-metrics"],
Yudi Xue's avatar
Yudi Xue committed
22
23
24
25
26
27
28
29
30
31
32
33
        )

        try:
            # Make a request to generate some metrics
            response = requests.get(f"{DEFAULT_URL_FOR_TEST}/health_generate")
            self.assertEqual(response.status_code, 200)

            # Get metrics
            metrics_response = requests.get(f"{DEFAULT_URL_FOR_TEST}/metrics")
            self.assertEqual(metrics_response.status_code, 200)
            metrics_content = metrics_response.text

Lianmin Zheng's avatar
Lianmin Zheng committed
34
35
            print(f"{metrics_content=}")

Yudi Xue's avatar
Yudi Xue committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
            # Verify essential metrics are present
            essential_metrics = [
                "sglang:prompt_tokens_total",
                "sglang:generation_tokens_total",
                "sglang:max_total_num_tokens",
                "sglang:context_len",
                "sglang:time_to_first_token_seconds",
                "sglang:time_per_output_token_seconds",
                "sglang:e2e_request_latency_seconds",
            ]

            for metric in essential_metrics:
                self.assertIn(metric, metrics_content, f"Missing metric: {metric}")

            # Verify model name label is present and correct
Lianmin Zheng's avatar
Lianmin Zheng committed
51
            expected_model_name = DEFAULT_SMALL_MODEL_NAME_FOR_TEST
Yudi Xue's avatar
Yudi Xue committed
52
53
54
55
56
57
58
59
60
61
            self.assertIn(f'model_name="{expected_model_name}"', metrics_content)
            # Verify metrics have values (not empty)
            self.assertIn("_sum{", metrics_content)
            self.assertIn("_count{", metrics_content)
            self.assertIn("_bucket{", metrics_content)

        finally:
            kill_child_process(process.pid, include_self=True)


Lianmin Zheng's avatar
Lianmin Zheng committed
62
63
if __name__ == "__main__":
    unittest.main()