A Simple But Effective Speed Comparison A Simple But Effective Speed Comparison

A little while ago, I wrote a simple .NET application that performs X amount of requests and calculates an average speed of those requests.� It does this by dropping the highest and lowest request times, then taking an average speed on the remaining requests.

This does a decent job for a straight up speed test.� However, a few possibilities could arise, such as CPU hogging that could skew the results.� Instead, I've made a few alterations and converted the speed tester to not be based on the number of requests, but instead based on a specific amount of time.� This should help eliminate some inconsistencies of doing a straight number of requests.


�If I look at the last weeks blog about require versus automatic loading of files, the results are quite similar, but I feel like it's a more accurate test.

For keeners, here is a link to the Github repo of the code: https://github.com/endyourif/TimeBasedTester

The simplicity of the code is right here, where it creates a new System.Timers.Timer.� The timer is started for a specific interval.� While the interval is executing, the process which we wish to test is continuously running and incrementing a counter.� When the timer is completed, it is stopped and the results are displayed:


using System;
using System.Timers;
namespace TimeBasedTester
{
class LinkChecker
{
private Timer _timer;
private bool _isRunning = true;
private int _timesExecuted = 0;
public void Check(string url, int numOfSeconds)
{
try
{
Console.WriteLine("## Starting to check - " + url + " ##");
_timer = new Timer(numOfSeconds*1000);
_timer.Interval = numOfSeconds*1000;
_timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
_timer.Start();
do
{
CheckLink.ReadUrl(url);
_timesExecuted++;
} while (_isRunning);
}
catch (Exception ex)
{
Console.WriteLine("## Unknown Exception " + ex.Message + " ##");
}
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
_timer.Stop();
_isRunning = false;
Console.WriteLine("## Finished.� Executed: " + _timesExecuted + " times. ##");
}
}
}

The nice part of this code, is by replacing the process inside of the do/while loop, you can effectively perform a speed test on much more than just websites!

Photo courtesy of�ekosimbah

Published on Sep 4, 2012

Tags: Optimization | ASP.NET MVC and Web API Tutorial | timer | speed comparison | Theory

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.