Skip to content

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

refactor: enhance timing breakdown in DOMWatchdog and DomService for improved performance insights

reformedot6859920d685f
diff --git a/browser_use/browser/watchdogs/dom_watchdog.py b/browser_use/browser/watchdogs/dom_watchdog.py
index e478ac7bb..79479e2c4 100644
--- a/browser_use/browser/watchdogs/dom_watchdog.py
+++ b/browser_use/browser/watchdogs/dom_watchdog.py
@@ -561,33 +561,90 @@ class DOMWatchdog(BaseWatchdog):
'πŸ” DOMWatchdog._build_dom_tree_without_highlights: βœ… DomService.get_serialized_dom_tree completed'
)
- # Calculate sum of all tracked timings (avoiding double-counting)
- # Exclude sub-timings when we have totals to avoid double-counting
- exclude_keys = {
- # Serializer sub-timings (already in serialize_accessible_elements_total_ms)
- 'create_simplified_tree_ms',
- 'calculate_paint_order_ms',
- 'optimize_tree_ms',
- 'bbox_filtering_ms',
- 'clickable_detection_time_ms', # Already in create_simplified_tree_ms
- 'assign_interactive_indices_ms',
- # Aggregate/overhead timings (derived from other timings)
- 'cdp_calls_total_ms', # Already in get_all_trees_total_ms
- 'get_dom_tree_overhead_ms', # Calculated overhead
- 'serialization_overhead_ms', # Calculated overhead
- 'get_serialized_dom_tree_overhead_ms', # Calculated overhead
- 'get_serialized_dom_tree_total_ms', # Top-level total that includes everything
- }
- tracked_time_ms = sum(v for k, v in timing_info.items() if k not in exclude_keys)
- untracked_time_ms = total_time_ms - tracked_time_ms
-
- # Format timing values to 2 decimal places
- timing_formatted = {k: round(v, 2) for k, v in timing_info.items()}
-
+ # Format hierarchical timing breakdown
self.logger.debug(f'⏱️ Total DOM tree time: {total_time_ms:.2f}ms')
- self.logger.debug(f'πŸ“Š Timing breakdown (all in ms): {timing_formatted}')
+ self.logger.debug('πŸ“Š 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')
+ 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')
+ if cdp_parallel_ms > 0.01:
+ self.logger.debug(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')
+
+ # 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')
+
+ # 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')
+
+ # 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')
+
+ # 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')
+ 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)
+ bbox_ms = timing_info.get('bbox_filtering_ms', 0)
+ assign_idx_ms = timing_info.get('assign_interactive_indices_ms', 0)
+ 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')
+ if clickable_ms > 0.01:
+ self.logger.debug(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')
+ if optimize_ms > 0.01:
+ self.logger.debug(f' β”‚ β”œβ”€ optimize_tree: {optimize_ms:.2f}ms')
+ if bbox_ms > 0.01:
+ self.logger.debug(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')
+
+ # Overheads
+ get_dom_overhead_ms = timing_info.get('get_dom_tree_overhead_ms', 0)
+ serialize_overhead_ms = timing_info.get('serialization_overhead_ms', 0)
+ 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')
+ if serialize_overhead_ms > 0.1:
+ self.logger.debug(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')
+
+ # Calculate total tracked time for validation
+ main_operations_ms = (
+ get_all_trees_ms
+ + build_ax_ms
+ + build_snapshot_ms
+ + construct_tree_ms
+ + serialize_total_ms
+ + get_dom_overhead_ms
+ + serialize_overhead_ms
+ + get_serialized_overhead_ms
+ )
+ 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')
+ self.logger.debug(f' ⚠️ untracked_time: {untracked_time_ms:.2f}ms')
# Update selector map for other watchdogs
self.logger.debug('πŸ” DOMWatchdog._build_dom_tree_without_highlights: Updating selector maps...')
diff --git a/browser_use/dom/service.py b/browser_use/dom/service.py
index 8bdd1812e..bf71a3907 100644
--- a/browser_use/dom/service.py
+++ b/browser_use/dom/service.py
@@ -259,6 +259,7 @@ class DomService:
self.logger.debug(f'πŸ” DEBUG: Capturing DOM snapshot for target {target_id}')
# Get actual scroll positions for all iframes before capturing snapshot
+ start_iframe_scroll = time.time()
iframe_scroll_positions = {}
try:
scroll_result = await cdp_session.cdp_client.send.Runtime.evaluate(
@@ -295,6 +296,7 @@ class DomService:
)
except Exception as e:
self.logger.debug(f'Failed to get iframe scroll positions: {e}')
+ iframe_scroll_ms = (time.time() - start_iframe_scroll) * 1000
# Define CDP request factories to avoid duplication
def create_snapshot_request():
@@ -314,7 +316,7 @@ class DomService:
params={'depth': -1, 'pierce': True}, session_id=cdp_session.session_id
)
- start = time.time()
+ start_cdp_calls = time.time()
# Create initial tasks
tasks = {
@@ -378,8 +380,11 @@ class DomService:
dom_tree = results['dom_tree']
ax_tree = results['ax_tree']
device_pixel_ratio = results['device_pixel_ratio']
- end = time.time()
- cdp_timing_ms = (end - start) * 1000
+ end_cdp_calls = time.time()
+ cdp_calls_ms = (end_cdp_calls - start_cdp_calls) * 1000
+
+ # Calculate total time for _get_all_trees and overhead
+ start_snapshot_processing = time.time()
# DEBUG: Log snapshot info and limit documents to prevent explosion
if snapshot and 'documents' in snapshot:
@@ -400,12 +405,19 @@ class DomService:
f'πŸ” DEBUG: Iframe #{doc_idx} {doc.get("frameId", "no-frame-id")} {doc.get("url", "no-url")} has {len(doc.get("nodes", []))} nodes'
)
+ snapshot_processing_ms = (time.time() - start_snapshot_processing) * 1000
+
+ # Return with detailed timing breakdown
return TargetAllTrees(
snapshot=snapshot,
dom_tree=dom_tree,
ax_tree=ax_tree,
device_pixel_ratio=device_pixel_ratio,
- cdp_timing={'cdp_calls_total_ms': cdp_timing_ms},
+ cdp_timing={
+ 'iframe_scroll_detection_ms': iframe_scroll_ms,
+ 'cdp_parallel_calls_ms': cdp_calls_ms,
+ 'snapshot_processing_ms': snapshot_processing_ms,
+ },
)
@observe_debug(ignore_input=True, ignore_output=True, name='get_dom_tree')