Code performance analysis in Visual Studio 2008

Edit on GitHub

Visual Studio developer, did you know you have a great performance analysis (profiling) tool at your fingertips? In Visual Studio 2008 this profiling tool has been placed in a separate menu item to increase visibility and usage. Allow me to show what this tool can do for you in this walktrough.

An application with a smell…

Before we can get started, we need a (simple) application with a “smell”. Create a new Windows application, drag a TextBox on the surface, and add the following code:

[code:c#]

private void Form1_Load(object sender, EventArgs e)
{
    string s = "";
    for (int i = 0; i < 1500; i++)
    {
        s = s + " test";
    }
    textBox1.Text = s;
}

[/code]

You should immediately see the smell in the above code. If you don’t: we are using string.Concat() for 1.500 times! This means a new string is created 1.500 times, and the old, intermediate strings, have to be disposed again. Smells like a nice memory issue to investigate!

Profiling

Performance wizardThe profiling tool is hidden under the Analyze menu in Visual Studio. After launching the Performance Wizard, you will see two options are available: sampling and instrumentation. In a “real-life” situation, you’ll first want to sample the entire application searching for performance spikes. Afterwards, you can investigate these spikes using instrumentation. Since we only have one simple application, let’s instrumentate immediately.

Upon completing the wizard, the first thing we’ll do is changing some settings. Right-click the root node, and select Properties. Check the “Collect .NET object allocation information” and “Also collect .NET object lifetime information” to make our profiling session as complete as possible:

Profiling property pages

Launch with profilingYou can now start the performance session from the toolpane. Note that you have two options to start: Launch with profiling and Launch with profiling paused. The first will immediately start profiling, the latter will first start your application and wait for your sign to start profiling. This can be useful if you do not want to profile your application startup but only a certain event that is started afterwards.

After the application run, simply close it and wait for the summary report to appear:

Performance Report Summary 1

WOW! Seems like string.Concat() is taking 97% of the application’s memory! That’s a smell indeed... But where is it coming from? In a larger application, it might not be clear which method is calling string.Concat() this many times. To discover where the problem is situated, there are 2 options…

Discovering the smell – option 1

Option 1 in discovering the smell is quite straight-forward. Right-click the item in the summary and pick Show functions calling Concat:

Functions allocating most memory

You are now transferred to the “Caller / Callee” view, where all methods doing a string.Concat() call are shown including memory usage and allocations. In this particular case, it’s easy to see where the issue might be situated. You can now right-click the entry and pick View source to be transferred to this possible performance killer.

Possible performance killer

Discovering the smell – option 2

Visual Studio 2008 introduced a cool new way of discovering smells: hotpath tracking. When you move to the Call Tree view, you’ll notice a small flame icon in the toolbar. After clicking it, Visual Studio moves down the call tree following the high inclusive numbers. Each click takes you further down the tree and should uncover more details. Again, string.Concat() seems to be the problem!

Hotpath tracking

Fixing the smell

We are about to fix the smell. Let’s rewrite our application code using StringBuilder:

[code:c#]

private void Form1_Load(object sender, EventArgs e)
{
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 1500; i++)
    {
        sb.Append(" test");
    }
    textBox1.Text = sb.ToString();
}

[/code]

In theory, this should perform better. Let’s run our performance session again and have a look at the results:

Performance Report Summary 2

Compare Peformance ReportsSeems like we fixed the glitch! You can now investigate further if there are other problems, but for this walktrough, the application is healthy now. One extra feature though: performance session comparison (“diff”). Simply pick two performance reports, right-click and pick Compare performance reports. This tool will show all delta values (= differences) between the two sessions we ran earlier:

Comparison report 

Update 2008-02-14: Some people commented on not finding the Analyze menu. This is only available in the Developer or Team Edition of Visual Studio. Click here for a full comparison of all versions.

Update 2008-05-29: Make sure to check my post on NDepend as well, as it offers even more insight in your code!

kick it on DotNetKicks.com

This is an imported post. It was imported from my old blog using an automated tool and may contain formatting errors and/or broken images.

Leave a Comment

avatar

13 responses

  1. Avatar for Bart
    Bart February 8th, 2008

    Nice post maarten!

    But I do wonder how this profiling stuff acts on really [b]big[/b] applications, where it is much harder to find those 'hotpaths' (I guess ?)

  2. Avatar for maartenba
    maartenba February 8th, 2008

    There's always only one hotpath :-) After you fixed that, you can go and track the next one.

  3. Avatar for Bart
    Bart February 8th, 2008

    I see :)
    I will spend some time in the sandbox this weekend to figure those things out :)

    But damn I keep reading the hotpaths word as ... yeah, you know what I mean :p Let's hope we never have to give any presentation about this topic :p (making a slip is easy is 2 easy on this one ;) )

  4. Avatar for Simone Busoli
    Simone Busoli February 14th, 2008

    This feature is available only in Visual Studio Team Suite or Development edition of Visual Studio Team System.

  5. Avatar for Hagrin
    Hagrin February 14th, 2008

    Maarten -

    Thank you for writing such an informative article. As someone that uses SQL Profiler on a daily basis to look at the performance of SQL scripts, I wasn't aware of this functionality in 2008 and I am very glad to see it. Thank you for making me aware of this feature!

  6. Avatar for skain
    skain February 14th, 2008

    What version of VS2008 is this included in? I've got Professional and I don't have an 'Analyze' menu. I've got 'Tools' where 'Analyze' is in the screen shot.

  7. Avatar for Troy Hunt
    Troy Hunt February 14th, 2008

    Before people go nuts trying to find the “Analyze” menu item it’s probably worth pointing out that this feature is only available in the Team editions and you won’t find it in lesser packages such as Professional. Of course there are always other options such as JetBrains dotTRACE: http://www.jetbrains.com/pr...

  8. Avatar for maartenba
    maartenba February 14th, 2008

    I'll post an update to this with a link to the VS2008 product comparison.

  9. Avatar for Rama G
    Rama G July 11th, 2008

    Very good kick off for people who wants to make use of Code Analysis tool

  10. Avatar for Rignesh
    Rignesh July 30th, 2008

    Its really superb post and found it very helpful for beginning... thanks a lot!

  11. Avatar for Anandachetan E
    Anandachetan E November 20th, 2008

    Thank you very much for the good explanation of the "Performance analysis" and your patience.

  12. Avatar for Partha Choudhury
    Partha Choudhury January 31st, 2009

    Nice post maarten. One quick question ... how do you remove profiling sessions once you're done testing?

  13. Avatar for maartenba
    maartenba January 31st, 2009

    There will be some .trx files in your solution folder, which you can easily remove.