jQuery ajax上传/下载测试 - 是我的代码声音?

时间:2015-01-20 23:04:03

标签: jquery ajax performance

我已经使用jQuery和ajax编写了一个简单的上传/下载速度测试来测试客户端' ul / dl使用我们自己的服务器速度。我知道这种性质的javascript测试并不是非常准确,但我与Speedof.me和Speedtest.net相比的结果是方式关闭。作为比较,我做了一些测试,没有其他互联网活动活跃:

Speedof.me  -       26.5 down  5.7 up
Speedtest.net -     26.5 down  5.3 up
OpenSpeedTest.com - 12.0 down  3.5 up
My Tool -            7.7 down  4.7 up

有人认为差异可能完全与服务器有关,但这真的可以解释这么大的差异吗?我不认为我们的服务器 慢...我们使用Coldfusion,因此增加了一点点因为CF具有随每个请求运行的函数(onRequestStart(),onRequestEnd( )),包括ajax请求。但是,我正在为我的ajax调用抑制这两个函数。我的基本代码如下。使用ajax,我下载并上传名为lorem.txt的文本文件,大小约为10MB。

    var ajaxTime= new Date().getTime();
    var fileSize = <cfoutput>#nFileSizeMegaBits#</cfoutput>;

    var request = jQuery.ajax({
    url: "/myPath/lorem.txt?s=" + ajaxTime,
    type: "GET",
    success: function(result) {//success                    
        var totalTime = new Date().getTime()-ajaxTime;
        var mbps = fileSize/(totalTime/1000);
        mbps = mbps.toFixed(2);
        nDLSpeedMbps = mbps;

        //this is where I populate the page with the test result    
        jQuery('#dlSpeedSpan').html(nDLSpeedMbps + "Mbps");

        //Do upload timer test
        //note that this new ajax call is done inside of the download ajax call's success function; so that both tests won't run at the same time
        var ajaxTimeUpload = new Date().getTime();
        var formData = new FormData();

        formData.append("xxtestTextxx", request.responseText);//i.e., all 10MB of the text from Lorem.txt

        var requestUpload = jQuery.ajax({
            url: '/myPath/blank.cfm',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            mimeType: 'multipart/form-data',
            cache: false,
            success: function(data) {
                var totalTimeUpload = new Date().getTime()-ajaxTimeUpload;
                var mbpsUpload = fileSize/(totalTimeUpload/1000);
                mbpsUpload = mbpsUpload.toFixed(2);
                nULSpeedMbps = mbpsUpload;

                //populate page with test result
                jQuery('#ulSpeedSpan').html(nULSpeedMbps + "Mbps");
            });

            requestUpload.fail(function(xhr, status) {
                jQuery('#ulSpeedSpan').html( "Ajax Request failed: " + status );
            });
        }
    });

    request.fail(function( jqXHR, textStatus ) {
        jQuery('#dlSpeedSpan').html( "Ajax Request failed: " + textStatus );
    });
});

0 个答案:

没有答案