DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Iptables Basic Commands for Novice
  • Transforming Telecom With AI/ML: A Deep Dive Into Smart Networks
  • Use Apache Kafka SASL OAUTHBEARER With Python
  • How We Trained a Neural Network to Generate Shadows in a Photo: Part 3

Trending

  • Distributed Caching: Enhancing Performance in Modern Applications
  • ChatGPT Code Smell [Comic]
  • Securing Cloud Storage Access: Approach to Limiting Document Access Attempts
  • Secure Your API With JWT: Kong OpenID Connect
  1. DZone
  2. Data Engineering
  3. IoT
  4. Simulate Network Latency and Packet Drop In Linux

Simulate Network Latency and Packet Drop In Linux

In this article, we will understand how tc command in Linux can be used to simulate network slowness and how we can simulate packet corruption.

By 
Chandra Shekhar Pandey user avatar
Chandra Shekhar Pandey
·
Jan. 19, 23 · Code Snippet
Like (2)
Save
Tweet
Share
8.0K Views

Join the DZone community and get the full member experience.

Join For Free

There may be a scenario when you want to test an application when the network is slow(we also call it high network latency). Or you are reproducing a customer scenario(having high network latency) where some anomalous behavior is observed. In the Chrome browser, we can easily Simulate a slower network connection. This is very helpful when we are working with web applications. But we can also have non-web applications, like web-service applications, messaging brokers, etc. Also, the slowness simulated using Chrome dev tools is more from the client side. 

In this article, we will understand how tc command in Linux can be used to simulate network slowness and how we can simulate packet corruption. I have followed a couple of articles on the web, and after testing the commands mentioned in the articles, I am sharing my experiences.

1. Run a basic HTTP application using the python utility. Then we can check the response time using the curl or ab utility.

 
$ python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

# we can use curl with following syntax to get statistics for total response time.
$ curl -o /dev/null -s -w 'Establish Connection: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n'  http://localhost:8000/
Establish Connection: 0.000331s
TTFB: 0.002978s
Total: 0.003120s

# we can also use ab utility to understand statistics of a request. Here -n switch is used for number of request to be sent.
$ ab -n 1 http://localhost:8000/
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        SimpleHTTP/0.6
Server Hostname:        localhost
Server Port:            8000

Document Path:          /
Document Length:        2571 bytes

Concurrency Level:      1
Time taken for tests:   0.003 seconds
Complete requests:      1
Failed requests:        0
Total transferred:      2727 bytes
HTML transferred:       2571 bytes
Requests per second:    374.11 [#/sec] (mean)
Time per request:       2.673 [ms] (mean)
Time per request:       2.673 [ms] (mean, across all concurrent requests)
Transfer rate:          996.29 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     3    3   0.0      3       3
Waiting:        3    3   0.0      3       3
Total:          3    3   0.0      3       3


2. Now set a delay of two seconds and then again check the response time. Furthermore, we would see that the total response time is increased due to the delay we set.

 
#set a delay of 2 second for loopback interface.
$ sudo tc qdisc replace dev lo root netem delay 2000ms

# show the rule set
$ tc qdisc show  dev lo
qdisc netem 8001: root refcnt 2 limit 1000 delay 2s

# we can use curl(or ab) with following syntax to get statistics for total response time.
$ curl -o /dev/null -s -w 'Establish Connection: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n'  http://localhost:8000/
Establish Connection: 4.000354s
TTFB: 8.002722s
Total: 8.002833s
cpandey@cpandey:~$ ab -n 1 http://localhost:8000/
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        SimpleHTTP/0.6
Server Hostname:        localhost
Server Port:            8000

Document Path:          /
Document Length:        2571 bytes

Concurrency Level:      1
Time taken for tests:   8.003 seconds
Complete requests:      1
Failed requests:        0
Total transferred:      2727 bytes
HTML transferred:       2571 bytes
Requests per second:    0.12 [#/sec] (mean)
Time per request:       8002.822 [ms] (mean)
Time per request:       8002.822 [ms] (mean, across all concurrent requests)
Transfer rate:          0.33 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:     4000 4000   0.0   4000    4000
Processing:  4003 4003   0.0   4003    4003
Waiting:     4002 4002   0.0   4002    4002
Total:       8003 8003   0.0   8003    8003


3. Finally, delete the rule we had set earlier; this is important as we were simulating a slow network. This would slow down the complete interface and associated network traffic. Thus once we finish our testing, it is important to delete the rule which we have set.

 
$ sudo tc qdisc delete dev lo root


4. We can also simulate the corruption of packets as per the percentage set. As per my testing, I found that this also helps in simulating network latency. I observed that packets are being re-transmitted because TCP protocol ensures data is received correctly, no data is missing, and in order.

 
$ sudo tc qdisc replace dev lo root netem corrupt 50%


e TCP protocol ensures data is received correctly, no data is missing, and in order.

That's it; I hope you will find this article interesting and helpful. Thank you.

Linux (operating system) Network POST (HTTP) Python (language) Style sheet (web development)

Opinions expressed by DZone contributors are their own.

Related

  • Iptables Basic Commands for Novice
  • Transforming Telecom With AI/ML: A Deep Dive Into Smart Networks
  • Use Apache Kafka SASL OAUTHBEARER With Python
  • How We Trained a Neural Network to Generate Shadows in a Photo: Part 3

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: