Linux 下 网速测试

电脑测试网速这个功能估计大家都用过,但是服务器需要测试外网的速度相比大家做法各有不同,有的人使用自己的服务器来测试上下行速度,有些通过下载某个网站的资源来测试下行速度,鉴于这些节点比较少,而且测试麻烦,我向大家推荐speedtest-cli.
speedtest是一个使用python编写的命令行脚本,通过调用speedtest.net测试上下行的接口来完成速度测试.最后我会测试运维生存时间所在服务器的外网速度。

1. 安装speedtest-cli

speedtest-cli需要在python 2.4-3.4的环境下,安装方法都很简单,自己选择以下最适合你的一种.

1.1 pip方式

什么是 pip 传送门>

  1. pip3 install speedtest-cli

1.2 github+pip方式

  1. pip3 install git+https://github.com/sivel/speedtest-cli.git

或者

  1. git clonehttps://github.com/sivel/speedtest-cli.git
  2. python speedtest-cli/setup.py install

1.3 下载脚本方式

前提是你要能上 Github

  1. curl -oL speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
  2. chmod 777 speedtest-cli
  3. ./speedtest-cli

2. speedtest-cli用法 命令很简单,直接上帮助

  1. speedtest-cli --help
  1. usage: speedtest-cli [-h] [--no-download] [--no-upload] [--single] [--bytes]
  2. [--share] [--simple] [--csv]
  3. [--csv-delimiter CSV_DELIMITER] [--csv-header] [--json]
  4. [--list] [--server SERVER] [--exclude EXCLUDE]
  5. [--mini MINI] [--source SOURCE] [--timeout TIMEOUT]
  6. [--secure] [--no-pre-allocate] [--version]
  7. Command line interface for testing internet bandwidth using speedtest.net.
  8. --------------------------------------------------------------------------
  9. https://github.com/sivel/speedtest-cli
  10. optional arguments:
  11. -h, --help show this help message and exit
  12. --no-download Do not perform download test
  13. --no-upload Do not perform upload test
  14. --single Only use a single connection instead of multiple. This
  15. simulates a typical file transfer.
  16. --bytes Display values in bytes instead of bits. Does not
  17. affect the image generated by --share, nor output from
  18. --json or --csv
  19. --share Generate and provide a URL to the speedtest.net share
  20. results image, not displayed with --csv
  21. --simple Suppress verbose output, only show basic information
  22. --csv Suppress verbose output, only show basic information
  23. in CSV format. Speeds listed in bit/s and not affected
  24. by --bytes
  25. --csv-delimiter CSV_DELIMITER
  26. Single character delimiter to use in CSV output.
  27. Default ","
  28. --csv-header Print CSV headers
  29. --json Suppress verbose output, only show basic information
  30. in JSON format. Speeds listed in bit/s and not
  31. affected by --bytes
  32. --list Display a list of speedtest.net servers sorted by
  33. distance
  34. --server SERVER Specify a server ID to test against. Can be supplied
  35. multiple times
  36. --exclude EXCLUDE Exclude a server from selection. Can be supplied
  37. multiple times
  38. --mini MINI URL of the Speedtest Mini server
  39. --source SOURCE Source IP address to bind to
  40. --timeout TIMEOUT HTTP timeout in seconds. Default 10
  41. --secure Use HTTPS instead of HTTP when communicating with
  42. speedtest.net operated servers
  43. --no-pre-allocate Do not pre allocate upload data. Pre allocation is
  44. enabled by default to improve upload performance. To
  45. support systems with insufficient memory, use this
  46. option to avoid a MemoryError
  47. --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 列出所有在中国的测试服务器

  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,我测试到上海这台机器的速度,并且生成图片报告

  1. 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 网速测试 - 图1
speedtest-cli

5 参考地址

项目地址:https://github.com/sivel/speedtest-cli

文档相关链接
http://www.ttlsa.com/linux/use-speedtest-cli-test-internet-speed/