Skip to content

Instantly share code, notes, and snippets.

@s1037989
Created June 27, 2025 12:08
Show Gist options
  • Save s1037989/44eb6e1c87e77e7904f0ffac9c9da709 to your computer and use it in GitHub Desktop.
Save s1037989/44eb6e1c87e77e7904f0ffac9c9da709 to your computer and use it in GitHub Desktop.
tc netem - Network Emulator is an enhancement of the Linux traffic control facilities that allow to add delay, packet loss, duplication and more other characteristics to packets out‐ going from a selected network interface.
// tc - traffic control
// netem - network emulator
// Start a web server that takes time to do something and respond with the Server-Timing HTTP header
$ perl -Mojo -E 'a("/" => sub ($c) { $c->timing->begin("routed"); $c->render_later; Mojo::IOLoop->timer(2 => sub { $c->timing->server_timing("routed", "Routed", $c->timing->elapsed("routed")); $c->render(text => "", status => 204); }) })->start' daemon
Web application available at http://127.0.0.1:3000
// Delete artificial 50ms roundtrip delay
$ sudo tc qdisc delete dev lo root netem delay 25ms
// Ping loopback
$ ping -4 -c 5 -D -I lo -n -r -U 127.0.0.1
PING 127.0.0.1 (127.0.0.1) from 127.0.0.1 lo: 56(84) bytes of data.
[1751024810.554486] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.029 ms
[1751024811.575270] 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.042 ms
[1751024812.599372] 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.054 ms
[1751024813.623361] 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.053 ms
[1751024814.648274] 64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.052 ms
--- 127.0.0.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4094ms
rtt min/avg/max/mdev = 0.029/0.046/0.054/0.009 ms
// Typical latency on a loopback
// Fetch the webpage and display Server-Timing and the Client Benchmark
$ perl -Mojo -E 'n { say g("http://127.0.0.1:3000")->headers->header("Server-Timing") }'
routed;desc="Routed";dur=2.000657
2.00809 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU)
// Typical latency/response time added to the 2s webapp
// Add artificial 50ms roundtrip delay
$ sudo tc qdisc add dev lo root netem delay 25ms
// Fetch the webpage and display Server-Timing and the Client Benchmark
$ ping -4 -c 5 -D -I lo -n -r -U 127.0.0.1
PING 127.0.0.1 (127.0.0.1) from 127.0.0.1 lo: 56(84) bytes of data.
[1751025629.387497] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=50.2 ms
[1751025630.388667] 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=50.1 ms
[1751025631.389869] 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=50.2 ms
[1751025632.390430] 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=50.1 ms
[1751025633.391322] 64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=50.1 ms
--- 127.0.0.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4004ms
rtt min/avg/max/mdev = 50.088/50.126/50.159/0.028 ms
// 50ms latency!!
// Fetch the webpage and display Server-Timing and the Client Benchmark
$ perl -Mojo -E 'n { say g("http://127.0.0.1:3000")->headers->header("Server-Timing") }'
routed;desc="Routed";dur=2.000952
2.10641 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU)
// 50 ms latency on all packets!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment