You develop an application, it starts growing, but at the same time your expanding userbase might slow the app down. So what can you do about it? Well, it’s time for load testing! I'll focus on the one of the easiest tools - Apache Bench.
You develop an application, it starts growing and growing, and you feel pretty good about it. But as it keeps growing, your userbase might not feel quite as good about it as you do - it’s infuriating to use a laggy app or website. So what can we do about it? Well, it’s time for load testing! We’ll focus on the one of the easiest tools - Apache Bench.
Let’s get started
The tool was originally intended for testing Apache HTTP servers, nevertheless it does a good job of testing any web server. It generates a flood of queries to the given URL and returns the results. You don’t have to install any additional plugins because ab is already on your OS X or Linux system. The only thing we have to do is run it in the console, for example:
$ ab -n 1000 -c 20 http://yoursite.com/
This benchmark hits the address 1000 times (-n option), making 20 concurrent requests (-c option) - basically the number of users making requests at the same time.
What do the results mean?
Let’s try testing the netguru site.
$ ab -n 500 -c 20 https://netguru.co/
As you can see I’ve made 500 requests with a concurrency level of 20. You might have expected all the requests to be completed with status code 200 (meaning that everything’s okay), but there were over 40 failed requests! Don’t worry - these failed requests don’t mean that there isn’t a status code 200, but rather that the response time across requests was different.
You may also notice two values of time per request. The first one indicates how much time was needed to complete one request. For users, this translates into how much time it took to load the whole page. The second value (across all concurrent requests) means how much the total time would increase if we added one extra request (-n 501) with the same concurrency level (at least, in theory). Moreover, there’s a couple of connection times possible for the requests.
Authentication? No problem!
You can also use Apache Bench to benchmark a website where the authentication is based on session. All you need is to get a cookie value before the test. For Rails applications, the session name will be called yourapplication_session. When you have the cookie, you can use:
$ ab -n 1000 -c 20 -C session_name=cookie_value http://yoursite.com/admin
What can I do with the data?
It’s worth observing how your application behaves as it gets bigger or with a sudden influx of visitors. With this data, you can consider how to improve your application’s performance or even check the traffic while benchmarking using New Relic.
If your dev soul is interested in more useful content like this, sign up for Ruby Brief, our Ruby on Rails-oriented monthly newsletter.