Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure the time profile of each django test?

I'd like to measure the (wall?) time each individual test case takes to run.

I suppose wrapping the test_runner in a timeit will do the job, but before I dive down that rabbit hole perhaps there is a smarter way to do it?

This already gives me a cProfile to pore over, but nothing really jumps out as horrendously bad. I'm thinking perhaps my time can be focused on the ones that run the longest.

time python -m cProfile -o keep-p4-serialize.profile manage.py test -v 3 -k --parallel 4

eg:

test_dependencies (api.tests.TestMetricClasses) ... ok (4.003s)
test_metrics (api.tests.TestMetricClasses) ... ok (8.329s)
test_parameters (api.tests.TestMetricClasses) ... ok (0.001s)
like image 212
John Mee Avatar asked Sep 28 '16 05:09

John Mee


1 Answers

Update:
Original post was in 2019 and it seems that the package discussed below (django-slowtest) is no longer maintained. As for now I don't see any maintained forks either.

--

Old question but I came across it and noticed that django-slowtest is not mentioned, which I think is absolutely worth mentioning. I've been using it for quite a while now and I'm very satisfied with the results. Only downside, it does not yet state that it supports Django >= 2 officially, but I have not encountered any issues and since it is in development only for me I feel safe using it.

  • works properly in --parallel
  • is configurable in how many slow tests you want to show after running tests
  • is configurable in threshold when it should mark a tests as slow
  • has report (export) functionality.
  • still maintained as of 2019-02

https://github.com/realpython/django-slow-tests

from docs: Install:

$ pip install django-slowtests

Add the following settings:

TEST_RUNNER = 'django_slowtests.testrunner.DiscoverSlowestTestsRunner'
NUM_SLOW_TESTS = 10

# (Optional)
SLOW_TEST_THRESHOLD_MS = 200  # Only show tests slower than 200ms

# (Optional)
ALWAYS_GENERATE_SLOW_REPORT = False  # Generate report only when requested using --slowreport flag

finally, run your tests

$ python manage.py test
Creating test database for alias 'default'...
..........
----------------------------------------------------------------------
Ran 10 tests in 0.413s

OK
Destroying test database for alias 'default'...

Ten slowest tests:
0.3597s test_detail_view_with_a_future_poll (polls.tests.PollIndexDetailTests)
0.0284s test_detail_view_with_a_past_poll (polls.tests.PollIndexDetailTests)
0.0068s test_index_view_with_a_future_poll (polls.tests.PollViewTests)
0.0047s test_index_view_with_a_past_poll (polls.tests.PollViewTests)
0.0045s test_index_view_with_two_past_polls (polls.tests.PollViewTests)
0.0041s test_index_view_with_future_poll_and_past_poll (polls.tests.PollViewTests)
0.0036s test_index_view_with_no_polls (polls.tests.PollViewTests)
0.0003s test_was_published_recently_with_future_poll (polls.tests.PollMethodTests)
0.0002s test_was_published_recently_with_recent_poll (polls.tests.PollMethodTests)
0.0002s test_was_published_recently_with_old_poll (polls.tests.PollMethodTests)
like image 143
Nrzonline Avatar answered Sep 29 '22 14:09

Nrzonline