Skip to content

Commit

Permalink
feat: use custom test client class with DJANGO_PEV_EXPLAIN_TEST_CLASS
Browse files Browse the repository at this point in the history
  • Loading branch information
uptickmetachu committed Jun 19, 2024
1 parent 8b2dda3 commit eaa9e65
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ INSTALLED_APPS = [
]
```


# Usage

Wrap some code with the explain context manager. All sql queries are captured
Expand Down Expand Up @@ -69,6 +70,13 @@ print(e.slowest.stacktrace)
pev_response.delete()
```

Optionally configure additional settings:
```python
# Replace the default test client used during explain with a custom class
DJANGO_PEV_EXPLAIN_TEST_CLIENT = 'django.test.Client'

```

**How to debug a slow endpoint in production**

If you have access to `python manage.py shell` on the production server;
Expand Down Expand Up @@ -96,7 +104,7 @@ print(e.slowest.visualize(title=f"Fetching {url}"))
- [ ] Add migration to ensure pg_stats_statement_info is correct
- [ ] Do not crash when its not available
- [ ] Add explain tab
= [ ] Add index suggester
- [ ] Add index suggester

# Disclaimer

Expand Down
3 changes: 2 additions & 1 deletion django_pev/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from django.http import HttpRequest, HttpResponseRedirect
from django.http.response import HttpResponse as HttpResponse
from django.shortcuts import render
from django.test import Client as TestClient
from django.urls import reverse
from django.utils.module_loading import import_string
from django.views.generic import FormView, TemplateView

from .utils import ExplainSet, explain, indexes, live_connections, maintenance, queries, space
Expand Down Expand Up @@ -116,6 +116,7 @@ class ExplainView(BaseView):
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
ctx = super().get_context_data(**kwargs)
explain_result = None
TestClient = import_string(getattr(settings, "DJANGO_PEV_EXPLAIN_TEST_CLIENT", "django.test.Client"))

if url := self.request.GET.get("url"):
client = TestClient()
Expand Down

0 comments on commit eaa9e65

Please sign in to comment.