PDA

View Full Version : Determining User Bandwidth


robocop
10-23-2002, 12:26 PM
Hello there to you all....im a newbie here just hoping to find some help. what i basically am trying to do is determine a users bandwidth and redirect them to the corresponding page. i kinda pieced this together from scripts all over the net....one thing that has me boggled is the actual math of it all...(i.e. how to tell whats a 56k modem user, whos a T1)...

also, im fairly new to JS, so can anyone tell me why the 'TimeElapsed' variable has (timeEnd - timeStart) divided by 1000 - 1.5? whats the purpose of dividing it by 1000 and then subtracting 1.5? that has me boggled as hell....

THANK YOU so much to anyone able to help me...ill be sure to look out for anyone who needs help here....

kasey:thumbsup:



<script language="JavaScript">
<!--

if (top.location != location) top.location.href = location.href;

// -->
</script>
<BASE target="_top">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">

<script language="JavaScript">
<!--

function RightNow() {
time = new Date();
return time.getTime();
}

function CalculateSpeed(timeStart) {
timeEnd = RightNow();
timeElapsed = (timeEnd - timeStart)/1000 - 0.15;
kbytes = 2/timeElapsed;
kbits = kbytes * 1.024 * 8;
if (kbits < 4) location.href = "http://www.pyroblue.com/kc/speedtest/slow.html";
else location.href = "http://www.pyroblue.com/kc/speedtest/fast.html";
}
function uniqueImageUrl(){
randomNum =Math.round(Math.random()*10000);
alert(randomNum);
var link = "http://www.pyroblue.com/kc/speedtest/60.jpg?uniq="+randomNum;
alert(link);
return link;
}

// -->
</script>
</head>
<BODY bgcolor="" text="#000000" link="#000066" vlink="#000066" alink="#000066" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0"><div align="center"><font face="Arial, Helvetica" size="2" color="#000000"><br><br>
<table border="1" cellspacing="0" cellpadding="9" width="280" bgcolor="#ffffff" bordercolor="#aaaaaa">
<tr>
<td align="center"><font face="Arial, Helvetica" size="2" color="#000000">Testing
your Internet connection<br>
<script language="JavaScript">
<!--
var timeStart = RightNow();
document.write('<img src='+uniqueImageUrl()+'width=5 height=5 onload="CalculateSpeed(timeStart);">')
// -->
</script>

beetle
10-23-2002, 03:22 PM
Well, I can't says that I know why the author subtracts .15 seconds, but I can tell you why the division by 1000 takes place.

Javascript times are in milliseconds...so a division of 1000 is necessary to calculate seconds.

robocop
10-23-2002, 03:24 PM
:thumbsup:

Yep. Just kinda figured that out....another guy here at work didnt know why they subtracted .15....now i gotta figure out the math.....damn...im a designer....math and i dont get along....

id like anything under 10/k second to go to the slow page, everything else above that to the 'fast page'

can you help?

kc

