2222# THE SOFTWARE.
2323
2424import sys
25+
2526import pytest
2627
28+
2729def extract_json_kernel_records (json_root ):
2830 """Extract kernel dispatch records from JSON output."""
2931 assert "rocprofiler-sdk-tool" in json_root , "missing rocprofiler-sdk-tool in JSON"
@@ -35,14 +37,19 @@ def extract_json_kernel_records(json_root):
3537 buffer_records = tool ["buffer_records" ]
3638
3739 for key in ("kernel_dispatch" , "kernel_trace" , "kernel_dispatch_trace" ):
38- if key in buffer_records and isinstance (buffer_records [key ], list ) and len (buffer_records [key ]) > 0 :
40+ if (
41+ key in buffer_records
42+ and isinstance (buffer_records [key ], list )
43+ and len (buffer_records [key ]) > 0
44+ ):
3945 return buffer_records [key ]
4046
4147 assert False , (
4248 "no kernel dispatch records found in JSON buffer_records keys="
4349 f"{ list (buffer_records .keys ())} "
4450 )
4551
52+
4653def _as_int (value , * , field = "value" ):
4754 assert value is not None , f"missing { field } "
4855 try :
@@ -52,12 +59,14 @@ def _as_int(value, *, field="value"):
5259 f"failed to parse int for { field } : { value !r} ({ exc } )"
5360 ) from exc
5461
62+
5563def _get_first_present (mapping , * keys ):
5664 for key in keys :
5765 if key in mapping and mapping [key ] is not None :
5866 return mapping [key ]
5967 return None
6068
69+
6170def _extract_dispatch_id_from_json_record (record ):
6271 """
6372 Prefer dispatch_info.dispatch_id.
@@ -79,6 +88,7 @@ def _extract_dispatch_id_from_json_record(record):
7988
8089 return _as_int (dispatch_id , field = "dispatch_id/correlation_id" )
8190
91+
8292def build_json_duration_map (records ):
8393 """
8494 Build map:
@@ -100,6 +110,7 @@ def build_json_duration_map(records):
100110 assert len (result ) > 0 , "no kernel records extracted from JSON"
101111 return result
102112
113+
103114def load_kernel_rows_via_rocpd (db_path ):
104115 """
105116 Use rocpd Python API and reuse rocpd.csv kernel query logic directly.
@@ -127,22 +138,33 @@ def load_kernel_rows_via_rocpd(db_path):
127138 assert len (rows ) > 0 , f"no rows returned from kernel query in db: { db_path } "
128139 return rows
129140
141+
130142def _extract_dispatch_id_from_db_row (row ):
131- value = _get_first_present (row , "Dispatch_Id" , "dispatch_id" , "Correlation_Id" , "correlation_id" )
143+ value = _get_first_present (
144+ row ,
145+ "Dispatch_Id" ,
146+ "dispatch_id" ,
147+ "Correlation_Id" ,
148+ "correlation_id" ,
149+ )
132150 return _as_int (value , field = "Dispatch_Id/dispatch_id/Correlation_Id/correlation_id" )
133151
152+
134153def _extract_start_from_db_row (row ):
135154 value = _get_first_present (row , "Start_Timestamp" , "start_timestamp" )
136155 return _as_int (value , field = "Start_Timestamp/start_timestamp" )
137156
157+
138158def _extract_end_from_db_row (row ):
139159 value = _get_first_present (row , "End_Timestamp" , "end_timestamp" )
140160 return _as_int (value , field = "End_Timestamp/end_timestamp" )
141161
162+
142163def _extract_duration_from_db_row (row ):
143164 value = _get_first_present (row , "Duration" , "duration" )
144165 return _as_int (value , field = "Duration/duration" )
145166
167+
146168def test_rocpd_kernel_trace_duration (json_data , db_path ):
147169 """
148170 Validate that kernel trace Duration exists on the rocpd CSV path and matches JSON.
@@ -174,15 +196,15 @@ def test_rocpd_kernel_trace_duration(json_data, db_path):
174196 end = _extract_end_from_db_row (row )
175197 duration = _extract_duration_from_db_row (row )
176198
177- assert start > 0 and end > 0 , (
178- f"invalid DB timestamps: start= { start } end= { end } dispatch_id= { dispatch_id } "
179- )
180- assert end >= start , (
181- f"DB end before start: start= { start } end= { end } dispatch_id= { dispatch_id } "
182- )
183- assert duration >= 0 , (
184- f"negative DB duration: duration= { duration } dispatch_id= { dispatch_id } "
185- )
199+ assert (
200+ start > 0 and end > 0
201+ ), f"invalid DB timestamps: start= { start } end= { end } dispatch_id= { dispatch_id } "
202+ assert (
203+ end >= start
204+ ), f"DB end before start: start= { start } end= { end } dispatch_id= { dispatch_id } "
205+ assert (
206+ duration >= 0
207+ ), f"negative DB duration: duration= { duration } dispatch_id= { dispatch_id } "
186208 assert duration == (end - start ), (
187209 f"DB duration mismatch: duration={ duration } != end-start={ end - start } "
188210 f"dispatch_id={ dispatch_id } "
@@ -258,9 +280,10 @@ def test_rocpd_kernel_trace_duration(json_data, db_path):
258280 raise AssertionError ("\n " .join (lines ) + detail )
259281
260282 assert matched_count > 0 , "No DB rows matched JSON records"
261- assert matched_count == total_count , (
262- f"Only { matched_count } /{ total_count } DB rows matched JSON"
263- )
283+ assert (
284+ matched_count == total_count
285+ ), f"Only { matched_count } /{ total_count } DB rows matched JSON"
286+
264287
265288if __name__ == "__main__" :
266289 rc = pytest .main (["-x" , __file__ ] + sys .argv [1 :])
0 commit comments