Laboratory is a Python library (version 1.0.2) that enables confident refactoring of critical code paths by running 'experiments' in production. Inspired by GitHub's Scientist, it executes new code (candidate) alongside existing code (control) in a randomized order, compares return values, records timing, and logs exceptions, providing a feedback loop for verification. The library is stable and addresses a timeless engineering problem, though its release cadence is slow.
pip install laboratoryVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up and run a basic experiment. You define a 'control' function (your existing code) and a 'candidate' function (your new code). The `Experiment` class runs both, returning the control's result. Under the hood, it compares results, records performance, and logs exceptions from the candidate, which can then be published to a metrics system (not shown in this basic example).
Initialize `Experiment(raise_on_mismatch=True)` to re-raise exceptions immediately for easier debugging in non-production environments. Implement robust reporting for candidate exceptions in production.
Subclass `laboratory.Experiment` and override the `publish()` method to send `Observation` data to your preferred metrics (e.g., StatsD) or logging system (e.g., Sentry, Prometheus).
This is intended behavior for safe refactoring. If you need to access the candidate's return value, you would do so via your custom `publish` implementation, which receives the `Observation` objects.
Ensure `experiment.control(your_function, args=(arg1, arg2))` and `experiment.candidate(another_function, args=(arg1, arg2))` are called with valid, callable functions and `args` are provided as a tuple or list.
To get immediate feedback, use `experiment = laboratory.Experiment(raise_on_mismatch=True)`. For production, implement a custom `publish` method in a subclass of `Experiment` to send mismatch reports to your alerting system (e.g., Slack, PagerDuty).
No dependency data recorded yet.