Diving into Visual Studio 2015 (Day #7): PerfTips Feature in Visual Studio 2015

Posted by

Introduction

This article is the continuation part of Debugging Improvements that were explained in Day #5, Day#6 of the series. In the earlier part of the series 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.

Series

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>();
        public MyProducts()
        {
            _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>
        public List<Product> FetchProduct() => (from p in _allProduct 
where Convert.ToInt32(p.ProductPrice) > 30000 select p).ToList();

        /// <summary>
        /// FetchProduct
        /// </summary>
        /// <param name="pCode"></param>
        /// <returns></returns>
        public Product 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>
        public Product FetchProduct(string productCode, string productName)
        {
            return _allProduct.Find(p => p.ProductCode == productCode 
&& p.ProductName==productName);
        }

        public List<Product> GetProductList()
        {
            return _allProduct;
        }
    }
}

where IProducts is a simple interface.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VS2015ConsoleApplication
{
    interface IProducts
    {
        Product FetchProduct(string productCode);
        Product FetchProduct(string productCode,string productName);
        List<Product> GetProductList();
    }
}

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VS2015ConsoleApplication
{
    class Program
    {
        static void Main()
        {
            var myProducts = new MyProducts();
            var allProducts = myProducts.GetProductList();
            foreach (var prod in allProducts)
            {
                Console.WriteLine(prod.ProductName);
            }
            foreach (var prod 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 a tooltip. The tooltip shows the span of time for which the code was running. While stepping through 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.

image

You can see the PerTips appeared saying time elapsed is less than 1 millisecond. We did not use 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 for each 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 milliseconds too while displaying the time elapsed in the previous step.

image

It clearly says the time elapsed till 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.

image

In our case, it is less than 4 millisecond. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.