Linux 下 网速测试
电脑测试网速这个功能估计大家都用过,但是服务器需要测试外网的速度相比大家做法各有不同,有的人使用自己的服务器来测试上下行速度,有些通过下载某个网站的资源来测试下行速度,鉴于这些节点比较少,而且测试麻烦,我向大家推荐speedtest-cli.
speedtest是一个使用python编写的命令行脚本,通过调用speedtest.net测试上下行的接口来完成速度测试.最后我会测试运维生存时间所在服务器的外网速度。
1. 安装speedtest-cli
speedtest-cli需要在python 2.4-3.4的环境下,安装方法都很简单,自己选择以下最适合你的一种.
1.1 pip方式
什么是 pip 传送门>
pip3 install speedtest-cli
1.2 github+pip方式
pip3 install git+https://github.com/sivel/speedtest-cli.git
或者
git clonehttps://github.com/sivel/speedtest-cli.git
python speedtest-cli/setup.py install
1.3 下载脚本方式
前提是你要能上 Github
curl -oL speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
chmod 777 speedtest-cli
./speedtest-cli
2. speedtest-cli用法 命令很简单,直接上帮助
speedtest-cli --help
usage: speedtest-cli [-h] [--no-download] [--no-upload] [--single] [--bytes]
[--share] [--simple] [--csv]
[--csv-delimiter CSV_DELIMITER] [--csv-header] [--json]
[--list] [--server SERVER] [--exclude EXCLUDE]
[--mini MINI] [--source SOURCE] [--timeout TIMEOUT]
[--secure] [--no-pre-allocate] [--version]
Command line interface for testing internet bandwidth using speedtest.net.
--------------------------------------------------------------------------
https://github.com/sivel/speedtest-cli
optional arguments:
-h, --help show this help message and exit
--no-download Do not perform download test
--no-upload Do not perform upload test
--single Only use a single connection instead of multiple. This
simulates a typical file transfer.
--bytes Display values in bytes instead of bits. Does not
affect the image generated by --share, nor output from
--json or --csv
--share Generate and provide a URL to the speedtest.net share
results image, not displayed with --csv
--simple Suppress verbose output, only show basic information
--csv Suppress verbose output, only show basic information
in CSV format. Speeds listed in bit/s and not affected
by --bytes
--csv-delimiter CSV_DELIMITER
Single character delimiter to use in CSV output.
Default ","
--csv-header Print CSV headers
--json Suppress verbose output, only show basic information
in JSON format. Speeds listed in bit/s and not
affected by --bytes
--list Display a list of speedtest.net servers sorted by
distance
--server SERVER Specify a server ID to test against. Can be supplied
multiple times
--exclude EXCLUDE Exclude a server from selection. Can be supplied
multiple times
--mini MINI URL of the Speedtest Mini server
--source SOURCE Source IP address to bind to
--timeout TIMEOUT HTTP timeout in seconds. Default 10
--secure Use HTTPS instead of HTTP when communicating with
speedtest.net operated servers
--no-pre-allocate Do not pre allocate upload data. Pre allocation is
enabled by default to improve upload performance. To
support systems with insufficient memory, use this
option to avoid a MemoryError
--version Show the version number and exit
3. speedtest-cli常用命令详解
3.1 list - 根据距离显示speedtest.net的测试服务器列表
3.2 server - 指定列表中id的服务器来做测试
3.3 share - 分享你的网速,该命令会在speedtest网站上生成网速测试结果的图片
4. speedtest-cli测试
4.1 列出所有在中国的测试服务器
speedtest-cli --list | grep China
[root@li229-122~]# speedtest-cli —list | grep China 1185)China Unicom(Changchun,China)[10534.35km] 3784)China Mobile(Urumqi,China)[10581.15km] 2667)Beijing Normal University(Beijing,China)[11117.03km] 2529)Beijing Normal University(Beijing,China)[11117.03km] 2816)Capital Online Data Service(Beijing,China)[11117.03km] 4354)SXmobile(Taiyuan,China)[11383.17km] 3973)China Telecom(Lanzhou,China)[11615.43km] 3633)China Telecom(Shanghai,China)[11983.37km] 3927)China Mobile Jiangsu Co.,Ltd.(Suzhou,China)[11989.27km] 2461)China Unicom(Chengdu,China)[12213.35km] 1028)Shepherd Software(Xiamen,China)[12785.57km] 1628)Xiamen Guangdian Xinxu(Xiamen,China)[12785.57km] 3891)GZinternet(Guangzhou,China)[13005.36km] 3871)SZWCDMA(Shenzhen,China)[13059.20km] 3819)SZU(Shenzhen,China)[13059.20km] 1536)STC(Hong Kong,China)[13088.37km] 1890)Telin(Hong Kong,China)[13088.37km]
结果解释
3633) China Telecom (Shanghai, China) [11983.37 km]
3633: 服务器id
china telecom:isp,这里是中国电信
shanghai,china :服务器所在地址
11983.37 km:两台服务器地理位置之间距离,我这台机器在美国,和上海相距11983.37公里,很远呐.
4.1 外网速度测试
服务器在美国linode,我测试到上海这台机器的速度,并且生成图片报告
speedtest-cli --server=3633 --share
[root@li229-122~]# speedtest-cli —server=3633 —share Retrieving speedtest.net configuration… Retrieving speedtest.net server list… Testing from Linode(173.255.219.122)… Hosted by China Telecom(Shanghai)[11983.37km]:23.603ms Testing download speed…………………………………. Download:24.84Mbit/s Testing upload speed………………………………………….. Upload:4.57Mbit/s Share results:http://www.speedtest.net/result/3240988007.png
通过结果可以看到,上海电信用户访问www.ttlsa.com可以达到4.57mbit,上传到www.ttlsa.com可以达到28.84mbit/s. share参数的效果图如下:
speedtest-cli
5 参考地址
项目地址:https://github.com/sivel/speedtest-cli
文档相关链接
http://www.ttlsa.com/linux/use-speedtest-cli-test-internet-speed/