Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

chore: improved timing logging

reformedote9656f7e0e59
diff --git a/browser_use/browser/watchdogs/dom_watchdog.py b/browser_use/browser/watchdogs/dom_watchdog.py
index 79479e2c4..40612112e 100644
--- a/browser_use/browser/watchdogs/dom_watchdog.py
+++ b/browser_use/browser/watchdogs/dom_watchdog.py
@@ -561,43 +561,42 @@ class DOMWatchdog(BaseWatchdog):
'πŸ” DOMWatchdog._build_dom_tree_without_highlights: βœ… DomService.get_serialized_dom_tree completed'
)
- # Format hierarchical timing breakdown
- self.logger.debug(f'⏱️ Total DOM tree time: {total_time_ms:.2f}ms')
- self.logger.debug('πŸ“Š Timing breakdown:')
+ # Build hierarchical timing breakdown as single multi-line string
+ timing_lines = [f'⏱️ Total DOM tree time: {total_time_ms:.2f}ms', 'πŸ“Š Timing breakdown:']
# get_all_trees breakdown
get_all_trees_ms = timing_info.get('get_all_trees_total_ms', 0)
if get_all_trees_ms > 0:
- self.logger.debug(f' β”œβ”€ get_all_trees: {get_all_trees_ms:.2f}ms')
+ timing_lines.append(f' β”œβ”€ get_all_trees: {get_all_trees_ms:.2f}ms')
iframe_scroll_ms = timing_info.get('iframe_scroll_detection_ms', 0)
cdp_parallel_ms = timing_info.get('cdp_parallel_calls_ms', 0)
snapshot_proc_ms = timing_info.get('snapshot_processing_ms', 0)
if iframe_scroll_ms > 0.01:
- self.logger.debug(f' β”‚ β”œβ”€ iframe_scroll_detection: {iframe_scroll_ms:.2f}ms')
+ timing_lines.append(f' β”‚ β”œβ”€ iframe_scroll_detection: {iframe_scroll_ms:.2f}ms')
if cdp_parallel_ms > 0.01:
- self.logger.debug(f' β”‚ β”œβ”€ cdp_parallel_calls: {cdp_parallel_ms:.2f}ms')
+ timing_lines.append(f' β”‚ β”œβ”€ cdp_parallel_calls: {cdp_parallel_ms:.2f}ms')
if snapshot_proc_ms > 0.01:
- self.logger.debug(f' β”‚ └─ snapshot_processing: {snapshot_proc_ms:.2f}ms')
+ timing_lines.append(f' β”‚ └─ snapshot_processing: {snapshot_proc_ms:.2f}ms')
# build_ax_lookup
build_ax_ms = timing_info.get('build_ax_lookup_ms', 0)
if build_ax_ms > 0.01:
- self.logger.debug(f' β”œβ”€ build_ax_lookup: {build_ax_ms:.2f}ms')
+ timing_lines.append(f' β”œβ”€ build_ax_lookup: {build_ax_ms:.2f}ms')
# build_snapshot_lookup
build_snapshot_ms = timing_info.get('build_snapshot_lookup_ms', 0)
if build_snapshot_ms > 0.01:
- self.logger.debug(f' β”œβ”€ build_snapshot_lookup: {build_snapshot_ms:.2f}ms')
+ timing_lines.append(f' β”œβ”€ build_snapshot_lookup: {build_snapshot_ms:.2f}ms')
# construct_enhanced_tree
construct_tree_ms = timing_info.get('construct_enhanced_tree_ms', 0)
if construct_tree_ms > 0.01:
- self.logger.debug(f' β”œβ”€ construct_enhanced_tree: {construct_tree_ms:.2f}ms')
+ timing_lines.append(f' β”œβ”€ construct_enhanced_tree: {construct_tree_ms:.2f}ms')
# serialize_accessible_elements breakdown
serialize_total_ms = timing_info.get('serialize_accessible_elements_total_ms', 0)
if serialize_total_ms > 0.01:
- self.logger.debug(f' β”œβ”€ serialize_accessible_elements: {serialize_total_ms:.2f}ms')
+ timing_lines.append(f' β”œβ”€ serialize_accessible_elements: {serialize_total_ms:.2f}ms')
create_simp_ms = timing_info.get('create_simplified_tree_ms', 0)
paint_order_ms = timing_info.get('calculate_paint_order_ms', 0)
optimize_ms = timing_info.get('optimize_tree_ms', 0)
@@ -606,17 +605,17 @@ class DOMWatchdog(BaseWatchdog):
clickable_ms = timing_info.get('clickable_detection_time_ms', 0)
if create_simp_ms > 0.01:
- self.logger.debug(f' β”‚ β”œβ”€ create_simplified_tree: {create_simp_ms:.2f}ms')
+ timing_lines.append(f' β”‚ β”œβ”€ create_simplified_tree: {create_simp_ms:.2f}ms')
if clickable_ms > 0.01:
- self.logger.debug(f' β”‚ β”‚ └─ clickable_detection: {clickable_ms:.2f}ms')
+ timing_lines.append(f' β”‚ β”‚ └─ clickable_detection: {clickable_ms:.2f}ms')
if paint_order_ms > 0.01:
- self.logger.debug(f' β”‚ β”œβ”€ calculate_paint_order: {paint_order_ms:.2f}ms')
+ timing_lines.append(f' β”‚ β”œβ”€ calculate_paint_order: {paint_order_ms:.2f}ms')
if optimize_ms > 0.01:
- self.logger.debug(f' β”‚ β”œβ”€ optimize_tree: {optimize_ms:.2f}ms')
+ timing_lines.append(f' β”‚ β”œβ”€ optimize_tree: {optimize_ms:.2f}ms')
if bbox_ms > 0.01:
- self.logger.debug(f' β”‚ β”œβ”€ bbox_filtering: {bbox_ms:.2f}ms')
+ timing_lines.append(f' β”‚ β”œβ”€ bbox_filtering: {bbox_ms:.2f}ms')
if assign_idx_ms > 0.01:
- self.logger.debug(f' β”‚ └─ assign_interactive_indices: {assign_idx_ms:.2f}ms')
+ timing_lines.append(f' β”‚ └─ assign_interactive_indices: {assign_idx_ms:.2f}ms')
# Overheads
get_dom_overhead_ms = timing_info.get('get_dom_tree_overhead_ms', 0)
@@ -624,11 +623,11 @@ class DOMWatchdog(BaseWatchdog):
get_serialized_overhead_ms = timing_info.get('get_serialized_dom_tree_overhead_ms', 0)
if get_dom_overhead_ms > 0.1:
- self.logger.debug(f' β”œβ”€ get_dom_tree_overhead: {get_dom_overhead_ms:.2f}ms')
+ timing_lines.append(f' β”œβ”€ get_dom_tree_overhead: {get_dom_overhead_ms:.2f}ms')
if serialize_overhead_ms > 0.1:
- self.logger.debug(f' β”œβ”€ serialization_overhead: {serialize_overhead_ms:.2f}ms')
+ timing_lines.append(f' β”œβ”€ serialization_overhead: {serialize_overhead_ms:.2f}ms')
if get_serialized_overhead_ms > 0.1:
- self.logger.debug(f' └─ get_serialized_dom_tree_overhead: {get_serialized_overhead_ms:.2f}ms')
+ timing_lines.append(f' └─ get_serialized_dom_tree_overhead: {get_serialized_overhead_ms:.2f}ms')
# Calculate total tracked time for validation
main_operations_ms = (
@@ -644,7 +643,10 @@ class DOMWatchdog(BaseWatchdog):
untracked_time_ms = total_time_ms - main_operations_ms
if untracked_time_ms > 1.0: # Only log if significant
- self.logger.debug(f' ⚠️ untracked_time: {untracked_time_ms:.2f}ms')
+ timing_lines.append(f' ⚠️ untracked_time: {untracked_time_ms:.2f}ms')
+
+ # Single log call with all timing info
+ self.logger.debug('\n'.join(timing_lines))
# Update selector map for other watchdogs
self.logger.debug('πŸ” DOMWatchdog._build_dom_tree_without_highlights: Updating selector maps...')