How to Create a Download speed test with .NET

c#

Solution

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();

Problem

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?

Original source