如何检查我的服务器的上传和下载速度?

8

我购买了一台服务器,需要检查它的互联网连接(速度)。

有没有简单的方法可以做到这一点?

我搜索了一下,但是没有找到任何相关的信息...

我尝试了以下方法:

<?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";


?>

这是正确的吗?


对于下载速度,我想到了这个:安装一个比特流命令行客户端并下载一个Linux发行版(不要太新,但也不要太旧,以便有很多种子)。通常所有这些种子都可以像您的服务器一样快地发送。 - aufziehvogel
看起来挺好的。能用吗? - PeeHaa
4个回答

8

尝试:

<?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";


?>

жҲ‘еҒҡдәҶиҝҷдёӘпјҡ<?php $link = 'http://speed.bezeqint.net/big.zip&#39;; $start = time(); $size = filesize($link); $file = file_get_contents($link); $end = time(); $time = $end - $start; $speed = $size / $time; echo "жңҚеҠЎеҷЁйҖҹеәҰдёәпјҡ$speed MB/s"; ?> жӯЈзЎ®еҗ—пјҹ - HTMHell
几乎,它将输出每秒字节数,而不是MB/s。 - lauriys

6
如果你有远程桌面,那么安装一个网页浏览器,然后转到 speedtest.net 进行速度测试。
如果没有,以下是如何测试服务器的下载速度:
- 以 root 身份登录 - 输入 wget http://cachefly.cachefly.net/100mb.test - 您将看到类似于 100%[======================================>] 104,857,600 10.7M/s - 10.7M/s 是下载速度。
如果您有多个服务器,可以通过在两个服务器之间传输文件来测试上传速度。

或者使用 speedtest-cli,它不需要在服务器上运行 GUI。 - Skylar Ittner

2

请选择一个运行速度快的服务器(例如谷歌),然后连接它。然后,测量从第一个数据包发送到接收第一个数据包所需的时间 - 这是上传时间。从接收第一个数据包到最后一个数据包所需的时间是下载时间。然后将下载时间减去上传时间,并除以传输的数据量,就得到了结果。

例子:

$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";

由于缺少Content-Length头,您将从Google的服务器收到错误消息。

运行几次,取平均值,但不要运行太多次,因为我认为Google不会喜欢这样做。


谢谢,但我得到的结果并不是很有意义...当我从服务器下载文件时,速度只有600kb/s,而我的网络连接速度是100MB/s...使用您的代码,我得到了以下结果:连接时间:75.366973877毫秒上传:1,000,037字节在0.192752122879秒内(5066.60377186kb/s)下载:1,081字节在2.69412994385E-5秒内(39183.8584071kb/s) - HTMHell
当您从服务器下载文件时,网络是瓶颈。服务器通常拥有更好的互联网连接,这就是为什么测试会连接到另一个服务器的原因。 - Niet the Dark Absol
是的,我通常通过 torrent 等方式以每秒 12 MB 的速度下载,但从我的服务器下载只有大约每秒 600 KB。 - HTMHell

0

关于下载,你可以创建一个脚本来计算平均下载速度:

$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

第一行。mictotime。另外,100 KB = 102400 B,而不是100000。 - lauriys
在我看来,微秒会更好,更准确。 - lauriys
我在发布几秒钟后注意到了 microtime 的问题。感谢您指出。关于文件大小,我认为这并不重要,在结果上也不会有太大的影响。 - Samy Dindane
如果文件大小为100KB,时间/微秒会有所不同:P。 - lauriys
100KB只是一个例子,这就是为什么我在file_get_contents()中提到了“the_url_of_a_pretty_big_file”。 - Samy Dindane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接