|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +import re |
| 4 | +import unittest |
| 5 | +from concurrent.futures import ThreadPoolExecutor |
| 6 | + |
| 7 | +from sglang.srt.utils import kill_process_tree |
| 8 | +from sglang.test.test_utils import ( |
| 9 | + DEFAULT_SMALL_MODEL_NAME_FOR_TEST, |
| 10 | + DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH, |
| 11 | + DEFAULT_URL_FOR_TEST, |
| 12 | + STDERR_FILENAME, |
| 13 | + STDOUT_FILENAME, |
| 14 | + CustomTestCase, |
| 15 | + popen_launch_server, |
| 16 | + send_concurrent_generate_requests, |
| 17 | + send_generate_requests, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +class TestMaxQueuedRequests(CustomTestCase): |
| 22 | + @classmethod |
| 23 | + def setUpClass(cls): |
| 24 | + cls.model = DEFAULT_SMALL_MODEL_NAME_FOR_TEST |
| 25 | + cls.base_url = DEFAULT_URL_FOR_TEST |
| 26 | + |
| 27 | + cls.stdout = open(STDOUT_FILENAME, "w") |
| 28 | + cls.stderr = open(STDERR_FILENAME, "w") |
| 29 | + |
| 30 | + cls.base_url = DEFAULT_URL_FOR_TEST |
| 31 | + cls.process = popen_launch_server( |
| 32 | + cls.model, |
| 33 | + cls.base_url, |
| 34 | + timeout=DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH, |
| 35 | + other_args=( |
| 36 | + "--max-running-requests", # Enforce max request concurrency is 1 |
| 37 | + "1", |
| 38 | + "--max-queued-requests", # Enforce max queued request number is 1 |
| 39 | + "1", |
| 40 | + ), |
| 41 | + return_stdout_stderr=(cls.stdout, cls.stderr), |
| 42 | + ) |
| 43 | + |
| 44 | + @classmethod |
| 45 | + def tearDownClass(cls): |
| 46 | + kill_process_tree(cls.process.pid) |
| 47 | + cls.stdout.close() |
| 48 | + cls.stderr.close() |
| 49 | + os.remove(STDOUT_FILENAME) |
| 50 | + os.remove(STDERR_FILENAME) |
| 51 | + |
| 52 | + def test_max_queued_requests_validation_with_serial_requests(self): |
| 53 | + """Verify request is not throttled when the max concurrency is 1.""" |
| 54 | + status_codes = send_generate_requests( |
| 55 | + self.base_url, |
| 56 | + num_requests=10, |
| 57 | + ) |
| 58 | + |
| 59 | + for status_code in status_codes: |
| 60 | + assert status_code == 200 # request shouldn't be throttled |
| 61 | + |
| 62 | + def test_max_queued_requests_validation_with_concurrent_requests(self): |
| 63 | + """Verify request throttling with concurrent requests.""" |
| 64 | + status_codes = asyncio.run( |
| 65 | + send_concurrent_generate_requests(self.base_url, num_requests=10) |
| 66 | + ) |
| 67 | + |
| 68 | + assert 200 in status_codes |
| 69 | + assert 503 in status_codes |
| 70 | + assert all(status_code in [200, 503] for status_code in status_codes) |
| 71 | + |
| 72 | + def test_max_running_requests_and_max_queued_request_validation(self): |
| 73 | + """Verify running request and queued request numbers based on server logs.""" |
| 74 | + rr_pattern = re.compile(r"#running-req:\s*(\d+)") |
| 75 | + qr_pattern = re.compile(r"#queue-req:\s*(\d+)") |
| 76 | + |
| 77 | + with open(STDERR_FILENAME) as lines: |
| 78 | + for line in lines: |
| 79 | + rr_match, qr_match = rr_pattern.search(line), qr_pattern.search(line) |
| 80 | + if rr_match: |
| 81 | + assert int(rr_match.group(1)) <= 1 |
| 82 | + if qr_match: |
| 83 | + assert int(qr_match.group(1)) <= 1 |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + unittest.main() |
0 commit comments