Add latency to localhost

Pádraig Brady taught me a great trick in a comment to a previous blog post and it was so neat I feel a need to highlight it further as it also makes it easier for me to find it again later!

To simulate a far away server, add RTT time to the localhost device. For example if we add 100 milliseconds (which then makes 200ms ping time to localhost):

$ tc qdisc add dev lo root handle 1:0 netem delay 100msec
Restore it back to normal again with:
$ tc qdisc del dev lo root
tc qdisc add dev lo root handle 1:0 netem delay 100msec

Restore it back to normal again with:

tc qdisc del dev lo root

15 Responses to “Add latency to localhost”

  1. urssur Says:

    That´s an interesting approach , but why not host the site on a local server , as in local in the country / town / at least continent ?

    I get the benefits of simulating lag, but we should aim to reduce it not accept it :D.

    Best of luck !

  2. Ted Says:

    How do you add latency to localhost on Windows?

  3. Eric Ryan Harrison Says:

    Nice post. I created a simple bash script to make this a little more flexible and make it so that I didn’t have to remember the syntax since I’ll probably only need to use this every once in a while.

    https://gist.github.com/740829

    Thanks again.

  4. Jeff Roberts Says:

    Cool trick. Thanks for highlighting it :)

  5. daniel Says:

    @urssur: I assume you didn’t actually read the blog post I made the other day about SFTP transfers over high latency connections? It’s a bless being able to simulate such connections on your own systems instead of having to dig up a system that actually features such a latency! I don’t like high latencies, but when I write network protocols for use in the real world…

    @ted: everything in Windows is a pain so I expect this is too…

    @Eric: oh, nice indeed. I may very well use exactly that here too. Thanks!

  6. dude Says:

    @Ted: Windows adds plenty of latency all on it’s own, dude.

  7. Pete Says:

    In reply to Ted’s Wndows question.. “Use IIS”.

  8. iii Says:

    Instead of making fun of Ted why not simply saying that you do not know… ;)

    *nix, open source, windows, Apple, iphone, … ayatollahs are boring…

  9. HGP Says:

    RE: Windows. Very true. On Vista there are significant differences between ‘localhost’ and ‘127.0.0.1′ This was improved in 7 but the internal resolution of localhost is still not as fast as it was in XP.

  10. daniel Says:

    Let me also mention that I in fact blogged about Window’s amazingly slow localhost a good while ago…

  11. Josh Says:

    On Windows (for HTTP/S connections) I use Charles debugger proxy which has a latency setting.

  12. daniel Says:

    @Josh: that’s indeed one way to accomplish something that in many cases is similar, yes. At least if you’re dealing with something HTTP like.

    But a HTTP/HTTPS proxy is actually not what I want for my case. I debugged a SSH/SFTP implementation and the last thing I want then is to have it first go through a proxy.

  13. Sampath Says:

    @Eric: zero value (0) fits better to be used (instead of “off”) as an option to disable.

  14. Vladimir Says:

    @Ted: for HTTP requests you also can use Fiddler on Windows.
    But if you need to perform more specific tasks (like in Daniel’s case) you can try LANforge ICE Network Emulator (www.candelatech.com). But it’s not free. Probably it provides functionality you need in trial version.
    (Or install Linux? :-)

  15. Tor Says:

    Very interesting. One thing would be good to know: does it reset on a reboot?