gaogaotiantian commented on code in PR #60150:
URL: https://github.com/apache/spark/pull/60150#discussion_r4607253285


##########
python/pyspark/profiler.py:
##########
@@ -170,15 +170,18 @@ def zero(value: Dict[str, Any]) -> Dict[str, Any]:
 
     @staticmethod
     def addInPlace(value1: Dict[str, Any], value2: Dict[str, Any]) -> 
Dict[str, Any]:

Review Comment:
   Thank you for the detailed suggestion. I compared the two forms before 
deciding, with stacks sampled from real Python code. The flat form needs one 
shared string per frame to be competitive; with that, for typical code it was 
smaller than the tree (81 KB vs 245 KB per task on Python 3.14). For deep 
recursion, though, it repeats every shared prefix: merged over 10 tasks, it 
took about 2.4 MB vs 268 KB for the tree, about 9x.
   
   Since typical stacks are shallow (a UDF body starts about 16 frames deep, 
and the library calls I measured add 4 to 54 frames), I went with the depth cap 
from item 7 instead. It bounds the pickle, the merge, the copy and any later 
renderer, and keeps the tree, which stays compact for shared prefixes. A 
collapsed-stack view for flame graphs can still be built from the tree when the 
results are shown, so I would like to keep the nested tree.
   



##########
python/pyspark/worker_util.py:
##########
@@ -448,6 +448,10 @@ def udf_profiler(self) -> Optional[str]:
     def udf_profiler_sampling_interval(self) -> int:
         return int(self.get("spark.sql.pyspark.udf.profiler.samplingInterval", 
100))
 
+    @property
+    def udf_profiler_sampling_aggregation(self) -> str:
+        return self.get("spark.sql.pyspark.udf.profiler.samplingAggregation", 
"stage")

Review Comment:
   Thanks. I would like to keep it as is, the same way as `samplingInterval`, 
whose default is also spelled in `SQLConf`, `RunnerConf` and the 
`WorkerSamplingProfiler.__init__` default. Both runners always send the conf, 
so the Python defaults are not used in practice, and since `_get_task_key()` 
lists the levels explicitly, a level added only on the Scala side fails with 
`VALUE_NOT_ALLOWED` instead of being silently treated as another level.
   



##########
python/pyspark/sql/tests/test_udf_profiler.py:
##########
@@ -826,6 +827,64 @@ def collect_idents(node, idents):
         self.assertEqual(0, len(self.sampling_profile_results))
         self.assertEqual(0, 
len(self.spark._profiler_collector._profile_results))
 
+    def test_sampling_profiler_aggregation(self):
+        for use_arrow in [True, False]:
+            add1 = udf(lambda x: x + 1, "long", useArrow=use_arrow)
+            # One query with two stages, split by the shuffle, of two tasks 
each.
+            df = (
+                self.spark.range(10, numPartitions=2)
+                .select(add1("id").alias("id"))
+                .repartition(2, "id")
+                .select(add1("id"))
+            )
+            for level, num_results, key_pattern in [
+                ("task", 4, r"^sampling_task_\d+_[01]$"),
+                ("stage", 2, r"^sampling_stage_\d+$"),
+                ("query", 1, r"^sampling_query_\d+$"),
+                (None, 2, r"^sampling_stage_\d+$"),  # The default level is 
"stage".
+            ]:
+                conf = {
+                    "spark.sql.pyspark.udf.profiler": "sampling",
+                    # Disable AQE so that the shuffle is not coalesced into 
one partition.
+                    "spark.sql.adaptive.enabled": "false",
+                }
+                if level is not None:
+                    conf["spark.sql.pyspark.udf.profiler.samplingAggregation"] 
= level
+                with self.subTest(use_arrow=use_arrow, level=level), 
self.sql_conf(conf):
+                    # Clear first, so that a failed subtest does not affect 
the next ones.
+                    self.spark.profile.clear()
+                    df.collect()
+
+                    keys = list(self.sampling_profile_results)
+                    self.assertEqual(num_results, len(keys), keys)
+                    for key in keys:
+                        self.assertRegex(key, key_pattern)
+
+    def test_sampling_profiler_merge_threads_by_name(self):
+        import threading

Review Comment:
   Removed in 8fb4b629ec3, thanks.
   



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