A fast and lightweight A/B testing library for Python.
Project description
Fast ABTest
A lightweight Python decorator for implementing A/B testing with automatic traffic distribution and built-in metrics monitoring. Compatible with FastAPI and works with both synchronous and asynchronous functions.
Installation
pip install fast-abtest
Quick Start
Basic Usage
from fast_abtest import ab_test, Metric
@ab_test(metrics=[Metric.LATENCY, Metric.ERRORS_TOTAL])
def recommendation_service(user_id: int) -> list[str]:
# Main variant (A) - receives remaining traffic percentage
return ["item1", "item2"]
@recommendation_service.register_variant(traffic_percent=30, disable_threshold=0.2)
def recommendation_service_b(user_id: int) -> list[str]:
# Variant B - gets 30% of the traffic. If the error rate exceeds 0.2, traffic redirection will stop.
return ["item3", "item4"]
FastAPI Integration
from fastapi import FastAPI, Depends
from fast_abtest import ab_test
app = FastAPI()
@app.get("/recommendations")
@ab_test(metrics=[])
async def get_recommendations(user_id: int):
return {"items": ["A1", "A2"]}
@get_recommendations.register_variant(traffic_percent=30)
async def get_recommendations_b(user_id: int):
return {"items": ["B1", "B2"]}
Important: For FastAPI, the route decorator (@app.get) must come before @ab_test.
Consistent distribution
from fastapi import FastAPI, Depends
from fast_abtest import ab_test
app = FastAPI()
@app.get("/recommendations")
@ab_test(metrics=[], consistency_key='user_id')
async def get_recommendations(user_id: int):
return {"items": ["A1", "A2"]}
@get_recommendations.register_variant(traffic_percent=30)
async def get_recommendations_b(user_id: int):
return {"items": ["B1", "B2"]}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fast_abtest-0.4.2.tar.gz.
File metadata
- Download URL: fast_abtest-0.4.2.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eac703c7ed5f18a0cb4c43ac2772d1520bd8560a58d3022d550fa2050a16d4a1
|
|
| MD5 |
ed436138d188e30333ca3e0c65b7cb7b
|
|
| BLAKE2b-256 |
e4fcfe16b634fa066092ec2e236183bcf48985c36515124c7fb2f04f51eb8a63
|
File details
Details for the file fast_abtest-0.4.2-py3-none-any.whl.
File metadata
- Download URL: fast_abtest-0.4.2-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.0 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6824fc7d0e5846e36a92cd8c4ec1558477c02853facba4ebaeb0dc1b20f5b9fb
|
|
| MD5 |
9f8d11567e2de4a12aea811ee815a801
|
|
| BLAKE2b-256 |
bd28bfa1353170345efe5d905c8100f02db98dc2d76055ff82b34d1fb0424889
|