Middleware that Prints the number of DB queries to the runserver console.

Overview

Django Querycount

Current Release License

Inspired by this post by David Szotten, this project gives you a middleware that prints DB query counts in Django's runserver console output.

django-querycount in action

Installation

pip install django-querycount

Just add querycount.middleware.QueryCountMiddleware to your MIDDLEWARE.

Notice that django-querycount is hard coded to work only in DEBUG mode set to true

Settings

There are two possible settings for this app: The first defines threshold values used to color output, while the second allows you customize requests that will be ignored by the middleware. The default settings are:

QUERYCOUNT = {
    'THRESHOLDS': {
        'MEDIUM': 50,
        'HIGH': 200,
        'MIN_TIME_TO_LOG':0,
        'MIN_QUERY_COUNT_TO_LOG':0
    },
    'IGNORE_REQUEST_PATTERNS': [],
    'IGNORE_SQL_PATTERNS': [],
    'DISPLAY_DUPLICATES': None,
    'RESPONSE_HEADER': 'X-DjangoQueryCount-Count'
}

The QUERYCOUNT['THRESHOLDS'] settings will determine how many queries are interpreted as high or medium (and the color-coded output). In previous versions of this app, this settings was called QUERYCOUNT_THRESHOLDS and that setting is still supported.

The QUERYCOUNT['IGNORE_REQUEST_PATTERNS'] setting allows you to define a list of regexp patterns that get applied to each request's path. If there is a match, the middleware will not be applied to that request. For example, the following setting would bypass the querycount middleware for all requests to the admin:

QUERYCOUNT = {
    'IGNORE_REQUEST_PATTERNS': [r'^/admin/']
}

The QUERYCOUNT['IGNORE_SQL_PATTERNS'] setting allows you to define a list of regexp patterns that ignored to statistic sql query count. For example, the following setting would bypass the querycount middleware for django-silk sql query:

QUERYCOUNT = {
    'IGNORE_SQL_PATTERNS': [r'silk_']
}

The QUERYCOUNT['RESPONSE_HEADER'] setting allows you to define a custom response header that contains the total number of queries executed. To disable this header, the supply None as the value:

QUERYCOUNT = {
    'RESPONSE_HEADER': None
}

New in 0.4.0. The QUERYCOUNT['DISPLAY_DUPLICATES'] setting allows you to control how the most common duplicate queries are displayed. If the setting is None (the default), duplicate queries are not displayed. Otherwise, this should be an integer. For example, the following setting would always print the 5 most duplicated queries:

QUERYCOUNT = {
    'DISPLAY_DUPLICATES': 5,
}

License

This code is distributed under the terms of the MIT license.

Testing

Run python manage.py test querycount to run the tests. Note that this will modify your settings so that your project is in DEBUG mode for the duration of the querycount tests.

(side-note: this project needs better tests; for the moment, there are only smoke tests that set up the middleware and call two simple test views).

Contributing

Bug fixes and new features are welcome! Fork this project and send a Pull Request to have your work included. Be sure to add yourself to AUTHORS.rst.

Owner
Brad Montgomery
Pythonista & Djangonaut. I write other languages too. Sometimes English.
Brad Montgomery
A toolbar overlay for debugging Flask applications

Flask Debug-toolbar This is a port of the excellent django-debug-toolbar for Flask applications. Installation Installing is simple with pip: $ pip ins

863 Dec 29, 2022
Cyberbrain: Python debugging, redefined.

Cyberbrain1(电子脑) aims to free programmers from debugging.

laike9m 2.3k Jan 07, 2023
Sentry is cross-platform application monitoring, with a focus on error reporting.

Users and logs provide clues. Sentry provides answers. What's Sentry? Sentry is a service that helps you monitor and fix crashes in realtime. The serv

Sentry 32.9k Dec 31, 2022
Silky smooth profiling for Django

Silk Silk is a live profiling and inspection tool for the Django framework. Silk intercepts and stores HTTP requests and database queries before prese

Jazzband 3.7k Jan 01, 2023
A gdb-like Python3 Debugger in the Trepan family

Abstract Features More Exact location information Debugging Python bytecode (no source available) Source-code Syntax Colorization Command Completion T

R. Bernstein 126 Nov 24, 2022
Visual profiler for Python

vprof vprof is a Python package providing rich and interactive visualizations for various Python program characteristics such as running time and memo

Nick Volynets 3.9k Jan 01, 2023
Never use print for debugging again

PySnooper - Never use print for debugging again PySnooper is a poor man's debugger. If you've used Bash, it's like set -x for Python, except it's fanc

Ram Rachum 15.5k Jan 01, 2023
Integration of IPython pdb

IPython pdb Use ipdb exports functions to access the IPython debugger, which features tab completion, syntax highlighting, better tracebacks, better i

Godefroid Chapelle 1.7k Jan 07, 2023
Voltron is an extensible debugger UI toolkit written in Python.

Voltron is an extensible debugger UI toolkit written in Python. It aims to improve the user experience of various debuggers (LLDB, GDB, VDB an

snare 5.9k Dec 30, 2022
OpenCodeBlocks an open-source tool for modular visual programing in python

OpenCodeBlocks OpenCodeBlocks is an open-source tool for modular visual programing in python ! Although for now the tool is in Beta and features are c

Mathïs Fédérico 1.1k Jan 06, 2023
An x86 old-debug-like program.

An x86 old-debug-like program.

Pablo Niklas 1 Jan 10, 2022
NoPdb: Non-interactive Python Debugger

NoPdb: Non-interactive Python Debugger Installation: pip install nopdb Docs: https://nopdb.readthedocs.io/ NoPdb is a programmatic (non-interactive) d

Ondřej Cífka 67 Oct 15, 2022
A powerful set of Python debugging tools, based on PySnooper

snoop snoop is a powerful set of Python debugging tools. It's primarily meant to be a more featureful and refined version of PySnooper. It also includ

Alex Hall 874 Jan 08, 2023
Inject code into running Python processes

pyrasite Tools for injecting arbitrary code into running Python processes. homepage: http://pyrasite.com documentation: http://pyrasite.rtfd.org downl

Luke Macken 2.7k Jan 08, 2023
🔥 Pyflame: A Ptracing Profiler For Python. This project is deprecated and not maintained.

Pyflame: A Ptracing Profiler For Python (This project is deprecated and not maintained.) Pyflame is a high performance profiling tool that generates f

Uber Archive 3k Jan 07, 2023
Hypothesis debugging with vscode

Hypothesis debugging with vscode

Oliver Mannion 0 Feb 09, 2022
Debugger capable of attaching to and injecting code into python processes.

DISCLAIMER: This is not an official google project, this is just something I wrote while at Google. Pyringe What this is Pyringe is a python debugger

Google 1.6k Dec 15, 2022
一个小脚本,用于trace so中native函数的调用。

trace_natives 一个IDA小脚本,获取SO代码段中所有函数的偏移地址,再使用frida-trace 批量trace so函数的调用。 使用方法 1.将traceNatives.py丢进IDA plugins目录中 2.IDA中,Edit-Plugins-traceNatives IDA输

296 Dec 28, 2022
Debugging manhole for python applications.

Overview docs tests package Manhole is in-process service that will accept unix domain socket connections and present the stacktraces for all threads

Ionel Cristian Mărieș 332 Dec 07, 2022
Trace any Python program, anywhere!

lptrace lptrace is strace for Python programs. It lets you see in real-time what functions a Python program is running. It's particularly useful to de

Karim Hamidou 687 Nov 20, 2022