PHP Speed Test for user connection speed without echo in current page

这一生的挚爱 提交于 2019-12-01 02:46:49

问题


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:


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.




回答2:


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.




回答3:


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



回答4:


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.




回答5:


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:)




回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/3943016/php-speed-test-for-user-connection-speed-without-echo-in-current-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!