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.
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Threading.Tasks;
- namespaceVS2015ConsoleApplication
- {
- 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"
- });
- }
-
-
-
-
- publicList < Product > FetchProduct() => (from p in _allProductwhere Convert.ToInt32(p.ProductPrice) > 30000 select p).ToList();
-
-
-
-
-
- publicProduct FetchProduct(string pCode)
- {
- return_allProduct.Find(p => p.ProductCode == pCode);
- }
-
-
-
-
-
-
- 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 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.