scroots
10-23-2002, 08:45 PM
i once considerd this and found there was no fixed way of determing a users bandwidth, i have just had a new thought on it though.
i`ll think n try.

scroots

robocop
10-23-2002, 09:15 PM
SWEET MAN....thanks a bunch......i did get a script to work.....ill post it here......(attatched)......but yeah, i tested it on AOL 56k and on our T1 here @ work...so far, on mac and pc, the aol goest to the slow page, and the T1 goes to the "fast page".....try it here: http://www.pyroblue.com/kc/speedtest/

but hey....im a designer, so math is EXTREMELY puzzling for me....so could someone figure out what number to put in the (if kbytes = <4) statement that would reflect less than 15k/second....i cant figure it out for the life of me......anyway, im attatching the file and pasting the script...here it is :


-----------------------------------------------------------------------------------------------

<html>
<head>
<title>Speed Check</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="resource-type" content="document">
<link rel=stylesheet href=../css/all.css type="text/css" media=screen>
<link rel=stylesheet href=../css/new.css type="text/css" media=all>
<script language="JavaScript">
<!--

if (top.location != location) top.location.href = location.href;

// -->
</script>
<base target="_top">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<script language="JavaScript">
<!--

function RightNow() {
time = new Date();
return time.getTime();
}

function CalculateSpeed(timeStart) {
timeEnd = RightNow();
timeElapsed = (timeEnd - timeStart)/1000;
kbytes = 4/timeElapsed;
kbits = kbytes * 1.024 * 8;
if (kbits < 35) location.href = "http://www.pyroblue.com/kc/speedtest/slow.html";
else location.href = "http://www.pyroblue.com/kc/speedtest/fast.html";
}
function uniqueImageUrl(){
randomNum =Math.round(Math.random()*10000);
//--- alert(randomNum);
var link = "http://www.pyroblue.com/kc/speedtest/60.jpg?uniq="+randomNum;
//--- alert(link);
return link;
}

// -->
</script>
</head>

<body bgcolor="" text="#000000" link="#000066" vlink="#000066" alink="#000066" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
<div align="center"><font face="Arial, Helvetica" size="2" color="#000000"><br>
<br>
<table border="1" cellspacing="0" cellpadding="9" width="280" bgcolor="#ffffff" bordercolor="#aaaaaa">
<tr>
<td align="center"><font face="Arial, Helvetica" size="2" color="#000000">Testing
your Internet connection<br>
<script language="JavaScript">
<!--
var timeStart = RightNow();
document.write('<img src='+uniqueImageUrl()+'width=5 height=5 onload="CalculateSpeed(timeStart);">')
// -->
</script>
</font></td>
</tr>
</table>
</font></div>
</body>
</html>



-----------------------------------------------------------------------------------------


THANK YOU ALL FOR YOUR HELP!!

kc

whammy
10-24-2002, 01:44 AM
There is no way of determining a user's bandwidth unless you calculate how long it takes for them to download a file (i.e. start time and end time)... and that result has to be returned to the server, so it's impossible to do it using javascript.

And it still won't be accurate due to the number of hops the user has to take to connect with your server, and any latency along the way (not to mention any bandwidth hogging programs the user may have installed). You can only get an average.

You might want to check out some sites like "speedguide.net" and see how they do it... however their results are not accurate either since your latency is totally dependent upon the connections from YOUR computer to the REMOTE HOST. :D

beetle
10-24-2002, 01:47 AM
Originally posted by whammy
There is no way of determining a user's bandwidth unless you calculate how long it takes for them to download a file (i.e. start time and end time)... and that result has to be returned to the server, so it's impossible to do it using javascript.

And it still won't be accurate due to the number of hops the user has to take to connect with your server, and any latency along the way. You can only get an average. True, but I think the acutal task here is to determine a slow or fast connection, and not the users actual bandwidth. The processes javascript has, I believe, are sufficient enough for this.

P.S. I was trying my darndest to beat you to 1000 posts, but you just got it. :( Grrrr. :p

whammy
10-24-2002, 01:51 AM
Heh. Cool... I don't care about how many posts I have, I'm just trying to help people and keep them from being confused.

The latter part is hard (with the plethora of technologies available for us to use - and you're much more of a javascript guru than I, although I know the programming basics and have actually managed to write some pretty good cross-browser scripts)... but if I can help someone along the way, then I'm doing something right. :D

beetle
10-24-2002, 02:09 AM
Hehe, I was kidding of course :D

whammy
10-24-2002, 02:13 AM
I know. I didn't realize it, but I'm actually the top poster on the forums, and I have been... ;)

I guess I should try to moderate my temper a little bit more. :D

robocop
10-24-2002, 03:17 PM
Hey there yall.....i just tested this baby, and it appears to work fine on all platforms, all browsers, etc.....if yall have time, could you look over my code to see if you spot anything? im VERY new at JS.....im more into actionscripting w/ flash, but these are REALLY close....


kc

ConfusedOfLife
10-24-2002, 09:44 PM
hehe! your second code shows that you deleted your problem! ( that minus .15!!! never mind, ppl usually do this! ), but I have another question, why do you devide 4 by your elapsed time to get the kilo bytes??? What's that 4? And don't you think that it could be enough for your redirection and you can make your decision based on that and you don't need the kbits?

PS: I know flash and you can see it in my site ( it's really ugly! ), and I wrote a super scientific calculator with action's script, but whatever I used was just if and for, I mean normal C commands! Can you introduce me to a source or something on actions script ( not it's help please! I can't go through all of that! )

beetle
10-24-2002, 10:01 PM
Originally posted by ConfusedOfLife
PS: I know flash and you can see it in my site ( it's really ugly! ), and I wrote a super scientific calculator with action's script, but whatever I used was just if and for, I mean normal C commands! Can you introduce me to a source or something on actions script ( not it's help please! I can't go through all of that! ) Post a new thread for this....

robocop
10-24-2002, 10:11 PM
the "4" is the size of the file that im hitting them with....."4K"

you wanna do the math and see if mine is right? just to double check....id like everything below 15k/second to be directed to the slow page ....

ConfusedOfLife
10-24-2002, 10:16 PM
Yeah, I know that I could write it in a private message or something, but we don't have a flash thread that I can write it there, maybe in general thread. I just thought that when you two are talking about the number of your posts, it mightn't be that bad if I ask my flash question! ( no offence, ok? )

beetle
10-24-2002, 10:45 PM
No harm, no foul

Check this out (http://www.sitepointforums.com/forumdisplay.php?s=&forumid=150)

robocop
10-24-2002, 11:01 PM
im not sure i grasp what youre saying.....im doing this in JS...not flash.....we decided that flash wasnt the best option.....

beetle
10-24-2002, 11:09 PM
Originally posted by robocop
im not sure i grasp what youre saying.....im doing this in JS...not flash.....we decided that flash wasnt the best option..... My above message is for ConfusedOfLife

robocop
10-28-2002, 05:20 PM
Hey there...has anyone out there tested this at all?

ConfusedOfLife
10-29-2002, 08:40 AM
I'll check it tonight, word!

robocop
10-29-2002, 03:25 PM
sweet....if anyone else out there wants to give'r a test run.....SWEET :)

kc

Roelf
10-29-2002, 07:14 PM
all i see is an ugly dude on a nice harley, tellin me to slow down a bit. I'm on cable, average download speed approx. 200 Kb/s

robocop
10-29-2002, 07:18 PM
yep. must work for you then.....fast users get the guy on the harley.....

slow users get : http://www.pyroblue.com/kc/speedtest/slow.html
fast users get: http://www.pyroblue.com/kc/speedtest/fast.html

all that is is basically a page redirect after the javascript acertains your speed....so....you could auto-send them to the 'fast' page....or the 'slow' page. the biker is just for a test......

kc

Roelf
10-29-2002, 07:27 PM
Originally posted by robocop
the biker is just for a test......

I knew that :D

robocop
10-29-2002, 07:28 PM
Ok....i figured you did....but sometimes....ya just never know :)

kc:thumbsup:

ConfusedOfLife
10-30-2002, 05:35 AM
It's working for me too! I get that little boy on bycicle, and it means that my speed is so slow :( But to be honest I still can not understand that division ( 4 / elapsedTime ) and I think that elapsedTime only can do the trick, and also I saw somewhere else that they use this to find out the CPU's speed for finding out the number of frames per second that they should use in their animation! But something is for sure robocop, your math is certainly better than me! do not belittle yourself!

beetle
10-30-2002, 01:14 PM
The divison by 4 is a byte to bit conversion, methinks.

robocop
10-30-2002, 03:08 PM
hey there, the number 4 is the size in kilobytes of the image that they are loading....once the image loads, the timer ends......

if you got the bike, are you on a slow connection?

kc

ConfusedOfLife
10-31-2002, 07:06 PM
bingo! Now I can understand! ( I was blind I think ), just one more question, why did you say if it's bigger than 35 then it redirects to a page? what's that 35?

PS:I'm on a very slow connection!

Thai
01-30-2003, 03:31 PM
I'm trying to figure this out myself... My question is, with that script there, does the <no cache> part of the script makes it so that the user is not caching the page so that the script determines there connection time each time?

My search on the net found this one:


<script type="text/javascript">
<!--
//Bandwidth Redirect ver:1.0
function setBandwidth(whichVal){
var expdate = new Date();
expdate.setTime(expdate.getTime() + (60*60*24*365));
document.cookie = 'setbandwidth=' + whichVal + '; expires=' + expdate.toGMTString() + '; path=/';
}
BCt=new Date();
BCs=BCt.getTime();
function bandwidthDetect() {
var args=bandwidthDetect.arguments;
var el=(document.layers)?document.layers['Layer1'].document.Myimage1:document.Myimage1;
if(el.complete){
BCt=new Date();
BCe=BCt.getTime();
BCd=(BCe-BCs)/1000;
if(document.cookie.indexOf('setbandwidth=1')>=1) {
location.href=args[1];}else
if(document.cookie.indexOf('setbandwidth=2')>=1) {s
location.href=args[2];}else
if(BCd<args[0]){setBandwidth(1);location.href=args[1];}else
if(BCd>=args[0]){setBandwidth(2);self.location.href=args[2];}
}
}
//BRD_End
//-->
</script>
</head>

<body text="#333333" link="#0066CC" vlink="#0066CC" alink="#FF0000" onload="bandwidthDetect('4.5','http://www.url.com/fast.html','http://www.url.com/slow.html')">

<div id="Layer1" style="position:absolute; left:0px; top:0px; width:174px; height:60px; z-index:1; visibility: hidden">

<img src="imagetoload.gif" alt="loadImage" name="Myimage1" width="174" height="60" border="0" id="Myimage1" /></div>

<div align="center">
<p>Determining your connection speed.... </p>
</div>
</body>
</html>


This one sets a cookie on the users computer instead... what is more efficient and reliable? Should I stick with this code or use the one on this thread? What do you guys think?

Thanks.

ConfusedOfLife
01-30-2003, 08:20 PM
There are some parts in this code that I want to explain:

It doesn't determine the size of the picture that it puts in the page to do the calculations. So, it means that downloading a 10GB picture is equal to downloading a 10KB picture! ( Do we have a 10GB picture?! I really want to see that!! )
Why does it write into a cookie? Having a cookie in a computer doesn't mean that you're always making a connection like your previous connection! Also if it wanted to decide based on the previous time speed, why doesn't it check the cookie at the beginning and then do the math if it doesn't exist?
In the following code:

if(document.cookie.indexOf('setbandwidth=1') >= 1 )

Does he mean :

if(document.cookie.indexOf('setbandwidth=1') != -1 )

coz he's writing at the begining of the cookie and even if he writes one million times with the same name, it'll be just rewritten and we do not have a counter in here.


And answering your question whether the previous code checks for caching, I have to say yes, it does it twice! Once in these headers that make the browser download the page again even if they're visited before:

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">

And another time when choosing the pictures, coz as you noticed it has lots of pictures and it chooses one of them randomly ( by using randomNum ).
I personally think that using robocop's code is better!

Thai
01-30-2003, 08:23 PM
Thanks! That helped a lot!

Tails
01-30-2003, 08:26 PM
I hope this is not off topic, but I have some questions about bandwidth. What exactly is it? My site says I have used 171 MB out of 1.0 GB. That's about 17%. Does this mean that (at this rate) my site will die in a year or 2? It is a free tripod site by the way. It would seem too bad to be true that a page on a free server has a life span like this. Is this "used up bandwidth" an annual thing? Will it be set to zero in a year or some amount of time? Maybe it will slowly go down? Or is this a circumstancial situaion that doesn't apply to bandwidth at all and is all tripod's doing to force people with no money to use their upgraded packages? I have to know. As a first and very successful web site, http://XFox_Prower.tripod.com, I have to know all I can about this problem. Other places like Angelfire don't seem to limit that, right? Someone please get back to me.

beetle
01-30-2003, 09:31 PM
Well, I don't know how tripod operates, but based on what you are telling me, you have transeferred 171MB worth of data to the surfing community. Once that total reaches 1.0GB, then tripod will freeze your account until 1) you pay them 2) you wait for your period to end/start over. You'd have to check with tripod to know if your 1GB allocation is monthly, yearly, or whatever.

Why don't you just pay for some decent hosting? Even a high-school kid should be able to scrape up 5-10 bucks a month.

ConfusedOfLife
01-30-2003, 09:47 PM
I found this for you ( thanx to me! ) :
http://slate.msn.com/default.aspx?id=2550

Tails
01-31-2003, 08:16 PM
I am a high school kid and I can't even scrape that much up in a year. I don't see any documentation on the tripod thing either. There's no faq area or person to mail questions to, which usually means fraud. So it can't be fraud if you're not tricked into paying their services. Good filemanager, bad service.

Tails
01-31-2003, 08:28 PM
Ok, the tripod stuff says it is monthly bandwidth. But I don't know how I can get so much in so little time. I mean, one page was at the top of the chart while other pages in the month were 1/10th of that. The only thing that can excuse such mysterious behavior is a site copier via ftp mirroring the site and I have not done that in a long time due to big files I don't care to backup or update often (I have plenty backups) sucking up the bandwidth. But maybe that is the problem. I wish I had a way to see what files or pages were the item of everyone's interest. All I get is good feedback though atleast. Any advice for anything? Thanks for all of your help.

beetle
01-31-2003, 08:52 PM
You'd have to have statistics reporting or something similar to know what pages/files are the most requested.

Seriously, you can't afford $7/mo (what I pay for hosting)? That's less than two trips to McDonalds.

Tails
02-01-2003, 05:47 PM
I'm poor, ok? McDonalds is a luxurious occasion where I am.

ConfusedOfLife
02-01-2003, 07:26 PM
Ok, I am not rich either! But you know what I do? I use others space! Yeah, they're lots of ppl who come to the company and the space that they want is really more than what they actually use! So, I use that space for them! If you look at the addresses that I give in different posts, you see that they're from different domains! What is the reason you think?!

Anyways, even though you can't have a homepage in this way, but you can upload things that you want! In Farsi we have a idiom that says : A jar maker always drinks water from a broken jar! So, most of us, in any profession, do not care that much about ourselves in that special field that we work. But I am registering my domain by asking my boss to give me domain and hosting for my next project. So, if you really need a place to show up, then you can do this.
But personally I don't like a weblog that much! I just wana have it for having my own customers! So, you wana have it, you need it, it's a good practice: Don't get money for one of your works and instead ask for a website from your boss!

Tails
02-01-2003, 07:37 PM
I always have this fear that something will go wrong and once I miss a payment, my site dies, I lose all the stuff I put into it, and even if I get it back, people think its down and never go back. This is a very big limitation of the internet.

ConfusedOfLife
02-02-2003, 08:29 PM
You have to be a little bit more optimistic my friend! And I didn't mean that you pay for a monthly registration, you can save money till you can register in yearly basis and then you will have no worries! Come on! Internet is a lot easier!

jreitz
03-04-2003, 08:20 PM
I am wondering if this same idea can be applied to Images or Flash objects. I have a header Flash object on my site, I would like for the users bandwidth to determin weather it gets loaded or a static image gets loaded. (making slower conections not suffer the flash... ) Can this set all the headers for all the pages (which are the same) to the same choice?

Maybe there is a better way?

John

crubbles
03-04-2003, 08:49 PM
Being a fellow high school student I sympathize with you Tails.

However, I have found some hosts that are a Godsend for poor people like us:

http://directnic.com
Hosting: $15 a year
Domain:$15 a year
Only downside to them is their server side capabilities....your limited to client side scripting

http://supar.org
Hosting: $35 a year
Domain: $7.95 a year
Excellent Basic package...120 MB of space, 7GB of bandwidth, unlimited subdomains, cgi, php.. pretty much everything really...

http://lonex.com
Hosting: $40 a year (if you sign up as a "reseller" ;) )
Domain: $7.95 a year
400 MB of space, 10GB of bandwidth, and all the goodies you could want

ConfusedOfLife
03-05-2003, 06:18 AM
It's a better for to detect flash, check this (http://www.codingforums.com/showthread.php?s=&threadid=15299) out!

jreitz
03-05-2003, 05:40 PM
ok sure, that works, but only for flash detection. Im, talking about bandwidth detection.... but maybe this can be combined in someway with that... any ideas out thee for the ultimate detection tool

beetle
03-05-2003, 06:40 PM
This works, but you'd need to have them load a decent-sized image to get a reliable reading. And, you (obviously?) won't know what their bandwidth is until after that image has been loaded.

http://www.peterbailey.net/test/bandwidth.htm

You'll need to clear you cache to run the test again, accurately.

ConfusedOfLife
03-05-2003, 07:32 PM
It's going to be a very long thread! I personally think that it's better that you provide 2 options for your visitors, one HTML and one Flash if the site is really worth it. If not and you're not trying to answer everyone, it's better that you do whatever you think is better! And it's not really important to detect if someone has Flash, coz if they wana work with Internet, sooner or later they have to download it! So, it's better that they do it in your site for the first time!