Broadband Speed testing on Raspberry Pi’s

Since I first got cable broadband, I’ve been interested in testing the speed I am getting. Over the years this has gotten easier with tools like speedtest.net. The only issue with Speedtest.net was that it was designed for use in a browser.

However – the community came the rescue and a github repository popped up with “Speedtest-cli” an unlicensed open source Command Line Interface (CLI) that used scripts to allow you to run the speedtest from the command line on Linux machines.

This tool works fantastically and is good to go. With a Python wrapper you can even code a speed test into your applications. Indeed I did with my early Netapp tools. However since I got my first Raspberry Pi 4 board, I found this version lacking and it would not give good results. I am on Virgin Media’s 350Mb/s package and my wired machines show I get 350Mb/s. But Speedtest-cli was maxing out at 200Mb/s.

Whilst looking for a solution, I found that Ookla (the company behind Speedtest.net) had brought out an official CLI version of their tools.

You can find information about this tool on their Speedtest.net website: https://www.speedtest.net/apps/cli

Installing their tool is a little more complex than installing the unofficial version as they have opted to run their own package manager. However, it is not too complicated. Just follow their instructions.

speedtest
Running for the first time will ask you to accept the terms and conditions.

Just type in “YES” and press return/enter and the first speed test will run.

When you accept the agreement, the tool creates a json file under your home directory in “~/.config/ookla/” called “speedtest-cli.json”.

If you remove this file or directory – you will need to accept the terms and conditions and license again.

Running in Python 3

One thing missing from the official CLI is a python wrapper, so if you want to run a speedtest in your Python code, you will need to write you own.

I have written a very basic version for you to play with:

import subprocess
import json


stdoutdata = subprocess.getoutput("speedtest -f json")
#print(stdoutdata.split()[0])
results = json.loads(stdoutdata)
for key in results:
   download = results["download"]["bandwidth"] 
   upload = results["upload"]["bandwidth"]
   isp = results["isp"]

down = str(round(download / 125000, 2))
up = str(round(upload / 125000, 2))

print("Download: "+ down + "Mbps")
print("Upload: " + up + "Mbps")
print("Internet Provider: " + isp)

I hope you find all this helpful!

Find me on Facebook or Mastodon if you want to discuss it!