如何检查上传和下载速度

时间:2014-01-27 08:00:42

标签: javascript file-upload upload download speed-test

我正在尝试使用USB Dongle编写用于检查ISP上传和下载速度的代码。加密狗中的服务器是Lighttpd。

我目前的下载计划是编写一个从服务器下载文件的shell脚本(该文件应该足够大)并在2秒内终止该进程。然后我可以获取文件大小。我重复该过程n时间(10这里),取平均值。从统计上来说,我认为移动平均线是最佳值。请帮忙。

此外,

我的上传计划是一个大文件(20 MB)并使用shell脚本上传它(也可以使用php),并使用它来测量速度。我不知道如何测量上传速度。我需要弥合通信空白,设备硬件具有价值,我需要获得正确的值才能显示给用户。需要帮助。

上传代码如下。(它是一个javascript文件) 注意:var params约为350 KiB

var uploadhttp;
var url = "http://www.example.com";
var params = 
"RANDOM CHARACTER SET. This lone character file should be large enough";




function getHTTPObject() {

    if(!uploadhttp && typeof XMLHttpRequest != "undefined") 
    {
      try
        {     
        if(BrowserType=="MSIE")
            uploadhttp=new XDomainRequest(); 
        else
            uploadhttp = new XMLHttpRequest();          
        }
        catch(e)
        {
            alert(e);
        }
/*      if(!uploadhttp){
            //alert("uploadhttp object creation failed");
            }
            else{
            //alert("uploadhttp object created successfully");
            }
 */ }

}

function handler() {//Call a function when the state changes.
    if(uploadhttp.readyState == 4 && uploadhttp.status == 200) {
        //alert(uploadhttp.responseText);
    }
}

function getMethod() {
    getHTTPObject();
    /* if(!uploadhttp)
        //alert("failed to create object"); */

    if(uploadhttp)
    {
        uploadhttp.open("GET", url, true);
        uploadhttp.onreadystatechange = handler;
        uploadhttp.send(null);
    }
}

function postMethod() {
    try
    {
    getHTTPObject();
    //if(!uploadhttp)
        //alert("failed to create object");
    if(uploadhttp)
    {
        uploadhttp.open("POST", url, true);//,"jio","rancore");
    }

    //Send the proper header infomation along with the request
    //uploadhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    uploadhttp.onreadystatechange = handler;
    if(BrowserType=="MSIE")
    {
        uploadhttp.send(params);
    }
    else
    {
        var iLen = bufferedData.length;
        uploadhttp.send(bufferedData);
    }
    }
    catch(e)
    {
       alert(e);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用wget下载文件 并使用“时间”测量下载时间。 所以,你可以这样写:

time wget http://....

wget也可以测量时间,但我更喜欢time,因为它会报告用于下载的全部时间。

要测试上传速度,您可以使用curl

time curl -i -F name=test -F filedata=@localfile.jpg http://example.org/upload
相关问题