Introducing Silk, live profiling for Django

Silk is a live profiling and inspection tool for the Django framework. It:

  • Intercepts requests and responses, recording:
    • SQL Queries
    • Response times
    • Headers
    • Bodies
  • Allows tracing of SQL queries via Python stack traces
  • Profiles blocks of code and functions
  • Generates code and commands for replaying requests

A live demo is actively profiling this site. The
github readme is currently acting as documentation and is available here.

Silk releases can be downloaded from here or installed via pip into your virtualenv:

1 pip install django-silk

Below is a feature summary:

Request Inspection

The Silk middleware intercepts and stores requests and responses and stores them in the configured database.
These requests can then be filtered and inspected using Silk’s UI through the request overview:

Further details on each request are also available by clicking the relevant request:

SQL Inspection

Silk also intercepts SQL queries that are generated by each request. We can get a summary on things like
the tables involved, number of joins and execution time:

Before diving into the stack trace to figure out where this request is coming from:


Silk can also be used to profile random blocks of code/functions. It provides a decorator and a context
manager for this purpose.

For example:

1 @silk_profile(name='View Blog Post')
2 def post(request, post_id):
3     p = Post.objects.get(pk=post_id)
4     return render_to_response('post.html', {
5         'post': p
6     })

Whenever a blog post is viewed we get an entry within the Silk UI:

Silk profiling not only provides execution time, but also collects SQL queries executed within the block in the same fashion as with requests:

Note that currently profiling does not occur outside of the request/response cycle.

Experimental Features

The below features are still in need of thorough testing and should be considered experimental.

Dynamic Profiling

One of Silk’s more interesting features is dynamic profiling. If for example we wanted to profile a function in a dependency to which we only have read-only access (e.g. system python libraries owned by root) we can add the following to to apply a decorator at runtime:

2     'module': '',
3     'function': ''
4 }]

which is roughly equivalent to:

1 class MyClass(object):
2     @silk_profile()
3     def bar(self):
4         pass

Code Generation

Silk currently generates two bits of code per request:

Both are intended for use in replaying the request. The curl command can be used to replay via command-line and the python code can be used within a Django unit test or simply as a standalone script.