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 8000

📦 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

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 8000

📌 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.3.tar.gz (12.5 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.3-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: api_profiler-0.1.3.tar.gz
  • Upload date:
  • Size: 12.5 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.3.tar.gz
Algorithm Hash digest
SHA256 00b162af335f2da5f40c1c538433e9207faddf6c3d94b281ddbef7664ffebd84
MD5 0a579fc331ceaf81030f1672e557e247
BLAKE2b-256 f1965b2e9ea5e0e1a52f28fd5fe4c9cf91cbb67d9775b547a380d039f7c118d4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: api_profiler-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 13.2 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b5a24ad02471733bdd759c5bd33a6aaa5d6f6592606c20b775d04ced42b93ea3
MD5 bc5cdb15df0f6e98af7211ba1171e1d0
BLAKE2b-256 f8c25a599c372d9d16f75b5e0ef7267c86308a4e57d858d22e774c65c9da7d34

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