# Collecting SpeedTest results in Influx on Raspberry Pi

> How to set up regular SpeedTest reports into a local InfluxDB on a Raspberry Pi

Author: Simon Hearne (https://simonhearne.com/about/)
Published: 2020-01-07
Updated: 2020-05-01
Canonical: https://simonhearne.com/2020/pi-speedtest-influx/
Tags: homeautomation, raspberrypi

---
Now that you've [got your Pi set up with Influx & Grafana](/2020/pi-influx-grafana/) and you're [collecting some system stats](/2020/pi-metrics-influx/), it's time to measure network performance!

[SpeedTest.net](https://www.speedtest.net/) by Ookla is probably the most popular connection testing services, and they handily [provide a CLI](https://www.speedtest.net/apps/cli) to run tests programmatically. We'll create a simple python script which runs a test and sends the data to influxdb, then set it to run once every 15 minutes using Cron.

First, we need to install the `speedtest-cli` client:

```bash
sudo apt install -y python-pip
sudo pip install speedtest-cli influxdb
```

Then we can create a python script `rpi-speedtest-influx.py` in our home directory to run tests:

```python
#!/usr/bin/env python

import datetime
import speedtest
from influxdb import InfluxDBClient

# influx configuration - edit these
ifuser = "grafana"
ifpass = "<yourpassword>"
ifdb   = "home"
ifhost = "127.0.0.1"
ifport = 8086
measurement_name = "speedtest"

# take a timestamp for this measurement
time = datetime.datetime.utcnow()

# run a single-threaded speedtest using default server
s = speedtest.Speedtest()
s.get_best_server()
s.download(threads=1)
s.upload(threads=1)
res = s.results.dict()

# format the data as a single measurement for influx
body = [
    {
        "measurement": measurement_name,
        "time": time,
        "fields": {
            "download": res["download"],
            "upload": res["upload"],
            "ping": res["ping"]
        }
    }
]

# connect to influx
ifclient = InfluxDBClient(ifhost,ifport,ifuser,ifpass,ifdb)

# write the measurement
ifclient.write_points(body)
```

[View gist on GitHub](https://gist.github.com/simonhearne/eabf033bae8cee77972b9e96d2775eea#file-rpi-speedtest-influx-py)

We'll need to make it executable: `chmod +x rpi-speedtest-influx.py` and then we can run it to test it out: `./rpi-speedtest-influx.py`. You should be able to see the measurement in influx as soon as the script has exited, check by running `influx` CLI and execute the following to check your measurement (swapping database and measurement names for those you defined in your python script):

```sql
use home
select * from speedtest
```

If everything looks ok, we can add a cron job for our user (using `crontab -e`) to run this script at a regular interval, e.g. every 15 minutes: `*/15 * * * * /home/pi/rpi-speedtest-influx.py`. Any more than 15 minutes is probably unnecessary and may have an adverse effect on your perceived network quality.

You can now simply add a panel to your Grafana instance to show the download, upload and ping results over time. [Here is a sample dashboard](https://gist.github.com/simonhearne/1b2b62e818fe6e097f8f8b2cbd6f2365) you can import which includes the panel shown below:

![SpeedTest Grafana dashboard.](https://simonhearne.com/images/grafana-net-stats.png)

