Skip to main content

Production-grade API performance analysis platform for Django and FastAPI

Project description

๐Ÿ”ฌ API Autopsy

Production-grade API performance analysis for Django and FastAPI.

Automatically detect performance issues โ€” N+1 queries, blocking async calls, slow serializers โ€” without external APM services.

Python 3.9+ License: MIT


โšก Quickstart

Install

pip install api-autopsy

Django

# settings.py
INSTALLED_APPS += ["api_autopsy"]

MIDDLEWARE += ["api_autopsy.integrations.django.middleware.APIAutopsyMiddleware"]

FastAPI

from fastapi import FastAPI
from api_autopsy.integrations.fastapi import APIAutopsyMiddleware

app = FastAPI()
app.add_middleware(APIAutopsyMiddleware)

That's it. Visit /api-autopsy/ to see the dashboard.


๐ŸŽฏ What It Detects

Category Detection
N+1 Queries Repeated SQL patterns across a single request
Duplicate Queries Identical SQL executed multiple times
Slow Queries Individual queries exceeding thresholds
Missing Indexes Slow WHERE clauses on non-primary columns
Slow Serializers DRF serializer timing and nested cost
Async Violations time.sleep(), requests.* in async handlers
Memory Bloat Per-request memory allocation deltas
External Calls Outbound HTTP call count and timing

Every detection comes with actionable recommendations.


๐Ÿ“Š Performance Score

Each endpoint receives a 0โ€“100 score based on:

  • Latency (30%) โ€” exponential decay from ideal threshold
  • Query Efficiency (30%) โ€” penalizes count, duplicates, N+1
  • Memory Usage (20%) โ€” penalizes large allocation deltas
  • Async Correctness (20%) โ€” binary penalties for blocking calls

๐Ÿ–ฅ๏ธ Dashboard

Auto-served at /api-autopsy/ with:

  • Endpoint ranking table (sortable by score, latency, queries)
  • Performance timeline (5-minute buckets over 24h)
  • Slow endpoint heatmap (by hour)
  • Analysis findings with severity and recommendations
  • Dark/light mode toggle
  • Auto-refresh every 30s

๐Ÿ”ง Configuration

Django

# settings.py
API_AUTOPSY = {
    "ENABLED": True,
    "STORAGE_BACKEND": "sqlite",      # sqlite | postgres | redis
    "STORAGE_DSN": "autopsy.db",
    "SAMPLE_RATE": 1.0,               # 0.0โ€“1.0
    "RETENTION_HOURS": 72,
    "DASHBOARD_PREFIX": "/api-autopsy",
}

FastAPI

from api_autopsy.config import configure

configure(
    sample_rate=0.1,       # 10% sampling in production
    retention_hours=48,
    storage_dsn="/var/data/autopsy.db",
)

๐Ÿš€ CI/CD Mode

# Generate JSON report
api-autopsy analyze --format json --output report.json

# Fail CI if score drops below threshold
api-autopsy analyze --threshold 70

# Detect regression against a baseline
api-autopsy analyze --baseline baseline.json --threshold 10

# Cleanup old data
api-autopsy cleanup --hours 24

Exit codes: 0 = pass, 1 = regression or critical findings.


๐Ÿ”Œ Extensibility

Custom Analyzer

from api_autopsy.plugins import register_analyzer
from api_autopsy.analyzers.base import BaseAnalyzer
from api_autopsy.core.context import Finding

@register_analyzer
class MyCustomAnalyzer(BaseAnalyzer):
    def analyze(self, ctx):
        findings = []
        if ctx.duration_ms > 1000:
            findings.append(Finding(
                severity="warning",
                category="slow_response",
                message=f"Response took {ctx.duration_ms:.0f}ms",
                recommendation="Consider caching or pagination.",
            ))
        return findings

Via Entry Points

# pyproject.toml
[project.entry-points."api_autopsy.plugins"]
my_plugin = "my_package.autopsy:register"

๐Ÿ—๏ธ Architecture

