Applications can behave differently on a slow connection even when every request eventually succeeds. On Linux, tc can apply a fixed netem delay on a test interface so you can see how software responds.
A root netem queue discipline holds packets before they leave the interface. A ping sent through that interface encounters the delay on the outgoing leg, so its round-trip time rises by about the amount of delay you set.
The queue discipline lives in the running kernel. Removing the interface or rebooting clears the delay.
Use a sudo-enabled account on a Linux system with iproute2 and the iputils version of ping. The peer must answer pings across the link from another host or network namespace.
Related: How to connect network namespaces with veth pairs
Run each command in the network namespace that owns the sending interface, with no sudo for the queue and ping checks if your shell is already there. If you followed the veth setup, use veth-app in app in place of lab0 and ping the address of veth-db in db (192.0.2.2 in the linked setup), prefixing each command from the host with sudo ip netns exec app in place of any leading sudo.
Steps to simulate network latency with tc netem:
A root delay affects all packets leaving the interface, including traffic for remote sessions. Use a link dedicated to testing.
- Inspect the root queue discipline on your test interface, shown as
lab0throughout.$ tc qdisc show dev lab0 root qdisc noqueue 0: root refcnt 2
Continue only if the root queue discipline is
noqueue. A different root queue discipline may be handling traffic shaping you depend on. - Measure the baseline round-trip time through
lab0to your test peer, shown as192.0.2.2throughout.$ ping -n -q -c 3 -I lab0 192.0.2.2 ##### snipped ##### 3 packets transmitted, 3 received, 0% packet loss, time 2032ms rtt min/avg/max/mdev = 0.045/0.210/0.490/0.198 ms
The second number in
min/avg/max/mdevgives the average, here0.210 ms. If any probe gets no reply, fix connectivity and rerun the baseline before adding delay. - Add a fixed outbound delay on
lab0, using100msas the sample delay.$ sudo tc qdisc add dev lab0 root handle 1: netem delay 100ms
The handle
1:identifies the new queue discipline. To remove the delay after testing, usesudo tc qdisc del dev lab0 root handle 1: netemwhile that handle still identifies the queue discipline you added. - Confirm that the average round-trip time through
lab0to192.0.2.2rises by about the configured delay.$ ping -n -q -c 3 -I lab0 192.0.2.2 ##### snipped ##### 3 packets transmitted, 3 received, 0% packet loss, time 2008ms rtt min/avg/max/mdev = 102.999/103.772/104.517/0.620 ms
All three probes should get replies. With
100msof delay, the average should rise by about100 ms. Here it rose from0.210 msto103.772 ms.