How to check download speed in asp.net


Description:-

In this article I will explain how to check and find the Internet Connection (Download) speed using C#. A file of known size will be downloaded from a remote server 5-10 times and the time required to download the file is noted. Finally the average of these readings will be used to calculate approximate Internet Connection or Download speed. For more accurate readings, you need to download larger files.

I am downloading a jQuery script file from Google CDN. The size of the file is 261 KB, which I have determined from windows explorer. The download speed of calculated by dividing the size of the file (in KB) with the difference in download start and end time (in seconds).

The above process is repeated 5 times and the result is stored in an array after each iteration. Follow given code to check your internet connection speed (download speed).

Default.aspx:-
<div>
  <asp:Label ID="lblDownloadSpeed" runat="server" />
  <asp:Button ID="Button1" Text="Check Speed" runat="server" OnClick="CheckSpeed" />
</div>

Step 2: Now Call a JavaScript in your webPage

<script type="text/javascript" src="Scripts/advajax.js"></script>

Step 3: Now generate Button Click event to Create Code for Check Download Speed.

Default.aspx.cs:-
protected void CheckSpeed(object sender, EventArgs e)
{
   double[] speeds = new double[5];
   for (int i = 0; i < 5; i++)
   {
      intjQueryFileSize = 261; //Size of File in KB.
      WebClient client = new WebClient();
      DateTime startTime = DateTime.Now;
      client.DownloadFile("http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js", Server.MapPath("~/jQuery.js"));
      DateTime endTime = DateTime.Now;
      speeds[i] = Math.Round((jQueryFileSize / (endTime - startTime).TotalSeconds));
   }
   lblDownloadSpeed.Text = string.Format("Download Speed: {0}KB/s", speeds.Average());
}

NameSpaces:-

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Linq;

Step 4: Now run you WebPage.
Step 5: Click on CheckSpeed button to Check Download Speed.

Related Posts

Previous
Next Post »

Thanks for comments.....