Click here to Skip to main content
Licence CPOL
First Posted 21 Dec 2007
Views 27,687
Downloads 214
Bookmarked 17 times

Bandwidth Meter for Web Application

By | 22 Dec 2007 | Article
Create a bandwidth meter using JavaScript to measure network bandwidth.

Introduction

This article describes how to create a bandwidth meter to measure the network speed for your web application.

Background

One of our clients 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 for several hours, I got an idea to make it using JavaScript. In this article, I will share my code to make a bandwidth meter using JavaScript.

Using the code

When a page is rendered to the browser, a stream of text is sent from the server. I used JavaScript and measured the time to transfer some dummy text. I then convert this time into a bandwidth measurement.

Below are the steps to create a bandwidth meter using JavaScript:

  1. Get the starting time before transferring dummy text:
  2. <script language="javascript" type="text/javascript"> 
       time = new Date();
       time1 = time.getTime();
    </script>
  3. Transfer a dummy text of 45000 bytes in the HTML comment tag. We use this text to measure the time to transfer 45000 bytes to the browser.
  4. <!-- Dummy Text to measure transfer speed -->
    <!-- Place dummy text of 45000 bytes (sample included in attachment)-->
  5. Here comes the real stuff. We have the start time before transferring the 45000 bytes, and the current time. We calculate the time difference to get the time taken to transfer 45000 bytes. Using this time difference, we calculate the actual bandwidth used to transfer 45000 bytes. That's it; now you are done with your own bandwidth meter :)
  6. <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>

History

  • 22 December, 2007: Initial version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ritesh Sutaria



India India

Member

Ritesh Sutaria has about 7+ years of IT experience on various technologies like ASP.net, PHP, VB, Oracle, PGSql, Sql Server. He has worked on various platforms including Windows, Linux. He has a powerfull problem solving skills which actually makes complex problems into simple problems.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberNasir Razzaq7:26 13 Feb '10  
QuestionBandwidth is based on the time? PinmemberMichael Sync23:48 21 Dec '07  
AnswerRe: Bandwidth is based on the time? PinmemberJcmorin2:56 22 Dec '07  
AnswerRe: Bandwidth is based on the time? PinmemberRitesh Sutaria9:54 22 Dec '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 22 Dec 2007
Article Copyright 2007 by Ritesh Sutaria
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid