Extract profile request cleanups (#29098)

This commit is contained in:
Lianmin Zheng
2026-06-24 11:22:58 -07:00
committed by GitHub
parent d6aacd2801
commit d5c566e59b
16 changed files with 179 additions and 202 deletions
@@ -1,22 +1,23 @@
import json
import unittest
from sglang.srt.managers.io_struct import ProfileReqInput
from sglang.srt.managers.io_struct import ProfileReq
from sglang.test.ci.ci_register import (
register_amd_ci,
register_cpu_ci,
register_cuda_ci,
)
from sglang.test.test_utils import CustomTestCase
register_cuda_ci(est_time=8, stage="base-b", runner_config="1-gpu-small")
register_amd_ci(est_time=9, suite="stage-b-test-1-gpu-small-amd")
register_cpu_ci(est_time=8, suite="base-c-test-cpu")
class TestProfileMergerHTTPAPI(unittest.TestCase):
def test_profile_req_input_merge_profiles_json_serialization(self):
class TestProfileMergerHTTPAPI(CustomTestCase):
def test_profile_req_merge_profiles_json_serialization(self):
# Test with merge_profiles=True
req_input = ProfileReqInput(
req = ProfileReq(
output_dir="/tmp/test",
num_steps=5,
activities=["CPU", "GPU"],
@@ -26,11 +27,11 @@ class TestProfileMergerHTTPAPI(unittest.TestCase):
# Convert to dict (as would happen in HTTP request)
req_dict = {
"output_dir": req_input.output_dir,
"num_steps": req_input.num_steps,
"activities": req_input.activities,
"profile_by_stage": req_input.profile_by_stage,
"merge_profiles": req_input.merge_profiles,
"output_dir": req.output_dir,
"num_steps": req.num_steps,
"activities": req.activities,
"profile_by_stage": req.profile_by_stage,
"merge_profiles": req.merge_profiles,
}
# Test JSON serialization
@@ -43,7 +44,7 @@ class TestProfileMergerHTTPAPI(unittest.TestCase):
self.assertEqual(parsed_data["activities"], ["CPU", "GPU"])
self.assertTrue(parsed_data["profile_by_stage"])
def test_profile_req_input_merge_profiles_json_deserialization(self):
def test_profile_req_merge_profiles_json_deserialization(self):
# Test JSON data as would come from HTTP request
json_data = {
"output_dir": "/tmp/test",
@@ -53,27 +54,27 @@ class TestProfileMergerHTTPAPI(unittest.TestCase):
"merge_profiles": True,
}
# Create ProfileReqInput from dict (as HTTP server would do)
req_input = ProfileReqInput(**json_data)
# Create ProfileReq from dict (as HTTP server would do)
req = ProfileReq(**json_data)
self.assertTrue(req_input.merge_profiles)
self.assertEqual(req_input.output_dir, "/tmp/test")
self.assertEqual(req_input.num_steps, 10)
self.assertEqual(req_input.activities, ["CPU", "GPU", "MEM"])
self.assertFalse(req_input.profile_by_stage)
self.assertTrue(req.merge_profiles)
self.assertEqual(req.output_dir, "/tmp/test")
self.assertEqual(req.num_steps, 10)
self.assertEqual(req.activities, ["CPU", "GPU", "MEM"])
self.assertFalse(req.profile_by_stage)
def test_profile_req_input_merge_profiles_default_value(self):
def test_profile_req_merge_profiles_default_value(self):
# Test with minimal data
json_data = {"output_dir": "/tmp/test"}
req_input = ProfileReqInput(**json_data)
self.assertFalse(req_input.merge_profiles)
req = ProfileReq(**json_data)
self.assertFalse(req.merge_profiles)
def test_profile_req_input_merge_profiles_explicit_false(self):
def test_profile_req_merge_profiles_explicit_false(self):
json_data = {"output_dir": "/tmp/test", "merge_profiles": False}
req_input = ProfileReqInput(**json_data)
self.assertFalse(req_input.merge_profiles)
req = ProfileReq(**json_data)
self.assertFalse(req.merge_profiles)
def test_http_api_parameter_flow(self):
# Simulate HTTP request data
@@ -85,8 +86,8 @@ class TestProfileMergerHTTPAPI(unittest.TestCase):
"merge_profiles": True,
}
# Create ProfileReqInput as HTTP server would
obj = ProfileReqInput(**request_data)
# Create ProfileReq as HTTP server would
obj = ProfileReq(**request_data)
# Verify the parameter is set correctly
self.assertTrue(obj.merge_profiles)
@@ -98,24 +99,24 @@ class TestProfileMergerHTTPAPI(unittest.TestCase):
def test_http_api_parameter_validation(self):
# Test with True
json_data = {"merge_profiles": True}
req_input = ProfileReqInput(**json_data)
self.assertTrue(req_input.merge_profiles)
req = ProfileReq(**json_data)
self.assertTrue(req.merge_profiles)
# Test with False
json_data = {"merge_profiles": False}
req_input = ProfileReqInput(**json_data)
self.assertFalse(req_input.merge_profiles)
req = ProfileReq(**json_data)
self.assertFalse(req.merge_profiles)
# Test with string "true" (should be converted by JSON parser)
json_data = {"merge_profiles": "true"}
req_input = ProfileReqInput(**json_data)
self.assertEqual(req_input.merge_profiles, "true") # String, not boolean
req = ProfileReq(**json_data)
self.assertEqual(req.merge_profiles, "true") # String, not boolean
def test_http_api_backward_compatibility(self):
# Test minimal request (no merge_profiles)
json_data = {}
req_input = ProfileReqInput(**json_data)
self.assertFalse(req_input.merge_profiles) # Should default to False
req = ProfileReq(**json_data)
self.assertFalse(req.merge_profiles) # Should default to False
# Test with other parameters but no merge_profiles
json_data = {
@@ -123,8 +124,8 @@ class TestProfileMergerHTTPAPI(unittest.TestCase):
"num_steps": 5,
"activities": ["CPU", "GPU"],
}
req_input = ProfileReqInput(**json_data)
self.assertFalse(req_input.merge_profiles) # Should default to False
req = ProfileReq(**json_data)
self.assertFalse(req.merge_profiles) # Should default to False
def test_http_api_parameter_combinations(self):
test_cases = [
@@ -163,8 +164,8 @@ class TestProfileMergerHTTPAPI(unittest.TestCase):
for test_case in test_cases:
with self.subTest(test_case["name"]):
req_input = ProfileReqInput(**test_case["data"])
self.assertEqual(req_input.merge_profiles, test_case["expected_merge"])
req = ProfileReq(**test_case["data"])
self.assertEqual(req.merge_profiles, test_case["expected_merge"])
if __name__ == "__main__":
@@ -227,7 +227,7 @@ class TestTraceReqContextDisabled(unittest.TestCase):
self.assertEqual(state, {"tracing_enable": False})
def test_setstate_disabled(self):
ctx = TraceReqContext.__new__(TraceReqContext)
ctx = TraceReqContext(rid="req-1")
ctx.__setstate__({"tracing_enable": True, "is_copy": False})
# opentelemetry_initialized is False → tracing forced off
self.assertFalse(ctx.tracing_enable)
@@ -539,7 +539,7 @@ class TestTraceReqContextEnabled(unittest.TestCase):
state = ctx.__getstate__()
ctx.trace_req_finish(ts=2000)
ctx2 = TraceReqContext.__new__(TraceReqContext)
ctx2 = TraceReqContext(rid="req-2")
ctx2.__setstate__(state)
self.assertTrue(ctx2.tracing_enable)
self.assertTrue(ctx2.is_copy)
@@ -567,7 +567,7 @@ class TestTraceReqContextEnabled(unittest.TestCase):
ctx.trace_req_finish(ts=3000)
self.assertIsNotNone(state.get("last_span_context"))
ctx2 = TraceReqContext.__new__(TraceReqContext)
ctx2 = TraceReqContext(rid="req-2")
ctx2.__setstate__(state)
self.assertIsNotNone(ctx2.last_span_context)
@@ -13,20 +13,21 @@ import shutil
import tempfile
import unittest
from sglang.srt.managers.io_struct import ProfileReq, ProfileReqInput, ProfileReqType
from sglang.srt.managers.io_struct import ProfileReq, ProfileReqType
from sglang.srt.utils.profile_merger import ProfileMerger
from sglang.test.ci.ci_register import (
register_amd_ci,
register_cpu_ci,
register_cuda_ci,
)
from sglang.test.test_utils import CustomTestCase
register_cuda_ci(est_time=9, stage="base-b", runner_config="1-gpu-small")
register_amd_ci(est_time=8, suite="stage-b-test-1-gpu-small-amd")
register_cpu_ci(est_time=8, suite="base-c-test-cpu")
class TestProfileMerger(unittest.TestCase):
class TestProfileMerger(CustomTestCase):
def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.profile_id = "test_profile_123"
@@ -205,21 +206,15 @@ class TestProfileMerger(unittest.TestCase):
empty_merger.merge_chrome_traces()
class TestProfileMergerIntegration(unittest.TestCase):
class TestProfileMergerIntegration(CustomTestCase):
def test_data_structures_merge_profiles(self):
# Test ProfileReqInput
req_input = ProfileReqInput()
self.assertFalse(req_input.merge_profiles)
req_input = ProfileReqInput(merge_profiles=True)
self.assertTrue(req_input.merge_profiles)
# Test ProfileReq
req = ProfileReq(type=ProfileReqType.START_PROFILE)
req = ProfileReq()
self.assertFalse(req.merge_profiles)
self.assertEqual(req.req_type, ProfileReqType.START_PROFILE)
req = ProfileReq(type=ProfileReqType.START_PROFILE, merge_profiles=True)
req = ProfileReq(merge_profiles=True)
self.assertTrue(req.merge_profiles)
def test_integration_parameters(self):
@@ -231,7 +226,8 @@ class TestProfileMergerIntegration(unittest.TestCase):
)
sig = inspect.signature(TokenizerControlMixin.start_profile)
self.assertIn("merge_profiles", sig.parameters)
self.assertIn("req", sig.parameters)
self.assertNotIn("merge_profiles", sig.parameters)
# Test SchedulerProfilerMixin
from sglang.srt.managers.scheduler_components.profiler_manager import (
@@ -248,7 +244,7 @@ class TestProfileMergerIntegration(unittest.TestCase):
self.assertIn("merge_profiles", sig.parameters)
class TestProfileMergerEdgeCases(unittest.TestCase):
class TestProfileMergerEdgeCases(CustomTestCase):
def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.profile_id = "test_edge_cases"