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

In addition, add a random packet loss. 500ms latency with 2.5% packet loss:

tc qdisc add dev lo root handle 1:0 netem delay 250msec loss 2.5%


15 thoughts on “Add latency to localhost”

  1. 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. @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!

  3. Instead of making fun of Ted why not simply saying that you do not know… 😉

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

  4. 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.

  5. @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.

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

  7. @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? 🙂

Comments are closed.