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?
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.
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.
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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With