Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Azure, PHP, OpenXML, VSTS, ...

About the author

Maarten Balliauw is an MVP ASP.NET and is currently employed as .NET Software Engineer at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP.
More about me More about me
Send mail E-mail me


Microsoft Most Valuable Professional - MVP - ASP.NET

Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn RealDolmen - Rock-solid passion for ICT
I'm a speaker at TechDays Belgium and TechDays Finland

Search

Latest Twitter

    Follow me on Twitter...

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2010

    Code performance analysis in Visual Studio 2008

    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:

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

    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:

    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();
    }

    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


    Comments

    Bart Belgium | Reply

    Thursday, February 07, 2008 9:35 PM

    Bart

    Nice post maarten!

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

    maartenba | Reply

    Friday, February 08, 2008 8:11 AM

    maartenba

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

    Bart Belgium | Reply

    Friday, February 08, 2008 12:08 PM

    Bart

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

    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 ;) )

    Simone Busoli Italy | Reply

    Wednesday, February 13, 2008 8:45 PM

    Simone Busoli

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

    Hagrin United States | Reply

    Wednesday, February 13, 2008 8:49 PM

    Hagrin

    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!

    skain United States | Reply

    Wednesday, February 13, 2008 9:23 PM

    skain

    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.

    Troy Hunt Australia | Reply

    Wednesday, February 13, 2008 10:58 PM

    Troy Hunt

    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/profiler/

    maartenba | Reply

    Thursday, February 14, 2008 7:56 AM

    maartenba

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

    alvinashcraft.com | Reply

    Thursday, February 14, 2008 3:00 PM

    pingback

    Pingback from alvinashcraft.com

    » Daily Bits - February 14, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links, development, gadgets and raising rugrats.

    weblogs.asp.net | Reply

    Sunday, February 17, 2008 8:00 PM

    pingback

    Pingback from weblogs.asp.net

    Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET - ScottGu's Blog

    blogs.estatic.org | Reply

    Sunday, February 17, 2008 8:27 PM

    pingback

    Pingback from blogs.estatic.org

    BusinessRx Reading List : Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET

    ab110.com | Reply

    Sunday, February 17, 2008 11:10 PM

    pingback

    Pingback from ab110.com

    2月17日链接篇: ASP.NET, ASP.NET AJAX, Visual Studio, .NET - Joycode@Ab110.com

    aspnetguru.wordpress.com | Reply

    Monday, February 18, 2008 1:44 PM

    pingback

    Pingback from aspnetguru.wordpress.com

    Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET « .NET Framework tips

    thinkingindotnet.wordpress.com | Reply

    Monday, February 18, 2008 6:50 PM

    pingback

    Pingback from thinkingindotnet.wordpress.com

    Enlaces 17 de Febrero: ASP.NET, ASP.NET AJAX, Visual Studio, .NET « Thinking in .NET

    expressionblog.com | Reply

    Wednesday, February 20, 2008 8:51 AM

    pingback

    Pingback from expressionblog.com

    Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET - Mirrored Blogs

    blog.darrenstokes.com | Reply

    Wednesday, February 20, 2008 1:31 PM

    pingback

    Pingback from blog.darrenstokes.com

    Visual Studio Links #2 : Mostly Programming Stuff

    Rama G India | Reply

    Friday, July 11, 2008 8:19 AM

    Rama G

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

    Rignesh India | Reply

    Wednesday, July 30, 2008 8:34 AM

    Rignesh

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

    Anandachetan E India | Reply

    Thursday, November 20, 2008 9:18 AM

    Anandachetan E

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

    RealDolmen blogs | Reply

    Wednesday, December 31, 2008 3:43 PM

    trackback

    Trackback from RealDolmen blogs

    Top blog posts in 2008

    Partha Choudhury United States | Reply

    Friday, January 30, 2009 8:51 PM

    Partha Choudhury

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

    maartenba | Reply

    Saturday, January 31, 2009 9:54 AM

    maartenba

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

    Add comment




      Country flag

    biuquote
    • Comment
    • Preview
    Loading