{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner
architects,devops,tips and tricks,tools & methods,osx

Simulate Network Latency, Packet Loss, and Low Bandwidth on Mac OSX

Sometimes while testing you may want to be able to simulate network latency, or packet loss, or low bandwidth. I have done this with Linux and tc/netem as well as with Shunra on Windows, but I had never done it on Mac OSX.

It turns out that Mac OSX includes ‘dummynet’ from FreeBSD which has the capability to do this WAN simulation.

Here is a quick example:

  • Inject 250ms latency and 10% packet loss on connections between my workstation and my development web server (
  • Simulate maximum bandwidth of 1Mbps
# Create 2 pipes and assigned traffic to and from our webserver to each:
$ sudo  ipfw add pipe 1 ip from any to
$ sudo  ipfw add pipe 2 ip from 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

A quick test:

$ ping
PING ( 56 data bytes
64 bytes from icmp_seq=0 ttl=63 time=515.939 ms
64 bytes from icmp_seq=1 ttl=63 time=519.864 ms
64 bytes from icmp_seq=2 ttl=63 time=521.785 ms
Request timeout for icmp_seq 3
64 bytes from icmp_seq=4 ttl=63 time=524.461 ms


$sudo  ipfw list |grep pipe
  01900 pipe 1 ip from any to out
  02000 pipe 2 ip from to any in
$ sudo  ipfw delete 01900
$ sudo  ipfw delete 02000
# or, flush all ipfw rules, not just our pipes
$ sudo ipfw -q flush

Notice that the round-trip on the ping is ~500ms.  That is because we applied a 250ms latency to both pipes, incoming and outgoing traffic.

Our example was very simple, but you can get quite complex since “pipes” are applied to traffic using standard ipfw firewall rules.  For example, you could specify different latency based on port, host, network, etc.

Packet loss is configured with the “plr” command.  Valid values are 0 – 1.  In our example above we used 0.1 which equals 10% packetloss.

This is a very handy way for developers on Mac’s to test their applications in a variety of network environments.  And you get it for FREE.  On Windows you need to buy a commercial tool to achieve this (at least that was true the last time I looked, in 2008.)

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks