Saturday, April 12, 2008

Bandwidth Meter for Web Application

Article described how to create Bandwidth meter to measure network bandwidth for your web application.

One of the client requested to have a Bandwidth meter for his web application. It was a big challenge to make a bandwidth meter in a browser. After thinking several hours I got an idea to make it using javascript. Here I will share my code to make bandwidth meter using javascript.

When a page is rendered to the browser it send stream of text from the server. Taking this idea i had used javascript and measured time to transfer dummy text of some size. Then I actually convert this time into bandwidth.

Below are few steps to create bandwidth meter using javascript.

1) Take starting time before transfering dummy text

<script language="javascript" type="text/javascript">
time = new Date();
time1 = time.getTime();
</script>

2) Transfer dummy text of 45000 bytes in HTML comment tag. Because we don't have to show this text to user. We are actually using this text to measure time to transfer 45000 bytes to the browser.


3) Here comes real stuff. Now we have start time before transfering 45000 bytes and current time, here calculate time difference which was actually taken to transfer 45000 bytes. Using this time difference calculate actuall bandwidth used to transfer 45000 bytes. Thats it now you are done with your own bandwidth meter :)

<script language="javascript" type="text/javascript">
time = new Date();
time2 = time.getTime();
ttime = time2 - time1;

if (ttime == 0)
ttime = .1;

ttime = ttime / 1000; //thousand mili seconds
kbps = 45 / ttime;
kbps = kbps * 8; // multiply by 8 to make kiloBITS instead of kiloBYTES
kbps = Math.round (kbps);
document.getElementById("lblBandwidth").innerHTML=kbps + " kbps";
</script>

1 comment:

Anonymous said...

It kept the original French Roulette structure, so the wheel had a double-zero pocket. In the USA, roulette premiered in Louisianna round 1850 however was later transferred to Mississippi and the rest of|the the rest of} the nation. Historically, European Roulette has gone 우리카지노 via plenty of hoops to turn out to be the most popular roulette variation these days. But the entrepreneurs and casino operators François and Louis Blanc opened a casino in Germany and later in Monte Carlo, providing roulette. Round Result – The ball will land on quantity of|numerous|a selection of} a selected shade, and the winners might be introduced. Round Start – The croupier throws the ball into the wheel, however you'll be able to|you presumably can} still place bets.