There are many optimizations on the server side, and there are also many optimization topics. In this article, we mainly focus on: if the server can generate pages faster, at the same time, we also pay attention to the faster arrival of the generated page to the client browser.
In fact, we are optimizing the following timeline:
Building a high-performance ASP. NET Site Chapter 6-performance bottleneck diagnosis and preliminary optimization (Part 1)-identifying performance bottlenecks
. Aspx
To shorten the time line above, the server needs to make better use of its resources, such as better utilization and allocation of memory resources and CPU resources. How to make full use of these resources is closely related to the quality of the code we write, efficient code often allows us to spend less money on more hardware devices (so the quality of code is very important ).
Let's take a look at the performance bottlenecks that may occur on the server:
Insufficient memory
Lack of Cache
CPU pressure
Handle request thread Problems
Next, we will introduce how to use the system performance diagnostic tool to identify which performance bottleneck causes slow resolution pages on the server. After finding out the problem using the performance diagnostic tool, perform a detailed analysis on the problem and collect the data. Based on the data, take corresponding measures to remedy the problem. As for how to solve each performance problem, the following articles will detail in a chapter. Please be careful, so we should first learn to discover the problem. When a website may have performance problems, do not immediately modify the site or server, but first diagnose the bottleneck. J
Memory
First, you must determine whether the server memory is insufficient. If the memory is insufficient, it will increase the server's CPU pressure and disk I/O read/write operations, naturally, the CPU and disk I/O read/write operations are reduced.
Why does memory not exist increase the CPU pressure and disk I/O read/write operations?
When the system memory is insufficient, the system will save some data transferred from the original memory to the disk and save it as pagefile. sys. When the data is needed, the system reads and writes data to the disk. Disk read/write operations consume CPU resources and increase disk I/O operations.
Next, let's take a look at how to identify performance bottlenecks with insufficient memory.
We will mainly describe how to diagnose this problem in the Window server system.
Window Server 2003
Enter"Perfmon". The following window is displayed. Click the "+" button on the toolbar and click"Performance object"Select from the drop-down list"Memory", And then select"Pages/sec"Counter. If this value is large, the CPU constantly exchanges data between memory and disk.
Windows Vista, Server 2008, Window 7
In Windows Vista, Windows Server 2008, and Windows 7You can run "perfmon"To open the performance monitoring window. And can run"ResmonTo open the resource monitoring window, which is more intuitive. In the resource monitoring window, you can see Hard Faults/sec ). check the value of each process. If the "hard error/second" value of the process is very high, it indicates that the server is out of memory. (We will describe how to solve this problem in subsequent articles. Here we will first describe how to find this problem)
Cache
As we all know, appropriate and practical cache policies can greatly improve the performance of the server. We usually cache data in memory, such as browser memory and proxy server memory. In addition, some common objects, some pages, and even the whole page can be cached.
The benefits of caching are as follows:
Shorten the Server Response Time
Reduces CPU usage pressure
Avoid frequent database reading
If you cache data in a browser or proxy server, you can also reduce unnecessary backhaul.
In general, we cache data that is frequently used or that consumes a large amount of resources each time Chengdu is generated.
But how can it be regarded as "frequently used "?
There are no standards. Let's look at the situation! For example, if a page is requested 10 times in one second, this page does not count as "frequent requests (other pages request 100 requests within 1 second). However, if this page is cached for 1 second, it also greatly improves the performance, because within one second, 90% of requests are responded by the cache. You can refer to the "5-minute cache rule ". As for how to cache, I will explain it in a later article.
CPU
We can run the same memory diagnosis as before"Perfmon"Command, and then in"Processor"Select under category"% Processor Time"Counter. As follows:
At the same time, we can run"Resmon"To open the" Resource Monitoring window:
You can see the first highlighted red box"CPU"Column, this is actually the response"% Processor Time"Counter monitoring results. Generally, if the value of a process is higher than 80%, it indicates that the process consumes a lot of CPU resources. If the w3wp.exe process consumes 80%, your site consumes a lot of CPU. We will discuss in subsequent articles: If the CPU pressure is reduced.
Request Processing thread
We know that every request sent to the server is handled by a thread in the application pool. In addition, the number of threads used to process requests is controlled by IIS. If there are no Idle threads in the application pool to handle new requests, the request will be placed in the Request queue for waiting. If the request queue on the server is too long and the server is too busy, the new request may be rejected by the server.
Generally, the number of threads available in an application pool is determined by the. NET Framework version installed on the server and some IIS settings.
. NET Framework Version |
Default number of available threads |
1.1. |
20 * Number of CPUs-8 |
2.0 |
12 * Number of CPUs |
3.5, 4.0 |
IIS 7 Classic Mode: 12 * Number of CPUs |
|
IIS 7 integration mode: 100 * Number of CPUs |
If there are not enough threads on the server to process requests, this is the so-called "thread hunger ". We can use the system performance counters to check whether the server of the site has this situation:
1. Run"Perfmon". As follows:
2. In the displayed performance monitoring window, select performance monitor as follows:
3. Click"+"Button, and then expand"ASP. NET"Category:
4. Add the following counters:
Request Execution Time |
Time spent processing a request (unit: milliseconds) |
Request Current |
The number of requests to be processed when ASP. NET is running, including the requests being processed and the requests waiting in the queue. |
5. Expand"ASP. NET Applications"Category to add the following counters:
Request Executing |
Number of requests being processed |
If the number of "Request Current" is greater than the number of Request Executing, it indicates that a Request is waiting for processing. The subsequent articles will detail how to deal with this situation.