Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test network speed using PHP/Javascript

I'm trying to find out a way to test network speed using PHP or Javascript.

Is this possible?

I'd ideally like to test the speed and the dynamically decide how much content to deliver to the client...

I should point out i'm largely talking about mobile devices. Most broadband connection differ very little so i'm aiming to gauge whether someone is connected to a WiFi network or is struggling on Cellular data network.

like image 216
benhowdle89 Avatar asked Jul 27 '11 20:07

benhowdle89


People also ask

How do I know if my PHP Internet is working?

The connection_status() function returns the current connection status. Possible values that can be returned are: 0 - CONNECTION_NORMAL - connection is running normally. 1 - CONNECTION_ABORTED - connection is aborted by user or network error.

Can you run a speed test from command line?

Speedtest® CLI With Speedtest CLI, you can easily: Measure internet connection performance metrics like download, upload, latency and packet loss natively without relying on a web browser.

How do I run a speed test on a specific IP?

Visit http://www.speedtest.net/ to test the upload and download speed of your IP connections and identify your public IP address.


2 Answers

Do an ajax request to download a fixed-size chunk of data and check the time before/after to get a rough idea of speeds:

var start = new Date();
$.ajax(....);
var end = new Date();

var elapsed = end.getTime() - start.getTime(); // elapsed time in milliseconds.

You'd need to use a large enough chunk of data to get valid requests, which'd mean you're wasting a fair amount of bandwidth just to do this test.

like image 104
Marc B Avatar answered Oct 19 '22 08:10

Marc B


They way I see it, you can use JS -> PHP -> JS call and time the response, but that is pretty inaccurate. Also, you'd need to use a fair amount of data (excess bandwidth usage concerns) and would never get an explicit answer due to server headers that exist/don't exist between browsers. There's also a concern with service providers (cough comcast cough) where they give you 12 mbit speeds for the first few seconds, but choke you down to 3mbits afterward, so now your "Test" would say they're on an OC line but the stream itself would now have a data deficit and be buffering constantly.

The best solution is to build the logic in to the streaming protocol that can adjust based on how much/little data is coming in. Maybe it starts out at a low bandwidth quality and raises the bar when it notices the buffer is growing faster than the data is playing (this is what Hulu, YouTube or Amazon video do).

like image 1
Brad Christie Avatar answered Oct 19 '22 10:10

Brad Christie