java-topology/defects/playwright/bench/bench-playwright-0001.py
russell@unturf.com 9a0253e724 browser-automation: 4 CWE-407 patches (selenium x2, playwright, webdriverio)
selenium-0001: SessionCapabilitiesMutator list.contains O(NxM) -> LinkedHashSet
  O(N+M). Grid Node session mutation hot path. Bench: 192x at N=M=1000.

selenium-0002: ChromiumOptions merge helpers consolidate four list.contains
  loops behind addArgumentsUnique/addEncodedExtensionsUnique. Bench: 254x
  at N=M=1000.

playwright-0001: roleUtils validRoles / allowsNameFromContent Array.includes
  on 20-70 element constant arrays per element. Converted to Set<string>
  at module load. Bench: 11x at N=10000 elements.

webdriverio-0001: xpath-conditions extractOrConditions orMatches.find +
  values.includes per regex match -> Map<attr, Set<values>>. Bench: 6x at
  K=V=60 in the 'mobileSelectorPerformanceOptimizer'.

Each defect ships: ticket, patch with complexity-gate header, Python
benchmark + correctness test, Makefile, outreach brief. All 16 tests
pass. UNDF IDs: 1276 (playwright), 1277 (selenium-0001), 1288
(selenium-0002), 1289 (webdriverio).
2026-04-22 18:14:39 -04:00

90 lines
3.2 KiB
Python

#!/usr/bin/env python3
# bench-playwright-0001.py
# roleUtils validRoles / allowsNameFromContent: per-element role validation
# during ARIA snapshot walks. Defect: Array.includes(role) on 70-entry const
# array = O(k) per element. N elements x k roles = O(N*k) per snapshot.
# Fix: Set.has(role) is O(1) per element -> O(N+k).
import sys
import time
# 70+ ARIA roles mirroring validRoles in roleUtils.ts
VALID_ROLES = [
"alert", "alertdialog", "application", "article", "banner", "blockquote",
"button", "caption", "cell", "checkbox", "code", "columnheader", "combobox",
"complementary", "contentinfo", "definition", "deletion", "dialog",
"directory", "document", "emphasis", "feed", "figure", "form", "generic",
"grid", "gridcell", "group", "heading", "img", "insertion", "link", "list",
"listbox", "listitem", "log", "main", "mark", "marquee", "math", "meter",
"menu", "menubar", "menuitem", "menuitemcheckbox", "menuitemradio",
"navigation", "none", "note", "option", "paragraph", "presentation",
"progressbar", "radio", "radiogroup", "region", "row", "rowgroup",
"rowheader", "scrollbar", "search", "searchbox", "separator", "slider",
"spinbutton", "status", "strong", "subscript", "superscript", "switch",
"tab", "table", "tablist", "tabpanel", "term", "textbox", "time", "timer",
"toolbar", "tooltip", "tree", "treegrid", "treeitem",
]
# Roles most commonly present on real pages (bias toward frequent matches)
ELEMENT_ROLE_POOL = ["button", "link", "heading", "textbox", "row", "cell",
"img", "list", "listitem", "", "main", "navigation"]
def bench_defective(n):
"""Array.includes on validRoles per element -> O(N*k)."""
# Simulate N page elements with attribute-derived roles
roles_to_check = [ELEMENT_ROLE_POOL[i % len(ELEMENT_ROLE_POOL)] for i in range(n)]
t0 = time.perf_counter()
found = 0
for role in roles_to_check:
# Array.includes scan
if role in VALID_ROLES: # python list __contains__ is O(k) linear
found += 1
dt = time.perf_counter() - t0
assert found >= 0
return dt
def bench_fixed(n):
"""Set.has on validRolesSet per element -> O(1) per element."""
valid_roles_set = set(VALID_ROLES)
roles_to_check = [ELEMENT_ROLE_POOL[i % len(ELEMENT_ROLE_POOL)] for i in range(n)]
t0 = time.perf_counter()
found = 0
for role in roles_to_check:
if role in valid_roles_set: # O(1)
found += 1
dt = time.perf_counter() - t0
assert found >= 0
return dt
TRIALS = 3
SIZES = [100, 500, 1000, 5000, 10000]
def run():
lines = []
header = "=== playwright-0001: roleUtils Array.includes vs Set.has ==="
print(header)
lines.append(header)
for n in SIZES:
def_times = [bench_defective(n) for _ in range(TRIALS)]
fix_times = [bench_fixed(n) for _ in range(TRIALS)]
d_ms = min(def_times) * 1000
f_ms = min(fix_times) * 1000
speedup = d_ms / f_ms if f_ms > 0 else float("inf")
line = f"N={n:<6}: defective={d_ms:.3f}ms fixed={f_ms:.3f}ms speedup={speedup:.1f}x"
print(line)
lines.append(line)
sys.stdout.flush()
return lines
if __name__ == "__main__":
run()