Skip to main content

Django profiling middleware CLI tool

Project description

API Profiler 🌀

A plug-and-play Django middleware profiler that tracks API performance and SQL query usage — no code changes required in your existing Django app.

✨ Features

  • ✅ Profiles total execution time of each request.
  • ✅ Logs detailed SQL queries executed per request.
  • ✅ Color-coded, structured, and readable output.
  • ✅ Works with existing Django projects out of the box.
  • ✅ Installs as a global CLI: profile run

📦 Installation

Once packaged and uploaded:

pip install api-profiler

(For local development, run from the root of the repo:)

pip install <<built_whl_file>>

🚀 Usage

Navigate to your Django project folder and run:

profile --set all #set all metrics as active
profile run  #run django server
profile --unset all #deactivate all metrics
profile --set sql response-headers
profile --unset sql response-body

This:

  • Injects the api_profiler middleware at runtime.
  • Runs your Django app at port 8000 (or any custom port).
  • Outputs profiling logs in your console.

📊 Sample Output

[INFO] 2025-05-26 16:49:31,674 - METHOD: GET     PATH: /users/ - None
[INFO] 2025-05-26 16:49:31,678 - Params:
        None - None
[INFO] 2025-05-26 16:49:31,678 - Headers:
        Content-Length:
        Content-Type: text/plain
        Host: 127.0.0.1:8000
        Connection: keep-alive
        Cache-Control: max-age=0
        Sec-Ch-Ua: "Chromium";v="136", "Google Chrome";v="136", "Not.A/Brand";v="99"
        Sec-Ch-Ua-Mobile: ?0
        Sec-Ch-Ua-Platform: "Windows"
        Dnt: 1
        Upgrade-Insecure-Requests: 1
        User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
        Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
        Sec-Fetch-Site: none
        Sec-Fetch-Mode: navigate
        Sec-Fetch-User: ?1
        Sec-Fetch-Dest: document
        Accept-Encoding: gzip, deflate, br, zstd
        Accept-Language: en-US,en;q=0.9,hi;q=0.8
        Cookie: csrftoken=XHCl7rQYwFje2xGEgFB2eNwLAV74A3Ru - None
[INFO] 2025-05-26 16:49:31,678 - Body:
 Size: 0 bytes - None
[INFO] 2025-05-26 16:49:31,678 -
--------------------------------------------------------------------------------
SQL Queries Summary
Path     : /users/
Total    : 10 queries
[001]
SELECT auth_user.id AS id,  auth_user.username AS username,  auth_user.email AS email
FROM auth_user
       Repeated: 10x | Total Time: 0.000 sec

Total Execution Time: 0.000 sec
--------------------------------------------------------------------------------
 - None
[INFO] 2025-05-26 16:49:31,679 - Response: []
 Size: 2 bytes - None
[INFO] 2025-05-26 16:49:31,679 - Response Headers:
        Content-Type: application/json - None
[INFO] 2025-05-26 16:49:31,679 - Status: 200 Total time taken: 0.005 seconds

🛠️ How It Works

  • Uses a runtime patching technique to inject middleware without needing to modify settings.py.
  • Automatically detects the Django project in the current directory.
  • Uses a CLI entry point (profile) for ease of use.
  • Provides clear logging using logging.config.dictConfig.

🔧 Developer Setup

Clone the repo and install dependencies:

git clone git@github.com:Av-sek/api-profiler.git
cd api_profiler
pip install -e .

OR

You can also build it using

pip install --upgrade build
python -m build (on project root dir)
pip install dist/api_profiler-0.1.0-py3-none-any.whl

Run it on any Django project:

profile run

📌 Roadmap Ideas

  • 🔍 Per-view performance breakdown
  • 📈 Export profiling data to JSON/CSV
  • 🌐 Web dashboard integration
  • 🔐 Auth headers masking
  • 🧪 Unit test coverage and integration tests

👤 Author

Abhishek Ghorashainee GitHub · LinkedIn


📄 License

This project is licensed under the MIT License.


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_profiler-0.1.4.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

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

api_profiler-0.1.4-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file api_profiler-0.1.4.tar.gz.

File metadata

  • Download URL: api_profiler-0.1.4.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for api_profiler-0.1.4.tar.gz
Algorithm Hash digest
SHA256 2d5ba88a860abe85e9f434a8113c9f2b6c9b3c4b91ac6f220cf8e68859dd504f
MD5 99c891703c514a0096ababeb4656d6b2
BLAKE2b-256 1ead7d4dfff72ad6fa2f39287ab527545c5f5c09ee156d048a8641868d197bfe

See more details on using hashes here.

File details

Details for the file api_profiler-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: api_profiler-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for api_profiler-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8bca39e1bcb1795be8cf72290059468d485fed2356239703924c0ffd5c2c64f2
MD5 927be6304f7a01a1308334a5edc038ab
BLAKE2b-256 ec8b75a422e2e4884bac05b3e93dfda07f7a630b1733b6ee85c55938f7472a9b

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