Skip to main content

Speed-optimized request recording and replaying for HTTPX

Project description

dvd-rw

Speed-optimized request recording and replaying for HTTPX.

dvd-rw aims to provide a vcrpy-like workflow with a focus on high scalability: efficient lookups and minimal overhead when recording/replaying thousands of requests. Core components use Pydantic v2 models for structured data and JSON serialization. A keyed index enables O(1)-ish replay lookups.

Quickstart

from dvd_rw.loader import DVDLoader
from dvd_rw.models import Matcher

with DVDLoader(
        file_path="/tmp/example.dvd.json",
        match_on=[Matcher.host, Matcher.method, Matcher.path, Matcher.query],
        extra_matchers=[],
) as dvd:
    # If the file did not exist yet, dvd.from_file will be False (recording allowed)
    # On later runs when the file exists, dvd.from_file will be True (replay only)
    # Do some requests here
    ...

Why dvd-rw?

If your use-case fits the request-replay features of the vcr(py), library, but you need faster performance, dvd-rw provides:

  • Fast lookups for requestsvia a keyed index; only a small bucket is scanned per request
  • Built in matchers (host, query, ...) are provided in a performance-optimized manner
  • DVD loading is optimized for speed by leveraging Pydantic where possible

Next Steps

The current version has minimal functionality: only HTTPX support, no cassette modes, etc...

The next versions are focused on improving the experience for my primary use case, testing:

  • PyTest integration (through a fixture and mark) to save DVDs for given test(s), allowing you to run against a real server once, and store the results for the next test run
    • A PyTest hook to only save the dvd in case of a successful test
  • Filtering which requests/request parts to store (functionality similar to before record request/response in vcrpy)

Long-term, I want to keep performance as a main goal:

  • Reduce dvd file size when saved through compression, if this is possible in a performant manner
  • Move file IO into rust

Feel free to open an issue for functionality that would make this library better

Installation

Using uv (preferred):

uv pip install -e . --group dev

Notes:

  • -e installs in editable mode for local development.
  • --group dev will include pytest for running the test suite.

Using pip (alternative):

python -m venv .venv && source .venv/bin/activate
python -m pip install -e .
python -m pip install pytest

Functionality

DVDLoader only saves on successful exit when changes were made (tracked via dvd.dirty). If the DVD was loaded from file (from_file=True), calling dvd.record_request (i.e., encountering an unsaved request) raises CannotRecord to prevent accidental writes.

Exception semantics: when recording, if an httpx request raises, the patcher records the exception class and message. On replay, the same exception type is reconstructed and raised (falling back to httpx.RequestError if constructor signatures don’t match exactly). This is powered by RequestExceptionInfo in dvd_rw.models.

Matching and performance

  • Builtin matchers are both hashed (for indexing) and compared (for equality) using these features: host, method, path, query, headers, scheme.
  • You can customize match_on to include only what you need; e.g., not matching on host
  • extra_matchers is a list of callables taking (recorded_request, incoming_request) and returning bool, applied after the hash bucket filter. Use this to build your own matching logic not supported by the base matcher(s)
  • For high throughput:
    • Use match on over extra matchers as much as possible. Generally, the more matchers you have, the faster request lookup can run.

Response selection semantics

DVD.get_response(request) returns the first matching response whose per-index _match_counts < 1, then increments. Each recorded pair is therefore returned at most once by default. If you need cycling or multiple uses, adapt the logic or build a higher-level helper.

Running tests

pytest -q

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dvd_rw-0.1.3.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dvd_rw-0.1.3-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file dvd_rw-0.1.3.tar.gz.

File metadata

  • Download URL: dvd_rw-0.1.3.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.8

File hashes

Hashes for dvd_rw-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a79fc5742050effaa883b768456462a7a232e012ed1e084fff724f9aab562b71
MD5 2d862d08db52a19f6fc9432adc3a03e5
BLAKE2b-256 6ebe098246bef033194c5a381b42802c6a136d2a977210284ab17c0e4221642e

See more details on using hashes here.

File details

Details for the file dvd_rw-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: dvd_rw-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.8

File hashes

Hashes for dvd_rw-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 be2381c8bc6a1d7be2fe79b4e650bc66e608cdd146d9cd9d9f1cd2cdcf1c2c44
MD5 581db1939e171dd809e3c90bcca29333
BLAKE2b-256 bdb7211586ade50a739fd985a647de035eb5479591abc2c7c911053d79ab7706

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page