jacksonh.tumblr.com

Notes &

Using EC2 and ApacheBench to load test your web server

Usually when we think about EC2 we think about using its elasticity to create highly scalable sites. One other cool way we can take advantage of EC2’s elasticity is to create instances for load testing our site, regardless of where it is hosted.

EC2s pricing model can be fairly expensive for full time hosting. If you don’t have huge demands I’d recommend something like Linode.  However, EC2s instance hour billing model is perfect for load testing your site.  For just a few bucks you create 100 nodes and wreak havoc on your server for an hour.

Apache comes with a nice tool for havoc wreaking called ApacheBench.  You give ApacheBench a url and a number of connections to make and it measures how long each of those connections takes.  

A typical session looks something like this:

> ab2 -n 1000 http://google.com/

This is ApacheBench, Version 2.3 <$Revision: 655654 $>

Benchmarking google.com (be patient)…..done

Server Software:        gws

Server Hostname:        google.com

Server Port:            80

Document Path:          /

Document Length:        219 bytes

Concurrency Level:      1

Time taken for tests:   0.060 seconds

Complete requests:      1

Failed requests:        0

Write errors:           0

Non-2xx responses:      1

Total transferred:      511 bytes

HTML transferred:       219 bytes

Requests per second:    16.59 [#/sec] (mean)

Time per request:       60.274 [ms] (mean)

Time per request:       60.274 [ms] (mean, across all concurrent requests)

Transfer rate:          8.28 [Kbytes/sec] received

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:       21   21   0.0     21      21

Processing:    39   39   0.0     39      39

Waiting:       39   39   0.0     39      39

Total:         60   60   0.0     60      60

If you want to make things a little more difficult you can set the concurrency level for ApacheBench:

ab2 -n 1000 -c 10 http://www.google.com/

This will create 1000 connections, 10 at a time.

ApacheBench is a pretty nice way of testing, but your hardware quickly becomes a limiting factor.  As hard as I try, my 3 year old laptop really can’t do much damage to a lean mean web serving machine.

So to beef up my server assault I’ve been using EC2. Luckily I happen to be the author of The Worlds Greatest Managed Library for Interacting with Cloud Providers.

Yo Dawg

Using MCloud I can easily create a bunch of nodes, install ApacheBench on them and then have each node run ApacheBench. The meat and potatoes of the code looks like this:

var deployment = new RunCommand (“zypper -n —no-gpg-checks in apache2-utils”);

for (int i = 0; i < num_nodes; i++) {

    Node n = driver.CreateNode (“test-” + i, size, image, location, auth_create);

    n.Deploy (deployment, auth_deploy);

    nodes.Add (n);

}

var run_deployment = new RunCommand (“ab2 -n {0} -c {1} {2}”, num_requests, num_concurrent, url);

nodes.AsParallel ().ForAll (n => n.Deploy (run_deployment, auth_deploy));

And here is the full sample.

This is not meant to be a generic testing tool, its just a sample app showing you what can be done with ApacheBench and MCloud.  

For more info on Apache Bench:

  1. http://httpd.apache.org/docs/2.0/programs/ab.html
  2. http://www.petefreitag.com/item/689.cfm

For more info on MCloud:

  1. MCloud getting started guide.