Search code examples
c#reflectionsystem.diagnostics

Performance Check for C# Method Calls


i want to create a class which write a file with all Method Calls in hierarchy structure and needed Time from my Program. I have no idea how to resolve this issue. All i found is the StackTrace and StackFrame class but this not really what i need. I want to start my programm and do some action. In background my class write a file (XML for example) with following or similar structure:

Method1: 220ms
  Method2: 180ms
    Method3: 150ms
  Method4: 25ms
    Method5: 20ms
Method6: 110ms

Is there a simple way to do this? Iam actualy searching in System.Diagnostics and System.Reflection Namespace but i got no idea how to do this.

I have not written a single line of code up to this time because i need a good hint.

Thanks in advance.


Solution

  • You might try Mini Profiler. I usually use it to trace execution time.

    Then to trace a method do this ( as mentioend in the docs) -

    using StackExchange.Profiling;
    ...
    var profiler = MiniProfiler.Current; // it's ok if this is null
    using (profiler.Step("Set page title"))
    {
        ViewBag.Title = "Home Page";
    }
    using (profiler.Step("Doing complex stuff"))
    {
        using (profiler.Step("Step A"))
        { // something more interesting here
            Thread.Sleep(100);
        }
        using (profiler.Step("Step B"))
        { // and here
            Thread.Sleep(250);
        }
    }
    

    It has a nice interface to show you how much time each step needed for web -

    enter image description here

    For windows and console based application use the windows extension mentioned here -

    http://nootn.github.io/MiniProfiler.Windows/

    and the process is mentioned here -

    http://www.nootn.com.au/2012/07/performance-profiling-console-or-winwpf.html#.U8i-TDS1bcg