Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Simple Speedtest Application

0.00/5 (No votes)
18 Oct 2007 1  
In this article, you will learn how to create a simple speedtest app.
Screenshot - speedtest.jpg

Introduction

Welcome to my first article here on CodeProject.
In this article, you will learn how to create a simple speedtest console application that calculates your download rate by downloading a file from my ftp-server.
You can, of course, use your own server if you want to.

You will learn how to use a WebClient to download files and save them in a specific location on your hard drive.

Please bear with my English.

Using the Code

To download a file from a webserver, you need to use a WebClient.
To create a new object of type WebClient, you first need to add this reference at the top of your program:

using System.Net;

Then you create the WebClient in the Main() method:

WebClient wc = new WebClient();

Next, we need to define a URL that the webclient will download the file from.
This is done in the following way:

Uri URL = new Uri("http://sixhoej.net/speedtest/1024kb.txt");

Now, we are almost ready to download the file. But before we start downloading, we must get the current tickcount. Otherwise we won't be able to calculate download time and download rate. To get the current tickcount, we'll do this:

// get current tickcount
double starttime = Environment.TickCount;

The tickcount tells us how many milliseconds have passed since the system was started.
Therefore it is great to calculate how long a certain operation took, since it is so precise.

Now, we can start downloading. To start downloading, we will call the wc.DownloadFile() method, passing in the URL to download from, and the location to save the file:

// download file from the specified URL, and save it to C:\speedtest.txt
wc.DownloadFile(URL, @"C:\speedtest.txt");

The application is blocked while the download is running. We can prevent that by using wc.DownloadFileAsync() instead, but then we wouldn't be able to calculate the download time and download rate. But in this case, it doesn't matter that the app is blocked, since it can't take any input from the user anyway.

When the downloading is finished, the application will be unblocked, and we are then able to calculate download time and download rate. First, we need to get the current tickcount one more time to calculate the download time:

// get current tickcount
double endtime = Environment.TickCount;

Then, we will calculate the number of seconds it took to download the file:

// how many seconds did it take?
// we are calculating this by subtracting starttime from endtime
// and dividing by 1000 (since the tickcount is in milliseconds.. 1000 ms = 1 
// sec)
double secs = Math.Floor(endtime - starttime) / 1000; 

// we also need a variable that shows the number of seconds
// without the decimal point. Therefore, we are rounding the value
// of 'secs'. 0 means that zero decimal numbers should be shown.
double secs2 = Math.Round(secs, 0);

Now, we have come to the exciting part.. How do we calculate the download rate?
Well, it's actually quite easy. We just divide 1024 by the number of seconds it took to download the file.

Now you're probably asking: "Why 1024?" - That's because 1024 bytes equals 1 kilobyte!

// calculate download rate in kb per sec.
// this is done by dividing 1024 by the number of seconds it
// took to download the file (1024 bytes = 1 kilobyte)
double kbsec = Math.Round(1024 / secs);

The only thing we need to do now, is to show the results to the user:

Console.WriteLine("\nCompleted. Statistics:\n");

Console.WriteLine("1mb download: \t{0} secs ({1} secs)", secs2, secs);
Console.WriteLine("Download rate: \t{0} kb/sec", kbsec);

Console.WriteLine("\nPress any key to exit...");
Console.Read();

And we're almost done. But before the application closes down, it needs to delete the file it just downloaded. That is done in this way:

Console.WriteLine("Cleaning up... (deleting downloaded file)");
try
{
   // delete downloaded file
   System.IO.File.Delete(@"C:\speedtest.txt");
   Console.WriteLine("Done.");
}
catch
{
   Console.WriteLine("Couldn't delete download file.");
   Console.WriteLine("To delete the file yourself, go to your C-drive and " + 
                     "look for the file 'speedtest.txt'.");
   Console.ReadKey();
}

And that's it!

Thank you for reading my first article. Please let me know what was good, what was bad, and what I need to improve.

Thank you.

Points of Interest

When I finished coding this app, I was amazed at how easy it was to create a speedtest.
I had no idea that such an advanced thing would be so easy to code (I'm thinking of those sites like speedtest.net and others - Alright, they are a lot more advanced than this program, but I'm still amazed).

History

  • 18 Oct 2007: First version
  • 19 Oct 2007: Second version - Now it loads the file directly into the computer's memory, instead of saving it on the hard drive.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here