-
Notifications
You must be signed in to change notification settings - Fork 2
Quick Start
smattymatty edited this page Aug 25, 2025
·
2 revisions
Get Django Mercury running in 5 minutes and start finding performance issues immediately.
- Python 3.10+
- Django 3.2+
- An existing Django project with tests
pip install django-mercury-performanceOr add to your requirements.txt:
django-mercury-performance>=1.0.0Mercury adapts to your experience level. Choose one:
# First-time setup with educational features
mercury-test --profile student
# This creates mercury_config.toml with:
# - Interactive wizards
# - Performance tutorials
# - Helpful error messages
# - Learning quizzes# Fast, efficient testing
mercury-test --profile expert
# This creates mercury_config.toml with:
# - Minimal output
# - No interruptions
# - Fast execution
# - Direct control# For CI/CD and AI tools
mercury-test --profile agent
# This creates mercury_config.toml with:
# - JSON output
# - No interaction
# - Structured data
# - Machine-readableUse DjangoMercuryAPITestCase for automatic monitoring:
# tests/test_performance.py
from django_mercury import DjangoMercuryAPITestCase
class QuickPerformanceCheck(DjangoMercuryAPITestCase):
"""Mercury automatically monitors ALL tests in this class."""
def test_user_list_api(self):
# Just write normal tests - Mercury watches automatically!
response = self.client.get('/api/users/')
self.assertEqual(response.status_code, 200)
def test_user_detail_api(self):
response = self.client.get('/api/users/1/')
self.assertEqual(response.status_code, 200)Use DjangoPerformanceAPITestCase for manual control:
# tests/test_performance_advanced.py
from django_mercury import DjangoPerformanceAPITestCase
from django_mercury import monitor_django_view
class PrecisePerformanceTest(DjangoPerformanceAPITestCase):
"""Control exactly what and when to monitor."""
def test_search_performance(self):
# Monitor specific operations
with monitor_django_view("user_search") as monitor:
response = self.client.get('/api/users/search/?q=john')
# Make specific assertions
self.assertResponseTimeLess(monitor, 100) # Under 100ms
self.assertQueriesLess(monitor, 10) # Under 10 queries
self.assertNoNPlusOne(monitor) # No N+1 problems# Run all tests with your profile settings
mercury-test
# Run specific app tests
mercury-test users.tests
# Run with helpful search output (student profile)
mercury-test --show-search
# Run tests in parallel (expert profile)
mercury-test --parallel 4# Standard Django test runner
python manage.py test tests.test_performance
# With specific test class
python manage.py test tests.test_performance.QuickPerformanceCheckπ¨ MERCURY PERFORMANCE DASHBOARD
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π Test: test_user_list_api
β±οΈ Response Time: 156ms (Grade: C)
ποΈ Database Queries: 45 (Grade: D)
π§ Memory Usage: 23MB (Grade: B)
β οΈ ISSUES DETECTED:
ββ N+1 Query Pattern in UserSerializer
π‘ LEARNING OPPORTUNITY:
Your API is making 1 query to get users, then 1 query per user
to get their profile. With 44 users = 45 queries total!
π§ HOW TO FIX:
Add .select_related('profile') to your queryset:
User.objects.select_related('profile').all()
π Learn more: Run 'mercury-test --learn n1-queries'
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
test_user_list_api 156ms 45q 23MB β οΈ N+1 SLOW
test_user_detail_api 23ms 3q 12MB β
PASS
Summary: 1/2 performance issues. Run with --details for more.
{
"test": "test_user_list_api",
"metrics": {
"response_time_ms": 156,
"queries": 45,
"memory_mb": 23
},
"issues": [{
"type": "n_plus_one",
"location": "UserSerializer",
"severity": "high",
"auto_fixable": false
}],
"grade": "D"
}# Make sure you're in your Django project directory
cd /path/to/your/django/project
# Or use deep search
mercury-test --search-deep# Check your test discovery
mercury-test --wizard # Interactive test selection (student)
# Or specify directly
mercury-test app_name.tests# Ensure Mercury is installed in your virtual environment
pip show django-mercury-performance
# If not found, reinstall
pip install django-mercury-performance# Student profile: Get helpful tips
mercury-test # Will show performance tips after slow tests
# Expert profile: Run in parallel
mercury-test --parallel 4-
Explore Educational Mode
mercury-test --learn # Start interactive tutorials -
Use the Wizard
mercury-test --wizard # Guided test selection -
Read About Issues
-
Set Custom Thresholds
class MyTest(DjangoPerformanceAPITestCase): @classmethod def setUpClass(cls): super().setUpClass() cls.set_performance_thresholds({ 'response_time_ms': 50, 'query_count_max': 5, })
-
Integrate with CI/CD
-
Export Metrics
mercury-test --agent --format=json > metrics.json -
Integrate with Tools
class TestN1(DjangoMercuryAPITestCase):
def test_serializer_performance(self):
# Mercury will automatically detect N+1 patterns
response = self.client.get('/api/posts/')
# Check the report for N+1 warningsclass TestEndpoints(DjangoPerformanceAPITestCase):
def test_critical_endpoint(self):
with monitor_django_view("payment_api") as monitor:
response = self.client.post('/api/payment/', data={...})
# Ensure payment API is fast
self.assertResponseTimeLess(monitor, 200) # 200ms max# Compare performance between code versions
git checkout main
mercury-test --record-baseline
git checkout feature-branch
mercury-test --compare-baseline- π Documentation: Full Documentation
- π¬ Community: GitHub Discussions
- π Issues: Report Bugs
-
π Learn: Run
mercury-test --learnfor interactive tutorials
β
Installed Django Mercury
β
Chose your profile (student/expert/agent)
β
Created first test file
β
Ran tests successfully
β
Understood the output
Congratulations! You're now testing performance with Django Mercury! π
Next: Learn Workflow Best Practices or explore Educational Mode