1

I've written a simple upload/download speed test using jQuery and ajax to test clients' ul/dl speed with our own server. I know that javascript tests of this nature aren't known for being greatly accurate, but my results in comparison to Speedof.me and Speedtest.net are way off. As a comparison, I did a few tests with no other internet activities active:

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

It's been suggested that the difference could be entirely server-related, but could that really account for so large a discrepancy? I don't think our server is that slow... We use Coldfusion, so that adds a slight twist in that CF has functions that get run with every request (onRequestStart(), onRequestEnd()), including ajax requests. But, I'm suppressing those two functions for my ajax calls. My basic code is below. Using ajax, I'm downloading and uploading a text file named lorem.txt that's approximately 10MB in size.

    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 );
    });
});
Cmaso
  • 1,095
  • 1
  • 10
  • 23
  • Are you loading the data from a file on disk on your server? If so the bottle neck could be server disk IO. Try generating the data on the server in memory and see if that makes a difference. – bhspencer Jan 20 '15 at 23:27
  • 1
    Where is your server located? How much bandwidth does your hosting company say your get? you could try using the command line tool iperf to test the speed of the link between you and your server. – bhspencer Jan 20 '15 at 23:34
  • @bhspencer Yes, Lorem.txt is a file I have sitting on the server disk, so I suppose what's really being measured in the download portion is the time it takes to read the file from server disk plus the time it takes to move the file's data from server to client. But that doesn't seem so different from what's being done here, where an image file is being used: http://stackoverflow.com/questions/5529718/how-to-detect-internet-speed-in-javascript. How can one remove the disk read time from the equation? – Cmaso Jan 21 '15 at 16:12
  • 1
    Instead of loading the file from disk or you could generate the data programmatically and write it to the output of what ever server side http framework you are using. However I don't think that your approach is likely to really give you what you are looking for. You are looking to measure your internet connection speed and downloading a file is not a good measure of that. HTTP runs on top of TCP so you have the HTTP overhead and the TCP ACK overhead. To really get an accurate measure of link speed you need to be sending UDP packets and that is not directly possible with javascript. – bhspencer Jan 22 '15 at 00:45

0 Answers0