The road to faster tests
37signals.com
37signals.com
While I've never implemented something like this, I imagine it would be straightforward for compiled languages if you have a good linker:
1) compile each unit test as a statically linked executable
2) make sure that each unit test outputs to an individual file; e.g. ./test_a >> test_results_a.xml
3) jigger your build process such that the linker removes dead and unused code from each test executable. Now each test executable contains code that is directly used by the test.
4) ensure that your build process only touches a statically linked test if a dependency has actually changed. This comes for free most of the time, but you may still have the linker touching files that it doesn't have to. You can remove these cases by staging the test files using a binary comparitor tool like rsync. If you have a crappy linker, or there's something in your test executables which is always changing (like a build stamp) you might need to compare using more advanced tools (like binutils) or something like Google's Courgette.
5) Run your tests only when the test is newer than the results--that is, when test_a is newer than test_a_result.xml. This is straightforward to implement using most build systems, like make, or using a test runner script.
Bam--now a test only runs if some dependent code has changed. If your test also uses data, you should list it as a dependency in your build system--either manually or by detecting open() calls at runtime via a shim over libc, or via strace. Your test run takes much less time, and, best of all, developers get a lot less spam to read through. This is perhaps a much greater benefit than increased speed.
Dynamic languages are a much harder nut to crack, as are integration tests that are loosely bound to the code.
I tried to do A for years. Most of the time you run the tests, but not always. And heaven help you if you are on a team. There will be someone who never runs tests. And you will end up having to schedule a chunk of time for regression fixing before every release. So to answer you question who cares about when file X uses file Y.
On the Java side, JUnit does exactly the same thing. However I usually see this manifest itself as out of memory errors rather than a time sink. Over the years I have gotten some VERY strange looks from clients when I null out instance variables in my tear down methods. Usually takes a couple of hours of explanation to gain some level of acceptance, if not understanding.
Note this is for the older 3.x versions of JUnit, I'm not 100% this is still true for the 4.x line.
Before: 914 seconds
After: 538 seconds
Awesome! Thanks to Jamis for sharing!You'd still want to run the full-suite when you commit, but it seems like a possible sanity check. Unless I'm overlooking something :)
(I mostly do unit testing, but also do some testing against APIs to ensure that I'm passing them data in the format they expect. One of my external service providers had downtime yesterday, and autotest caught it about six hours before they did. I wasn't successful in waking them up, but at least I was able to push a hotfix to AR to minimize inconvenience while I waited for their API to come back.)
[Edit: I might consider, if tests were taking that long, spinning up a fleet of VPSes to munch through them for me, maybe with a continuous integration server. Unit tests should be disgustingly parallel, since they're small units of work and shouldn't depend on each other. That means if one Macbook can execute the suite in 15 minutes then you should be able to have 30 Macbook-equivalents running either in the server room or the cloud munch through the same test suite in 30 seconds, give or take a little startup time.)
I'm running a Mac Pro with 8 cores, so there is a fair bit of parallelization I can do locally too. Unfortunately, the tests all depend on the database, and while I can certainly use tools like deep-test to spin up separate DB's for each worker, I've found that doing so adds a full 60 seconds to the test run. I fear that until we eliminate the database from (most of) our tests, super-fast runs will continue to elude us.
CI and distributed tests are good things, no question, but I'm still looking for ways to make it possible to run my tests locally in TDD-fashion. I'm far from out of ideas, it's just a matter of making time to experiment.
Most of my coding is done in minute loops (add test / watch tests fail / add couple of lines / watch tests succeed). YMMV.
Your CI server should be able to accept a patchset and run it without committing it. TeamCity does this (or roll your own CI server can do this too!).
Then there's hydra: https://github.com/ngauthier/hydra
And the excellent presentation on speeding up your tests with hydra that I can't find. It was one of those showoff presentations on GitHub somewhere...
It is a step by step guide with code showing how the guy reduced his suite's running time from half an hour to 15 seconds.
A lot of it is preventing so many db objects like in the OP, but it also discusses parallelizing the tests using hydra to make use of multicore. This ties into the db access thing as well though as to do parallell tests you have to make all the cases set up and tear down their own fixtures transactionally. Not doing this is a leading cause of slow tests anyway.
He even has metrics for stuff like running the whole suite off a ramdisk, the effects of SSDs, etc etc. He worked on this at his company for weeks/months apparently.
It's too hard to explain here, just watch that video if you do ANY ruby testing!
(Also, on our moderately large codebase it noticeably impaired the performance of my whole machine because of the way it constantly scanned the entire directory tree. I know there was some talk of adding edge triggering via kqueue or fsevents, but no one had done it at that time.)
Find autotest/discovery.rb for your project to see something like:
Autotest.add_discovery { "Rspec2" }
Autotest.add_discovery { "Rails" }
# both of the frameworks have their predefined rulesets
No one prevents you from adding your own discovery rules.Also you're looking for autotest-fsevent if you're using a mac. Probably something similar exists for linux.
One nice side-effect of this support is that it also gave us code coverage deltas from the last build, so the system would send out embarrassing numbers with your commit if you'd dropped the overall coverage of the module.
There's more to it than simply checking each line with a coverage tool--you also need to rerun tests when any implicitly referenced global data, strings, input files, command line parameters, etc. change, including calls to third party libraries for which the source is not available. Usually a build system and compiler can track some of these dependencies already, so you may be able to leverage those.
As long as you occasionally run the full test suite, though, you may not care to cover these cases for your incremental tests.