Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the performance analysis tools that come with VS2012

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to use the performance analysis tools that come with VS2012". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First take a look at the console code (download the source code):

The copy code is as follows:

Static void Main (string [] args)

{

Int I = 10000

While (iMurt-> 0)

{

Core c=new Core ()

C.Process (DateTime.Now.ToString ())

}

}

Public class Core

{

Public void Process (string input)

{

/ / process logic

String result = string.Format ("{0}-{1}", DateTime.Now, input)

/ / log to file

Log (result)

}

Public void Log (string message)

{

String fileName = System.IO.Path.Combine (AppDomain.CurrentDomain.BaseDirectory, "log.txt")

String msg = "{Now}: {Message}"

Msg = msg.Replace ("{Now}", DateTime.Now.ToString ("yyyy-MM-dd HH:mm:ss"))

Msg = msg.Replace ("{Message}", message)

Using (StreamWriter sw = System.IO.File.AppendText (fileName))

{

Sw.WriteLine (msg)

Sw.Flush ()

Sw.Close ()

}

}

}

Click the "finish" button, and the analysis will be carried out automatically. After running the console program directly, vs2012 will automatically display the analysis report, as follows:

Learn from the "summary" report above

The System.IO.File.AppendText function and the System.AppDomain.get_BaseDirectory function are the most time-consuming. We need to optimize the use of these two functions first (we'll talk about it later). The five most time-consuming functions are listed.

Report View categories:

There are many types of report views generated, the default shown above is the Summary view, and the other types of views are listed as follows:

So how do we track these time-consuming functions? We need to move to the function details view, as follows:

The red box on the right represents the time-consuming distribution ratio. Obviously, the Process function in the above figure takes up a large proportion.

The red box below represents the corresponding code, and will also highlight the lines of code with high performance loss, as well as the corresponding loss ratio (99.2% of the figure is due to a total of three loss points of this line of code: 83.3% "14.4%" 1.5%).

We need to track down and go to the most serious process function to check. The Process bar in the red box on the right side of our click enters the detailed analysis interface, as follows:

It seems that the main problem comes from the "Log (result)" code line, which accounts for 73.5%. Continue to track it in depth, as shown below:

This is almost enough. The two lines that are more lossy here are: get the fileName and AppendText to the log file code lines, and optimize them respectively:

Get the file name optimization log file name is the same, so do not have to calculate every time Log (msg), directly extract to static fileName variable AppendText optimization, once involving the fileName O operation, the speed is slow, can not be avoided (from a single line of code point of view), what to do? Then change the structure: the main program writes the log to the queue, and another thread writes to the disk.

Modify the code as follows:

The copy code is as follows:

Public class Core

{

Public void Process (string input)

{

/ / process logic

String result = string.Format ("{0}-{1}", DateTime.Now, input)

/ / log to file

Log (result)

}

Private static List log = new List ()

Public static void Log (string message) / / fileName is removed because this variable is no longer needed because other threads are responsible for writing to disk

{

String msg = "{Now}: {Message}"

Msg = msg.Replace ("{Now}", DateTime.Now.ToString ("yyyy-MM-dd HH:mm:ss"))

Msg = msg.Replace ("{Message}", message)

Log.Add (msg)

}

}

We run the performance analysis again, as follows:

Let's compare the graphs of the first time and this time:

The performance after improvement is obviously better than that before improvement.

Custom performance analysis

We can add/remove performance metrics by modifying properties, such as adding some / some Windows counters, collecting Windows events, collecting the lifecycle of .NET objects, etc. We can do the following to set the performance metrics:

A more detailed report will be generated as follows:

The report lists which objects are generated the most (in this case, string), which functions cause the most memory allocation, and so on

In the tags view, you can see the windows counter data collected every 500ms, as shown in the following figure:

In the object Lifetime view, you can see all the data for various objects from new to dispose, as shown in the following figure:

That's good, right?

Let's talk about how to analyze the performance of independent programs through VS2012. In fact, it's very simple. Just take care of the following figure, as we all know:

Let's talk about how to analyze the performance of web projects.

First open the web project solution, and then directly analyze the performance. Some people will say that there is no request for operation. This is simple, and there are several solutions:

Open another VS environment (whether remote or local), through web load tests to crazy requests through loadrunner/qtp to simulate sad human requests.

This is the end of the content of "how to use the performance analysis tools that come with VS2012". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report