Opened 2 years ago

Last modified 2 days ago

#20461 assigned Cleanup/optimization

Support for running Django tests in parallel

Reported by: senko Owned by: aaugustin
Component: Testing framework Version: master
Severity: Normal Keywords:
Cc: cmawebsite@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Running the entire Django test suite is slow, causing people to skip running the entire test suite until just before they push/send a patch.

In theory, at least if one's using in-memory SQLite database, it should be possible to parallelize the tests and make the run faster. There are possibly other potential ways the tests can clobber each other (eg. allocating the same ports for LiveServerTestCases, using the same memcached key prefixes, etc...), but none seem insurmountable at a first thought.

Attachments (1) (3.1 KB) - added by senko 2 years ago.
Run django tests in parallel

Download all attachments as: .zip

Change History (15)

comment:1 Changed 2 years ago by senko

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement set
  • Status changed from new to assigned

I've got a proof-of-concept runner that splits the tests into N groups and runs the each group independently. In completely unscientific tests on my (dual core) laptop, I got ~3x speedups.

The runner is here:

It's not in a mergeable state yet, as it doesn't address any potential http port allocation or memcached prefix allocation problems. I also couldn't find a reliable way to mimic runtest's behaviour in getting the tests to run, so I think I actually run more tests (by default, if no labels are set) than runtests does by default.

If this entire excercise makes sense, at some point the functionality would probalby need to get moved to runtests itself (which would solve the "can't get exactly the test labels we need" problem).

comment:2 Changed 2 years ago by akaariai

Seems like a good idea to me (even if complete implementation might be hard). If we want to do this for other databases than in-memory sqlite we will need separate databases for each parallel process.

comment:3 Changed 2 years ago by akaariai

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 2 years ago by akaariai

I discussed this on #django-dev IRC channel, and it seems we do not want this into django repo. However, it would be excellent if you could write a standalone script (usable from $HOME/bin/ for example) by which you could run the in-memory tests in parallel. The script doesn't need to do more than what the current script does.

Having a fast way to run all tests, even if the output is ugly and it is usable only for in-memory sqlite would be a valuable addition.

comment:5 Changed 2 years ago by anonymous

Ahh, too bad, I just got the integration into working (with extra --workers=N param), avoiding the test label discovery problem I had earlier. Okay, I'll adapt it into the standalone script - actually that way it's easier to specify multiple settings files (needed if you use database other than the in-memory sqlite) without breaking the runtests usage.

I'll attached the updated script here.

Changed 2 years ago by senko

Run django tests in parallel

comment:6 Changed 2 years ago by senko

I've attached the script. It can be located anywhere as long as either the current working directory is django/tests or that it's passed via the --testdir argument.

Script usage:

~/bin/ --testdir=/path/to/django/tests --runners=<N> --settings=test_sqlite [test_labels ...]

All arguments except --testdir=<dir> and --runners=<N> are passed as-is to A good heuristic for the optimal number of runners is 2 * cpu cores you have (from the assumption that some of the tests are actually using the CPU and some are waiting for I/O at any given time - in any case, produces good results on my laptop :)

The script discovers all the tests that you want to run (or uses the test labels provided manually), splits them into N chunks, and starts N parallel runtests processes, one for each chunk of the labels. So it will not speed up a single test label execution (these usually don't last very long, anyways) Also, the test discovery in it sucks, as I try to mimic runtests behaviour but don't impleent the actual discovery logic (and can't reuse it easily from runtests), so it actually runs *more* tests than the default runtests (I've no idea why).

In case it matters to anyone: the script is licensed under the same terms as Django itself.

Is there a good place to put the script so it's more visible/useful to people, without them having to sift through Trac?

Also: I'm going to continue poking at runtests and trying to make it work in parallel for non-sqlite databases as well, to satisfy my own curiosity. I'd appreciate if this ticket could stay open for a while so I have a place to report my findings (if any).

comment:7 Changed 2 years ago by senko

I've updated my branch experimenting with runtests for parallel run. The modified runtests can run sqlite, mysql and postgres tests in parallel with no problems (these are all that I've tested). The only special thing needed is to make sure the test database name is different in each worker (which is easily done by using a randomized name in your test settings file).

The major downside remaining that I can see is that the test output is not nice (interspersed from N parallel workers), and could be unreadable if you have a lot of errors. In the usual case where you want to quickly run all the tests ("I'm done with this bit, let's check nothing else got broken"), at least for me it's not so much of a problem.

Code (still) at:

comment:8 Changed 7 months ago by aaugustin

That last link is a 404 these days. senko, do you have the latest code around?

comment:9 Changed 7 months ago by aaugustin

While Django is thread-safe in normal use, it isn't during tests because override_settings has process-wide effects.

That's why parallelizing tests requires processes, not threads. (Knowing this would have saved me some time.)

comment:10 Changed 7 months ago by aaugustin

  • Owner changed from senko to aaugustin

comment:11 Changed 7 months ago by collinanderson

  • Cc cmawebsite@… added

comment:12 Changed 7 months ago by aaugustin

  • Has patch set
  • Patch needs improvement unset

comment:13 Changed 6 months ago by timgraham

  • Patch needs improvement set

Still a work in progress as far as I can tell.

comment:14 Changed 2 days ago by aaugustin

  • Patch needs improvement unset
Note: See TracTickets for help on using tickets.
Back to Top