Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Diving Into Visual Studio 2015: Debugging Improvements (PerfTips Feature)

DZone's Guide to

Diving Into Visual Studio 2015: Debugging Improvements (PerfTips Feature)

This article will cover yet another debugging improvement of Visual Studio 2015—the new PerfTips feature. Read on and see what it's all about.

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

Introduction

This article is the continuation of my Debugging Improvements were explained in Day 5 and Day 6 of the series.

In the earlier part of the series, we covered topics like breakpoint configuration improvements and the new improved error list, tool window support for lambda, and LINQ queries in Visual Studio 2015. This article will cover another debugging improvement of Visual Studio 2015 i.e. new PerfTips feature.

Prerequisites

Visual Studio 2015 Express has been used in this tutorial to explain the concepts and features. For samples and practice, a Visual Studio solution is created having a console application named VS2015ConsoleApplication.The console application contains a MyProduct class containing product as an entity specific basic operations like fetching the product, returning the list of products as shown below.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
namespace VS2015ConsoleApplication  
{  
    public class MyProducts: IProducts  
    {  
        List < Product > _allProduct = new List < Product > ();  
        publicMyProducts()  
        {  
            _allProduct.Add(new Product  
            {  
                ProductCode = "0001", ProductName = "IPhone", ProductPrice = "60000", ProductType = "Phone", ProductDescription = "Apple IPhone"  
            });  
            _allProduct.Add(new Product  
            {  
                ProductCode = "0002", ProductName = "Canvas", ProductPrice = "20000", ProductType = "Phone", ProductDescription = "Micromax phone"  
            });  
            _allProduct.Add(new Product  
            {  
                ProductCode = "0003", ProductName = "IPad", ProductPrice = "30000", ProductType = "Tab", ProductDescription = "Apple IPad"  
            });  
            _allProduct.Add(new Product  
            {  
                ProductCode = "0004", ProductName = "Nexus", ProductPrice = "30000", ProductType = "Phone", ProductDescription = "Google Phone"  
            });  
            _allProduct.Add(new Product  
            {  
                ProductCode = "0005", ProductName = "S6", ProductPrice = "40000", ProductType = "Phone", ProductDescription = "Samsung phone"  
            });  
        }  
        /// <summary>  
        /// FetchProduct having price greater that 3000  
        /// </summary>  
        /// <returns></returns>  
        publicList < Product > FetchProduct() => (from p in _allProductwhere Convert.ToInt32(p.ProductPrice) > 30000 select p).ToList();  
        /// <summary>  
        /// FetchProduct  
        /// </summary>  
        /// <param name="pCode"></param>  
        /// <returns></returns>  
        publicProduct FetchProduct(string pCode)  
        {  
            return_allProduct.Find(p => p.ProductCode == pCode);  
        }  
        /// <summary>  
        /// FetchProduct with productCode and productName  
        /// </summary>  
        /// <param name="productCode"></param>  
        /// <param name="productName"></param>  
        /// <returns></returns>  
        publicProduct FetchProduct(string productCode, string productName)  
        {  
            return_allProduct.Find(p => p.ProductCode == productCode && p.ProductName == productName);  
        }  
        publicList < Product > GetProductList()  
        {  
            return_allProduct;  
        }  
    }  
}  


Where IProducts is a simple interface.

usingSystem;  
usingSystem.Collections.Generic;  
usingSystem.Linq;  
usingSystem.Text;  
usingSystem.Threading.Tasks;  
namespaceVS2015ConsoleApplication  
{  
    interfaceIProducts  
    {  
        Product FetchProduct(string productCode);  
        Product FetchProduct(string productCode, stringproductName);  
        List < Product > GetProductList();  
    }  
}  


In the following Program.cs file the FetchProduct() method is called to get the list of all the products. 

usingSystem;  
usingSystem.Collections.Generic;  
usingSystem.Linq;  
usingSystem.Text;  
usingSystem.Threading.Tasks;  
namespaceVS2015ConsoleApplication  
{  
    classProgram  
    {  
        static void Main()  
        {  
            varmyProducts = new MyProducts();  
            varallProducts = myProducts.GetProductList();  
            foreach(varprod in allProducts)  
            {  
                Console.WriteLine(prod.ProductName);  
            }  
            foreach(varprod in allProducts)  
            {  
                if (Convert.ToInt32(prod.ProductPrice) > 30000) Console.WriteLine(String.Format("ProductName : {0} and ProductPrice : {1}", prod.ProductName, prod.ProductPrice));  
            }  
            Console.ReadLine();  
        }  
    }  
}  


New PerfTips Feature

The new PerfTips feature in Visual Studio 2015 provides performance centric information. A developer can get performance specific information without depending upon any tool, code or stopwatch implementation. While debugging the code, the PerfTips feature displays performance information in the form of tooltip. The tooltip shows the span of time for which the code was running. While stepping over the code, it displays the span of time that the code was running during that step. Even if we run from breakpoint to breakpoint, the PerfTip feature displays the span of time the code was running between the two breakpoints. Let’s cover this feature through practical examples.

Let’s place the breakpoint at the start of the first foreach loop in Program.cs class... let’s say we want to get the length of time elapsed while reaching to this line of code. Run the application.

You can see the PerfTips appeared saying time elapsed is less than 1 millisecond. We did not used any stopwatch or code or any third party tool to get this time elapsed. It is the new perfTips feature of Visual Studio 2015 that shows the time elapsed during program execution till this breakpoint. Now if I again run the application but with one change. I have put Thread.Sleep for 10 milliseconds after both the foreach loops and place the breakpoint there. Now when you run the application and step over the Thread.Sleep(10) breakpoint, the PerfTips feature will add that elapsed 10 millisecond too while displaying the time elapsed in previous step.

It clearly says the time elapsed until the previous step was less than or equal to 14 milliseconds. You can also get the time elapsed between two breakpoints. Let’s try to do that. Let’s put the first breakpoint at the start of the method and another at last line of the method. Now when you run the application the code execution stops at the first breakpoint. Simply press F5 or continue with the execution, the execution will halt at the second breakpoint, therefore showing the total time elapsed for the complete method to execute.

In this case, it is less than 4 milliseconds. You can leverage the capabilities of this feature for troubleshooting performance bottlenecks in your application.

Conclusion

In this article we covered one of the critical features of troubleshooting performance bottlenecks i.e. PerfTips. In my next article, I’ll introduce you to new Diagnostic Tool Window of Visual Studio 2015.We’ll learn how to use the new Diagnostic tool window and how to leverage its capability.

Check out some of my previous posts:

For more technical articles you can reach out to my personal blog

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:
visual studio ,visual studio 2015 ,debugging

Published at DZone with permission of Akhil Mittal. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}