A Simple But Effective Speed Comparison

Published on Sep 4, 2012 by Jamie Munro

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 ofekosimbah

Tags: Optimization | ASP.NET | timer | speed comparison | .net | Theory

Related Posts

blog comments powered by Disqus