Skip to content

Instantly share code, notes, and snippets.

@KevM
Created February 18, 2015 19:47
Show Gist options
  • Save KevM/27a41aca605cf50593b9 to your computer and use it in GitHub Desktop.
Save KevM/27a41aca605cf50593b9 to your computer and use it in GitHub Desktop.
Wrote this quick little performance testing harness. Guessing this is a C# right of passage kind of thing.
public static void MeasureMe(Action action, int count=100, int warmup = 10, int runs = 1,string messageFormat = "It took {0}ms to do this.")
{
Enumerable.Range(0, warmup).Each(i =>
{
action();
});
Enumerable.Range(0, runs).Each(r =>
{
var watch = new Stopwatch();
watch.Start();
Enumerable.Range(0, count).Each(i =>
{
action();
});
watch.Stop();
var output = String.Format(messageFormat, watch.ElapsedMilliseconds);
Console.WriteLine(output);
});
}
@KevM
Copy link
Author

KevM commented Feb 18, 2015

Example usage:

var menuSource = Container.GetInstance<MyWorkMenuSource>();
var menuSource2 = Container.GetInstance<MyWorkMenuSource2>();

MeasureMe(() =>
{
    menuSource.GetMenu();
}, count:100, runs:3, messageFormat:"Original:{0}");

MeasureMe(() =>
{
    menuSource2.GetMenu();
}, count: 100, runs: 3, messageFormat: "Replacement:{0}");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment