Speedtestを自動化して回線速度を監視したい

TL;DR 見出しにジャンプ
COVID-19対策のリモートワークや小中高生による日中の家庭用回線のトラフィック増加により、マトモにNetflixも観られないため、在宅勤務(自宅警備)のプロである私はフレックスタイム制(昼夜逆転)の導入に踏み切ることにした。
QoLを最大限に高めるため、最も適切な起床・就寝時間を割り出したく、スピードテストを自動化する。
Speedtest CLI 見出しにジャンプ
回線速度を測定したいなら、一定サイズのデータのアップロード・ダウンロードをすれば良い。
とはいえ、speedtest.netをCLIで使えるSpeedtest CLIなるものが公開されていたので使うことにした。
macOS、Windows、Linux、FreeBSDに対応している。
各OS用のダウンロード・インストール方法は公式サイトに書かれている通り。
https://www.speedtest.net/ja/apps/cli
今回はWindowsで行う。
使い方 見出しにジャンプ
ダウンロードしたzipを解凍して任意の場所に配置し、環境変数Pathを設定するか、カレントディレクトリで操作する。
例: "C:\bin\ookla-speedtest-1.0.0-win64\speedtest.exe"
speedtest -h
でヘルプが見られる。付属のMarkdownも参照。
.\speedtest -h
Speedtest by Ookla is the official command line client for testing the speed and performance of your internet connection.
Version: speedtest 1.0.0.2
Usage: speedtest [<options>]
-h, --help Print usage information
-V, --version Print version number
-L, --servers List nearest servers
-s, --server-id=# Specify a server from the server list using its id
-I, --interface=ARG Attempt to bind to the specified interface when connecting to servers
-i, --ip=ARG Attempt to bind to the specified IP address when connecting to servers
-o, --host=ARG Specify a server, from the server list, using its host's fully qualified domain name
-p, --progress=yes|no Enable or disable progress bar (Note: only available for 'human-readable'
or 'json' and defaults to yes when interactive)
-P, --precision=# Number of decimals to use (0-8, default=2)
-f, --format=ARG Output format (see below for valid formats)
-u, --unit[=ARG] Output unit for displaying speeds (Note: this is only applicable
for 窶鷲uman-readable窶・output format and the default unit is Mbps)
-a Shortcut for [-u auto-decimal-bits]
-A Shortcut for [-u auto-decimal-bytes]
-b Shortcut for [-u auto-binary-bits]
-B Shortcut for [-u auto-binary-bytes]
--selection-details Show server selection details
-v Logging verbosity. Specify multiple times for higher verbosity
--output-header Show output header for CSV and TSV formats
Valid output formats: human-readable (default), csv, tsv, json, jsonl, json-pretty
Machine readable formats (csv, tsv, json, jsonl, json-pretty) use bytes as the unit of measure with max precision
Valid units for [-u] flag:
Decimal prefix, bits per second: bps, kbps, Mbps, Gbps
Decimal prefix, bytes per second: B/s, kB/s, MB/s, GB/s
Binary prefix, bits per second: kibps, Mibps, Gibps
Binary prefix, bytes per second: kiB/s, MiB/s, GiB/s
Auto-scaled prefix: auto-binary-bits, auto-binary-bytes, auto-decimal-bits, auto-decimal-bytes
speedtest
コマンドだけでスピードテストが実行できる
.\speedtest
==============================================================================
You may only use this Speedtest software and information generated
from it for personal, non-commercial use, through a command line
interface on a personal computer. Your use of this software is subject
to the End User License Agreement, Terms of Use and Privacy Policy at
these URLs:
https://www.speedtest.net/about/eula
https://www.speedtest.net/about/terms
https://www.speedtest.net/about/privacy
==============================================================================
Do you accept the license? [type YES to accept]: YES
License acceptance recorded. Continuing.
Speedtest by Ookla
Server: IPA CyberLab - Bunkyo (id = 14623)
ISP: Open Computer Network
Latency: 13.41 ms (1.74 ms jitter)
Download: 32.84 Mbps (data used: 50.2 MB)
Upload: 42.81 Mbps (data used: 46.2 MB)
Packet Loss: 0.0%
Result URL: https://www.speedtest.net/result/c/99593e05-50d1-4c0c-890a-ee8e4eb95808
speedtest --servers
でスピードテストサーバ一覧が出る。
.\speedtest --servers
Closest servers:
ID Name Location Country
==============================================================================
7139 SoftEther Corporation Tsukuba Japan
14623 IPA CyberLab Bunkyo Japan
20976 GLBB Japan Tokyo Japan
15047 OPEN Project (via 20G SINET) Tokyo Japan
24333 Rakuten Mobile , Inc Tokyo Japan
28910 fdcservers.net Tokyo Japan
21569 i3D.net Tokyo Japan
18516 GIAM PING VIETPN.COM Tokyo Japan
6087 Allied Telesis Capital Corporation Fussa-shi Japan
8407 Allied Telesis Capital Corporation Sagamihara Japan
.\speedtest -s <ID>
で上記のIDを指定することで、特定のSpeedtestサーバに限定できる。
.\speedtest -s 15047
Speedtest by Ookla
Server: OPEN Project (via 20G SINET) - Tokyo (id = 15047)
ISP: SoftEther Corporation
Latency: 13.15 ms (0.09 ms jitter)
Download: 50.01 Mbps (data used: 74.5 MB)
Upload: 52.88 Mbps (data used: 71.9 MB)
Packet Loss: 0.0%
Result URL: https://www.speedtest.net/result/c/4c4c5631-0982-4712-bc09-2220add4afe3
自動化 見出しにジャンプ
ここからはPowerShellをベースに進める。
speedtest -f json
でjson形式で返してくれる。
対応フォーマット: human-readable (既定値), csv, tsv, json, jsonl, json-pretty
※見やすいように整形している
.\speedtest -f json
{
"type": "result",
"timestamp": "2020-03-07T15:16:21Z",
"ping": {
"jitter": 1.423,
"latency": 16.251999999999999
},
"download": {
"bandwidth": 4966200,
"bytes": 73284688,
"elapsed": 15001
},
"upload": {
"bandwidth": 5719822,
"bytes": 51957059,
"elapsed": 9207
},
"packetLoss": 0,
"isp": "Open Computer Network",
"interface": {
"internalIp": "192.168.11.2",
"name": "",
"macAddr": "XX:XX:XX:XX:XX:XX",
"isVpn": false,
"externalIp": "XXX.XXX.XXX.XXX"
},
"server": {
"id": 14623,
"name": "IPA CyberLab",
"location": "Bunkyo",
"country": "Japan",
"host": "speed.coe.ad.jp",
"port": 8080,
"ip": "103.95.184.74"
},
"result": {
"id": "4ac0f15a-9286-4a49-8b1a-ee91628c0759",
"url": "https://www.speedtest.net/result/c/4ac0f15a-9286-4a49-8b1a-ee91628c0759"
}
}
Speedtestを実行し、結果をjsonからPowerShellで扱えるハッシュテーブルに変換
$Result = & "C:\bin\ookla-speedtest-1.0.0-win64\speedtest.exe" -f json | ConvertFrom-Json
ping
$Result.ping.latency
13.399
ping (decimal -> integer)
[int]$Result.ping.latency
13
下り
-f
でjson等を指定した場合、単位はbytes
$Result.download.bandwidth
9474733
下り (bps -> Mbps)
$Result.download.bandwidth / 125000
75.797864
下り (bps -> Mbps) (decimal -> integer)
[int]($Result.download.bandwidth / 125000)
76
上り (bps -> Mbps) (decimal -> integer)
[int]($Result.upload.bandwidth / 125000)
52
ISP
$Result.isp
SoftEther Corporation
SpeedTestサーバ名
$Result.server.name
OPEN Project (via 20G SINET)
結果毎の固有のURL
$Result.result.url
https://www.speedtest.net/result/c/f89732bc-0884-447b-9fef-7c86b1ddf94b
Speedtest.ps1 見出しにジャンプ

SlackやDiscordのWebhook、Windowsのトーストに対応したSpeedtestスクリプト。
タスクスケジューラで数時間おきに実行することで、時間帯毎の統計を取れる。
- トリガー
- タスクの開始: スケジュールに従う
- 繰り返し間隔:
6 時間
- 操作
- 操作: プログラムの開始
- プログラム/スクリプト:
powershell
またはpwsh
- 引数の追加:
-WindowStyle Hidden -File "C:\Repository\pwsh\Speedtest.ps1"
※例
- 設定
- タスクを停止するまでの時間: 無効
監視の結果 見出しにジャンプ
午前2時から8時までは空いているので、古き良きVDSLでも上下50Mbpsは下らない。
ただし、一般的な人類が活動する時間帯では20Mbps程度、ゴールデンタイムでは6Mbpsまで落ちることが分かった。
普段は体感できるほど影響がなかったので、明らかにコロナの影響。
事態が収束するまで、2時-8時に活動できるよう睡眠時間をズラすことにした。概日リズムが長い私にとって、夜更かしはお手の物である。
なんだけど、

https://www.softether.jp/7-news/2020.03.06
SoftEtherありがとう!!OCN頑張って!!