Asynchronous programming In. NET: APM/EAP and async/await (1)

Source: Internet
Author: User
Tags apm website performance

Asynchronous programming In. NET: APM/EAP and async/await (1)

Overview

After I wrote an article about the past and present lives of async and await, we seem to have some questions about how async and await can improve website processing capabilities, the blog park itself has made many attempts. Let's answer this question again today. At the same time, we will try async and await in WinForm, and compare the differences between the asynchronous programming mode APM/EAP and async/await Before 4.5, finally, we will discuss the interaction between different threads.

IIS has the processing capability problem, but WinForm is the UI response problem, and the UI thread of WinForm is the same from the beginning to the end, so there is a certain difference between the two. Someone may ask, are there anyone writing WinForm? Well, it is really an old thing. It is not as dazzling as WPF, and its technology is not as advanced as WPF. But in terms of architecture, whether it is Web, WinForm, or WPF or Mobile, these are just the presentation layer, aren't they? Currently, large systems generally involve desktop clients, Web terminals, mobile phones, and tablets. This is why the application layer and service layer exist. ASP. net mvc, WinForm, WFP, Android/IOS/WP are all performance layers. In the performance layer, we should only process the logic related to the "performance, any business-related logic should be processed at a lower level. For architecture issues, we will go further and further, And let alone I didn't prompt you. Today we will see another old technology Web Service in. NET.

I also want to remind you that the content of this article is a little long and involves many knowledge points. Therefore, I recommend: "First, first, then,". first, then, it is the first choice for reading a long article in the 21st century, it is the beginning of good communication. Do you want to know what will make you different? Do you want to know why Shanghai is raining so heavily today? Remember to read it first. What you stick with is not my article, but the valuable spirit that we will go to work in the heavy rain! You deserve it first!

How can async/await improve IIS processing capability?

First, the response capability is not entirely about the performance of our program. Sometimes your program may have no problems and has been carefully optimized, but the response capability is still not up, website performance analysis is a complex task. Sometimes it can only be achieved through experience and constant attempts. Of course, what we are talking about today is the processing capability of IIS, or the performance of IIS, but it is not the performance of the Code itself. Even if async/await can improve the processing capability of IIS, for users, the time from initiating a request to page rendering is complete, it won't be because we have added async/await to make much changes.

In addition, asynchronous ASP. NET is not only available for async/await, ASP. NET has an asynchronous Page in the Web Form era, including ASP. net mvc does not have asynchronous Controller? Async/await is new and cool, but it only makes some improvements based on the original technology, making it easier for programmers to write asynchronous code. We often say that Microsoft is fond of new bottles and old wines. At least we need to see what this new bottle has brought to us. No matter what the product is, it cannot be perfect at the beginning, so it is constantly updated, it can also be said that it is a correct way to do things.

Steps for ASP. NET Parallel Processing

ASP. the article on how NET works in IIS has introduced in detail how a request is sent from the client to the server's HTTP. SYS finally enters the CLR for processing. It is strongly recommended that students who do not understand this section read this article first to help you understand this section), but all the steps are based on the assumption of a thread. IIS itself is a multi-threaded working environment. what changes will happen from the multi-threaded perspective? First, let's take a look at the figure below the timeline. Note: The following steps are based on the integration mode after IIS7.0.

Let's take a look at the above steps:

What factors will control our response capabilities?

From the above mentioned several major valves, we can draw the following digital controls or influence our response capabilities.

Length of the HTTP. SYS queue

I don't think we need to explain this. The default value is 1000. This value depends on the processing speed of the clr io and Worker threads behind us. If neither of them can be processed, this number will be useless. Because they will be stored in the queue at the process pool level, it will only cause a waste of memory.

Maximum number of I/O threads and maximum number of Worker threads

These two values can be configured in web. config.

MaxIoThreads: Maximum number of I/O threads requested from the HTTP. SYS queue

MaxWorkerThreads: the maximum number of Worker threads in the CLR that actually process requests

MinIoThreads: Minimum number of I/O threads requested from the HTTP. SYS queue

MinWorkerThreads: the minimum number of Worker threads in the CLR that actually process requests

