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

Introduction

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

You can learn my previous parts,

In the earlier part of the series we covered topics like breakpoint configuration improvements and 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.

  1. usingSystem;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Linq;  
  4. usingSystem.Text;  
  5. usingSystem.Threading.Tasks;  
  6. namespaceVS2015ConsoleApplication  
  7. {  
  8.     public class MyProducts: IProducts  
  9.     {  
  10.         List < Product > _allProduct = new List < Product > ();  
  11.         publicMyProducts()  
  12.         {  
  13.             _allProduct.Add(new Product  
  14.             {  
  15.                 ProductCode = "0001", ProductName = "IPhone", ProductPrice = "60000", ProductType = "Phone", ProductDescription = "Apple IPhone"  
  16.             });  
  17.             _allProduct.Add(new Product  
  18.             {  
  19.                 ProductCode = "0002", ProductName = "Canvas", ProductPrice = "20000", ProductType = "Phone", ProductDescription = "Micromax phone"  
  20.             });  
  21.             _allProduct.Add(new Product  
  22.             {  
  23.                 ProductCode = "0003", ProductName = "IPad", ProductPrice = "30000", ProductType = "Tab", ProductDescription = "Apple IPad"  
  24.             });  
  25.             _allProduct.Add(new Product  
  26.             {  
  27.                 ProductCode = "0004", ProductName = "Nexus", ProductPrice = "30000", ProductType = "Phone", ProductDescription = "Google Phone"  
  28.             });  
  29.             _allProduct.Add(new Product  
  30.             {  
  31.                 ProductCode = "0005", ProductName = "S6", ProductPrice = "40000", ProductType = "Phone", ProductDescription = "Samsung phone"  
  32.             });  
  33.         }  
  34.         /// <summary>  
  35.         /// FetchProduct having price greater that 3000  
  36.         /// </summary>  
  37.         /// <returns></returns>  
  38.         publicList < Product > FetchProduct() => (from p in _allProductwhere Convert.ToInt32(p.ProductPrice) > 30000 select p).ToList();  
  39.         /// <summary>  
  40.         /// FetchProduct  
  41.         /// </summary>  
  42.         /// <param name="pCode"></param>  
  43.         /// <returns></returns>  
  44.         publicProduct FetchProduct(string pCode)  
  45.         {  
  46.             return_allProduct.Find(p => p.ProductCode == pCode);  
  47.         }  
  48.         /// <summary>  
  49.         /// FetchProduct with productCode and productName  
  50.         /// </summary>  
  51.         /// <param name="productCode"></param>  
  52.         /// <param name="productName"></param>  
  53.         /// <returns></returns>  
  54.         publicProduct FetchProduct(string productCode, string productName)  
  55.         {  
  56.             return_allProduct.Find(p => p.ProductCode == productCode && p.ProductName == productName);  
  57.         }  
  58.         publicList < Product > GetProductList()  
  59.         {  
  60.             return_allProduct;  
  61.         }  
  62.     }  
  63. }  
Where IProducts is a simple interface.
  1. usingSystem;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Linq;  
  4. usingSystem.Text;  
  5. usingSystem.Threading.Tasks;  
  6. namespaceVS2015ConsoleApplication  
  7. {  
  8.     interfaceIProducts  
  9.     {  
  10.         Product FetchProduct(string productCode);  
  11.         Product FetchProduct(string productCode, stringproductName);  
  12.         List < Product > GetProductList();  
  13.     }  
  14. }  
In the following Program.cs file the FetchProduct() method is called to get the list of all the products. 
  1. usingSystem;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Linq;  
  4. usingSystem.Text;  
  5. usingSystem.Threading.Tasks;  
  6. namespaceVS2015ConsoleApplication  
  7. {  
  8.     classProgram  
  9.     {  
  10.         static void Main()  
  11.         {  
  12.             varmyProducts = new MyProducts();  
  13.             varallProducts = myProducts.GetProductList();  
  14.             foreach(varprod in allProducts)  
  15.             {  
  16.                 Console.WriteLine(prod.ProductName);  
  17.             }  
  18.             foreach(varprod in allProducts)  
  19.             {  
  20.                 if (Convert.ToInt32(prod.ProductPrice) > 30000) Console.WriteLine(String.Format("ProductName : {0} and ProductPrice : {1}", prod.ProductName, prod.ProductPrice));  
  21.             }  
  22.             Console.ReadLine();  
  23.         }  
  24.     }  
  25. }  
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 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 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 first breakpoint. Simply press F5 or continue with the execution, the execution will halt at second breakpoint, therefore showing the total time elapsed for complete method to execute.



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

Conclusion

In this article we covered one of the critical features of trouble shooting performance bottle necks 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.

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

Up Next
    Ebook Download
    View all
    Learn
    View all