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.2.tar.gz (12.0 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.2-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: api_profiler-0.1.2.tar.gz
  • Upload date:
  • Size: 12.0 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.2.tar.gz
Algorithm Hash digest
SHA256 7e99b4b1ed06379718e06a3694028bd34aa79077a6c767bd4b43f0a83e673624
MD5 b353177c80e3fbe6ba0fe0387f5a555b
BLAKE2b-256 c102c8d58ecfbb4aba0b5872d0c8b20551caad5ed74b21a8d72eca7b6e9b8c39

See more details on using hashes here.

File details

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

File metadata

  • Download URL: api_profiler-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 12.8 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0ab18860c8d32efb5f49ea7b26b3321d60d9e13daa8a92fcd437c3a83c9e85c7
MD5 9dfcceb304a01afadb59e3ac138175be
BLAKE2b-256 811c6f55d6c7b6eb4bc01a0ff9e6caa8bafb22eefa244bba0c55962431e39c77

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