{"id":1206,"date":"2012-04-11T10:26:03","date_gmt":"2012-04-11T16:26:03","guid":{"rendered":"http:\/\/thesmithfam.org\/blog\/?p=1206"},"modified":"2019-08-12T07:15:07","modified_gmt":"2019-08-12T13:15:07","slug":"throttling-your-network-connection-on-mac-os-x","status":"publish","type":"post","link":"https:\/\/thesmithfam.org\/blog\/2012\/04\/11\/throttling-your-network-connection-on-mac-os-x\/","title":{"rendered":"Throttling Your Network Connection on Mac OS X"},"content":{"rendered":"<p>Sometimes you just need to sloooooow doooooooown to test how your software behaves when your internet connection is crappy.<\/p>\n<p>Linux has <a href=\"http:\/\/linux-ip.net\/articles\/Traffic-Control-HOWTO\/\">tc<\/a> to do this, but what about Mac OS X?<\/p>\n<p>That&#8217;s where <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ipfirewall\">ipfw<\/a> comes in. It does a lot of stuff. I mean a <b>lot<\/b>, but we&#8217;re just going to use it to slow down our internet connection today.<\/p>\n<p>Here&#8217;s an example that throttles your web browsing experience to 50 KBytes\/second:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nsudo ipfw pipe 1 config bw 50KByte\/s &gt;\/dev\/null\r\nsudo ipfw add 1 pipe 1 src-port 80\r\nsudo ipfw add 1 pipe 1 dst-port 80\r\n<\/pre>\n<p>And to turn it off (this is an important step!):<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nsudo ipfw delete 1\r\n<\/pre>\n<p>To make this super easy to use, I wrote a handy little shell script called <b>network-throttle<\/b>, which you can put in your PATH and run like this:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nnetwork-throttle on --port 80 --rate 50KByte\/s\r\n<\/pre>\n<p>And to turn it off:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nnetwork-throttle off\r\n<\/pre>\n<p>You can download the shell script below. Put it in your PATH and name it <b>network-throttle<\/b>.<\/p>\n<p>Or, if you like things shiny, pointy, and clicky, you can use the <a href=\"http:\/\/stackoverflow.com\/questions\/9659382\/installing-apples-network-link-conditioner-tool\">Apple Network Link Conditioner<\/a> by installing X-Code.<\/p>\n<p><img decoding=\"async\" src=\"\/images\/network-link-conditioner-mac-osx.png\" \/><\/p>\n<p>Here&#8217;s the magical shell script:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n#\r\n# Throttles your Mac OS X internet connection on one port.\r\n# Handy for testing\r\n\r\nset -e\r\n\r\nRATE=15KByte\/s\r\nPORT=80\r\nPIPE_NUMBER=1\r\nACTION=\r\n\r\nfunction usage()\r\n{\r\n    echo $1\r\n    echo\r\n    echo &quot;Usage: `basename &quot;$0&quot;` &lt;action&gt; &#x5B;options]&quot;\r\n    echo &quot;  Action:&quot;\r\n    echo &quot;     on&quot;\r\n    echo &quot;     off&quot;\r\n    echo\r\n    echo &quot;  Options:&quot;\r\n    echo &quot;  --rate &lt;rate&gt;&quot;\r\n    echo &quot;      Example: --rate 100KByte\/s&quot;\r\n    echo &quot;  --port &lt;port&gt; (default is 80 if you don't specify --port)&quot;\r\n    echo &quot;      Example: --port 80&quot;\r\n    exit 1\r\n}\r\n\r\nfunction turn_throttling_off()\r\n{\r\n    echo &quot;Turning off network throttling&quot;\r\n    sudo ipfw delete $PIPE_NUMBER || echo &quot;Is it already turned off?&quot;\r\n}\r\n\r\nfunction turn_throttling_on()\r\n{\r\n    echo &quot;Throttling traffic to port $PORT: $RATE&quot;\r\n    sudo ipfw pipe $PIPE_NUMBER config bw $RATE &gt;\/dev\/null\r\n    sudo ipfw add $PIPE_NUMBER pipe $PIPE_NUMBER src-port $PORT &gt;\/dev\/null\r\n    sudo ipfw add $PIPE_NUMBER pipe $PIPE_NUMBER dst-port $PORT &gt;\/dev\/null\r\n}\r\n\r\n# Grab command line args:\r\nwhile &#x5B; -n &quot;$1&quot; ]; do\r\n  case $1 in\r\n    --rate)\r\n      shift\r\n      RATE=$1\r\n      ;;\r\n    --port)\r\n      shift\r\n      PORT=$1\r\n      ;;\r\n    *)\r\n      ACTION=$1\r\n  esac\r\n  shift\r\ndone\r\n\r\n&#x5B; -n &quot;$ACTION&quot; ] || usage &quot;Error: no action specified&quot;\r\n\r\ncase $ACTION in\r\n  on)\r\n    turn_throttling_off &gt;\/dev\/null 2&gt;&amp;1 # in case it's already on, clear out the old one\r\n    turn_throttling_on\r\n    ;;\r\n  off)\r\n    turn_throttling_off\r\n    ;;\r\n  *)\r\n    usage &quot;Error: Bad action specified&quot;\r\n    ;;\r\nesac\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you just need to sloooooow doooooooown to test how your software behaves when your internet connection is crappy. Linux has tc to do this, but what about Mac OS X? That&#8217;s where ipfw comes in. It does a lot of stuff. I mean a lot, but we&#8217;re just going to use it to slow [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1206","post","type-post","status-publish","format-standard","hentry","category-code-and-cruft"],"_links":{"self":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/1206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/comments?post=1206"}],"version-history":[{"count":17,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/1206\/revisions"}],"predecessor-version":[{"id":1504,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/1206\/revisions\/1504"}],"wp:attachment":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/media?parent=1206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/categories?post=1206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/tags?post=1206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}