Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / MSIL

Performance Testing

3.32/5 (9 votes)
29 Sep 20052 min read 1   194  
Attach performance hooks to an assembly through a command line utility.

Introduction

Last week I was tasked with improving the performance of our ASP.NET application. Not having much experience with performance testing and after reading a number of articles, I decided to start with some of the more popular tools that were available. I quickly discovered that for whatever reason, the metrics provided by these tools can be quite skewed and in some cases clearly incorrect.

The solution

What I really wanted to do was to create a disposable class and wrap the contents of each method in a using statement.

For example:

C#
private void Form1_Load(object sender, System.EventArgs e)
{
   MessageBox.Show("Test");
}

becomes:

C#
private void Form1_Load(object sender, System.EventArgs e)
{
   using (PerformanceMonitor.PerformanceMonitor pm = 
               new PerformanceMonitor.PerformanceMonitor())
   {
       MessageBox.Show("Test");
   }
}

The constructor and destructor of the PerformanceMonitor class can output to a text file / event log / etc. The PerformanceMonitor can be customized as required.

I thought there’s got to be an easier way of adding in performance logging mechanism that runs better than the tools I had previously tried.

The result is a utility that disassembles an assembly using ILDASM, modifies the MSIL and reassembles the MSIL using ILASM. The end result is that each method has the using statement wrapped around it.

How to use

Before running the utility, the PerformanceMonitor class must be compiled and copied to the same directory as the assembly that is to be modified.

The utility and a sample PerformanceMonitor class are attached. The utility is a command line application with the usage: AttachPerformanceHooks.exe TestAssembly.dll.

This utility can be attached to any un-obfuscated, unsigned assembly. The results obtained so far using the PerformanceMonitor attached are not skewed and do not give incorrect results.

The PerformanceMonitor class will output to a text file in a directory supplied through the application setting: "PerformanceMonitor.LogDir".

Note: For larger assemblies (e.g. 90 MB MSIL) the utility can take a while to run.

Conclusion

So far I have been using this for a week and it has made my job much easier. I hope you will also find this utility useful.

References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here