#!/usr/bin/env python3
"""Fixed-version, CPU-only synthetic HTML benchmark. Execute this TXT with Python."""
import argparse
import datetime as dt
import gc
import hashlib
import importlib.metadata
import json
import os
from pathlib import Path
import platform
import statistics
import subprocess
import sys
import tempfile
import time
import venv
import zipfile
DEPENDENCIES = [{'name': 'beautifulsoup4', 'version': '4.14.3', 'filename': 'beautifulsoup4-4.14.3-py3-none-any.whl', 'url': 'https://files.pythonhosted.org/packages/1a/39/47f9197bdd44df24d67ac8893641e16f386c984a0619ef2ee4c51fbbc019/beautifulsoup4-4.14.3-py3-none-any.whl', 'sha256': '0918bfe44902e6ad8d57732ba310582e98da931428d231a5ecb9e7c703a735bb', 'size': 107721, 'requires_dist': ['soupsieve>=1.6.1', 'typing-extensions>=4.0.0', 'cchardet; extra == "cchardet"', 'chardet; extra == "chardet"', 'charset-normalizer; extra == "charset-normalizer"', 'html5lib; extra == "html5lib"', 'lxml; extra == "lxml"']}, {'name': 'parsel', 'version': '1.10.0', 'filename': 'parsel-1.10.0-py2.py3-none-any.whl', 'url': 'https://files.pythonhosted.org/packages/12/18/35d1d947553d24909dca37e2ff11720eecb601360d1bac8d7a9a1bc7eb08/parsel-1.10.0-py2.py3-none-any.whl', 'sha256': '6a0c28bd81f9df34ba665884c88efa0b18b8d2c44c81f64e27f2f0cb37d46169', 'size': 17266, 'requires_dist': ['cssselect>=1.2.0', 'jmespath', 'lxml', 'packaging', 'w3lib>=1.19.0']}, {'name': 'lxml', 'version': '6.0.2', 'filename': 'lxml-6.0.2-cp314-cp314-macosx_10_13_universal2.whl', 'url': 'https://files.pythonhosted.org/packages/03/15/d4a377b385ab693ce97b472fe0c77c2b16ec79590e688b3ccc71fba19884/lxml-6.0.2-cp314-cp314-macosx_10_13_universal2.whl', 'sha256': 'b0c732aa23de8f8aec23f4b580d1e52905ef468afb4abeafd3fec77042abb6fe', 'size': 8659801, 'requires_dist': ['cssselect>=0.7; extra == "cssselect"', 'html5lib; extra == "html5"', 'BeautifulSoup4; extra == "htmlsoup"', 'lxml_html_clean; extra == "html-clean"']}, {'name': 'soupsieve', 'version': '2.8', 'filename': 'soupsieve-2.8-py3-none-any.whl', 'url': 'https://files.pythonhosted.org/packages/14/a0/bb38d3b76b8cae341dad93a2dd83ab7462e6dbcdd84d43f54ee60a8dc167/soupsieve-2.8-py3-none-any.whl', 'sha256': '0cc76456a30e20f5d7f2e14a98a4ae2ee4e5abdc7c5ea0aafe795f344bc7984c', 'size': 36679, 'requires_dist': None}, {'name': 'typing_extensions', 'version': '4.15.0', 'filename': 'typing_extensions-4.15.0-py3-none-any.whl', 'url': 'https://files.pythonhosted.org/packages/18/67/36e9267722cc04a6b9f15c7f3441c2363321a3ea07da7ae0c0707beb2a9c/typing_extensions-4.15.0-py3-none-any.whl', 'sha256': 'f0fa19c6845758ab08074a0cfa8b7aecb71c999ca73d62883bc25cc018c4e548', 'size': 44614, 'requires_dist': None}, {'name': 'cssselect', 'version': '1.3.0', 'filename': 'cssselect-1.3.0-py3-none-any.whl', 'url': 'https://files.pythonhosted.org/packages/ee/58/257350f7db99b4ae12b614a36256d9cc870d71d9e451e79c2dc3b23d7c3c/cssselect-1.3.0-py3-none-any.whl', 'sha256': '56d1bf3e198080cc1667e137bc51de9cadfca259f03c2d4e09037b3e01e30f0d', 'size': 18786, 'requires_dist': None}, {'name': 'jmespath', 'version': '1.0.1', 'filename': 'jmespath-1.0.1-py3-none-any.whl', 'url': 'https://files.pythonhosted.org/packages/31/b4/b9b800c45527aadd64d5b442f9b932b00648617eb5d63d2c7a6587b7cafc/jmespath-1.0.1-py3-none-any.whl', 'sha256': '02e2e4cc71b5bcab88332eebf907519190dd9e6e82107fa7f83b1003a6252980', 'size': 20256, 'requires_dist': None}, {'name': 'w3lib', 'version': '2.3.1', 'filename': 'w3lib-2.3.1-py3-none-any.whl', 'url': 'https://files.pythonhosted.org/packages/58/dd/56f0d8af71e475ed194d702f8b4cf9cea812c95e82ad823d239023c6558c/w3lib-2.3.1-py3-none-any.whl', 'sha256': '9ccd2ae10c8c41c7279cd8ad4fe65f834be894fe7bfdd7304b991fd69325847b', 'size': 21751, 'requires_dist': None}, {'name': 'packaging', 'version': '25.0', 'filename': 'packaging-25.0-py3-none-any.whl', 'url': 'https://files.pythonhosted.org/packages/20/12/38679034af332785aac8774540895e234f4d07f7545804097de4b666afd8/packaging-25.0-py3-none-any.whl', 'sha256': '29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484', 'size': 66469, 'requires_dist': None}]
PREFIX = 'beautifulsoup-parsel-local-2026-11-21-req065-a1'
REPEATS = 9
def utc():
return dt.datetime.now(dt.timezone.utc).isoformat()
def encode(obj):
return (json.dumps(obj, ensure_ascii=False, indent=2, allow_nan=False) + '\n').encode('utf-8')
def make_cases():
# Expected strings are explicit literals, not produced by either parser.
rich = [
('Hello world', 'Hello world'),
('日本語の文章とEnglish', '日本語の文章とEnglish'),
('A bold deep Z', 'A bold deep Z'),
('Tom & Jerry <tag> 🐱', 'Tom & Jerry 🐱'),
('前中後', '前中後'),
('line1
line2', 'line1line2'),
(' left\t middle right\n', ' left\t middle right\n'),
('', ''),
('a b © A', 'a\u00a0b © A'),
('xy link', 'xy link'),
]
whitespace = [(' ', ' '), (' \t ', ' \t '), ('\n \n', '\n \n'), ('', '')]
groups = [('plain100', 100, [('plain ASCII text', 'plain ASCII text'), ('日本語テキスト', '日本語テキスト')]),
('rich100', 100, rich), ('rich1000', 1000, rich),
('whitespace12', 12, whitespace)]
cases = []
for name, count, templates in groups:
paragraphs, expected = [], []
for i in range(count):
markup, truth = templates[i % len(templates)]
paragraphs.append(f'{markup}
')
expected.append(truth)
html = 'Artificialnot a paragraph
' + ''.join(paragraphs) + ''
cases.append(dict(id=name, paragraphs=count, html=html, expected=expected,
html_utf8_bytes=len(html.encode('utf-8')),
html_sha256=hashlib.sha256(html.encode('utf-8')).hexdigest()))
return cases
def validation(case, parse, extract):
actual = extract(parse(case['html']))
if len(actual) != case['paragraphs'] or any(type(x) is not str for x in actual):
raise RuntimeError('Unexpected result shape')
matches = sum(a == b for a, b in zip(actual, case['expected'], strict=True))
return dict(actual=actual, matched_paragraphs=matches, mismatched_paragraphs=len(actual)-matches,
exact_expected_list=int(actual == case['expected']),
output_sha256=hashlib.sha256(encode(actual)).hexdigest())
def batch(operation, iterations):
# Keep cyclic GC enabled, including its automatic collections in measured intervals.
gc.collect()
begin = time.perf_counter_ns()
for _ in range(iterations):
operation()
elapsed = time.perf_counter_ns() - begin
if elapsed <= 0:
raise RuntimeError('Nonpositive timer interval')
return dict(elapsed_ns=elapsed, operations=iterations,
milliseconds_per_operation=elapsed / iterations / 1_000_000)
def worker(destination):
import bs4
from parsel import Selector
from lxml import etree
started = utc()
load_start = os.getloadavg()
versions = {d['name']: importlib.metadata.version(d['name']) for d in DEPENDENCIES}
if versions != {d['name']: d['version'] for d in DEPENDENCIES}:
raise RuntimeError('Dependency version mismatch')
parsers = {'beautifulsoup': lambda html: bs4.BeautifulSoup(html, 'lxml'),
'parsel': lambda html: Selector(text=html, type='html')}
extractors = {'beautifulsoup': lambda tree: [p.get_text() for p in tree.find_all('p')],
'parsel': lambda tree: [p.xpath('string(.)').get() for p in tree.css('p')]}
cases = make_cases()
results, correctness, warmups, orders = [], [], [], []
for ci, case in enumerate(cases):
before = {tool: validation(case, parse, extractors[tool]) for tool, parse in parsers.items()}
trees = {tool: parse(case['html']) for tool, parse in parsers.items()}
iterations = 10 if case['paragraphs'] >= 1000 else 30
for pi, phase in enumerate(('parse_only', 'extract_only', 'parse_extract')):
operations = {}
for tool in parsers:
parse, extract = parsers[tool], extractors[tool]
if phase == 'parse_only':
operations[tool] = lambda parse=parse: parse(case['html'])
elif phase == 'extract_only':
operations[tool] = lambda extract=extract, tree=trees[tool]: extract(tree)
else:
operations[tool] = lambda parse=parse, extract=extract: extract(parse(case['html']))
rows = {tool: dict(id=f"{case['id']}-{phase}-{tool}", case_id=case['id'], phase=phase,
tool=tool, trials=[]) for tool in parsers}
tools = list(parsers)
if (ci + pi) % 2:
tools.reverse()
for tool in tools:
warmups.append(dict(case_id=case['id'], phase=phase, tool=tool, **batch(operations[tool], iterations)))
for repeat in range(REPEATS):
order = tools if repeat % 2 == 0 else list(reversed(tools))
orders.append(dict(case_id=case['id'], phase=phase, repeat=repeat+1, order=order[:]))
for tool in order:
rows[tool]['trials'].append(dict(repeat=repeat+1, **batch(operations[tool], iterations)))
results.extend(rows.values())
after = {tool: validation(case, parse, extractors[tool]) for tool, parse in parsers.items()}
for tool in parsers:
if before[tool] != after[tool]:
raise RuntimeError('Output changed between pre/post validation')
actual_a, actual_b = after['beautifulsoup']['actual'], after['parsel']['actual']
differences = [dict(index=i, expected=case['expected'][i], beautifulsoup=a, parsel=b)
for i, (a,b) in enumerate(zip(actual_a, actual_b, strict=True)) if a != b]
correctness.append(dict(id=case['id'], paragraphs=case['paragraphs'], before=before, after=after,
cross_matched_paragraphs=case['paragraphs']-len(differences), cross_mismatched_paragraphs=len(differences),
exact_cross_list=int(actual_a == actual_b), differences=differences))
raw = dict(request_id='req-20261022-065', topic_id='beautifulsoup-parsel-local-hikaku',
started_utc=started, ended_utc=utc(),
environment=dict(os=platform.platform(), machine=platform.machine(), python=sys.version,
logical_cpus=os.cpu_count(), gil_enabled=sys._is_gil_enabled(), versions=versions,
libxml_version=list(etree.LIBXML_VERSION), libxml_compiled_version=list(etree.LIBXML_COMPILED_VERSION),
execution='CPU only; no GPU/Metal libraries imported or initialized',
load_average_start=load_start, load_average_end=os.getloadavg(),
timer=vars(time.get_clock_info('perf_counter'))),
conditions=dict(repeats=REPEATS, warmup_batches=1, batches_operations='10 for 1000 paragraphs; otherwise 30',
phases={'parse_only':'HTML str to tree', 'extract_only':'cached tree to ordered p descendant text list',
'parse_extract':'HTML str to tree to ordered p descendant text list'},
parser_settings={'beautifulsoup':"BeautifulSoup(html, 'lxml'); find_all('p'); p.get_text()",
'parsel':"Selector(text=html, type='html'); css('p'); p.xpath('string(.)').get()"},
order='alternate tools each repeat; invert starting tool each case and phase',
gc='enabled; manual collect before each batch outside timer; automatic collections included',
excluded='network, venv, imports, input generation, preparse for extract_only, validation, file I/O, final deferred GC',
expected_contract='p descendant character data in document order, entity decoding, no trimming/normalization, no separator at br',
scope='synthetic valid HTML only; no external inference or AI quality evaluation'),
unit_map={'results[].trials[].elapsed_ns':'ns', 'results[].trials[].operations':'回',
'results[].trials[].milliseconds_per_operation':'ミリ秒',
'correctness[].cross_matched_paragraphs':'件', 'correctness[].cross_mismatched_paragraphs':'件',
'correctness[].after.beautifulsoup.matched_paragraphs':'件',
'correctness[].after.parsel.matched_paragraphs':'件'},
inputs=cases, correctness=correctness, warmups=warmups, order_log=orders, results=results)
dest = Path(destination)
dest.mkdir()
(dest/f'{PREFIX}-raw.json').write_bytes(encode(raw))
(dest/f'{PREFIX}-inputs.json').write_bytes(encode(cases))
print(json.dumps(dict(event='measurement_complete', cases=len(cases), measured_batches=len(results)*REPEATS,
correctness=[{k:c[k] for k in ('id','cross_matched_paragraphs','cross_mismatched_paragraphs')} for c in correctness])))
for row in results:
print(row['id'], f"{statistics.median(t['milliseconds_per_operation'] for t in row['trials']):.4f}", 'ms/op')
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--output-dir', required=True)
parser.add_argument('--worker', action='store_true')
args = parser.parse_args()
if args.worker:
worker(args.output_dir)
return
target = Path(args.output_dir).resolve()
if target.exists():
raise FileExistsError('Output directory must be new')
if sys.version_info[:2] != (3,14) or platform.system() != 'Darwin' or platform.machine() != 'arm64' or not sys._is_gil_enabled():
raise RuntimeError('Pinned binary wheel requires GIL CPython 3.14 on macOS arm64')
with tempfile.TemporaryDirectory(prefix='req065-html-replay-', dir='/tmp') as temporary:
temp = Path(temporary)
environment = temp/'venv'
venv.EnvBuilder(with_pip=False).create(environment)
python = environment/'bin/python'
site = Path(subprocess.check_output([str(python), '-I', '-c', 'import sysconfig; print(sysconfig.get_path("purelib"))'], text=True).strip())
dependencies = []
for spec in DEPENDENCIES:
wheel = temp/spec['filename']
subprocess.run(['curl','-q','--fail','--silent','--show-error','--location','--retry','2',spec['url'],'--output',str(wheel)],check=True)
payload = wheel.read_bytes()
digest = hashlib.sha256(payload).hexdigest()
if digest != spec['sha256'] or len(payload) != spec['size']:
raise RuntimeError('Wheel hash/size mismatch: '+spec['name'])
with zipfile.ZipFile(wheel) as archive:
for item in archive.infolist():
relative = Path(item.filename)
if relative.is_absolute() or '..' in relative.parts:
raise RuntimeError('Unsupported wheel member')
archive.extractall(site)
dependencies.append(dict(**spec, actual_sha256=digest, actual_size=len(payload), fetched_utc=utc()))
stage = temp/'stage'
completed = subprocess.run([str(python),'-I',str(Path(__file__).resolve()),'--worker','--output-dir',str(stage)],text=True,capture_output=True)
if completed.returncode:
raise RuntimeError(completed.stderr)
(stage/f'{PREFIX}-dependencies.json').write_bytes(encode(dependencies))
(stage/f'{PREFIX}-run-log.txt').write_text(completed.stdout+completed.stderr,encoding='utf-8')
(stage/f'{PREFIX}-replay.txt').write_bytes(Path(__file__).read_bytes())
# Export only after download, execution, validation and serialization all succeed.
target.mkdir()
for item in stage.iterdir():
with (target/item.name).open('xb') as stream:
stream.write(item.read_bytes())
print(completed.stdout,end='')
if __name__ == '__main__':
main()