How To Use ApacheBench To Do Load Testing on an Arch Linux VPS
Load testing is a good idea before any deployment. Although it's not something to be skimped out on, sometimes it's nice to quickly establish a best-case scenario for a project before running more detailed tests down the road.
The ApacheBench tool (ab) can load test servers by sending an arbitrary number of concurrent requests. Although ab was designed for testing Apache installations, it can be used to benchmark any HTTP server.
In this tutorial, we will see how a Ruby interpreter with different servers performs under load. The tutorial steps assume a fresh Arch Linux x86_64 image. The results were obtained from a 512 MB droplet.
Refresh the package database.
Install the apache package to get access to ApacheBench. Alternatively, it is contained in the apache-tools package in the AUR.
pacman -S apache
Limited Privilege User
Next, create the user that will manage Ruby. It's not a good idea to run some of the commands in the next section as root.
useradd -m -d /home/test test
Switch to the new user.
The Ruby Version Manager makes it easy to work with different Ruby environments. It takes care of the process of installing specific Ruby versions and isolating gemsets. It is currently installed by running a bash script from their website.
\curl -L https://get.rvm.io | bash -s stable
In order to use the rvm command, you need to first run the rvm script.
Next, install Ruby 2.0.0. RVM generally builds Ruby from source, so this step may take a while.
rvm install 2.0.0
Switch to the new Ruby. This might happen by default after the installation, but checking doesn't hurt.
rvm use 2.0.0
Now that Ruby is installed, you can create a simple site and see how many requests it can handle.
Install Sinatra. It's a microframework/DSL for creating Ruby web applications. The --no-* flags skip the documentation.
gem install sinatra --no-rdoc --no-ri
Create the sample sinatra app which just echoes "hello world".
cd ~ vi app.rb # app.rb require 'sinatra' get '/' do 'hello world' end
Run the server.
With the server finally up, you can start load testing. A call to ab looks like this:
ab -n <num_requests> -c <concurrency> <addr>:<port><path>
Open another terminal and ssh into the server again. Run a test with ApacheBench. I used 1000 requests with a concurrency of 100. Don't forget the final '/' at the end for the path.
ab -n 1000 -c 100 http://localhost:4567/ Server Software: WEBrick/1.3.1 Server Hostname: 0.0.0.0 Server Port: 4567 Document Path: / Document Length: 11 bytes Concurrency Level: 100 Time taken for tests: 2.950 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 288000 bytes HTML transferred: 11000 bytes Requests per second: 338.94 [#/sec] (mean) Time per request: 295.041 [ms] (mean) Time per request: 2.950 [ms] (mean, across all concurrent requests) Transfer rate: 95.33 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 1 2.8 0 15 Processing: 117 285 94.3 268 553 Waiting: 70 248 91.8 234 544 Total: 117 286 93.6 271 553 Percentage of the requests served within a certain time (ms) 50% 271 66% 327 75% 354 80% 361 90% 413 95% 468 98% 512 99% 539 100% 553 (longest request)
My results converged around 300 requests/second. WEBrick is not known for its speed. Go ahead and interrupt the server with Ctrl-c.
Thin is a popular ruby web server that uses Mongrel for parsing and EventMachine for non-blocking IO. Install Thin and run the server again. Sinatra should load Thin automatically and let you know ("...with backup from Thin").
gem install thin ruby app.rb
Now, try the load test again. It should be a bit faster this time.
Note: Thin does not appear to allow ApacheBench connections via localhost, but it allows 0.0.0.0 or 127.0.0.1.
ab -n 1000 -c 100 http://0.0.0.0:4567/ ... Concurrency Level: 100 Time taken for tests: 0.989 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 244000 bytes HTML transferred: 11000 bytes Requests per second: 1011.13 [#/sec] (mean) Time per request: 98.899 [ms] (mean) Time per request: 0.989 [ms] (mean, across all concurrent requests) Transfer rate: 240.93 [Kbytes/sec] received ...
At least in this case, it looks like Thin makes for a notably faster server than WEBrick at over 1000 requests/second (You can try raising the total requests, but it didn't get much higher for me). Not bad.
Obviously, these results do not reflect realistic server performance. HTTP is just one piece of the puzzle. A slow templating engine and/or database will drag these numbers down significantly. Still, it gives you a quick ballpark figure for comparison.
Other performance tools you might be interested in: