Spenserrrr commented on code in PR #59892:
URL: https://github.com/apache/spark/pull/59892#discussion_r4597138147


##########
python/pyspark/eval_handlers/_arrow.py:
##########
@@ -867,29 +866,12 @@ def __init__(self, udf_descs: list[UDF], runner_conf: 
RunnerConf, eval_conf: Eva
             for f in eval_conf.input_type  # type: ignore[union-attr]
         ]
 
-    @property
-    def serializer(self) -> ArrowStreamSerializer:
-        return ArrowStreamSerializer(
-            write_start_stream=True,
-            flush_per_batch=os.environ.get("SPARK_PIPELINED_UDF") == "1",
-            collect_timing=self._runner_conf.detailed_timing_enabled,
-        )
-
     def run(self, split_index: int, data: Iterator[pa.RecordBatch]) -> 
Iterator[pa.RecordBatch]:
         import pyarrow as pa
 
         runner_conf = self._runner_conf
         metrics = WorkerMetrics()
-        if runner_conf.detailed_timing_enabled:
-            phase_timers = (
-                metrics.measure("pythonInputPreparationTime"),
-                metrics.measure("pythonUDFExecutionTime"),
-                metrics.measure("pythonOutputPreparationTime"),
-            )
-            metrics.set("pythonNumTimingReports", 1)
-            metrics.set("pythonNumTimedBatches", 0)
-        else:
-            phase_timers = None
+        phase_timers = self._create_phase_timers()

Review Comment:
   Moved phase-timer creation to the base handler's `__init__`. The local 
`phase_timers` tuple is gone; the handlers now use the existing 
`_detailed_timing_enabled` gate and named timer attributes.



##########
python/pyspark/eval_handlers/_base.py:
##########
@@ -320,6 +328,27 @@ def profiling_func(*args: Any, **kwargs: Any) -> Any:
     def serializer(self) -> Serializer:
         """Serializer for both the input and output streams."""
 
+    def _create_phase_timers(
+        self,
+    ) -> Optional[tuple["_WorkerTimer", "_WorkerTimer", "_WorkerTimer"]]:
+        """Create input-preparation, UDF-execution and output-preparation 
timers.
+
+        Serializers measure reads and writes. Exclude nested input iteration 
from UDF
+        time; count consumed transport batches, not groups or output batches. 
Framework
+        work outside the measured intervals remains unattributed.
+        """
+        if not self._detailed_timing_enabled:
+            return None
+        metrics = WorkerMetrics()
+        timers = (
+            metrics.measure("pythonInputPreparationTime"),
+            metrics.measure("pythonUDFExecutionTime"),
+            metrics.measure("pythonOutputPreparationTime"),
+        )

Review Comment:
   Bound the three timers to `self._input_preparation_timer`, 
`self._udf_execution_timer`, and `self._output_preparation_timer`, and use them 
directly when publishing the totals.



##########
python/pyspark/sql/tests/arrow/test_arrow_python_udf.py:
##########
@@ -727,7 +753,7 @@ def produce():
                 serializer = ArrowStreamSerializer(
                     write_start_stream=write_start_stream,
                     flush_per_batch=flush_per_batch,
-                    collect_timing=True,
+                    detailed_timing_enabled=True,

Review Comment:
   Added the `collect_timing` -> `detailed_timing_enabled` rename to the PR 
description.



##########
python/pyspark/eval_handlers/_pandas.py:
##########
@@ -243,49 +246,106 @@ def __init__(
         )
 
     def run(self, split_index: int, data: "Iterator[pa.RecordBatch]") -> 
"Iterator[pa.RecordBatch]":

Review Comment:
   I kept the timing boundaries inline here since mapInPandas has to subtract 
input iteration nested inside UDF execution. The timer setup now lives in the 
shared base.



##########
python/pyspark/eval_handlers/_pandas.py:
##########
@@ -243,49 +246,106 @@ def __init__(
         )
 
     def run(self, split_index: int, data: "Iterator[pa.RecordBatch]") -> 
"Iterator[pa.RecordBatch]":
+        metrics = WorkerMetrics()
+        phase_timers = self._create_phase_timers()
+        clock = time.perf_counter_ns
+        input_preparation_duration_ns = 0

Review Comment:
   Moved the collector reference and timer handles into `__init__`. I kept the 
duration totals and batch count local to `run`: they're updated throughout the 
loop and published once at the end. This avoids repeated attribute updates in 
the hot loop; the timer attributes themselves are only read at publication.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to