Sometimes it’s useful to test your web application for situations where internet connections are less than stellar. It turns out that Mac OS X has a builtin utility called ipfw than, among other things, can do just this.

I first saw this technique from Joe Miller’s post on the subject. I packaged up the settings he mentioned into a little shell script:

#!/bin/bash
set -e
USAGE="Usage: $0 [ip_address | reset]"
if [ "$#" == "0" ]; then
echo "$USAGE"
exit 1
fi
ip=$1
if [ "$ip" = "reset" ]; then
printf 'Resetting... '
sudo ipfw list | grep 'pipe' | awk '{print $1}' | while read name; do sudo ipfw delete $name ; done
echo 'Done'
else
echo "Creating rules to simulate latency to $ip... "
# Create 2 pipes and assigned traffic to and from our webserver to each:
sudo ipfw add pipe 1 ip from any to $ip
sudo ipfw add pipe 2 ip from $ip to any
# Configure the pipes we just created:
sudo ipfw pipe 1 config delay 250ms bw 1Mbit/s plr 0.1
sudo ipfw pipe 2 config delay 250ms bw 1Mbit/s plr 0.1
echo "Done"
fi
view raw hinder.sh hosted with ❤ by GitHub

You can drop that somewhere in your $PATH and chmod +x to make it executable. You can call it whatever you want, but I called mine “hinder”. After that, it’s simply a matter of using it:

$ hinder www.google.com

Now when you visit google.com, you should see some marked slowness. To reset, just run:

$ hinder reset

Google is now fast again.

The script works by adding 250ms delay to both directions of network traffic. It also adds a packet-loss percentage of 10%. You can play with these numbers to get even more latency simulation. Enjoy!