from __future__ import annotations from html import escape from pathlib import Path from typing import TYPE_CHECKING, Any import pytest from scrapy import Request from scrapy.http import HtmlResponse from scrapy.linkextractors import LinkExtractor from scrapy.utils.request import fingerprint if TYPE_CHECKING: from pytest_codspeed import BenchmarkFixture # type: ignore[import-not-found] pytest.importorskip("pytest_codspeed", reason="Benchmarks require pytest-codspeed") RESPONSE_URL = "https://www.example.com/catalogue/page-1.html" # Links that each scenario returns for the benchmark page. They are fewer than # the anchors of the page because links to images, to other non-crawlable files # and to non-HTTP schemes are rejected, and, except in the scenario that keeps # duplicates, because the links that the navigation repeats are collapsed. LINKS = 63 DUPLICATE_LINKS = 88 CANONICAL_LINKS = 60 FILTERED_LINKS = 45 # Requests built from LINKS links that point to a different resource. # Canonicalization maps the rest to one that another link already covers, e.g. # two fragments of a page, or two spellings of one percent-escape. FINGERPRINTS = 60 def _read_corpus() -> tuple[list[str], list[str]]: """Return the URLs of ``urls.txt``, and its first group of URLs. The first group is the site navigation, which the benchmark page repeats. """ groups: list[list[str]] = [[]] for line in (Path(__file__).parent / "urls.txt").read_text().splitlines(): line = line.strip() if not line or line.startswith("#"): if groups[-1]: groups.append([]) continue groups[-1].append(line) urls = [url for group in groups for url in group] return urls, groups[0] def _build_page(urls: list[str], navigation: list[str]) -> bytes: """Return an HTML page that links to *urls*. Every link is surrounded by the markup of a product listing, so that benchmarks also cover walking over the elements and attributes that a real page puts between links. """ def item(index: int, url: str) -> str: href = escape(url) return ( f'
A description of product {index}.
' f"