Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Final touches to the preloading dependencies article

  • Loading branch information...
commit bdb95fec00d39366d40a0470e0b55406b61735e1 1 parent e25154d
@jeffkreeftmeijer authored
View
14 _posts/12-06-11-preloading-dependencies-for-faster-test-suite-start-up-times.markdown
@@ -5,9 +5,9 @@ excerpt: To speed up our start-up times between test runs, we'll write a really
published: true
---
-Tools like [Spin](https://github.com/jstorimer/spin) and [Spork](https://github.com/sporkrb/spork) help you speed up your test runs by preloading your dependencies and running your tests without reloading the whole stack for each run. That sounds like magic, but it's actually quite simple to build a tool that can do something like that. In this article, we'll write a dependency preloader in about twenty lines of code. Ready? Let's go!
+Tools like [Spin](https://github.com/jstorimer/spin) and [Spork](https://github.com/sporkrb/spork) help you speed up your test runs by preloading your application's dependencies and running your test suite without reloading the whole stack each time. That sounds like magic, but it's actually quite simple to build a tool that can do something like that. In this article, we'll write a dependency preloader in about twenty lines of Ruby. Ready? Let's go!
-To keep things understandable, We'll start out with a simple file named `test.rb`, that pretends to be a running test file by printing out a hundred dots.
+To keep things understandable, We'll start out with a simple file named `test.rb`, pretending to be a test file by printing out a hundred dots:
{% highlight ruby %}
require File.expand_path 'slow'
@@ -18,7 +18,7 @@ require File.expand_path 'slow'
end
{% endhighlight %}
-It requires a file named `slow.rb`, which will simulate your suite's startup time, caused by loading dependencies:
+It requires a file named `slow.rb`, which will simulate your suite's startup time:
{% highlight ruby %}
sleep 2
@@ -29,7 +29,7 @@ As you might have guessed, when you run `ruby test.rb`, it'll take two seconds b
### Forking
-In this case, let's pretend `slow.rb` is a dependency that doesn't change too often, like Rails or some other Rubygem you use in your application. That means it's safe to preload it and run the test suite with it a couple of times, instead of reloading it for every run. We can do that by requiring it once and forking the process every time we run our test suite. A simple example of that could look like this, which will run `test.rb` three times without reloading `slow.rb`:
+In this case, `slow.rb` is a dependency that doesn't change too often, like Rails or some other library your application depends on. That means it's safe to preload it and run the test suite with it a couple of times, instead of reloading it for each run. We can do that by requiring it once and forking the process every time we run our test suite. A simple example of that could look like this, which will run `test.rb` three times without reloading `slow.rb`:
{% highlight ruby %}
require File.expand_path 'slow'
@@ -44,11 +44,11 @@ end
{% endhighlight %}
<span class="small"><a href="https://gist.github.com/2909445/ec2f2245bb8b8ee411723f02eccaa0e9c1e65f30">https://gist.github.com/2909445/ec2f22…</a></span>
-After preloading `slow.rb` on the first line, we'll go into a loop which creates a subprocess using `fork`. In this subprocess, we'll require `test.rb`. Finally, we'll call `Process.wait`, which will halt to wait for the subprocess to exit. Because `slow.rb` is already required in the main process before forking off, it won't be loaded again by `test.rb` in the forked subprocesses.
+After preloading `slow.rb` on the first line, we'll go into a loop which creates a subprocess using `fork`. We'll require `test.rb` in this subprocess andinally, we'll call `Process.wait` in the main process, which will halt to wait for the subprocess to exit. Because `slow.rb` is already required in the main process before forking off, it won't be loaded again by `test.rb` in the forked subprocesses.
### Server
-On to the real thing. To make this usable as an actual tool, we need to turn it into a server that can receive messages and run tests when prompted to. To be able to talk between two Ruby processes, we'll use DRb. Let's create a file named `server.rb`:
+On to the real thing. To make this usable as an actual tool, we need to turn it into a server that can receive messages and run tests when prompted to. To be able to talk between two Ruby processes, we'll use [DRb](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/drb/rdoc/DRb.html). Let's create a file named `server.rb`:
{% highlight ruby %}
require 'drb'
@@ -97,4 +97,4 @@ As you'll see, your tests will start almost instantly.
Note: if you want to use your new preloader in a Rails application, you'll probably want to preload `config/application`, since that's the file that starts your application and tells Bundler to require your dependencies.
-See how easy it is to preload dependencies instead of loading them before every test run? Of course, this could use a lot of work, but this was just an attempt to show you a really simple way to ease the pain of your application's startup time when running your tests. If you're looking for something more polished, check out [Spin](https://github.com/jstorimer/spin), by [@jstorimer](http://twitter.com/jstorimer "Jesse Storimer").
+See how easy it is to preload dependencies instead of loading them before each test run? Of course, this could use a lot of work, but this was just an attempt to show you a really simple way to ease the pain of your application's startup time when running your tests. If you're looking for something more polished, check out [Spin](https://github.com/jstorimer/spin), by [@jstorimer](http://twitter.com/jstorimer "Jesse Storimer").
Please sign in to comment.
Something went wrong with that request. Please try again.