Performance is one of the most important aspects of an application. Without reasonable performance your application becomes unusable. You can improve the performance of your application if you know the bottle necks that is causing performance problems. In this article we will take a look at the DotTrace, a tool by JetBrains for profiling the performance of an application.
Introduction:
Performance is one of the most important aspects of an application. Without reasonable performance your application becomes unusable. You can improve the performance of your application if you know the bottle necks that is causing performance problems. In this article we will take a look at the DotTrace, a tool by JetBrains for profiling the performance of an application.
About the Application:
The application that we are going to profile will be responsible for creating random Polygons on the screen. We will start by creating 5 sided Polygons and later increase it to 50 sided Polygons. The DotTrace tool will be used to profile the application performance before and after the increase in sides.
Implementing the Polygon Application:
The Polygon application will consist of a simple Button control and the PictureBox control. When the button is clicked the random Polygons are generated and displayed in the PictureBox control. Let’s take a look at the DrawShapes method which is responsible for creating the Polygons.
private void DrawShapes()
{
const int width = 20;
const int height = 20;
int minWidth = 0;
int minHeight = 0;
string randomColorName = String.Empty;
string[] randomColorNames = GetAllColorNames();
Bitmap bitmap = new Bitmap(800, 800);
Graphics g = Graphics.FromImage(bitmap);
for (int i = 1; i <= 500; i++)
{
minWidth = GetRandomNumber(0, 800);
minHeight = GetRandomNumber(0, 800);
Color randomColor = Color.FromName(randomColorNames[GetRandomNumber(0, randomColorNames.Length - 1)]);
Pen p = new Pen(randomColor);
p.Width = 5;
g.DrawPolygon(new Pen(randomColor), GetRandomPoints());
g.FillPolygon(new SolidBrush(randomColor), GetRandomPoints());
}
pictureBox1.Image = bitmap;
}
Nothing fancy going inside the DrawShapes method! One of the important methods is GetAllColorNames which is responsible for fetching all the colors used in the .NET framework.
private string[] GetAllColorNames()
{
List<String> colorNames = new List<string>();
foreach (KnownColor color in Enum.GetValues(typeof(KnownColor)))
{
colorNames.Add(color.ToString());
}
return colorNames.ToArray();
}
The Enum.GetValues method is used to get all the values of the enumeration type. Now, let’s see how we can profile our application.
Profiling the Application:
Run the DotTrace tool and uncheck the “Start profiling when the application starts”. This is because we only want to profile the code which runs after clicking the Button to generate the Polygons.
Here is the Polygons Application running and creating Polygons of 5 sides.
Seems kind of cool right!
Our application will be creating 500 Polygons. We are just going to increase the number of sides of the Polygon.
Here is the Profile data captured when the Button click is executed.
The DotTrace profiler let us know the hotspots of the application. From the above report we can see that 15.98% of the work is performed inside the button1_Click method. Out of that 15.96% of the work is performed inside the DrawShapes method. The DotTrace profiler goes in further depth to point out that of 15.96% work 9.56% is performed by FillPolygon method and 3.64% is performed by DrawPolygon. This also makes it clear that FillPolygon is a more expensive method than DrawPolygon.
Now, let’s change the Polygon sides to 50 and see how much strain we put on our application. Here is the screenshot of the application with 50 Polygons.
Now, let’s take a look at the report generated by the DotTrace tool for the above application creating 50 sided Polygons.
Yikes! 55.21% of the work is performed inside the button1_Click method. Out of which 30.58% is performed in the FillPolygon method and 21.06% is performed inside the DrawPolygon method.
Using the above report we can determine the bottle necks of our application.
Conclusion:
DotTrace is a very convenient tool to find bottle necks in an application. This really comes in handy when parsing complicated code where naked eyes and debugging tools fails.
[Download Sample]