Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create a Download speed test with .NET

Tags:

c#

I'd like to create a speed test to test the connection.

What I would like is a 15sec download which then gives me the average download speed.

Anyone knows how to create this? or has a better idea to make a speed test?

like image 798
Aussie Ausbourne Avatar asked Jul 05 '09 14:07

Aussie Ausbourne


People also ask

How do I test my internet speed using CMD?

In Windows, click the start menu and type "CMD" to access the command prompt. Type "ping google.com" and choose "enter" to execute the function and retrieve a list of results. The results have several lines of data that show the speed along with other metrics.

How do I test my internet download speed?

Some of the more popular speed test services include Speedtest.net, Fast.com or CloudFlare. Whether you install an app or use a website, it's a good idea to run the test a few times to get a sense of your connection's performance.


2 Answers

This sample will try to download googletalk, and then outputs details of the download.

ps. when trying to time and operation avoid using DateTime as they can cause problems or inacurecy, always use Stopwatch available at System.Diagnostics namespace.

const string tempfile = "tempfile.tmp";
System.Net.WebClient webClient = new System.Net.WebClient();

Console.WriteLine("Downloading file....");

System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
webClient.DownloadFile("http://dl.google.com/googletalk/googletalk-setup.exe", tempfile);
sw.Stop();

System.IO.FileInfo fileInfo = new System.IO.FileInfo(tempfile);
long speed = fileInfo.Length / sw.Elapsed.Seconds;

Console.WriteLine("Download duration: {0}", sw.Elapsed);
Console.WriteLine("File size: {0}", fileInfo.Length.ToString("N0"));
Console.WriteLine("Speed: {0} bps ", speed.ToString("N0"));

Console.WriteLine("Press any key to continue...");
Console.ReadLine();
like image 80
kay.one Avatar answered Oct 22 '22 07:10

kay.one


As publicENEMY says, the kay.one's answer could give a wrong speed, because the HDD's speed can be lower than the network speed (for example: Google Gigabit Fiber is much faster than a 5200rpm HDD's average write speed)

This is an example code derived from the kay.one's answer, but downloads the data content into a System.Byte[], and therefore in memory.

Also i notice that after the very first download, the speed increases dramatically and jumps over the real network speed, because System.Net.WebClient uses the IE's download cache: for my requirements i only add the t querystring parameter, clearly unique for each request.

EDIT

as.beaulieu finds an issue using TimeSpan.Seconds for the calculation, both for very fast and very slow downloads.

We just need to use TimeSpan.TotalSeconds instead.

Console.WriteLine("Downloading file....");

var watch = new Stopwatch();

byte[] data;
using (var client = new System.Net.WebClient())
{
    watch.Start();
    data = client.DownloadData("http://dl.google.com/googletalk/googletalk-setup.exe?t=" + DateTime.Now.Ticks);
    watch.Stop();
}

var speed = data.LongLength / watch.Elapsed.TotalSeconds; // instead of [Seconds] property

Console.WriteLine("Download duration: {0}", watch.Elapsed);
Console.WriteLine("File size: {0}", data.Length.ToString("N0"));
Console.WriteLine("Speed: {0} bps ", speed.ToString("N0"));

Console.WriteLine("Press any key to continue...");
Console.ReadLine();
like image 31
T-moty Avatar answered Oct 22 '22 08:10

T-moty