Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Would this result be accurate for testing download speed?

const string tempfile = "5MB.zip";
WebClient webClient = new WebClient();
Uri uri = new Ur("http://download.thinkbroadband.com/5MB.zip");
Console.WriteLine("Download test starting please wait....");
Stopwatch sw = Stopwatch.StartNew();
webClient.DownloadFile(uri, tempfile);
sw.Stop();
FileInfo fileInfo = new FileInfo(tempfile);
long speed = fileInfo.Length / sw.Elapsed.Seconds;
Console.WriteLine("Speed: {0} bps ", speed.ToString("N0"));
Posted

Yes, that should work. You might want to run it on multiple URLs and at multiple times of the day to get a more realistic average speed.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Feb-11 13:23pm    
Correct, my 5.
I prefer a bit different and straightforward way of timing (in my answer)
--SA
Nish Nishant 11-Feb-11 13:29pm    
Okay, I have some comments on that. I'll comment on your answer.
Better use DataTime:

C#
System.DateTime before = System.DateTime.Now;
//download all
System.DateTime after = System.DateTime.Now;
System.TimeSpan duration = after - before;
double seconds = duration.TotalSeconds;
double milliseconds = duration.TotalMilliseconds;


Pay attention: your accuracy is the same, no matter what units you use, duration is calculated into double value.

—SA
 
Share this answer
 
v2
Comments
krazyazkid 11-Feb-11 13:29pm    
Thank you for your quick response :)
Nish Nishant 11-Feb-11 13:31pm    
Do see my comment response to Kryukov too. StopWatch has much better resolution compared to using DateTime.Now, so your approach is correct (and should not be changed).
Nish Nishant 11-Feb-11 13:30pm    
DateTime is not as accurate as StopWatch. So for a speed test, I would certainly use Stopwatch ahead of comparing DateTime values.
Sergey Alexandrovich Kryukov 11-Feb-11 14:06pm    
Really? I did not know that. So, how the accuracies compare?
(The accuracy was enough for my tests, because I intentionally selected longer tests.)
Thank you.
--SA
Nish Nishant 11-Feb-11 18:01pm    
Kryukov, see http://msdn.microsoft.com/en-us/library/system.datetime.now.aspx

From that link: "The Now property is frequently used to measure performance. However, because of its low resolution, it is not suitable for use as a benchmarking tool. A better alternative is to use the Stopwatch class."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900