Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions Lib/test/test_minidom.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,6 @@

importcopy
importpickle
importtime
importio
fromtestimportsupport
importunittest
Expand DownExpand Up@@ -178,23 +177,36 @@ def testAppendChild(self):
deftestAppendChildNoQuadraticComplexity(self):
impl=getDOMImplementation()

newdoc=impl.createDocument(None, "some_tag", None)
top_element=newdoc.documentElement
children= [newdoc.createElement(f"child-{i}") foriinrange(1, 2**15+1)]
element=top_element

start=time.monotonic()
forchildinchildren:
element.appendChild(child)
element=child
end=time.monotonic()

# This example used to take at least 30 seconds.
# Conservative assertion due to the wide variety of systems and
# build configs timing based tests wind up run under.
# A --with-address-sanitizer --with-pydebug build on a rpi5 still
# completes this loop in <0.5 seconds.
self.assertLess(end-start, 4)
defwork(n):
doc=impl.createDocument(None, "some_tag", None)
element=doc.documentElement
total_calls=0

# Count attribute accesses as a proxy for work done
defgetattribute_counter(self, attr):
nonlocaltotal_calls
total_calls+=1
returnobject.__getattribute__(self, attr)

withsupport.swap_attr(Element, "__getattribute__", getattribute_counter):
for_inrange(n):
child=doc.createElement("child")
element.appendChild(child)
element=child
returntotal_calls

# Doubling N should not ~quadruple the work.
w1=work(1024)
w2=work(2048)
w3=work(4096)

self.assertGreater(w1, 0)
r1=w2/w1
r2=w3/w2
self.assertLess(
max(r1, r2), 3.2,
msg=f"Possible quadratic behavior: work={w1,w2,w3} ratios={r1,r2}"
)

deftestSetAttributeNodeWithoutOwnerDocument(self):
# regression test for gh-142754
Expand Down
Loading