This article is the 13th day article of JSL \ (Nippon System Giken ) Advent Calendar 2019.
Recently, I felt that there was a timing when the WiFi in the company slowed down, but I did not know quantitatively how slow it was, so I wrote a script to run a speed test regularly.
-I wrote a program in Python to measure the speed of in-house WiFi using Speedtest CLI -Internet connection measurement for developers. --It is convenient to be able to execute a speed test from CUI
First of all, I wanted to run it regularly with cron etc., so I thought about running a speed test with CUI
In my image, the speed test was measured as follows: search → execute….
I tried to organize the means to measure the network line speed on Linux | Developers \ .IO ) It seems that speedtest.net
provides the CLI when I investigated while looking at the article etc.
The Speedtest CLI also offers a Python package,
--It is possible to install binary with pip --It seems that you can use the speedtest command in a virtual environment without managing it with brew. --It seems that it can be imported on Python
I found out
I built an environment using pipenv
$ mkdir wifi_speedtest
$ cd $_
#If pipenv is not installed
# brew install pipenv
#Create virtual environment
$ pipenv --python 3.7
#Package installation
$ pipenv install speedtest-cli
#Enable virtual environment
$ pipenv shell
#test the speedtest command
(.venv) $ speedtest --version
speedtest-cli 2.1.2
Python 3.7.4 (default, Oct 12 2019, 18:55:28) [Clang 11.0.0 (clang-1100.0.33.8)]
You can get a list of connection destination servers provided by volunteers with speedtest --list
, so decide on one and make a note of the number.
(.venv) $ speedtest --list | grep Tokyo
15047) OPEN Project (via 20G SINET) (Tokyo, Japan) [6.34 km]
24333) Rakuten Mobile , Inc (Tokyo, Japan) [6.34 km]
28910) fdcservers.net (Tokyo, Japan) [6.34 km]
18516) GIAM PING VIETPN.COM (Tokyo, Japan) [6.34 km]
22247) Tokyonet (Castro, Brazil) [18486.74 km]
If you specify as follows, the speed test will always be executed on the specified server. For example, for a server provided by Rakuten Mobile, Inc
, the number would be 24333
.
#Run speed test
(.venv) $ speedtest --server 24333
Retrieving speedtest.net configuration...
Testing from XXX (xx.xx.xx.xx)...
Retrieving speedtest.net server list...
Retrieving information for the selected server...
Hosted by Rakuten Mobile , Inc (Tokyo) [6.34 km]: 200.123 ms
Testing download speed................................................................................
Download: 16.08 Mbit/s
Testing upload speed......................................................................................................
Upload: 31.96 Mbit/s
I was able to measure safely (some client information is hidden)
However, this is difficult to handle in the program, so I will add --json
and get the result in json format
(.venv) $ speedtest --server 24333 --json
result
{"download": 20208058.464686207, "upload": 54426180.687909536, "ping": 48.215, "server": {"url": "http://ookla.mbspeed.net:8080/speedtest/upload.php", "lat": "35.6833", "lon": "139.6833", "name": "Tokyo", "country": "Japan", "cc": "JP", "sponsor": "Rakuten Mobile , Inc", "id": "24333", "host": "ookla.mbspeed.net:8080", "d": 6.336536019993832, "latency": 48.215}, "timestamp": "2019-12-23T07:23:16.316637Z", "bytes_sent": 68534272, "bytes_received": 25353712, "share": null, "client": {"ip": "xx.xx.xx.xx", "lat": "xx.xxxx", "lon": "xxx.xxxx", "isp": "XXX", "isprating": "3.7", "rating": "0", "ispdlavg": "0", "ispulavg": "0", "loggedin": "0", "country": "JP"}}
Consider dealing with this on Python
Since it is provided as a Python Package, I thought about executing a speed test in Python in a pluggable manner, but this time I thought about handling the execution result in CUI as it is, so speedtest with subprocess module
Decided to run the command
subprocess ---subprocess management — Python 3 \ .8 \ .1 documentation
wifi_speedtest.py
def get_speedtest_result():
process = subprocess.run(['speedtest', '--server', '24333', '--json'], capture_output=True)
return json.loads(process.stdout)
def bit_to_mbit(bit):
"""
Making a feeling that does not care about errors
"""
return bit / 1024 / 1024
result = get_speedtest_result()
print(bit_to_mbit(result["download"]), bit_to_mbit(result["upload"]))
result
13.46394215 50.48002296
There are several ways to execute a command with subprocess
, but this time I got the standard output withrun ()
+ capture_output = True
, which is officially mentioned in the usage example.
This time, I hit the API of SpreadSheet to record it (details are omitted below).
-Edit Google Spreadsheets in Python -Qiita
I think it's a good idea to skip to Slack and record it in any way you like.
As an aside, using Pipenv's scripts was convenient because it was not necessary to enable the virtual environment when executing from cron (processes executed via pipenv run are treated the same as execution in virtual environment).
Pipfile
[scripts]
dev = 'python wifi_speedtest.py'
Recommended Posts