Mihai Albert
Mihai Albert

Reputation: 1493

Profile Time Spent in the Inner .NET Framework Methods

In Visual Studio - is there a way to profile the time spent in the inner methods of .NET Framework ?

An example - consider a good-old fashioned ArrayList, and adding some random numbers to it:

static void Main(string[] args)
{
    const int noNumbers = 10000;  // 10k
    ArrayList numbers = new ArrayList();
    Random random = new Random(1);  // use the same seed as to make
                                    //  benchmarking consistent
    for (int i = 0; i < noNumbers; i++)
    {
        int currentNumber = random.Next(10);  // generate a non-negative
                                              //  random number less than 10
        numbers.Add(currentNumber); // BOXING occurs here
    }
}

I can step into the .NET Framework source code just fine while debugging. One can use the default Microsoft symbols and the source code for .NET (as described in this answer) or go the dotPeek route (detailed here). As for the cleanest option of just using the Reference Source symbols - as Hans Passant was saying in his answer almost 5 years ago - the framework version (down to security updates installed) for which the symbols were created would have to match exactly to your version; you'd have to be really lucky to get that working (I wasn't). Bottom line - there are 2 ways I can successfully use to step into the .NET source code.

For the sample at hand, there aren't big differences between the Reference Source code and the dotPeek reverse-engineered code - that is for the methods invoked by ArrayList's Add - namely the Capacity setter and ArrayList's EnsureCapacity, of which the latter can be seen below (ReferenceSource code on the left, dotPeek source code on the right):

enter image description here

Running an "Instrumentation" profiling session will return a breakdown of the time spent in each method, but as long as the .NET types go, it appears that one only gets to see the methods the respective code called "directly" - in this case the function that Adds elements to the ArrayList, the one that generates a Random int, and the respective types' constructors. But there's no trace of EnsureCapacity or Capacity's setter, which are both invoked heavily by ArrayList's Add.

enter image description here

Drilling down on a specific .NET method doesn't show any of the methods it called in turn, nor any source code (despite being able to see that very code earlier, while stepping into with the debugger):

enter image description here

How can one get to see those additional, "inner" .NET methods ? If Visual Studio can't do it, perhaps another tool can ?

PS There is a very similar question here, however it's almost 10 years old, and there's not much there that brings light to the problem.

Later Update: As KolA very well points out, JetBrains dotTrace can show this. A line-by-line profiling session below: enter image description here

Upvotes: 2

Views: 573

Answers (1)

KolA
KolA

Reputation: 756

perhaps another tool can ?

DotTrace can profile performance down to properties if that's what you're looking for. This example is for generic List<T> not ArrayList but I think it shouldn't matter.

enter image description here

Upvotes: 2

Related Questions