Saturday, February 20, 2010

Slowing Things Down Under OS X

You know what they say "you need to slow down to see some certain things". In case of web-development, sometimes you need to emulate a realistically slow internet connection to see some problems with your project. See how quickly it loads up, how it receives the images, especially it useful to test file-uploads and ajax operations. So here is a little tip how can slow down requests to your local server to test the things properly.

Under OS X you have access to the FreeBSD traffic shaping util ipfw. First of all you need to define some rule which will work on local interactions.
sudo ipfw add 1 pipe 1 src-ip
sudo ipfw add 1 pipe 2 dst-ip

After that we can set up the connection speed, and also I like to add some delay to emulate a request to a remote server
sudo ipfw pipe 1 config bw 40Byte/s delay 100ms

After that all your local requests will slow down to 40Byte/s speed with a realistic 100ms response delay.

When you have done with your tests, you can remove the rule with the following command.
sudo ipfw delete 1

Now it is all interesting but lets wrap it up in a simple shell script so we didn't need to memorize all the fancy calls.
sudo touch /usr/bin/slowdown
sudo chmod +x /usr/bin/slowdown

After that open the file with your favorite editor and write the following little script


if [ $1 = "clear" ] ; then

ipfw delete 1


ipfw add 1 pipe 1 src-ip
ipfw add 1 pipe 2 dst-ip
ipfw pipe 1 config bw $1KByte/s delay 100ms


Now you can create and remove pipes just like that
sudo slowdown 40

And to remove the rule call it like that
sudo slowdown clear

That's all about it. Happy testing!

No comments: