StrataFrame Forum

Profiling Example

http://forum.strataframe.net/Topic27390.aspx

By Greg McGuffey - 6/10/2010

This is a sample project to show how to add profiling to an application. This was prompted by a question regarding how to speed up the startup of an application (http://forum.strataframe.net/FindPost27389.aspx).



This profiler implements IDisposable to make it very easy to profile a block of code:



public void SomeMethod()

{

  using (Profiler profile = new Profiler(System.Reflection.MethodBase.GetCurrentMethod()))

    //-- Code being profiled

  }

}




You can nest profilers if necessary. You can also just mark a place in code:



//-- in a block of profiled code

profile.AddMark("Label indicating location in code");




The profiler uses a logger to actually track the profile messages. This uses an interface to allow you to create a logger that meets the needs of the specific app. This could log to a file, to a database, to a web service. The sample has three included. Two are form based and one is MessageBox based (used to debug the profiler initially).



The profiling code can be left in source, as it can easily be turned off, add almost no overhead. You can also use command line arguments to control profiling. This is handy if you need to profile a specific client app, but want it off otherwise. You'd use something like:



myapp.exe --EnableProfiling




to turn on profiling. You can also turn it on for only specific types:



myapp.exe --EnableProfiling:MyApp.Tools.Widget,MyApp.UI.WidgetForm




This will enable profiling in any method in either the MyApp.Tools.Widget class or the MyApp.UI.WidgetForm class (of course, you have to add the profiling code as desired to the code itself).