Reprinted: http://www.vchelp.net/ Original author's name fang (fangguicheng@21cn.com)
Body asynchronous IO, APC, IO completion port, thread pool and high performance server's performance metrics to achieve high-performance path server performance indicators as a network server program, performance is always the first indicator. Performance can be defined: in a given hardware condition and time, the amount of task can be processed. Server design that can maximize hardware performance is a good design. The design of a good server should also consider the average service. For each client, the server should give each client average service, which cannot make a client that does not have "hunger" for a long time. Scalability, that is, as the hardware ability is improved, the performance of the server can grow linearly. A high-performance approach is a practical server calculation is complicated, often mixing IO calculations and CPU calculations. IO Calculation refers to the calculation model in the calculation model, such as file servers, mail servers, etc., mixing a large number of network IO and file IO; CPU calculation means that there is no or few IO, such as encryption / decryption, etc. Coding / decoding, mathematical calculation, etc. In CPU calculations, single-threading and multi-threaded model effects are quite. "Win32 multi-threaded performance" said "In a single processor computer, the CPU-based task is not possible to perform fast than serial execution, but we can see that the thread is created and switched under Windows NT. The additional overhead is very small; for very short calculations, concurrent execution is only 10% lower than the serial execution, while the calculation length increases, these two times are very close. "Visible, for pure CPU calculations If there is only one CPU, the multi-thread model is inappropriate. Consider a service that performs intensive CPU calculations, if there are dozens of such threads concurrently, excessive task switches have caused unnecessary performance losses. In programming implementation, single-threaded model calculation model is inconvenient for server programming. Therefore, it is appropriate to use a thread pool working model for the CPU. The QueueUserWorkItem function is ideal for calculating a CPU to the thread pool. Thread pool implementation will strive to reduce this unnecessary thread switching, and control the number of concurrent threads as the number of CPUs. We really need to care about IO calculations, and general web server programs are often accompanied by a large number of IO calculations. How to improve performance is to avoid waiting for the end of IO, causing CPU idle, trying to utilize hardware capabilities, allowing one or more IO devices to perform concurrently with CPUs. Asynchronous IO, APC, and IO completed ports can be achieved. For web servers, if the number of concurrent requests is less, the simple multi-thread model can be credited. If a thread is suspended because the IO operation is complete, the operating system will dispatch another ready thread to put into operation, thereby forming concurrent execution. Classic web server logic uses multi-threaded / multi-process mode. When a client initiates a connection to the server, the server will create a thread that allows this new thread to handle subsequent transactions. This programming method representing a client object is very intuitive and easy to understand in a dedicated thread / process. This approach has a limitancy for large network server programs. First, create a thread / process and destruction thread / process is very high, especially if the server uses a TCP "short connection" mode or UDP mode communication, for example, after the client initiates a connection, send one Request, after the server responds to this request, the connection is turned off. If you design the HTTP server in a classic manner, it is very bad to create thread / destroying threads.