views:

75

answers:

7

Hey guys,

I am looking for a possibility to check the user connection speed. It is supposed to be saved as a cookie and javascript files as well as css files will be adapted if the speed is slow.

The possibility for testing speed i have at the moment ist the following

    $kb = 512;

    flush();
    //
    echo "<!-";
    $time = explode(" ",microtime());
    for($x=0;$x<$kb;$x++){
        echo str_pad('', 512, '.');
        flush();
    }
    $time_end = explode(" ",microtime());
    echo "->";

    $start = $time[0] + $time[1];
    $finish = $time_end[0] + $time_end[1];
    $deltat = $finish - $start;

    return round($kb / $deltat, 3);

While it works, I do not like it to put so many characters into my code also if I echo all this I can not save the result in a cookie because there has already been an output.

Could one do something like this in a different file wor something? Do you have any solution?

Thanks in advance.

+1  A: 

The problem here is that you can't really solve this nicely, and probably not in pure PHP. The approach you've taken will make the user download (512x512) = 262 144 bytes of useless data, which is much bigger than most complete pages. If the user is on a slow connection, they may assume your site is down before the speed test is over (with 10 kB/sec, it'd take half a minute before anything interesting shows up on screen!).

You could make an AJAX request for a file of a known size and time how long that takes. The problem here is that the page needs to be already loaded for that to work, so it'd only work for subsequent pages.

You could make a "loading" page (like you see on GMail when accessing it from a slow connection) that preloads the data, with a link to the low-bandwidth version (or maybe a redirect if the loading is taking too long).

Or you could save the "start" time in the cookie and make an AJAX request when the page is done loading - that would give you the actual loading time of your page; if that's, say, over 10 seconds, you may want to switch to the low-bandwidth version.

None of these, however, will get you the speed on the very first access; and sending a big empty page up front is not a very good first impression either.

Piskvor
And how precise is this?
fabrik
He needs the speed **before** generating the page so he can choose what CSS file to include.
Alin Purcaru
@Alin Purcaru: do tell, how do you get the speed *before* you download anything?
Piskvor
@fabrik: The precision is OK - good enough to decide between fast/slow version. It will always depend on other factors - is the user doing something else with the browser, are there other transfers, etc.
Piskvor
@Alin Purcaru: Could we focus on the question instead of trolling? Thank you. I understand that he's trying to measure the speed and give out a high speed/low speed version; however, there's no pretty way to solve this - the AJAX measure would be only useful for the *following* pages; for the first page, I'm afraid there's no useful way to both measure the speed and do something useful based on the measure. Don't think that saying "use AJAX" automatically means "it's web2.0, AJAX is the magical make-it-work dust". There are valid uses for it.
Piskvor
+2  A: 

The only idea what i can come up is a redirect.

  • Measure users' speed
  • Redirect to index

While this isn't a nice solution it only need to measure users' speed only once so i think it's excusable.

fabrik
A: 

After you've determined the user's speed, send javascript to the browser to set the cookie and then do a refresh or redirect in cases where the speed is below what you'd like.

Jack Shedd
+1  A: 

How about using javascript to time how long it takes to load a page. Then use javascript to set the cookie.

microtime in javascript http://phpjs.org/functions/microtime:472

Using jQuery

<head>
<!-- include jquery & other html snipped -->

<script>

function microtime (get_as_float) {
    // http://kevin.vanzonneveld.net
    // +   original by: Paulo Freitas
    // *     example 1: timeStamp = microtime(true);
    // *     results 1: timeStamp > 1000000000 && timeStamp < 2000000000

    var now = new Date().getTime() / 1000;
    var s = parseInt(now, 10);

    return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;
}

function setCookie(c_name, value, expiredays) {
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

start = microtime(true);

$(window).load(function () {
  // everything finished loading
  end = microtime(true);
  diff = end - start;
  // save in a cookie for the next 30 days

    setCookie('my_speed_test_cookie', diff, 30);
});

</script>
</head>
<body>
<p>some page to test how long it loads</p>
<img src="some_image_file.png">
</body>

Some pitfalls: - The page would need to start loading first. JQuery would need to be loaded (or you can rework the above code to avoid jQuery)

  • testing speed on ASCII / Latin data may not give the best result, because the characters may get compressed. Besides the high level gzip compression, Some modems / lines (if not all) have basic compression that is able to detect repeating characters and tell the other end that the next 500 are repeat of ' '. I guess it would be best to use binary data that has been compressed
Allen Hamilton
A: 

The only thing I can think of would be to subscribe to a service which offers an IP to net speed lookup. These services work by building a database of IP addresses and cataloging their registered intended use. They're not always accurate, but they do provide a starting point. Look up the user's IP address against one of these and see what it returns.

Ip2Location.com provides such a database, beginning with their DB13 product.

Of course, if your goal is a mobile version of the site, user agent sniffing is a better solution.

Ryan Chouinard
+2  A: 

Do you have any solution?

My solution is to not bother with the speed test at all. Here's why:

You stated that the reason for the test is to determine which JS/CSS files to send. You have to keep in mind that browsers will cache these files after the first download (so long as they haven't been modified). So in effect, you are sending 256K of test data to determine if you should send, say, an additional 512K?

Just send the data and it will be cached. Unless you have MBs of JS/CSS (in which case you need a site redesign, not a speed test) the download time will be doable. Speed tests should be reserved for things such as streaming video and the like.

webbiedave
Well, that actually sounds right. The Idea was more or less to provide this version if there is a need for it. The system I am building is a base for many web pages to come and if there would be big backgrounds and such it might be good to provide user with the choice of having a low bandwidth version. I think I will do it manually though, just providing 2 buttons. Thanks for all the answers though, if I ever need to do it, I know where to look.
Lukas Oppermann
A: 

you visit the first page(maybe 100kB with all external files), a session is immeadeatly started with $_SESSION["start_time"] = time();

when page finished loading(jQuery window load or smth:) u send a request again with time, u compute the speed (jQueryRequestTime - $_SESSION["start_time"] / PageSize) and set another session variable, the next link he clicks then can include custom css/js approved for that

ofc this is not perect:)

w13531