aln / speedtest-php
使用speedtest.net测试网络带宽的库和命令行界面
v1.0.8
2024-03-07 12:41 UTC
Requires
- php: >=7.0
README
使用speedtest.net测试网络带宽的库和命令行界面。
此项目的目标是运行服务器端速度测试,通过命令行或Web界面。
安装
作为独立包
下载并安装
composer install
然后运行
./bin/speedtest
(IP地址在演示中被隐藏)
作为项目依赖
composer require aln/speedtest-php
然后运行
./vendor/bin/speedtest
CLI使用
$ ./bin/speedtest -h
usage: speedtest [-h] [--no-download] [--no-upload] [--single] [--bytes]
[--share] [--simple] [--json] [--list] [--server=SERVER]
[--exclude=EXCLUDE] [--source=SOURCE] [--timeout=TIMEOUT]
[--version]
Command line interface for testing internet bandwidth using speedtest.net.
--------------------------------------------------------------------------
https://github.com/aln-1/speedtest-php
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
--share Generate and provide a URL to the speedtest.net share
results image
--simple Suppress verbose output and progress, only shows results
--json Output in JSON format. Speeds listed in bits and not
affected by --bytes. Can be combined with --simple
to supress progress
--list Display a list of speedtest.net servers sorted by
distance
--server=SERVER Specify a server ID to test against. Can be comma
separated values
--exclude=EXCLUDE Exclude a server from selection. Can be comma
separated values
--source=SOURCE Source IP address to bind to or interface name
--timeout=TIMEOUT HTTP timeout in seconds, default 10
--version Show the version number
库
最小化代码
$ php -a Interactive shell php > require 'vendor/autoload.php'; php > $speedtest = new Aln\Speedtest\Speedtest(); php > $speedtest->getServers(); php > $speedtest->getBestServer(); php > $speedtest->download(); php > $speedtest->upload(); php > $results = $speedtest->results(); php > print_r($results); Aln\Speedtest\Result Object ( [latency:protected] => 4.57 [download:protected] => 47888585.578516 [upload:protected] => 64841042.860629 [bytesReceived:protected] => 59881235 [bytesSent:protected] => 82579808 )
单元
- 延迟 = 毫秒
- 下载/上传 = 比特/秒
- 接收的字节数/发送的字节数 = 字节
带有进度回调
$ php -a Interactive shell php > require 'vendor/autoload.php'; php > $config = new Aln\Speedtest\Config(); php > $config->setCallback(function ($results) { print_r($results); }); php > $speedtest = new Aln\Speedtest\Speedtest($config); php > $speedtest->getServers(); php > $speedtest->getBestServer(); php > $speedtest->download(); Aln\Speedtest\Result Object ( [latency:protected] => 4.40 [download:protected] => 0 [upload:protected] => [bytesReceived:protected] => 0 [bytesSent:protected] => 0 ) // More dump was here ... Aln\Speedtest\Result Object ( [latency:protected] => 4.40 [download:protected] => 27519752.835724 [upload:protected] => [bytesReceived:protected] => 37153149 [bytesSent:protected] => 0 )
(只显示下载方法的第一个和最后一个回调结果)
Web演示
一个页面演示可在resources/demo.php
中找到
鸣谢
此项目是由Matt Martz从Python版本移植过来的
许可证
此项目采用Apache许可证版本2.0 - 请参阅LICENSE文件以获取详细信息