Test network speed using PHP/Javascript
Asked Answered
S

2

6

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.

Spiculum answered 27/7, 2011 at 20:34 Comment(8)
May be able to test latency using a JS call to a PHP file and back (benchmark the response time based on payload) but I can't imagine that's accurate.Hanschen
@Brad Christie, maybe not, but a good shout neverthelessSpiculum
Depending on what content you're thinking about delivering I don't think this is an ideal approach. Maybe if you're developing for mobile browsers this would be viable so that you don't serve images to mobile networks that are serving at lower-than-dialup speeds.Stolzer
This is exactly what i'm intending to do :)Spiculum
3G, 2G, broadband, Cellular....Spiculum
@Spiculum I think you should modify the question to put mobile devices. It changes the question a little.Cultism
See this: #5530218Louie
Duplicate: How to detect internet speed in JavaScript?Sociopath
S
2

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.

Stickpin answered 27/7, 2011 at 20:38 Comment(5)
Hmmm, those were my fears. Nice idea anywaySpiculum
@genesis: Should, as long as your benchmark variable is outside of the scope of the response. I'd probably add an error:/setTimeout default though in case the ajax fails.Hanschen
@BradChristie: You didn't get it. javascript goes straight way and does not way till $.ajax() is completed...Corduroy
@genesis: I interpreted it as the end being within the ajax call, but that may just be placing my understanding atop Marc's code. You're right, as written it would fail. But with a little logic, it's feasible. ;-)Hanschen
@genesis: ajax is normally done asynchronously, but that just changes where/when you do the "end" time gathering. and you can always do a synchronous request as well if need be.Stickpin
H
1

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

Hanschen answered 27/7, 2011 at 20:47 Comment(3)
Most cable operators have bursting enabled now, so they can claim "we're sending you data a gigabit rates(*) [miles later] for picosecond periods".Stickpin
@MarcB: Yup, signed that contract on my wonderful xfinity service (that I absolutely love).Hanschen
Can't wait for my telco (Sasktel) to complete its FTTH build-out, then I can ditch cable. 200mbit starting speeds, plus IPtv/phone/cell all bundled onboard.Stickpin

© 2022 - 2024 — McMap. All rights reserved.