api_autopsy/
โ”œโ”€โ”€ config.py              # Central configuration + auto-detection
โ”œโ”€โ”€ plugins.py             # Plugin discovery system
โ”œโ”€โ”€ core/
โ”‚   โ”œโ”€โ”€ context.py         # contextvars-based RequestContext
โ”‚   โ””โ”€โ”€ registry.py        # Collector/Analyzer registry
โ”œโ”€โ”€ collectors/
โ”‚   โ”œโ”€โ”€ base.py            # Abstract collector
โ”‚   โ”œโ”€โ”€ timing.py          # Request duration
โ”‚   โ”œโ”€โ”€ sql.py             # SQL queries + duplicates
โ”‚   โ”œโ”€โ”€ memory.py          # Memory delta (tracemalloc)
โ”‚   โ”œโ”€โ”€ http_calls.py      # Outbound HTTP tracking
โ”‚   โ””โ”€โ”€ cache.py           # Cache hit/miss tracking
โ”œโ”€โ”€ analyzers/
โ”‚   โ”œโ”€โ”€ base.py            # Abstract analyzer
โ”‚   โ”œโ”€โ”€ orm.py             # N+1, duplicates, slow queries
โ”‚   โ”œโ”€โ”€ serializer.py      # DRF serializer profiling
โ”‚   โ””โ”€โ”€ async_safety.py    # Blocking I/O detection
โ”œโ”€โ”€ scoring/
โ”‚   โ””โ”€โ”€ engine.py          # Weighted 0โ€“100 scoring
โ”œโ”€โ”€ storage/
โ”‚   โ”œโ”€โ”€ base.py            # Abstract storage
โ”‚   โ””โ”€โ”€ sqlite.py          # SQLite backend (default)
โ”œโ”€โ”€ dashboard/
โ”‚   โ”œโ”€โ”€ views.py           # Django + ASGI handlers
โ”‚   โ””โ”€โ”€ templates/
โ”‚       โ””โ”€โ”€ index.html     # Dashboard UI
โ”œโ”€โ”€ integrations/
โ”‚   โ”œโ”€โ”€ django/
โ”‚   โ”‚   โ”œโ”€โ”€ apps.py        # Django AppConfig
โ”‚   โ”‚   โ””โ”€โ”€ middleware.py   # WSGI middleware
โ”‚   โ””โ”€โ”€ fastapi/
โ”‚       โ””โ”€โ”€ middleware.py   # ASGI middleware
โ””โ”€โ”€ cli/
    โ””โ”€โ”€ main.py            # CLI entry point

๐Ÿงช Testing

pip install -e ".[dev]"
pytest tests/ -v

Production Safety

  • Sampling: Set sample_rate to 0.01โ€“0.1 for high-traffic
  • Overhead: < 5% with sampling; collectors are lightweight
  • Retention: Auto-cleanup via retention_hours or CLI
  • Async-safe: Uses contextvars โ€” no thread-local issues

License

MIT

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

api_autopsy-0.1.0.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

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

api_autopsy-0.1.0-py3-none-any.whl (40.5 kB view details)

Uploaded Python 3

File details

Details for the file api_autopsy-0.1.0.tar.gz.

File metadata

  • Download URL: api_autopsy-0.1.0.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for api_autopsy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0de0479d52657649c3c714bddaadb2d0e21dea216fe795e3ed4367e2ae014862
MD5 7dc4cbefb17c115e7d0f28c98de87744
BLAKE2b-256 4c567682e8c910c94c2f9176b7538974e97a3a94e6f65cf1b7cab528c05c9770

See more details on using hashes here.

File details

Details for the file api_autopsy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: api_autopsy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 40.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for api_autopsy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 29fb845ba9af56e69c5ddc0323019da3cf4434b8461c386e36cd31f32c9560fc
MD5 bc0286d26b13e674a07cfe71e393f403
BLAKE2b-256 bdb31b9f54e57d1f73c823ef0f0bc2294c9bf59fa1e3bd3ca692e620aca7499b

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