Network performance
Bandwidth, transfer time calculations, what slows a network, and measuring latency with a ping.
Do this lesson in the simulatorWhy does a video buffer at home in the evening but not in the morning? Why is a game laggy on Wi-Fi but fine on a cable? This lesson looks at what makes a network fast or slow, how to calculate how long a file takes to send, and measures the delay on the robots' own radio.
Speed and bandwidth
Bandwidth is the most data a connection can carry each second, measured in bits per second: kilobits (kbps), megabits (Mbps) or gigabits (Gbps) per second. Notice it is bits, not bytes: a 100 Mbps connection carries 100 million bits a second, which is 12.5 megabytes.
Calculating transfer time
time = file size in bits ÷ transfer speed in bits per second
A 50 MB photo album over a 20 Mbps connection: 50 MB is 50 × 8 = 400 megabits, and 400 ÷ 20 = 20 seconds.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def transfer_seconds(size_mb, speed_mbps):
return size_mb * 8 / speed_mbps # megabytes to megabits, then divide by the speed
for name, size_mb in [("web page", 2), ("photo album", 50), ("film", 4000)]:
print(f"{name}: {transfer_seconds(size_mb, 20):.2f} s at 20 Mbps, {transfer_seconds(size_mb, 1000):.2f} s at 1 Gbps")
Read an exam question carefully to see whether sizes use 1000 or 1024 as the multiple; it will tell you.
What slows a network down?
| Factor | Why it matters |
|---|---|
| Bandwidth | a connection with more bandwidth carries more data each second |
| Number of users | everyone on a connection shares its bandwidth; in the evening, more people stream at once |
| Transmission media | fibre is faster than copper, and both usually beat wireless |
| Distance and interference | wireless signals weaken with distance and through walls, and nearby networks interfere; lost data must be sent again |
| Latency | the delay for data to get from one device to another and back. It matters most for games and video calls |
| Errors | damaged or lost data has to be re-sent, which takes more time |
Measuring latency
Latency is measured with a ping: send a tiny message and time how long the answer takes to come back. The Server robot on this mat answers ping with pong:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
start = clock()
send("ping")
reply = None
while reply is None and clock() - start < 2:
wait(0.02)
for sender, text in messages():
reply = text
print("reply:", reply, "after", round(clock() - start, 2), "seconds")
On a good network, a ping to a nearby server takes a few milliseconds. The Server on the mat is slower on purpose, so the delay is easy to see.
Task: ping the server
Ping the Server three times. For each ping, send ping, time how long until a reply arrives using clock(), and print ping <n>: <seconds> s, rounded to two decimal places. Then print average: <seconds> s, also rounded to two decimal places.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
Challenges
- How long does a 2 GB file take over a 50 Mbps connection? Over 1 Gbps?
- Ten people share a 100 Mbps connection equally. How long does each take to download a 250 MB update?
- Why does latency matter more for an online game than for downloading a film?