Benchmark code blocks easy

Have you been testing code speed like this?


var start = DateTime.Now;
int i;
for(i = 0; i < 1000; i++)
{
    DoSomething();
}
var stop = DateTime.Now;
var total = stop - start;
var timePerIteration = total.Ticks / i;

Or maybe you’ve found the Stopwatch class and been happy with its superior time precision?

Better, I’d say, but I’ve quite had it. Plus I needed to benchmark a local website and needed to test parallel requests (something similar to ab – ApacheBench)

What does a programmer in such a case? Writes his own tools! 😛 That’s how BenchmarkNET appeared. Using BenchmarkNET you can write the same thing as above only much shorter and with a better timing precision :

var result = Benchmark.Sequentially(() => DoSomething(), 1000);

Neat? That’s not all. The result can easily printed out to a console or inspected with a debugger :

Benchmarking an HTTP operation over 10 parallel threads, 100 times for each thread?

var dl2 = Benchmark.Parallel(new BenchmarkParams<WebClient>(c => c.DownloadString("http://localhost/"), 100), 10, () => new WebClient());

The project has been published as open source (LGPL license) on CodePlex. You can discuss it, file bugs, or even contribute to it.

Have fun and if you test it out, please leave some feedback!

Later edit : It seems some people already dig it 😉

  1. Bogdan Ungureanu

    And your employer knows about this BenchmarkNET and approves it? 😈

  2. . Isn’t it just 100 times, according to the example?

    Otherwise a really flexible alternative to stopwatch. Good job!

  3. .NET 4.0 has a more useful benchmarking mechanism, with which you can accurately determine processor and memory utilization. Unfortunately, I don’t remember the name of the class. All I remember is that I used it once and it placed the application in profiling mode, therefore it was terribly slow.

    • Stopwatch? CLR Profiler?

    • Yes it is possible, just add the WCF seivrce (*.svc + seivrce dll) to the ASP.NET project. IIS will be able to serve request for both. Make sure you put the required configuration into your web.config.It is not the only solution but it should be easy to implement.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.