Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Support running some benchmarks on a CodSpeed runner to include wall time (#8080)

Adrianf30f8bf1d5a4
diff --git a/.github/workflows/codspeed.yml b/.github/workflows/codspeed.yml
index d2db7f8d5..1d4f2d540 100644
--- a/.github/workflows/codspeed.yml
+++ b/.github/workflows/codspeed.yml
@@ -60,3 +60,46 @@ jobs:
with:
mode: simulation
run: tox -e benchmark
+
+ benchmark-walltime:
+ # Only benchmarks marked "walltime" (ones where a real concurrency
+ # win can't be shown by instruction counting) run here, on
+ # CodSpeed's dedicated, minutes-limited macro runner; everything
+ # else runs above, on a regular GitHub-hosted runner.
+ if: github.event.action != 'closed'
+ runs-on: codspeed-macro
+ env:
+ UV_PYTHON_PREFERENCE: only-system
+ permissions:
+ contents: read
+ id-token: write # OIDC authentication with CodSpeed
+
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ persist-credentials: false
+
+ - name: Set up Python 3.14
+ uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
+ with:
+ python-version: '3.14'
+
+ - name: Set up uv
+ uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
+ with:
+ cache-dependency-glob: |
+ pyproject.toml
+ tox.ini
+
+ - name: Install dependencies
+ run: |
+ uv tool install --with tox-uv tox
+ tox -n -e benchmark
+ - name: Run benchmarks
+ uses: CodSpeedHQ/action@0ca9cbbf4623b599a6c3ed4fc8a922942705d9f1 # v5.0.2
+ with:
+ mode: walltime
+ # No benchmark is walltime-marked on some branches (e.g.
+ # this one), which would otherwise fail the step.
+ allow-empty: true
+ run: tox -e benchmark -- tests/benchmarks -m walltime
diff --git a/pyproject.toml b/pyproject.toml
index 551b07ee7..e55149ecb 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -289,6 +289,7 @@ markers = [
"requires_boto3: marks tests that need botocore and boto3",
"requires_mitmproxy: marks tests that need a mitmdump executable",
"requires_internet: marks tests that need real Internet access",
+ "walltime: marks benchmarks that need CodSpeed's walltime mode to be meaningful",
]
filterwarnings = [
# Envs with pinned old dependencies opt out of this, see tox.ini.
diff --git a/tests/benchmarks/test_httpcompression.py b/tests/benchmarks/test_httpcompression.py
new file mode 100644
index 000000000..f37cc341a
--- /dev/null
+++ b/tests/benchmarks/test_httpcompression.py
@@ -0,0 +1,176 @@
+from __future__ import annotations
+
+import sys
+from gzip import GzipFile
+from io import BytesIO
+from typing import TYPE_CHECKING, Any
+
+import pytest
+
+from scrapy import Request, Spider
+from scrapy.http import Response
+from scrapy.utils.gz import gunzip
+from tests import get_testdata
+from tests.benchmarks import crawl
+
+if TYPE_CHECKING:
+ from collections.abc import AsyncIterator
+
+ from pytest_codspeed import BenchmarkFixture # type: ignore[import-not-found]
+
+ from scrapy.crawler import Crawler
+
+pytest.importorskip("pytest_codspeed", reason="Benchmarks require pytest-codspeed")
+
+try:
+ import brotli
+except ImportError:
+ import brotlicffi as brotli
+
+if sys.version_info >= (3, 14):
+ from compression import zstd
+else:
+ from backports import zstd
+
+# Concurrent responses per crawl, high enough that decoding several of them
+# in threads can overlap with the reactor doing other work.
+DOMAINS = 32
+
+# A real page, so that tiling it to reach a target size keeps a realistic
+# compression ratio (unlike, say, a repeated short string).
+_PAGE = gunzip(get_testdata("compressed", "html-gzip.bin"))
+
+
+def _html(size: int) -> bytes:
+ """A real HTML payload of exactly *size* bytes."""
+ return (_PAGE * (size // len(_PAGE) + 1))[:size]
+
+
+def _compress(codec: str, data: bytes) -> bytes:
+ if codec == "gzip":
+ buf = BytesIO()
+ with GzipFile(fileobj=buf, mode="wb") as f:
+ f.write(data)
+ return buf.getvalue()
+ if codec == "br":
+ compressed: bytes = brotli.compress(data)
+ return compressed
+ if codec == "zstd":
+ zstd_compressed: bytes = zstd.compress(data)
+ return zstd_compressed
+ raise ValueError(codec) # pragma: no cover
+
+
+def _handler_for(body: bytes, codec: str) -> type:
+ """Build a download handler class that returns *body* for every request.
+
+ Mirrors :class:`tests.benchmarks.NullDownloadHandler`'s concurrency
+ tracking, but returns a real compressed body so
+ :class:`~scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware`
+ has real decompression work to do.
+ """
+
+ class _CompressedDownloadHandler:
+ lazy = False
+
+ def __init__(self, crawler: Crawler):
+ self._crawler = crawler
+ self._active = 0
+
+ @classmethod
+ def from_crawler(cls, crawler: Crawler) -> _CompressedDownloadHandler:
+ return cls(crawler)
+
+ async def download_request(self, request: Request) -> Response:
+ self._active += 1
+ self._crawler.stats.max_value("benchmark/peak_concurrency", self._active)
+ try:
+ return Response(
+ request.url,
+ request=request,
+ body=body,
+ headers={"Content-Encoding": codec},
+ )
+ finally:
+ self._active -= 1
+
+ async def close(self) -> None:
+ pass
+
+ return _CompressedDownloadHandler
+
+
+class _CompressedSpider(Spider):
+ name = "benchmark-compression"
+ domains: int = DOMAINS
+
+ async def start(self) -> AsyncIterator[Any]:
+ for domain in range(self.domains):
+ yield Request(f"http://d{domain}.example.com/")
+
+ def parse(self, response: Response) -> None:
+ return None
+
+
+def _crawl_compressed(codec: str, size: int) -> Crawler:
+ body = _compress(codec, _html(size))
+ settings = {
+ "DOWNLOAD_HANDLERS": {"http": _handler_for(body, codec)},
+ "CONCURRENT_REQUESTS": DOMAINS,
+ "LOG_ENABLED": False,
+ }
+ crawler = crawl(_CompressedSpider, settings, domains=DOMAINS)
+ assert crawler.stats.get_value("httpcompression/response_count") == DOMAINS
+ return crawler
+
+
+# (codec, decompressed size). gzip gets the full sweep, since it is the most
+# common encoding; br and zstd only bracket a couple of representative sizes.
+# Sizes span small (a typical API response) to large (a big page or file), so
+# that a change affecting only large responses shows up on some of these and
+# not others.
+_CASES = [
+ *(
+ pytest.param("gzip", size, id=f"gzip-{size}")
+ for size in (
+ 4 * 1024,
+ 20 * 1024,
+ 100 * 1024,
+ 1024 * 1024,
+ 4 * 1024 * 1024,
+ )
+ ),
+ *(
+ pytest.param(codec, size, id=f"{codec}-{size}")
+ for codec in ("br", "zstd")
+ for size in (20 * 1024, 200 * 1024)
+ ),
+]
+
+
[email protected](("codec", "size"), _CASES)
+def test_decode_concurrency(
+ benchmark: BenchmarkFixture,
+ codec: str,
+ size: int,
+) -> None:
+ """Wall-clock cost of decoding many concurrent compressed responses.
+
+ Tracks whatever :class:`HttpCompressionMiddleware
+ <scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware>`
+ currently does under concurrency, at a range of sizes, so that a change
+ to how (or whether) it decodes off the reactor thread shows up as a
+ regression or improvement here, size by size.
+ """
+ benchmark(lambda: _crawl_compressed(codec, size))
+
+
+def test_decode_typical(benchmark: BenchmarkFixture) -> None:
+ """Overhead of decoding a typical, small gzip-compressed API response.
+
+ Uses the real (not monkeypatched) threshold: most responses stay under
+ it and are decoded inline, so this guards that common case against added
+ overhead.
+ """
+ benchmark(lambda: _crawl_compressed("gzip", 8 * 1024))
diff --git a/tox.ini b/tox.ini
index 8e0b28a9e..f928e2d21 100644
--- a/tox.ini
+++ b/tox.ini
@@ -400,4 +400,9 @@ passenv =
*codspeed*
*ci*
commands =
- pytest {posargs:tests/benchmarks} --codspeed --codspeed-mode=simulation
+ # No --codspeed-mode: CodSpeedHQ/action re-invokes this once per
+ # configured mode, selecting it through CODSPEED_RUNNER_MODE (passed
+ # through by the *codspeed* passenv above); a hardcoded mode here would
+ # override that on every run. Without it (e.g. running this tox env
+ # locally), pytest-codspeed defaults to walltime.
+ pytest {posargs:tests/benchmarks} --codspeed