php 如何检查我的服务器的上传和下载速度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11283801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check my server's upload and download speed?
提问by HTMHell
I bought a server and I need to check it's internet connection (speed).
我买了一台服务器,我需要检查它的互联网连接(速度)。
Is there an easy way to do that?
有没有简单的方法来做到这一点?
I googled but I couldn't find anything...
我用谷歌搜索但我找不到任何东西......
I did this:
我这样做了:
<?php
$link = 'http://speed.bezeqint.net/big.zip';
$start = time();
$size = filesize($link);
$file = file_get_contents($link);
$end = time();
$time = $end - $start;
$speed = $size / $time;
echo "Server's speed is: $speed MB/s";
?>
Is it correct?
这是正确的吗?
回答by lauriys
Try:
尝试:
<?php
$link = 'http://speed.bezeqint.net/big.zip';
$start = time();
$size = filesize($link);
$file = file_get_contents($link);
$end = time();
$time = $end - $start;
$size = $size / 1048576;
$speed = $size / $time;
echo "Server's speed is: $speed MB/s";
?>
回答by Nikola K.
If you have the remote desktop, then install a web browser and go to speedtest.netand test a speed.
如果您有远程桌面,请安装 Web 浏览器并转到speedtest.net并测试速度。
If not, here's how you can test your server's download speed:
如果没有,您可以通过以下方法测试服务器的下载速度:
- log in as root
- type
wget http://cachefly.cachefly.net/100mb.test - you'll see something like
100%[======================================>] 104,857,600 10.7M/s- 10.7M/s is a download speed.
- 以 root 身份登录
- 类型
wget http://cachefly.cachefly.net/100mb.test - 你会看到类似
100%[======================================>] 104,857,600 10.7M/s- 10.7M/s 是下载速度。
If you have more than 1 server, you can test upload speed by transfering files between 2 servers.
如果您有 1 个以上的服务器,您可以通过在 2 个服务器之间传输文件来测试上传速度。
回答by Niet the Dark Absol
Have it connect to a server you know runs fast (such as Google). Then, measure how long it takes from sending the first packet to receiving the first packet - that's your upload time. The time from receiving the first to last packets is the download time. Then divide by the amount of data transferred and there's your result.
让它连接到您知道运行速度很快的服务器(例如 Google)。然后,测量从发送第一个数据包到接收第一个数据包所需的时间 - 这就是您的上传时间。从接收到第一个数据包到最后一个数据包的时间就是下载时间。然后除以传输的数据量,就是你的结果。
Example:
例子:
$times = Array(microtime(true));
$f = fsockopen("google.com",80);
$times[] = microtime(true);
$data = "POST / HTTP/1.0\r\n"
."Host: google.com\r\n"
."\r\n"
.str_repeat("a",1000000); // send one megabyte of data
$sent = strlen($data);
fputs($f,$data);
$firstpacket = true;
$return = 0;
while(!feof($f)) {
$return += strlen(fgets($f));
if( $firstpacket) {
$firstpacket = false;
$times[] = microtime(true);
}
}
$times[] = microtime(true);
fclose($f);
echo "RESULTS:\n"
."Connection: ".(($times[1]-$times[0])*1000)."ms\n"
."Upload: ".number_format($sent)." bytes in ".(($times[2]-$times[1]))."s (".($sent/($times[2]-$times[1])/1024)."kb/s)\n"
."Download: ".number_format($return)." bytes in ".(($times[3]-$times[2]))."s (".($return/($times[3]-$times[2])/1024)."kb/s)\n";
(You will get an error message from Google's servers, on account of the Content-Lengthheader missing)
(由于Content-Length缺少标头,您将收到来自 Google 服务器的错误消息)
Run it a few times, get an average, but don't run it too much because I don't think Google would like it too much.
运行几次,得到一个平均值,但不要运行太多,因为我认为 Google 不会太喜欢它。
回答by Samy Dindane
For the download, you can create a script that will calculate the average download speed:
对于下载,您可以创建一个脚本来计算平均下载速度:
$start = time(true);
$fileSize = '10240'; // if the file's size is 10MB
for ($i=0; $i<10; $i++) {
file_get_contents('the_url_of_a_pretty_big_file');
}
$end = time(true);
$speed = ($fileSize / ($end - $start)) / $i * 8;
echo $speed; // will return the speed in kbps

