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).
120 lines
4 KiB
Python
120 lines
4 KiB
Python
#!/usr/bin/env python3
|
|
# UNDF: UNDF-2026-000001289 (webdriverio-0001)
|
|
#
|
|
# CWE-407: Algorithmic Complexity
|
|
#
|
|
# Defect:
|
|
# webdriverio-0001: extractOrConditions scans orMatches array via .find and
|
|
# .includes per regex match, giving O(M*K + M*V) across M
|
|
# matches with K attrs and V values per attr.
|
|
#
|
|
# Fix:
|
|
# Replace the array-of-objects + .find + .includes pattern with
|
|
# Map<string, Set<string>>. Per-match cost becomes amortized O(1).
|
|
#
|
|
# Complexity gate (from bench/results.txt on this machine):
|
|
# K=V=60 defective=15.8ms, fixed=2.6ms.
|
|
# Fixed must complete in <5ms at K=V=40. k-scaling <17.5x.
|
|
|
|
import importlib.util
|
|
import os
|
|
import sys
|
|
import unittest
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
BENCH = os.path.join(os.path.dirname(HERE), "bench")
|
|
sys.path.insert(0, BENCH)
|
|
|
|
|
|
def _load(fname):
|
|
path = os.path.join(BENCH, fname)
|
|
spec = importlib.util.spec_from_file_location(fname, path)
|
|
mod = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(mod)
|
|
return mod
|
|
|
|
|
|
_mod = _load("bench-webdriverio-0001.py")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Correctness: Map-based impl must produce the same (attr, value) pairs as
|
|
# the array-based impl, in the same insertion order.
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _extract_defective(pairs):
|
|
"""Array-of-objects dedup mirroring extractOrConditions original impl."""
|
|
or_matches = []
|
|
for attr, a, b in pairs:
|
|
existing = None
|
|
for m in or_matches:
|
|
if m["attr"] == attr:
|
|
existing = m
|
|
break
|
|
if existing is None:
|
|
or_matches.append({"attr": attr, "values": [a, b]})
|
|
else:
|
|
if a not in existing["values"]:
|
|
existing["values"].append(a)
|
|
if b not in existing["values"]:
|
|
existing["values"].append(b)
|
|
out = []
|
|
for m in or_matches:
|
|
for v in m["values"]:
|
|
out.append((m["attr"], v))
|
|
return out
|
|
|
|
|
|
def _extract_fixed(pairs):
|
|
"""Map<attr, Set<values>> impl."""
|
|
or_matches = {}
|
|
for attr, a, b in pairs:
|
|
if attr not in or_matches:
|
|
or_matches[attr] = [] # list to preserve order
|
|
seen = set(or_matches[attr])
|
|
if a not in seen:
|
|
or_matches[attr].append(a)
|
|
seen.add(a)
|
|
if b not in seen:
|
|
or_matches[attr].append(b)
|
|
out = []
|
|
for attr, values in or_matches.items():
|
|
for v in values:
|
|
out.append((attr, v))
|
|
return out
|
|
|
|
|
|
class TestWebdriverio0001Correctness(unittest.TestCase):
|
|
def test_empty(self):
|
|
self.assertEqual(_extract_fixed([]), _extract_defective([]))
|
|
|
|
def test_single_pair(self):
|
|
pairs = [("class", "a", "b")]
|
|
self.assertEqual(_extract_fixed(pairs), _extract_defective(pairs))
|
|
|
|
def test_dedup_same_attr(self):
|
|
pairs = [("class", "a", "b"), ("class", "b", "c"), ("class", "a", "d")]
|
|
self.assertEqual(_extract_fixed(pairs), _extract_defective(pairs))
|
|
|
|
def test_multiple_attrs(self):
|
|
pairs = [("id", "1", "2"), ("class", "a", "b"), ("id", "3", "1")]
|
|
self.assertEqual(_extract_fixed(pairs), _extract_defective(pairs))
|
|
|
|
|
|
class TestWebdriverio0001ComplexityGate(unittest.TestCase):
|
|
def test_fixed_wallclock_K40_V40(self):
|
|
t_s = min(_mod.bench_fixed(40, 40) for _ in range(3))
|
|
self.assertLess(t_s * 1000, 5.0,
|
|
f"fixed took {t_s*1000:.3f}ms at K=V=40, expected <5ms")
|
|
|
|
def test_fixed_scaling_sub_quadratic(self):
|
|
t_10 = min(_mod.bench_fixed(10, 10) for _ in range(3))
|
|
t_40 = min(_mod.bench_fixed(40, 40) for _ in range(3))
|
|
ratio = t_40 / t_10 if t_10 > 0 else float("inf")
|
|
# 4x scaling on K and V -> M = K*V grows 16x; O(M) is 16x, O(M^2) is 256x
|
|
self.assertLess(ratio, 64,
|
|
f"fixed K=40/K=10 ratio {ratio:.2f}x, expected <64x (sub-quadratic)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|