KoffeeKoder


  • Application Profiling Using DotTrace
    published on 8/26/2008 7:43:45 PM
  • DotTrace is a profiling tool introduced by JetBrains which is used to measure bottle necks for your console,windows and web applications. In this post I will create a simple windows application which will draw random Polygons on the screen. I will measure the performance of the application as we increase the number of sides of the Polygons. The DotTrace tool will be used to point out where the bottle neck of the application resides.

    Here is the code to draw random Polygons:



     public partial class Form1 : Form
        {
            private Random random = new Random();

            public const int POLYGON_SIDES = 5;

            public Form1()
            {
                InitializeComponent();
            }

            private int GetRandomNumber(int min,int max)
            {
                return random.Next(min, max);
            }

            private string[] GetAllColorNames()
            {
                List<String> colorNames = new List<string>();
               
               
                foreach(KnownColor color in Enum.GetValues(typeof(KnownColor)))
                {
                    colorNames.Add(color.ToString());
                }

                return colorNames.ToArray();
            }

            private Point[] GetRandomPoints()
            {
                Point[] points = new Point[POLYGON_SIDES];

                for (int i = 0; i < POLYGON_SIDES; i++)
                {
                    Point p = new Point(GetRandomNumber(0, 800), GetRandomNumber(0, 800));
                    points[i] = p;
                }

                return points;
            }

            private void DrawShapes()
            {
                string randomColorName = String.Empty;
                string[] randomColorNames = GetAllColorNames();

                Bitmap bitmap = new Bitmap(800, 800);

                Graphics g = Graphics.FromImage(bitmap);
               
                for (int i = i; i <= 500; i++)
                {
                    Color randomColor = Color.FromName(randomColorNames
    [GetRandomNumber(0,randomColorNames.Length-1)]);    
                    g.DrawPolygon(new Pen(randomColor), GetRandomPoints());
                    g.FillPolygon(new SolidBrush(randomColor), GetRandomPoints());
                }

                pictureBox1.Image = bitmap;
            }

            private void button1_Click(object sender, EventArgs e)
            {
                DrawShapes();
            }
        }




    Also, note that the POLYGON_SIZES has been set to 5 which means that the Polygon will have 5 sides. Here is the screenshot of the result when you run this application.




    Next, start the DotTrace application and give the path your windows application. The DotTrace will run your application and it will allow you to capture the snapshot. Here is the result of the capture when I clicked on the generate button 4 times.




    The DotTrace is pointing to the DrawShapes method where 15.96% of the work is done. Inside the DrawShapes method the FillPolygon and DrawPolygon are the most expensive methods. The methods are faded since they are part of the .NET framework.

    Now, let's increase the number of Polygons to 50. Simply, change the POLYGON_SIDES to 50 and run the application again.

    Here is the screenshot of the application when Polygon sides are 50.




    As, you can see the screenshot is very complicated and as you expect the performance will be very bad to create a Polygons with 50 sides. Here is the report from the DotTrace.




    Wow! that FillPolygon is really kicking our butt! It took around 2 seconds to render and salvaged 30.58% of the performance.

    Anyway, the purpose of this post was to demonstrate DotTrace. This is a really great tool. Next, time we will see how to use DotTrace on Web Application.