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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
benhowdle89
  • 36,900
  • 69
  • 202
  • 331
  • 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. – Brad Christie Jul 27 '11 at 20:37
  • @Brad Christie, maybe not, but a good shout nevertheless – benhowdle89 Jul 27 '11 at 20:38
  • 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. – MoarCodePlz Jul 27 '11 at 20:40
  • This is exactly what i'm intending to do :) – benhowdle89 Jul 27 '11 at 20:42
  • 3G, 2G, broadband, Cellular.... – benhowdle89 Jul 27 '11 at 20:44
  • @benhowdle89 I think you should modify the question to put mobile devices. It changes the question a little. – Amir Raminfar Jul 27 '11 at 20:55
  • See this: http://stackoverflow.com/questions/5529718/how-to-detect-internet-speed-in-javascript – advncd Mar 17 '14 at 22:48
  • Duplicate: [How to detect internet speed in JavaScript?](https://stackoverflow.com/questions/5529718/how-to-detect-internet-speed-in-javascript) –  Jul 11 '22 at 22:47

2 Answers2

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Hmmm, those were my fears. Nice idea anyway – benhowdle89 Jul 27 '11 at 20:40
  • @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. – Brad Christie Jul 27 '11 at 20:56
  • @BradChristie: You didn't get it. javascript goes straight way and does not way till $.ajax() is completed... – genesis Jul 27 '11 at 20:57
  • @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. ;-) – Brad Christie Jul 27 '11 at 20:59
  • @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. – Marc B Jul 27 '11 at 21:01
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).

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • Most cable operators have bursting enabled now, so they can claim "we're sending you data a gigabit rates(*) [miles later] for picosecond periods". – Marc B Jul 27 '11 at 20:49
  • @MarcB: Yup, signed that contract on my _wonderful_ xfinity service (that I absolutely _love_). – Brad Christie Jul 27 '11 at 20:51
  • 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. – Marc B Jul 27 '11 at 21:01