The default values of minIoThreads and minWorkerThreads are 1. Reasonably increasing minIoThreads can avoid unnecessary thread creation and destruction. If the value of maxIoThreads is too large or unreasonable, most requests will be put in the queue at the process pool level. Therefore, maxIoThreads has a certain relationship with maxWorkerThreads. Assume that one worker thread can process 10 requests in 1 S. If our machine configuration only allows us to process 100 requests at the same time, the reasonable maxThreads is 10. However, the IO thread does not need to process a request for 10 seconds. It is faster than the woker thread because it only needs to process requests from HTTP. the SYS queue can be taken over. Assume that an IO thread can process 20 requests in 1 s, corresponding to the upper limit of 100 requests, then the reasonable value of maxIoThreads should be 5.

Maximum number of concurrent requests

The process pool-level queue provides a certain buffer for our CLR. Note that this queue has not yet entered the CLR, so it will not occupy any resources in our hosting environment, that is, the request is stuck out of the CLR. We need to configure it at the aspnet. config level. We can find it in the. net fraemwork installation directory. It is generally C: \ Windows \ Microsoft. NET \ Framework \ v4.0.30319 if you install 4.0.

MaxConcurrentRequestPerCPU: Maximum number of concurrent requests allowed by each CPU. When the sum of the requests being processed by the worker thread in CLR is greater than this value, requests from IO threads will be placed in the queue at the process pool level.

MaxConcurrentThreadsPerCPU: set to 0 to disable.

RequestQueueLimit: the length of the queue in HTTP. SYS. We can configure it at the web. config/system. web/processModel node.

What does async and await do?

Finally, let's get started. Take ASP. net mvc as an example. If async Action is not used, there is no doubt that it is executed in a Woker thread. When we access some web services or read files, this Worker thread will be blocked. Assume that the execution time of this Action is 100 ms, and it takes 80 ms for other web services to access. Ideally, the Worker thread can respond to 10 requests in one second. Assume that our maxWorkerThreads is 10, then we can always respond to 100 requests in one second. If we want to increase the number of responsive requests to 200, what should we do?

Some people may say that this is not simple. Can I tune maxWorkerThreads to 20? In fact, we do not have any problems, it is indeed possible, but also can play a role. So why do we need to pay more for the weekly chapter async/await? Why is my mind dizzy? What problems does async/await solve for us? It can release the current worker thread when we access the web service and put it back into the thread pool so that it can process other requests. Like the IO thread, the IO thread is only responsible for handing requests to the Worker thread or putting them into a queue at the process pool level, and then processing other requests in the HTTP. SYS queue. When the web service returns the result to us, it will randomly take a new woker thread into the thread pool to continue the execution. That is to say, we have reduced the waiting time and used the thread in full.

Let's compare the usage of async/awit and the unavailability,

No async/await: 20 woker threads can process 200 requests in 1 s.

The total conversion time is 20*1000 ms = 20000 ms,

The waiting time is 200*80 ms = 16000 ms.

That is to say, Using async/await saves at least ms, and the 20 worker threads will process the request again, even if the processing time of each request is 160 ms, we can add another requests. And don't forget that Ms is obtained based on synchronization, including wait time, so there may be more actual situations. Of course, we didn't calculate the thread switching time here, so there is a difference in the actual situation, but it should not be very large, because our threads are all operations based on the thread pool.

If 20 Worker threads do not use Asynchronization, 200 requests can be processed in 1 s, while 360 requests can be processed in asynchronous mode, which increases by 80% immediately! After asynchronous processing, the required Worker thread data will be greatly reduced by about 50% for the same number of requests. A thread will allocate at least 1 MB of memory on the heap, if it is 1000 threads, it is 1 GB of capacity. Although the memory is cheap now, it is better to save the summary, in addition, fewer threads can reduce the CPU consumption of the thread pool during Thread Maintenance.

Note: The above data is not real test data. In actual situations, the request time is not 100 ms, and the time spent on web service is not 80 ms. I just want to give you a thought :), therefore, the improvement in response capability after Using async and await has a great relationship with the time we used to block these IO and network.

Suggestions

I don't know what you get. First of all, we need to know that async/await is not a panacea. We can't expect the performance to improve if we want to write two optical keys. Remember,A cpu can only execute one thread within the same period of time.This is why async and await are recommended for I/O or network operations. In this scenario, we use MVC sites to access the WCF or Web Service, which is very suitable for asynchronous operations. In the preceding example, the time for reading the web service within 80 ms does not require cpu operations for most of the time, so that the cpu can be used by other threads. If it is not an operation for reading the web service, it is a complex computing operation, so wait for the cpu to pop up the table.

The second point is that in addition to the use of Asynchronization in the program, the configuration of IIS mentioned above is very important. If Asynchronization is used, please remember to increase the value of maxWorkerThreads and maxConcurrentRequestPerCPU.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.