Yicong-Huang commented on code in PR #59892:
URL: https://github.com/apache/spark/pull/59892#discussion_r4592132070


##########
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:
   `phase_timers` variable is not being used. maybe we can remove it?



##########
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:
   if this is a rename, let's mention it in 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:
   this code is too invasive, and looks like we have to duplicate it twice, 
each in arrow and pandas. I don't know if there is a better way as performance 
is the top priority. I guess we have to accept it for now



##########
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:
   those are all initialize code. I do think they can be moved to `__init__` 
and also, bind to self? I hope it is not because accessing self is slower...



##########
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:
   also, can we move `_create_phase_timers()` into init? This sounds like an 
init part, not part of runtime. 



##########
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:
   maybe easier to access, just to bind those timers to `self`?



-- 
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